1
0
mirror of synced 2025-12-09 07:18:12 +08:00

Add service provider mode support for WxPayCodepayRequest

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-08-31 14:57:06 +00:00
parent d424a9791c
commit f04efb8c21
2 changed files with 85 additions and 8 deletions

View File

@@ -45,6 +45,58 @@ public class WxPayCodepayRequest implements Serializable {
*/
@SerializedName(value = "mchid")
protected String mchid;
/**
* <pre>
* 字段名服务商应用ID
* 变量名sp_appid
* 是否必填:否
* 类型string[1,32]
* 描述:
* 服务商模式下使用由微信生成的应用ID全局唯一。
* 示例值wxd678efh567hg6787
* </pre>
*/
@SerializedName(value = "sp_appid")
protected String spAppid;
/**
* <pre>
* 字段名:服务商商户号
* 变量名sp_mchid
* 是否必填:否
* 类型string[1,32]
* 描述:
* 服务商模式下使用,服务商商户号,由微信支付生成并下发。
* 示例值1230000109
* </pre>
*/
@SerializedName(value = "sp_mchid")
protected String spMchid;
/**
* <pre>
* 字段名子商户应用ID
* 变量名sub_appid
* 是否必填:否
* 类型string[1,32]
* 描述:
* 服务商模式下使用由微信生成的应用ID全局唯一。
* 示例值wxd678efh567hg6787
* </pre>
*/
@SerializedName(value = "sub_appid")
protected String subAppid;
/**
* <pre>
* 字段名:子商户商户号
* 变量名sub_mchid
* 是否必填:否
* 类型string[1,32]
* 描述:
* 服务商模式下使用,子商户商户号,由微信支付生成并下发。
* 示例值1230000109
* </pre>
*/
@SerializedName(value = "sub_mchid")
protected String subMchid;
/**
* <pre>
* 字段名:商品描述

View File

@@ -1146,15 +1146,40 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
@Override
public WxPayCodepayResult codepay(WxPayCodepayRequest request) throws WxPayException {
if (StringUtils.isBlank(request.getAppid())) {
request.setAppid(this.getConfig().getAppId());
// 判断是否为服务商模式如果设置了sp_appid或sp_mchid或sub_mchid中的任何一个则认为是服务商模式
boolean isPartnerMode = StringUtils.isNotBlank(request.getSpAppid())
|| StringUtils.isNotBlank(request.getSpMchid())
|| StringUtils.isNotBlank(request.getSubMchid());
if (isPartnerMode) {
// 服务商模式
if (StringUtils.isBlank(request.getSpAppid())) {
request.setSpAppid(this.getConfig().getAppId());
}
if (StringUtils.isBlank(request.getSpMchid())) {
request.setSpMchid(this.getConfig().getMchId());
}
if (StringUtils.isBlank(request.getSubAppid())) {
request.setSubAppid(this.getConfig().getSubAppId());
}
if (StringUtils.isBlank(request.getSubMchid())) {
request.setSubMchid(this.getConfig().getSubMchId());
}
String url = String.format("%s/v3/pay/partner/transactions/codepay", this.getPayBaseUrl());
String body = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
return GSON.fromJson(body, WxPayCodepayResult.class);
} else {
// 直连商户模式
if (StringUtils.isBlank(request.getAppid())) {
request.setAppid(this.getConfig().getAppId());
}
if (StringUtils.isBlank(request.getMchid())) {
request.setMchid(this.getConfig().getMchId());
}
String url = String.format("%s/v3/pay/transactions/codepay", this.getPayBaseUrl());
String body = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
return GSON.fromJson(body, WxPayCodepayResult.class);
}
if (StringUtils.isBlank(request.getMchid())) {
request.setMchid(this.getConfig().getMchId());
}
String url = String.format("%s/v3/pay/transactions/codepay", this.getPayBaseUrl());
String body = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
return GSON.fromJson(body, WxPayCodepayResult.class);
}
@Override