1
0
mirror of synced 2025-12-14 10:58:07 +08:00

🎨 #2903 【微信支付】优化服务商模式的微信支付分相关代码

1.优化请求参数赋值逻辑
2.新增服务商模式的”授权/解除授权服务回调通知结果“实体类
This commit is contained in:
LinZhaoguan
2022-12-17 14:39:20 +08:00
committed by GitHub
parent 749f326f94
commit e0a39c8d68
6 changed files with 128 additions and 30 deletions

View File

@@ -3,20 +3,19 @@ package com.github.binarywang.wxpay.bean.payscore;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.common.util.json.WxGsonBuilder;
/**
* @author hallkk
* created on 2022/05/18
*/
@Data @Data
@SuperBuilder @SuperBuilder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Accessors(chain = true) @Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class WxPartnerPayScoreRequest extends WxPayScoreRequest { public class WxPartnerPayScoreRequest extends WxPayScoreRequest {
private static final long serialVersionUID = 6269843192878112955L; private static final long serialVersionUID = 6269843192878112955L;
@@ -24,15 +23,27 @@ public class WxPartnerPayScoreRequest extends WxPayScoreRequest {
return WxGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
/**
* 子商户appid
*/
@SerializedName("sub_appid") @SerializedName("sub_appid")
private String subAppid; private String subAppid;
/**
* 子商户mchid
*/
@SerializedName("sub_mchid") @SerializedName("sub_mchid")
private String subMchid; private String subMchid;
/**
* [收付通子商户申请绑定支付分服务]的商户系统内部服务订单号
*/
@SerializedName("out_apply_no") @SerializedName("out_apply_no")
private String outApplyNo; private String outApplyNo;
/**
* [收付通子商户申请绑定支付分服务]的绑定结果通知地址
*/
@SerializedName("result_notify_url") @SerializedName("result_notify_url")
private String resultNotifyUrl; private String resultNotifyUrl;

View File

@@ -0,0 +1,67 @@
package com.github.binarywang.wxpay.bean.payscore;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import java.io.Serializable;
/**
* 授权/解除授权服务回调通知结果
* <pre>
* 文档地址https://pay.weixin.qq.com/wiki/doc/apiv3_partner/Offline/apis/chapter6_2_23.shtml
* </pre>
*/
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class WxPartnerUserAuthorizationStatusNotifyResult extends UserAuthorizationStatusNotifyResult implements Serializable {
private static final long serialVersionUID = 8809250065540275783L;
/**
* <pre>
* 字段名子商户应用ID
* 变量名sub_appid
* 是否必填:是
* 类型string[1,32]
* 描述:
* 子商户申请的公众号或移动应用APPID。
* 示例值wxd678efh567hg6787
* </pre>
*/
@SerializedName(value = "sub_appid")
private String subAppId;
/**
* <pre>
* 字段名:子商户的商户号
* 变量名sub_mchid
* 是否必填:是
* 类型string[1,32]
* 描述:
* 子商户商户号,由微信支付生成并下发。
* 示例值1230000109
* </pre>
*/
@SerializedName(value = "sub_mchid")
private String subMchId;
/**
* <pre>
* 字段名子商户公众号下openid
* 变量名sub_mchid
* 是否必填:是
* 类型string[1,32]
* 描述:
* 微信用户在商户对应sub_appid下的唯一标识。传了sub_appid的情况下则只返回sub_openid
* 示例值oUpF8uMuAJO_M2pxb1Q9zNjWeS6o
* </pre>
*/
@SerializedName(value = "sub_openid")
private String subOpenid;
}

View File

@@ -1,10 +1,7 @@
package com.github.binarywang.wxpay.service; package com.github.binarywang.wxpay.service;
import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader; import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
import com.github.binarywang.wxpay.bean.payscore.PayScoreNotifyData; import com.github.binarywang.wxpay.bean.payscore.*;
import com.github.binarywang.wxpay.bean.payscore.UserAuthorizationStatusNotifyResult;
import com.github.binarywang.wxpay.bean.payscore.WxPartnerPayScoreRequest;
import com.github.binarywang.wxpay.bean.payscore.WxPartnerPayScoreResult;
import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.exception.WxPayException;
/** /**
@@ -241,7 +238,7 @@ public interface PartnerPayScoreService {
/** /**
* <pre> * <pre>
* 授权/解除授权服务回调数据处理 * 授权/解除授权服务回调数据处理
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter4_4.shtml * 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/Offline/apis/chapter6_2_23.shtml
* </pre> * </pre>
* *
* @param notifyData 通知数据 * @param notifyData 通知数据
@@ -249,12 +246,12 @@ public interface PartnerPayScoreService {
* @return 解密后通知数据 return user authorization status notify result * @return 解密后通知数据 return user authorization status notify result
* @throws WxPayException the wx pay exception * @throws WxPayException the wx pay exception
*/ */
UserAuthorizationStatusNotifyResult parseUserAuthorizationStatusNotifyResult(String notifyData, SignatureHeader header) throws WxPayException; WxPartnerUserAuthorizationStatusNotifyResult parseUserAuthorizationStatusNotifyResult(String notifyData, SignatureHeader header) throws WxPayException;
/** /**
* <pre> * <pre>
* 支付分回调内容解析方法 * 支付分回调内容解析方法
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter5_2.shtml * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/Offline/apis/chapter6_2_9.shtml
* </pre> * </pre>
* *
* @param data the data * @param data the data

View File

@@ -1370,4 +1370,11 @@ public interface WxPayService {
* @return the transfers service * @return the transfers service
*/ */
TransferService getTransferService(); TransferService getTransferService();
/**
* 获取服务商支付分服务类
* @return the partner pay score service
*/
PartnerPayScoreService getPartnerPayScoreService();
} }

View File

@@ -118,6 +118,9 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
@Getter @Getter
private final TransferService transferService = new TransferServiceImpl(this); private final TransferService transferService = new TransferServiceImpl(this);
@Getter
private final PartnerPayScoreService partnerPayScoreService = new PartnerPayScoreServiceImpl(this);
@Getter @Getter
private final MerchantTransferService merchantTransferService = new MerchantTransferServiceImpl(this); private final MerchantTransferService merchantTransferService = new MerchantTransferServiceImpl(this);

View File

@@ -1,10 +1,7 @@
package com.github.binarywang.wxpay.service.impl; package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader; import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
import com.github.binarywang.wxpay.bean.payscore.PayScoreNotifyData; import com.github.binarywang.wxpay.bean.payscore.*;
import com.github.binarywang.wxpay.bean.payscore.UserAuthorizationStatusNotifyResult;
import com.github.binarywang.wxpay.bean.payscore.WxPartnerPayScoreRequest;
import com.github.binarywang.wxpay.bean.payscore.WxPartnerPayScoreResult;
import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.PartnerPayScoreService; import com.github.binarywang.wxpay.service.PartnerPayScoreService;
@@ -41,22 +38,24 @@ public class PartnerPayScoreServiceImpl implements PartnerPayScoreService {
request.setAppid(request.getAppid()); request.setAppid(request.getAppid());
request.setServiceId(request.getServiceId()); request.setServiceId(request.getServiceId());
WxPayConfig config = this.payService.getConfig(); WxPayConfig config = this.payService.getConfig();
String permissionNotifyUrl = config.getPayScorePermissionNotifyUrl(); if(StringUtils.isBlank(request.getAppid())){
if (StringUtils.isBlank(permissionNotifyUrl)) { request.setAppid(config.getAppId());
throw new WxPayException("授权回调地址未配置");
} }
String authorizationCode = request.getAuthorizationCode(); if(StringUtils.isBlank((request.getServiceId()))){
if (StringUtils.isBlank(authorizationCode)) { request.setServiceId(config.getServiceId());
}
if (StringUtils.isBlank(request.getNotifyUrl())) {
request.setNotifyUrl(config.getPayScorePermissionNotifyUrl());
}
if (StringUtils.isBlank(request.getAuthorizationCode())) {
throw new WxPayException("authorizationCode不允许为空"); throw new WxPayException("authorizationCode不允许为空");
} }
request.setNotifyUrl(permissionNotifyUrl);
String result = this.payService.postV3(url, request.toJson()); String result = this.payService.postV3(url, request.toJson());
return WxPartnerPayScoreResult.fromJson(result); return WxPartnerPayScoreResult.fromJson(result);
} }
@Override @Override
public WxPartnerPayScoreResult permissionsQueryByAuthorizationCode(String serviceId, String subMchid, String authorizationCode) public WxPartnerPayScoreResult permissionsQueryByAuthorizationCode(String serviceId, String subMchid, String authorizationCode) throws WxPayException {
throws WxPayException {
if (StringUtils.isBlank(authorizationCode)) { if (StringUtils.isBlank(authorizationCode)) {
throw new WxPayException("authorizationCode不允许为空"); throw new WxPayException("authorizationCode不允许为空");
} }
@@ -163,7 +162,15 @@ public class PartnerPayScoreServiceImpl implements PartnerPayScoreService {
String url = this.payService.getPayBaseUrl() + "/v3/payscore/partner/serviceorder"; String url = this.payService.getPayBaseUrl() + "/v3/payscore/partner/serviceorder";
WxPayConfig config = this.payService.getConfig(); WxPayConfig config = this.payService.getConfig();
request.setNotifyUrl(config.getPayScoreNotifyUrl()); if(StringUtils.isBlank(request.getAppid())){
request.setAppid(config.getAppId());
}
if(StringUtils.isBlank((request.getServiceId()))){
request.setServiceId(config.getServiceId());
}
if(StringUtils.isBlank((request.getNotifyUrl()))){
request.setNotifyUrl(config.getPayScoreNotifyUrl());
}
String result = this.payService.postV3(url, request.toJson()); String result = this.payService.postV3(url, request.toJson());
return WxPartnerPayScoreResult.fromJson(result); return WxPartnerPayScoreResult.fromJson(result);
@@ -229,10 +236,14 @@ public class PartnerPayScoreServiceImpl implements PartnerPayScoreService {
public void completeServiceOrder(WxPartnerPayScoreRequest request) throws WxPayException { public void completeServiceOrder(WxPartnerPayScoreRequest request) throws WxPayException {
String outOrderNo = request.getOutOrderNo(); String outOrderNo = request.getOutOrderNo();
String url = String.format("%s/v3/payscore/partner/serviceorder/%s/complete", this.payService.getPayBaseUrl(), outOrderNo); String url = String.format("%s/v3/payscore/partner/serviceorder/%s/complete", this.payService.getPayBaseUrl(), outOrderNo);
request.setAppid(request.getAppid()); WxPayConfig config = this.payService.getConfig();
request.setServiceId(request.getServiceId()); if (StringUtils.isBlank(request.getServiceId())) {
request.setServiceId(config.getServiceId());
}
if (StringUtils.isBlank(request.getSubMchid())) {
request.setSubMchid(config.getSubMchId());
}
request.setOutOrderNo(null); request.setOutOrderNo(null);
request.setSubMchid(request.getSubMchid());
this.payService.postV3(url, request.toJson()); this.payService.postV3(url, request.toJson());
} }
@@ -254,7 +265,9 @@ public class PartnerPayScoreServiceImpl implements PartnerPayScoreService {
public WxPartnerPayScoreResult syncServiceOrder(WxPartnerPayScoreRequest request) throws WxPayException { public WxPartnerPayScoreResult syncServiceOrder(WxPartnerPayScoreRequest request) throws WxPayException {
String outOrderNo = request.getOutOrderNo(); String outOrderNo = request.getOutOrderNo();
String url = String.format("%s/v3/payscore/partner/serviceorder/%s/sync", this.payService.getPayBaseUrl(), outOrderNo); String url = String.format("%s/v3/payscore/partner/serviceorder/%s/sync", this.payService.getPayBaseUrl(), outOrderNo);
request.setAppid(this.payService.getConfig().getAppId()); if (StringUtils.isBlank(request.getAppid())) {
request.setAppid(this.payService.getConfig().getAppId());
}
request.setOutOrderNo(null); request.setOutOrderNo(null);
String result = payService.postV3(url, request.toJson()); String result = payService.postV3(url, request.toJson());
return WxPartnerPayScoreResult.fromJson(result); return WxPartnerPayScoreResult.fromJson(result);
@@ -284,7 +297,7 @@ public class PartnerPayScoreServiceImpl implements PartnerPayScoreService {
} }
@Override @Override
public UserAuthorizationStatusNotifyResult parseUserAuthorizationStatusNotifyResult(String notifyData, SignatureHeader header) throws WxPayException { public WxPartnerUserAuthorizationStatusNotifyResult parseUserAuthorizationStatusNotifyResult(String notifyData, SignatureHeader header) throws WxPayException {
PayScoreNotifyData response = parseNotifyData(notifyData, header); PayScoreNotifyData response = parseNotifyData(notifyData, header);
PayScoreNotifyData.Resource resource = response.getResource(); PayScoreNotifyData.Resource resource = response.getResource();
String cipherText = resource.getCipherText(); String cipherText = resource.getCipherText();
@@ -293,7 +306,7 @@ public class PartnerPayScoreServiceImpl implements PartnerPayScoreService {
String apiV3Key = this.payService.getConfig().getApiV3Key(); String apiV3Key = this.payService.getConfig().getApiV3Key();
try { try {
String result = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key); String result = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key);
UserAuthorizationStatusNotifyResult notifyResult = GSON.fromJson(result, UserAuthorizationStatusNotifyResult.class); WxPartnerUserAuthorizationStatusNotifyResult notifyResult = GSON.fromJson(result, WxPartnerUserAuthorizationStatusNotifyResult.class);
notifyResult.setRawData(response); notifyResult.setRawData(response);
return notifyResult; return notifyResult;
} catch (GeneralSecurityException | IOException e) { } catch (GeneralSecurityException | IOException e) {