Skip to content

对服务商模式的微信支付分进行优化 #2903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,47 @@
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;

/**
* @author hallkk
* created on 2022/05/18
*/

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class WxPartnerPayScoreRequest extends WxPayScoreRequest {
private static final long serialVersionUID = 6269843192878112955L;

public String toJson() {
return WxGsonBuilder.create().toJson(this);
}

/**
* 子商户appid
*/
@SerializedName("sub_appid")
private String subAppid;

/**
* 子商户mchid
*/
@SerializedName("sub_mchid")
private String subMchid;

/**
* [收付通子商户申请绑定支付分服务]的商户系统内部服务订单号
*/
@SerializedName("out_apply_no")
private String outApplyNo;

/**
* [收付通子商户申请绑定支付分服务]的绑定结果通知地址
*/
@SerializedName("result_notify_url")
private String resultNotifyUrl;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.github.binarywang.wxpay.service;

import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
import com.github.binarywang.wxpay.bean.payscore.PayScoreNotifyData;
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.bean.payscore.*;
import com.github.binarywang.wxpay.exception.WxPayException;

/**
Expand Down Expand Up @@ -241,20 +238,20 @@ WxPartnerPayScoreResult cancelServiceOrder(String serviceId, String appId, Strin
/**
* <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>
*
* @param notifyData 通知数据
* @param header 通知头部数据,不传则表示不校验头
* @return 解密后通知数据 return user authorization status notify result
* @throws WxPayException the wx pay exception
*/
UserAuthorizationStatusNotifyResult parseUserAuthorizationStatusNotifyResult(String notifyData, SignatureHeader header) throws WxPayException;
WxPartnerUserAuthorizationStatusNotifyResult parseUserAuthorizationStatusNotifyResult(String notifyData, SignatureHeader header) throws WxPayException;

/**
* <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>
*
* @param data the data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1370,4 +1370,11 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
* @return the transfers service
*/
TransferService getTransferService();

/**
* 获取服务商支付分服务类
* @return the partner pay score service
*/
PartnerPayScoreService getPartnerPayScoreService();

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
@Getter
private final TransferService transferService = new TransferServiceImpl(this);

@Getter
private final PartnerPayScoreService partnerPayScoreService = new PartnerPayScoreServiceImpl(this);

@Getter
private final MerchantTransferService merchantTransferService = new MerchantTransferServiceImpl(this);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.github.binarywang.wxpay.service.impl;

import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
import com.github.binarywang.wxpay.bean.payscore.PayScoreNotifyData;
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.bean.payscore.*;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.PartnerPayScoreService;
Expand Down Expand Up @@ -41,22 +38,24 @@ public WxPartnerPayScoreResult permissions(WxPartnerPayScoreRequest request) thr
request.setAppid(request.getAppid());
request.setServiceId(request.getServiceId());
WxPayConfig config = this.payService.getConfig();
String permissionNotifyUrl = config.getPayScorePermissionNotifyUrl();
if (StringUtils.isBlank(permissionNotifyUrl)) {
throw new WxPayException("授权回调地址未配置");
if(StringUtils.isBlank(request.getAppid())){
request.setAppid(config.getAppId());
}
String authorizationCode = request.getAuthorizationCode();
if (StringUtils.isBlank(authorizationCode)) {
if(StringUtils.isBlank((request.getServiceId()))){
request.setServiceId(config.getServiceId());
}
if (StringUtils.isBlank(request.getNotifyUrl())) {
request.setNotifyUrl(config.getPayScorePermissionNotifyUrl());
}
if (StringUtils.isBlank(request.getAuthorizationCode())) {
throw new WxPayException("authorizationCode不允许为空");
}
request.setNotifyUrl(permissionNotifyUrl);
String result = this.payService.postV3(url, request.toJson());
return WxPartnerPayScoreResult.fromJson(result);
}

@Override
public WxPartnerPayScoreResult permissionsQueryByAuthorizationCode(String serviceId, String subMchid, String authorizationCode)
throws WxPayException {
public WxPartnerPayScoreResult permissionsQueryByAuthorizationCode(String serviceId, String subMchid, String authorizationCode) throws WxPayException {
if (StringUtils.isBlank(authorizationCode)) {
throw new WxPayException("authorizationCode不允许为空");
}
Expand Down Expand Up @@ -163,7 +162,15 @@ public WxPartnerPayScoreResult createServiceOrder(WxPartnerPayScoreRequest reque
String url = this.payService.getPayBaseUrl() + "/v3/payscore/partner/serviceorder";

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());

return WxPartnerPayScoreResult.fromJson(result);
Expand Down Expand Up @@ -229,10 +236,14 @@ public WxPartnerPayScoreResult modifyServiceOrder(WxPartnerPayScoreRequest reque
public void completeServiceOrder(WxPartnerPayScoreRequest request) throws WxPayException {
String outOrderNo = request.getOutOrderNo();
String url = String.format("%s/v3/payscore/partner/serviceorder/%s/complete", this.payService.getPayBaseUrl(), outOrderNo);
request.setAppid(request.getAppid());
request.setServiceId(request.getServiceId());
WxPayConfig config = this.payService.getConfig();
if (StringUtils.isBlank(request.getServiceId())) {
request.setServiceId(config.getServiceId());
}
if (StringUtils.isBlank(request.getSubMchid())) {
request.setSubMchid(config.getSubMchId());
}
request.setOutOrderNo(null);
request.setSubMchid(request.getSubMchid());
this.payService.postV3(url, request.toJson());
}

Expand All @@ -254,7 +265,9 @@ public WxPartnerPayScoreResult payServiceOrder(String serviceId, String appId, S
public WxPartnerPayScoreResult syncServiceOrder(WxPartnerPayScoreRequest request) throws WxPayException {
String outOrderNo = request.getOutOrderNo();
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);
String result = payService.postV3(url, request.toJson());
return WxPartnerPayScoreResult.fromJson(result);
Expand Down Expand Up @@ -284,7 +297,7 @@ public WxPartnerPayScoreResult queryServiceAccountState(String outApplyNo) throw
}

@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.Resource resource = response.getResource();
String cipherText = resource.getCipherText();
Expand All @@ -293,7 +306,7 @@ public UserAuthorizationStatusNotifyResult parseUserAuthorizationStatusNotifyRes
String apiV3Key = this.payService.getConfig().getApiV3Key();
try {
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);
return notifyResult;
} catch (GeneralSecurityException | IOException e) {
Expand Down