Skip to content

Commit f04efb8

Browse files
Copilotbinarywang
andcommitted
Add service provider mode support for WxPayCodepayRequest
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
1 parent d424a97 commit f04efb8

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayCodepayRequest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,58 @@ public class WxPayCodepayRequest implements Serializable {
4545
*/
4646
@SerializedName(value = "mchid")
4747
protected String mchid;
48+
/**
49+
* <pre>
50+
* 字段名:服务商应用ID
51+
* 变量名:sp_appid
52+
* 是否必填:否
53+
* 类型:string[1,32]
54+
* 描述:
55+
* 服务商模式下使用,由微信生成的应用ID,全局唯一。
56+
* 示例值:wxd678efh567hg6787
57+
* </pre>
58+
*/
59+
@SerializedName(value = "sp_appid")
60+
protected String spAppid;
61+
/**
62+
* <pre>
63+
* 字段名:服务商商户号
64+
* 变量名:sp_mchid
65+
* 是否必填:否
66+
* 类型:string[1,32]
67+
* 描述:
68+
* 服务商模式下使用,服务商商户号,由微信支付生成并下发。
69+
* 示例值:1230000109
70+
* </pre>
71+
*/
72+
@SerializedName(value = "sp_mchid")
73+
protected String spMchid;
74+
/**
75+
* <pre>
76+
* 字段名:子商户应用ID
77+
* 变量名:sub_appid
78+
* 是否必填:否
79+
* 类型:string[1,32]
80+
* 描述:
81+
* 服务商模式下使用,由微信生成的应用ID,全局唯一。
82+
* 示例值:wxd678efh567hg6787
83+
* </pre>
84+
*/
85+
@SerializedName(value = "sub_appid")
86+
protected String subAppid;
87+
/**
88+
* <pre>
89+
* 字段名:子商户商户号
90+
* 变量名:sub_mchid
91+
* 是否必填:否
92+
* 类型:string[1,32]
93+
* 描述:
94+
* 服务商模式下使用,子商户商户号,由微信支付生成并下发。
95+
* 示例值:1230000109
96+
* </pre>
97+
*/
98+
@SerializedName(value = "sub_mchid")
99+
protected String subMchid;
48100
/**
49101
* <pre>
50102
* 字段名:商品描述

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,15 +1146,40 @@ public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayEx
11461146

11471147
@Override
11481148
public WxPayCodepayResult codepay(WxPayCodepayRequest request) throws WxPayException {
1149-
if (StringUtils.isBlank(request.getAppid())) {
1150-
request.setAppid(this.getConfig().getAppId());
1151-
}
1152-
if (StringUtils.isBlank(request.getMchid())) {
1153-
request.setMchid(this.getConfig().getMchId());
1149+
// 判断是否为服务商模式:如果设置了sp_appid或sp_mchid或sub_mchid中的任何一个,则认为是服务商模式
1150+
boolean isPartnerMode = StringUtils.isNotBlank(request.getSpAppid())
1151+
|| StringUtils.isNotBlank(request.getSpMchid())
1152+
|| StringUtils.isNotBlank(request.getSubMchid());
1153+
1154+
if (isPartnerMode) {
1155+
// 服务商模式
1156+
if (StringUtils.isBlank(request.getSpAppid())) {
1157+
request.setSpAppid(this.getConfig().getAppId());
1158+
}
1159+
if (StringUtils.isBlank(request.getSpMchid())) {
1160+
request.setSpMchid(this.getConfig().getMchId());
1161+
}
1162+
if (StringUtils.isBlank(request.getSubAppid())) {
1163+
request.setSubAppid(this.getConfig().getSubAppId());
1164+
}
1165+
if (StringUtils.isBlank(request.getSubMchid())) {
1166+
request.setSubMchid(this.getConfig().getSubMchId());
1167+
}
1168+
String url = String.format("%s/v3/pay/partner/transactions/codepay", this.getPayBaseUrl());
1169+
String body = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
1170+
return GSON.fromJson(body, WxPayCodepayResult.class);
1171+
} else {
1172+
// 直连商户模式
1173+
if (StringUtils.isBlank(request.getAppid())) {
1174+
request.setAppid(this.getConfig().getAppId());
1175+
}
1176+
if (StringUtils.isBlank(request.getMchid())) {
1177+
request.setMchid(this.getConfig().getMchId());
1178+
}
1179+
String url = String.format("%s/v3/pay/transactions/codepay", this.getPayBaseUrl());
1180+
String body = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
1181+
return GSON.fromJson(body, WxPayCodepayResult.class);
11541182
}
1155-
String url = String.format("%s/v3/pay/transactions/codepay", this.getPayBaseUrl());
1156-
String body = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
1157-
return GSON.fromJson(body, WxPayCodepayResult.class);
11581183
}
11591184

11601185
@Override

0 commit comments

Comments
 (0)