Skip to content

Commit 81df397

Browse files
w22296437978binarywang
authored andcommitted
#1010 微信支付模块增加单次分账接口
1 parent 70ce3a2 commit 81df397

File tree

10 files changed

+351
-5
lines changed

10 files changed

+351
-5
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.binarywang.wxpay.bean.profitsharing;
2+
3+
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
4+
import com.thoughtworks.xstream.annotations.XStreamAlias;
5+
import lombok.Data;
6+
import lombok.EqualsAndHashCode;
7+
import lombok.NoArgsConstructor;
8+
9+
/**
10+
* @author Wang GuangXin 2019/10/22 10:06
11+
* @version 1.0
12+
*/
13+
@Data
14+
@EqualsAndHashCode(callSuper = true)
15+
@NoArgsConstructor
16+
@XStreamAlias("xml")
17+
public class ProfitSharingResult extends BaseWxPayResult {
18+
/**
19+
* 微信订单号.
20+
*/
21+
@XStreamAlias("transaction_id")
22+
private String transactionId;
23+
/**
24+
* 商户分账单号.
25+
*/
26+
@XStreamAlias("out_order_no")
27+
private String outOrderNo;
28+
/**
29+
* 微信分账单号.
30+
*/
31+
@XStreamAlias("order_id")
32+
private String orderId;
33+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.github.binarywang.wxpay.bean.profitsharing;
2+
3+
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
4+
import com.github.binarywang.wxpay.constant.WxPayConstants;
5+
import com.github.binarywang.wxpay.exception.WxPayException;
6+
import com.thoughtworks.xstream.annotations.XStreamAlias;
7+
import lombok.*;
8+
import me.chanjar.weixin.common.annotation.Required;
9+
10+
/**
11+
* @author Wang GuangXin 2019/10/21 17:57
12+
* @version 1.0
13+
*/
14+
@Data
15+
@EqualsAndHashCode(callSuper = true)
16+
@Builder(builderMethodName = "newBuilder")
17+
@NoArgsConstructor
18+
@AllArgsConstructor
19+
@XStreamAlias("xml")
20+
public class ProfitsharingRequest extends BaseWxPayRequest {
21+
private static final long serialVersionUID = 212049937430575842L;
22+
23+
/**
24+
* <pre>
25+
* 字段名:微信订单号.
26+
* 变量名:transaction_id
27+
* 是否必填:是
28+
* String(32)
29+
* 示例值:4208450740201411110007820472
30+
* 描述:微信支付订单号
31+
* </pre>
32+
*/
33+
@XStreamAlias("transaction_id")
34+
@Required
35+
private String transactionId;
36+
37+
/**
38+
* <pre>
39+
* 字段名:商户分账单号.
40+
* 变量名:out_order_no
41+
* 是否必填:是
42+
* String(64)
43+
* 示例值:P20150806125346
44+
* 描述:商户系统内部的分账单号,在商户系统内部唯一(单次分账、多次分账、完结分账应使用不同的商户分账单号),同一分账单号多次请求等同一次。只能是数字、大小写字母_-|*@
45+
* </pre>
46+
*/
47+
@XStreamAlias("out_order_no")
48+
@Required
49+
private String outOrderNo;
50+
51+
/**
52+
* <pre>
53+
* 字段名:分账接收方列表.
54+
* 变量名:receivers
55+
* 是否必填:是
56+
* String(10240)
57+
* 示例值:[
58+
* {
59+
* "type": "MERCHANT_ID",
60+
* "account":"190001001",
61+
* "amount":100,
62+
* "description": "分到商户"
63+
* },
64+
* {
65+
* "type": "PERSONAL_WECHATID",
66+
* "account":"86693952",
67+
* "amount":888,
68+
* "description": "分到个人"
69+
* }
70+
* ]
71+
* 描述:分账接收方列表,不超过50个json对象,不能设置分账方作为分账接受方,使用Json格式
72+
* </pre>
73+
*/
74+
@XStreamAlias("receivers")
75+
@Required
76+
private String receivers;
77+
78+
79+
@Override
80+
protected void checkConstraints() throws WxPayException {
81+
/**
82+
* 目前仅支持HMAC-SHA256
83+
*/
84+
this.setSignType(WxPayConstants.SignType.HMAC_SHA256);
85+
}
86+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.binarywang.wxpay.bean.profitsharing;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @author Wang GuangXin 2019/10/22 11:07
7+
* @version 1.0
8+
*/
9+
public class Receiver implements Serializable {
10+
private String type;
11+
private String account;
12+
private Integer amount;
13+
private String description;
14+
15+
/**
16+
* @param type MERCHANT_ID:商户ID
17+
* PERSONAL_WECHATID:个人微信号PERSONAL_OPENID:个人openid(由父商户APPID转换得到)PERSONAL_SUB_OPENID: 个人sub_openid(由子商户APPID转换得到)
18+
* @param account 类型是MERCHANT_ID时,是商户ID
19+
* 类型是PERSONAL_WECHATID时,是个人微信号
20+
* 类型是PERSONAL_OPENID时,是个人openid
21+
* 类型是PERSONAL_SUB_OPENID时,是个人sub_openid
22+
* @param amount 分账金额,单位为分,只能为整数,不能超过原订单支付金额及最大分账比例金额
23+
* @param description 分账的原因描述,分账账单中需要体现
24+
*/
25+
public Receiver(String type, String account, Integer amount, String description) {
26+
this.type = type;
27+
this.account = account;
28+
this.amount = amount;
29+
this.description = description;
30+
}
31+
32+
public String getType() {
33+
return type;
34+
}
35+
36+
public String getAccount() {
37+
return account;
38+
}
39+
40+
public Integer getAmount() {
41+
return amount;
42+
}
43+
44+
public String getDescription() {
45+
return description;
46+
}
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.binarywang.wxpay.bean.profitsharing;
2+
3+
import com.google.gson.Gson;
4+
5+
import java.io.Serializable;
6+
import java.util.ArrayList;
7+
8+
/**
9+
* @author Wang GuangXin 2019/10/22 11:01
10+
* @version 1.0
11+
*/
12+
13+
public class ReceiverList implements Serializable {
14+
private static final long serialVersionUID = -1316860887694489921L;
15+
ArrayList list;
16+
17+
private ReceiverList() {
18+
}
19+
20+
/**
21+
* 获取一个实例
22+
* @return
23+
*/
24+
public static ReceiverList getInstance() {
25+
ReceiverList receiverList = new ReceiverList();
26+
receiverList.list = new ArrayList();
27+
return receiverList;
28+
}
29+
30+
/**
31+
* 添加一个分账条目
32+
* 注意微信上限为50个
33+
* @param receiver
34+
* @return
35+
*/
36+
public ReceiverList add(Receiver receiver) {
37+
this.list.add(receiver);
38+
return this;
39+
}
40+
41+
42+
/**
43+
* 转为JSON格式
44+
* @return
45+
*/
46+
public String toJSONString() {
47+
Gson gson = new Gson();
48+
return gson.toJson(this.list);
49+
}
50+
51+
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/constant/WxPayConstants.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.github.binarywang.wxpay.constant;
22

3-
import java.text.Format;
4-
import java.util.List;
5-
3+
import com.google.common.collect.Lists;
64
import org.apache.commons.lang3.time.FastDateFormat;
75

8-
import com.google.common.collect.Lists;
6+
import java.text.Format;
7+
import java.util.List;
98

109
/**
1110
* <pre>
@@ -273,4 +272,23 @@ public static class RefundStatus {
273272
*/
274273
public static final String CHANGE = "CHANGE";
275274
}
275+
276+
public static class ReceiverType {
277+
/**
278+
* 商户id
279+
*/
280+
public static final String MERCHANT_ID = "MERCHANT_ID";
281+
/**
282+
* 个人微信号
283+
*/
284+
public static final String PERSONAL_WECHATID = "PERSONAL_WECHATID";
285+
/**
286+
* 个人openid
287+
*/
288+
public static final String PERSONAL_OPENID = "PERSONAL_OPENID";
289+
/**
290+
* 个人sub_openid
291+
*/
292+
public static final String PERSONAL_SUB_OPENID = "PERSONAL_SUB_OPENID";
293+
}
276294
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.binarywang.wxpay.service;
2+
3+
import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingResult;
4+
import com.github.binarywang.wxpay.bean.profitsharing.ProfitsharingRequest;
5+
import com.github.binarywang.wxpay.exception.WxPayException;
6+
7+
/**
8+
* @author Wang GuangXin 2019/10/22 10:05
9+
* @version 1.0
10+
*/
11+
public interface ProfitSharingService {
12+
/**
13+
* 单次分账请求按照传入的分账接收方账号和资金进行分账,同时会将订单剩余的待分账金额解冻给特约商户。故操作成功后,订单不能再进行分账,也不能进行分账完结。
14+
* <p>
15+
* 接口频率:30QPS
16+
* 文档详见: https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_1&index=1
17+
* 接口链接:https://api.mch.weixin.qq.com/secapi/pay/profitsharing
18+
*
19+
* @param profitsharingRequest
20+
* @return
21+
* @throws WxPayException the wx pay exception
22+
*/
23+
ProfitSharingResult profitsharing(ProfitsharingRequest profitsharingRequest) throws WxPayException;
24+
25+
;
26+
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public interface WxPayService {
6565
*/
6666
EntPayService getEntPayService();
6767

68+
/**
69+
* 获取分账服务类.
70+
*
71+
* @return the ent pay service
72+
*/
73+
ProfitSharingService getProfitSharingService();
74+
6875
/**
6976
* 设置企业付款服务类,允许开发者自定义实现类.
7077
*
@@ -304,6 +311,7 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
304311
* @throws WxPayException the wx pay exception
305312
*/
306313
WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxPayException;
314+
307315
/**
308316
* <pre>
309317
* 查询红包记录.

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType;
1919
import com.github.binarywang.wxpay.exception.WxPayException;
2020
import com.github.binarywang.wxpay.service.EntPayService;
21+
import com.github.binarywang.wxpay.service.ProfitSharingService;
2122
import com.github.binarywang.wxpay.service.WxPayService;
2223
import com.github.binarywang.wxpay.util.SignUtils;
2324
import com.google.common.base.Joiner;
@@ -59,7 +60,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
5960
static ThreadLocal<WxPayApiData> wxApiData = new ThreadLocal<>();
6061

6162
private EntPayService entPayService = new EntPayServiceImpl(this);
62-
63+
private ProfitSharingService profitSharingService = new ProfitSharingServiceImpl(this);
6364
/**
6465
* The Config.
6566
*/
@@ -70,6 +71,11 @@ public EntPayService getEntPayService() {
7071
return entPayService;
7172
}
7273

74+
@Override
75+
public ProfitSharingService getProfitSharingService() {
76+
return profitSharingService;
77+
}
78+
7379
@Override
7480
public void setEntPayService(EntPayService entPayService) {
7581
this.entPayService = entPayService;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.binarywang.wxpay.service.impl;
2+
3+
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
4+
import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingResult;
5+
import com.github.binarywang.wxpay.bean.profitsharing.ProfitsharingRequest;
6+
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
7+
import com.github.binarywang.wxpay.exception.WxPayException;
8+
import com.github.binarywang.wxpay.service.ProfitSharingService;
9+
import com.github.binarywang.wxpay.service.WxPayService;
10+
11+
/**
12+
* @author Wang GuangXin 2019/10/22 10:13
13+
* @version 1.0
14+
*/
15+
public class ProfitSharingServiceImpl implements ProfitSharingService {
16+
private WxPayService payService;
17+
public ProfitSharingServiceImpl(WxPayService payService) {
18+
this.payService = payService;
19+
}
20+
21+
@Override
22+
public ProfitSharingResult profitsharing(ProfitsharingRequest request) throws WxPayException {
23+
request.checkAndSign(this.payService.getConfig());
24+
String url = this.payService.getPayBaseUrl() + "/secapi/pay/profitsharing";
25+
26+
String responseContent = this.payService.post(url, request.toXML(), true);
27+
ProfitSharingResult result = BaseWxPayResult.fromXML(responseContent, ProfitSharingResult.class);
28+
result.checkResult(this.payService, request.getSignType(), true);
29+
return result;
30+
}
31+
}

0 commit comments

Comments
 (0)