View Javadoc
1   package com.foxinmy.weixin4j.pay.api;
2   
3   import com.alibaba.fastjson.TypeReference;
4   import com.foxinmy.weixin4j.exception.WeixinException;
5   import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
6   import com.foxinmy.weixin4j.pay.model.WeixinPayAccount;
7   import com.foxinmy.weixin4j.pay.profitsharing.*;
8   import com.foxinmy.weixin4j.pay.type.ProfitIdType;
9   import com.foxinmy.weixin4j.pay.type.SignType;
10  import com.foxinmy.weixin4j.pay.type.profitsharing.ReturnAccountType;
11  import com.foxinmy.weixin4j.util.RandomUtil;
12  import com.foxinmy.weixin4j.xml.XmlStream;
13  
14  import java.util.List;
15  
16  /**
17   * 微信商户平台分账接口(直连商户/服务商)
18   *
19   * @author kit (kit.li@qq.com)
20   * @date 2020年05月20日
21   * @since weixin4j-pay 1.1.0
22   */
23  public class ProfitSharingApi extends MchApi {
24  
25      public ProfitSharingApi(WeixinPayAccount weixinAccount) {
26          super(weixinAccount);
27      }
28  
29      /**
30       * 添加分账接收方
31       * 服务商代子商户发起添加分账接收方请求,后续可通过发起分账请求将结算后的钱分到该分账接收方。
32       *
33       * @param receiver
34       *          分帐接收方
35       * @return
36       * @see Receiver
37       * @see ReceiverResult
38       * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_3&index=4">添加分账接收方</a>
39       *
40       */
41      public ReceiverResult addReceiver(Receiver receiver) throws WeixinException {
42          ReceiverRequest receiverRequest = new ReceiverRequest(receiver);
43          super.declareMerchant(receiverRequest);
44          String url = getRequestUri("profit_sharing_add_receiver_uri");
45          receiverRequest.setSign(weixinSignature.sign(receiverRequest, SignType.HMAC$SHA256));
46          String para = XmlStream.toXML(receiverRequest);
47          WeixinResponse response = weixinExecutor.post(url, para);
48          return response.getAsObject(new TypeReference<ReceiverResult>(){});
49      }
50  
51      /**
52       * 删除分账接收方
53       * 商户发起删除分账接收方请求,删除后不支持将结算后的钱分到该分账接收方。
54       *
55       * @param receiver
56       *          分帐接收方
57       * @return
58       * @throws WeixinException
59       * @see Receiver
60       * @see ReceiverResult
61       * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_4&index=5">删除分账接收方</a>
62       */
63      public ReceiverResult removeReceiver(Receiver receiver) throws WeixinException {
64          ReceiverRequest receiverRequest = new ReceiverRequest(receiver);
65          super.declareMerchant(receiverRequest);
66          String url = getRequestUri("profit_sharing_remove_receiver_uri");
67          receiverRequest.setSign(weixinSignature.sign(receiverRequest, SignType.HMAC$SHA256));
68          String para = XmlStream.toXML(receiverRequest);
69          WeixinResponse response = weixinExecutor.post(url, para);
70          return response.getAsObject(new TypeReference<ReceiverResult>(){});
71      }
72  
73      /**
74       * 请求分帐
75       *
76       * @param transactionId
77       *          微信订单号
78       * @param outOrderNo
79       *          商户分帐单号
80       * @param receivers
81       *          分帐接收方
82       * @param multi
83       *          是否多次分帐,默认为单次分帐,即调用分帐成功后马上解冻剩余金额给商户,不需要完结分帐。多次分帐可多次调用分帐API,需调完结分帐结束分帐
84       * @return
85       * @throws WeixinException
86       * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_1&index=1">请求单次分帐</a>
87       */
88      public ProfitSharingResult profitSharing(String transactionId, String outOrderNo, List<ReceiverProfit> receivers,
89                                               Boolean multi) throws WeixinException {
90          ProfitSharingRequest request = new ProfitSharingRequest(transactionId, outOrderNo, receivers);
91          super.declareMerchant(request);
92          String url = multi==null || multi.booleanValue()==false ? getRequestUri("profit_sharing_uri") :
93                  getRequestUri("multi_profit_sharing_uri");
94          request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
95          String para = XmlStream.toXML(request);
96          WeixinResponse response = getWeixinSSLExecutor().post(url, para);
97          return response.getAsObject(new TypeReference<ProfitSharingResult>(){});
98      }
99  
100     /**
101      * 分帐查询
102      *
103      * @param transactionId
104      *          微信订单号
105      * @param outOrderNo
106      *          商户分帐单号
107      * @return
108      * @throws WeixinException
109      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_2&index=3">分帐查询</a>
110      */
111     public ProfitSharingResult profitSharingQuery(String transactionId, String outOrderNo) throws WeixinException {
112         ProfitSharingRequest request = new ProfitSharingRequest(transactionId, outOrderNo, null);
113         request.setMchId(weixinAccount.getMchId());
114         request.setNonceStr(RandomUtil.generateString(16));
115         request.setSubMchId(weixinAccount.getSubMchId());
116         String url = getRequestUri("profit_sharing_query_uri");
117         request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
118         String para = XmlStream.toXML(request);
119         WeixinResponse response = weixinExecutor.post(url, para);
120         return response.getAsObject(new TypeReference<ProfitSharingResult>(){});
121     }
122 
123     /**
124      * 完结分账
125      * 1、不需要进行分账的订单,可直接调用本接口将订单的金额全部解冻给本商户
126      * 2、调用多次分账接口后,需要解冻剩余资金时,调用本接口将剩余的分账金额全部解冻给特约商户
127      * 3、已调用请求单次分账后,剩余待分账金额为零,不需要再调用此接口。
128      *
129      * @param transactionId
130      *          微信订单号
131      * @param outOrderNo
132      *          商户分帐单号
133      * @param description
134      *          分帐完结描述
135      * @return
136      * @throws WeixinException
137      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_5&index=6">完结分账</a>
138      */
139     public ProfitSharingResult profitSharingFinish(String transactionId, String outOrderNo, String description)
140             throws WeixinException {
141         ProfitSharingRequest request = new ProfitSharingRequest(transactionId, outOrderNo, null);
142         request.setDescription(description);
143         super.declareMerchant(request);
144         String url = getRequestUri("profit_sharing_finish_uri");
145         request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
146         String para = XmlStream.toXML(request);
147         WeixinResponse response = getWeixinSSLExecutor().post(url, para);
148         return response.getAsObject(new TypeReference<ProfitSharingResult>(){});
149     }
150 
151     /**
152      * 分账回退
153      *
154      * @param id
155      *          分帐单号
156      * @param outReturnNo
157      *          商户回退单号
158      * @param returnAccountType
159      *          回退方类型
160      * @param returnAccount
161      *          回退方账号
162      * @param returnAmount
163      *          回退金额
164      * @param description
165      *          回退描述
166      * @return
167      * @throws WeixinException
168      * @see ProfitSharingReturnRequest
169      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_7&index=7">分账回退</a>
170      */
171     public ProfitSharingReturnResult profitSharingReturn(ProfitId id, String outReturnNo,
172                                                          ReturnAccountType returnAccountType, String returnAccount,
173                                                          int returnAmount, String description) throws WeixinException{
174         ProfitSharingReturnRequest request;
175         if(id.getIdType()== ProfitIdType.ORDER_ID){
176             request = new ProfitSharingReturnRequest(id.getId(), null, outReturnNo, returnAccountType,
177                     returnAccount, returnAmount, description);
178         }else{
179             request = new ProfitSharingReturnRequest(null, id.getId(), outReturnNo, returnAccountType,
180                     returnAccount, returnAmount, description);
181         }
182         super.declareMerchant(request);
183         String url = getRequestUri("profit_sharing_return_uri");
184         request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
185         String para = XmlStream.toXML(request);
186         WeixinResponse response = getWeixinSSLExecutor().post(url, para);
187         return response.getAsObject(new TypeReference<ProfitSharingReturnResult>(){});
188     }
189 
190     /**
191      * 回退结果查询
192      *
193      * @param id
194      *          分帐单号
195      * @param outReturnNo
196      *          商户回退单号
197      * @return
198      * @throws WeixinException
199      * @see ProfitSharingReturnRequest
200      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_8&index=8">回退结果查询</a>
201      */
202     public ProfitSharingReturnResult profitSharingReturnQuery(ProfitId id, String outReturnNo) throws WeixinException{
203         ProfitSharingReturnRequest request;
204         if(id.getIdType()== ProfitIdType.ORDER_ID){
205             request = new ProfitSharingReturnRequest(id.getId(), null, outReturnNo);
206         }else{
207             request = new ProfitSharingReturnRequest(null, id.getId(), outReturnNo);
208         }
209         super.declareMerchant(request);
210         String url = getRequestUri("profit_sharing_return_query_uri");
211         request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
212         String para = XmlStream.toXML(request);
213         WeixinResponse response = weixinExecutor.post(url, para);
214         return response.getAsObject(new TypeReference<ProfitSharingReturnResult>(){});
215     }
216 }