mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
🆕 #2627【微信支付】增加V3分账动账通知解析方法
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.github.binarywang.wxpay.service;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
|
||||
import com.github.binarywang.wxpay.bean.profitsharingV3.*;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
|
||||
@@ -161,4 +162,22 @@ public interface ProfitSharingV3Service {
|
||||
*/
|
||||
ProfitSharingReceiver deleteProfitSharingReceiver(ProfitSharingReceiver receiver) throws WxPayException;
|
||||
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 分账动账通知
|
||||
*
|
||||
* 分账或分账回退成功后,微信会把相关变动结果发送给分账接收方(只支持商户)。
|
||||
* 对后台通知交互时,如果微信收到应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功。
|
||||
* 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_10.shtml
|
||||
* </pre>
|
||||
*
|
||||
* @param notifyData 分账通知实体
|
||||
* @param header 分账通知头 {@link SignatureHeader}
|
||||
* @return {@link ProfitSharingNotifyData} 资源对象
|
||||
* @throws WxPayException the wx pay exception
|
||||
* @see <a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_10.shtml">微信文档</a>
|
||||
*/
|
||||
ProfitSharingNotifyData getProfitSharingNotifyData(String notifyData, SignatureHeader header) throws WxPayException;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,24 @@
|
||||
package com.github.binarywang.wxpay.service.impl;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
|
||||
import com.github.binarywang.wxpay.bean.profitsharingV3.*;
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import com.github.binarywang.wxpay.service.ProfitSharingV3Service;
|
||||
import com.github.binarywang.wxpay.service.WxPayService;
|
||||
import com.github.binarywang.wxpay.v3.auth.Verifier;
|
||||
import com.github.binarywang.wxpay.v3.util.AesUtils;
|
||||
import com.github.binarywang.wxpay.v3.util.RsaCryptoUtil;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 微信支付V3-资金应用-分账Service
|
||||
*
|
||||
@@ -82,4 +91,48 @@ public class ProfitSharingV3ServiceImpl implements ProfitSharingV3Service {
|
||||
String result = this.payService.postV3WithWechatpaySerial(url, GSON.toJson(request));
|
||||
return GSON.fromJson(result, ProfitSharingReceiver.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProfitSharingNotifyData getProfitSharingNotifyData(String notifyData, SignatureHeader header) throws WxPayException {
|
||||
ProfitSharingNotifyData response = parseNotifyData(notifyData, header);
|
||||
ProfitSharingNotifyData.Resource resource = response.getResource();
|
||||
String cipherText = resource.getCipherText();
|
||||
String associatedData = resource.getAssociatedData();
|
||||
String nonce = resource.getNonce();
|
||||
String apiV3Key = this.payService.getConfig().getApiV3Key();
|
||||
try {
|
||||
String result = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key);
|
||||
ProfitSharingNotifyData notifyResult = GSON.fromJson(result, ProfitSharingNotifyData.class);
|
||||
return notifyResult;
|
||||
} catch (GeneralSecurityException | IOException e) {
|
||||
throw new WxPayException("解析报文异常!", e);
|
||||
}
|
||||
}
|
||||
|
||||
private ProfitSharingNotifyData parseNotifyData(String data, SignatureHeader header) throws WxPayException {
|
||||
if (Objects.nonNull(header) && !this.verifyNotifySign(header, data)) {
|
||||
throw new WxPayException("非法请求,头部信息验证失败");
|
||||
}
|
||||
return GSON.fromJson(data, ProfitSharingNotifyData.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验通知签名
|
||||
*
|
||||
* @param header 通知头信息
|
||||
* @param data 通知数据
|
||||
* @return true:校验通过 false:校验不通过
|
||||
*/
|
||||
private boolean verifyNotifySign(SignatureHeader header, String data) throws WxPayException {
|
||||
String beforeSign = String.format("%s\n%s\n%s\n",
|
||||
header.getTimeStamp(),
|
||||
header.getNonce(),
|
||||
data);
|
||||
Verifier verifier = this.payService.getConfig().getVerifier();
|
||||
if (verifier == null) {
|
||||
throw new WxPayException("证书检验对象为空");
|
||||
}
|
||||
return verifier.verify(header.getSerialNo(),
|
||||
beforeSign.getBytes(StandardCharsets.UTF_8), header.getSigned());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user