ProfitSharingApi.java

  1. package com.foxinmy.weixin4j.pay.api;

  2. import com.alibaba.fastjson.TypeReference;
  3. import com.foxinmy.weixin4j.exception.WeixinException;
  4. import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
  5. import com.foxinmy.weixin4j.pay.model.WeixinPayAccount;
  6. import com.foxinmy.weixin4j.pay.profitsharing.*;
  7. import com.foxinmy.weixin4j.pay.type.ProfitIdType;
  8. import com.foxinmy.weixin4j.pay.type.SignType;
  9. import com.foxinmy.weixin4j.pay.type.profitsharing.ReturnAccountType;
  10. import com.foxinmy.weixin4j.util.RandomUtil;
  11. import com.foxinmy.weixin4j.xml.XmlStream;

  12. import java.util.List;

  13. /**
  14.  * 微信商户平台分账接口(直连商户/服务商)
  15.  *
  16.  * @author kit (kit.li@qq.com)
  17.  * @date 2020年05月20日
  18.  * @since weixin4j-pay 1.1.0
  19.  */
  20. public class ProfitSharingApi extends MchApi {

  21.     public ProfitSharingApi(WeixinPayAccount weixinAccount) {
  22.         super(weixinAccount);
  23.     }

  24.     /**
  25.      * 添加分账接收方
  26.      * 服务商代子商户发起添加分账接收方请求,后续可通过发起分账请求将结算后的钱分到该分账接收方。
  27.      *
  28.      * @param receiver
  29.      *          分帐接收方
  30.      * @return
  31.      * @see Receiver
  32.      * @see ReceiverResult
  33.      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_3&index=4">添加分账接收方</a>
  34.      *
  35.      */
  36.     public ReceiverResult addReceiver(Receiver receiver) throws WeixinException {
  37.         ReceiverRequest receiverRequest = new ReceiverRequest(receiver);
  38.         super.declareMerchant(receiverRequest);
  39.         String url = getRequestUri("profit_sharing_add_receiver_uri");
  40.         receiverRequest.setSign(weixinSignature.sign(receiverRequest, SignType.HMAC$SHA256));
  41.         String para = XmlStream.toXML(receiverRequest);
  42.         WeixinResponse response = weixinExecutor.post(url, para);
  43.         return response.getAsObject(new TypeReference<ReceiverResult>(){});
  44.     }

  45.     /**
  46.      * 删除分账接收方
  47.      * 商户发起删除分账接收方请求,删除后不支持将结算后的钱分到该分账接收方。
  48.      *
  49.      * @param receiver
  50.      *          分帐接收方
  51.      * @return
  52.      * @throws WeixinException
  53.      * @see Receiver
  54.      * @see ReceiverResult
  55.      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_4&index=5">删除分账接收方</a>
  56.      */
  57.     public ReceiverResult removeReceiver(Receiver receiver) throws WeixinException {
  58.         ReceiverRequest receiverRequest = new ReceiverRequest(receiver);
  59.         super.declareMerchant(receiverRequest);
  60.         String url = getRequestUri("profit_sharing_remove_receiver_uri");
  61.         receiverRequest.setSign(weixinSignature.sign(receiverRequest, SignType.HMAC$SHA256));
  62.         String para = XmlStream.toXML(receiverRequest);
  63.         WeixinResponse response = weixinExecutor.post(url, para);
  64.         return response.getAsObject(new TypeReference<ReceiverResult>(){});
  65.     }

  66.     /**
  67.      * 请求分帐
  68.      *
  69.      * @param transactionId
  70.      *          微信订单号
  71.      * @param outOrderNo
  72.      *          商户分帐单号
  73.      * @param receivers
  74.      *          分帐接收方
  75.      * @param multi
  76.      *          是否多次分帐,默认为单次分帐,即调用分帐成功后马上解冻剩余金额给商户,不需要完结分帐。多次分帐可多次调用分帐API,需调完结分帐结束分帐
  77.      * @return
  78.      * @throws WeixinException
  79.      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_1&index=1">请求单次分帐</a>
  80.      */
  81.     public ProfitSharingResult profitSharing(String transactionId, String outOrderNo, List<ReceiverProfit> receivers,
  82.                                              Boolean multi) throws WeixinException {
  83.         ProfitSharingRequest request = new ProfitSharingRequest(transactionId, outOrderNo, receivers);
  84.         super.declareMerchant(request);
  85.         String url = multi==null || multi.booleanValue()==false ? getRequestUri("profit_sharing_uri") :
  86.                 getRequestUri("multi_profit_sharing_uri");
  87.         request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
  88.         String para = XmlStream.toXML(request);
  89.         WeixinResponse response = getWeixinSSLExecutor().post(url, para);
  90.         return response.getAsObject(new TypeReference<ProfitSharingResult>(){});
  91.     }

  92.     /**
  93.      * 分帐查询
  94.      *
  95.      * @param transactionId
  96.      *          微信订单号
  97.      * @param outOrderNo
  98.      *          商户分帐单号
  99.      * @return
  100.      * @throws WeixinException
  101.      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_2&index=3">分帐查询</a>
  102.      */
  103.     public ProfitSharingResult profitSharingQuery(String transactionId, String outOrderNo) throws WeixinException {
  104.         ProfitSharingRequest request = new ProfitSharingRequest(transactionId, outOrderNo, null);
  105.         request.setMchId(weixinAccount.getMchId());
  106.         request.setNonceStr(RandomUtil.generateString(16));
  107.         request.setSubMchId(weixinAccount.getSubMchId());
  108.         String url = getRequestUri("profit_sharing_query_uri");
  109.         request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
  110.         String para = XmlStream.toXML(request);
  111.         WeixinResponse response = weixinExecutor.post(url, para);
  112.         return response.getAsObject(new TypeReference<ProfitSharingResult>(){});
  113.     }

  114.     /**
  115.      * 完结分账
  116.      * 1、不需要进行分账的订单,可直接调用本接口将订单的金额全部解冻给本商户
  117.      * 2、调用多次分账接口后,需要解冻剩余资金时,调用本接口将剩余的分账金额全部解冻给特约商户
  118.      * 3、已调用请求单次分账后,剩余待分账金额为零,不需要再调用此接口。
  119.      *
  120.      * @param transactionId
  121.      *          微信订单号
  122.      * @param outOrderNo
  123.      *          商户分帐单号
  124.      * @param description
  125.      *          分帐完结描述
  126.      * @return
  127.      * @throws WeixinException
  128.      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_5&index=6">完结分账</a>
  129.      */
  130.     public ProfitSharingResult profitSharingFinish(String transactionId, String outOrderNo, String description)
  131.             throws WeixinException {
  132.         ProfitSharingRequest request = new ProfitSharingRequest(transactionId, outOrderNo, null);
  133.         request.setDescription(description);
  134.         super.declareMerchant(request);
  135.         String url = getRequestUri("profit_sharing_finish_uri");
  136.         request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
  137.         String para = XmlStream.toXML(request);
  138.         WeixinResponse response = getWeixinSSLExecutor().post(url, para);
  139.         return response.getAsObject(new TypeReference<ProfitSharingResult>(){});
  140.     }

  141.     /**
  142.      * 分账回退
  143.      *
  144.      * @param id
  145.      *          分帐单号
  146.      * @param outReturnNo
  147.      *          商户回退单号
  148.      * @param returnAccountType
  149.      *          回退方类型
  150.      * @param returnAccount
  151.      *          回退方账号
  152.      * @param returnAmount
  153.      *          回退金额
  154.      * @param description
  155.      *          回退描述
  156.      * @return
  157.      * @throws WeixinException
  158.      * @see ProfitSharingReturnRequest
  159.      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_7&index=7">分账回退</a>
  160.      */
  161.     public ProfitSharingReturnResult profitSharingReturn(ProfitId id, String outReturnNo,
  162.                                                          ReturnAccountType returnAccountType, String returnAccount,
  163.                                                          int returnAmount, String description) throws WeixinException{
  164.         ProfitSharingReturnRequest request;
  165.         if(id.getIdType()== ProfitIdType.ORDER_ID){
  166.             request = new ProfitSharingReturnRequest(id.getId(), null, outReturnNo, returnAccountType,
  167.                     returnAccount, returnAmount, description);
  168.         }else{
  169.             request = new ProfitSharingReturnRequest(null, id.getId(), outReturnNo, returnAccountType,
  170.                     returnAccount, returnAmount, description);
  171.         }
  172.         super.declareMerchant(request);
  173.         String url = getRequestUri("profit_sharing_return_uri");
  174.         request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
  175.         String para = XmlStream.toXML(request);
  176.         WeixinResponse response = getWeixinSSLExecutor().post(url, para);
  177.         return response.getAsObject(new TypeReference<ProfitSharingReturnResult>(){});
  178.     }

  179.     /**
  180.      * 回退结果查询
  181.      *
  182.      * @param id
  183.      *          分帐单号
  184.      * @param outReturnNo
  185.      *          商户回退单号
  186.      * @return
  187.      * @throws WeixinException
  188.      * @see ProfitSharingReturnRequest
  189.      * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_8&index=8">回退结果查询</a>
  190.      */
  191.     public ProfitSharingReturnResult profitSharingReturnQuery(ProfitId id, String outReturnNo) throws WeixinException{
  192.         ProfitSharingReturnRequest request;
  193.         if(id.getIdType()== ProfitIdType.ORDER_ID){
  194.             request = new ProfitSharingReturnRequest(id.getId(), null, outReturnNo);
  195.         }else{
  196.             request = new ProfitSharingReturnRequest(null, id.getId(), outReturnNo);
  197.         }
  198.         super.declareMerchant(request);
  199.         String url = getRequestUri("profit_sharing_return_query_uri");
  200.         request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
  201.         String para = XmlStream.toXML(request);
  202.         WeixinResponse response = weixinExecutor.post(url, para);
  203.         return response.getAsObject(new TypeReference<ProfitSharingReturnResult>(){});
  204.     }
  205. }