fix 微信退款报错

This commit is contained in:
xxm1995
2023-07-13 11:21:15 +08:00
parent c39ab63846
commit d06f021d87
4 changed files with 20 additions and 30 deletions

View File

@@ -1,11 +1,12 @@
package cn.bootx.platform.daxpay.core.channel.wechat.service; package cn.bootx.platform.daxpay.core.channel.wechat.service;
import cn.bootx.platform.common.spring.exception.RetryableException; import cn.bootx.platform.common.spring.exception.RetryableException;
import cn.bootx.platform.daxpay.code.pay.PayChannelEnum;
import cn.bootx.platform.daxpay.code.paymodel.WeChatPayCode; import cn.bootx.platform.daxpay.code.paymodel.WeChatPayCode;
import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayConfig; import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayConfig;
import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayment;
import cn.bootx.platform.daxpay.core.payment.entity.Payment; import cn.bootx.platform.daxpay.core.payment.entity.Payment;
import cn.bootx.platform.daxpay.core.refund.local.AsyncRefundLocal; import cn.bootx.platform.daxpay.core.refund.local.AsyncRefundLocal;
import cn.bootx.platform.daxpay.dto.payment.RefundableInfo;
import cn.bootx.platform.daxpay.exception.payment.PayFailureException; import cn.bootx.platform.daxpay.exception.payment.PayFailureException;
import cn.bootx.platform.starter.file.service.FileUploadService; import cn.bootx.platform.starter.file.service.FileUploadService;
import cn.hutool.core.codec.Base64; import cn.hutool.core.codec.Base64;
@@ -24,6 +25,7 @@ import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import static cn.bootx.platform.daxpay.code.pay.PayStatusCode.REFUND_PROCESS_FAIL; import static cn.bootx.platform.daxpay.code.pay.PayStatusCode.REFUND_PROCESS_FAIL;
@@ -63,10 +65,14 @@ public class WeChatPayCancelService {
/** /**
* 退款 * 退款
*/ */
public void refund(Payment payment, WeChatPayment weChatPayment, BigDecimal amount, public void refund(Payment payment, BigDecimal amount,
WeChatPayConfig weChatPayConfig) { WeChatPayConfig weChatPayConfig) {
String totalFee = weChatPayment.getAmount().multiply(BigDecimal.valueOf(100)).toBigInteger().toString(); RefundableInfo refundableInfo = payment.getRefundableInfo().stream()
.filter(o -> Objects.equals(o.getPayChannel(), PayChannelEnum.WECHAT.getCode()))
.findFirst()
.orElseThrow(() -> new PayFailureException("未找到微信支付的详细信息"));
String refundFee = amount.multiply(BigDecimal.valueOf(100)).toBigInteger().toString(); String refundFee = amount.multiply(BigDecimal.valueOf(100)).toBigInteger().toString();
String totalFee = refundableInfo.getAmount().multiply(BigDecimal.valueOf(100)).toBigInteger().toString();
// 设置退款号 // 设置退款号
AsyncRefundLocal.set(IdUtil.getSnowflakeNextIdStr()); AsyncRefundLocal.set(IdUtil.getSnowflakeNextIdStr());
Map<String, String> params = RefundModel.builder() Map<String, String> params = RefundModel.builder()
@@ -79,7 +85,7 @@ public class WeChatPayCancelService {
.nonce_str(WxPayKit.generateStr()) .nonce_str(WxPayKit.generateStr())
.build() .build()
.createSign(weChatPayConfig.getApiKeyV2(), SignType.HMACSHA256); .createSign(weChatPayConfig.getApiKeyV2(), SignType.HMACSHA256);
// 获取证书文件 // 获取证书文件
if (StrUtil.isNotBlank(weChatPayConfig.getP12())){ if (StrUtil.isNotBlank(weChatPayConfig.getP12())){
String errorMsg = "微信p.12证书未配置,无法进行退款"; String errorMsg = "微信p.12证书未配置,无法进行退款";
AsyncRefundLocal.setErrorMsg(errorMsg); AsyncRefundLocal.setErrorMsg(errorMsg);

View File

@@ -1,14 +1,13 @@
package cn.bootx.platform.daxpay.core.channel.wechat.service; package cn.bootx.platform.daxpay.core.channel.wechat.service;
import cn.bootx.platform.common.core.exception.BizException; import cn.bootx.platform.common.core.exception.BizException;
import cn.bootx.platform.common.core.util.BigDecimalUtil;
import cn.bootx.platform.daxpay.code.pay.PayChannelEnum; import cn.bootx.platform.daxpay.code.pay.PayChannelEnum;
import cn.bootx.platform.daxpay.code.pay.PayStatusCode; import cn.bootx.platform.daxpay.code.pay.PayStatusCode;
import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPaymentManager;
import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayment;
import cn.bootx.platform.daxpay.core.pay.local.AsyncPayInfoLocal; import cn.bootx.platform.daxpay.core.pay.local.AsyncPayInfoLocal;
import cn.bootx.platform.daxpay.core.payment.entity.Payment; import cn.bootx.platform.daxpay.core.payment.entity.Payment;
import cn.bootx.platform.daxpay.core.payment.service.PaymentService; import cn.bootx.platform.daxpay.core.payment.service.PaymentService;
import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPaymentManager;
import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayment;
import cn.bootx.platform.daxpay.dto.pay.AsyncPayInfo; import cn.bootx.platform.daxpay.dto.pay.AsyncPayInfo;
import cn.bootx.platform.daxpay.dto.payment.PayChannelInfo; import cn.bootx.platform.daxpay.dto.payment.PayChannelInfo;
import cn.bootx.platform.daxpay.dto.payment.RefundableInfo; import cn.bootx.platform.daxpay.dto.payment.RefundableInfo;
@@ -105,16 +104,7 @@ public class WeChatPaymentService {
/** /**
* 更新退款 * 更新退款
*/ */
public void updatePayRefund(WeChatPayment weChatPayment, BigDecimal amount) { public void updatePayRefund(Long paymentId, BigDecimal amount) {
BigDecimal refundableBalance = weChatPayment.getRefundableBalance().subtract(amount);
weChatPayment.setRefundableBalance(refundableBalance);
if (BigDecimalUtil.compareTo(refundableBalance, BigDecimal.ZERO) == 0) {
weChatPayment.setPayStatus(PayStatusCode.TRADE_REFUNDED);
}
else {
weChatPayment.setPayStatus(PayStatusCode.TRADE_REFUNDING);
}
weChatPaymentManager.updateById(weChatPayment);
} }
} }

View File

@@ -4,7 +4,6 @@ import cn.bootx.platform.daxpay.code.pay.PayChannelEnum;
import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPayConfigManager; import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPayConfigManager;
import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPaymentManager; import cn.bootx.platform.daxpay.core.channel.wechat.dao.WeChatPaymentManager;
import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayConfig; import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayConfig;
import cn.bootx.platform.daxpay.core.channel.wechat.entity.WeChatPayment;
import cn.bootx.platform.daxpay.core.channel.wechat.service.WeChatPayCancelService; import cn.bootx.platform.daxpay.core.channel.wechat.service.WeChatPayCancelService;
import cn.bootx.platform.daxpay.core.channel.wechat.service.WeChatPaymentService; import cn.bootx.platform.daxpay.core.channel.wechat.service.WeChatPaymentService;
import cn.bootx.platform.daxpay.core.payment.service.PaymentService; import cn.bootx.platform.daxpay.core.payment.service.PaymentService;
@@ -47,7 +46,7 @@ public class WeChatPayRefundStrategy extends AbsPayRefundStrategy {
} }
/** /**
* 退款前对处理 包含必要的校验以及对Payment对象的创建和保存操作 * 退款前对处理, 初始化微信支付配置
*/ */
@Override @Override
public void doBeforeRefundHandler() { public void doBeforeRefundHandler() {
@@ -60,18 +59,10 @@ public class WeChatPayRefundStrategy extends AbsPayRefundStrategy {
*/ */
@Override @Override
public void doRefundHandler() { public void doRefundHandler() {
WeChatPayment weChatPayment = weChatPaymentManager.findByPaymentId(this.getPayment().getId()) weChatPayCancelService.refund(this.getPayment(), this.getRefundModeParam().getAmount(), this.weChatPayConfig);
.orElseThrow(() -> new PayFailureException("微信支付记录不存在")); weChatPaymentService.updatePayRefund(this.getPayment().getId(), this.getRefundModeParam().getAmount());
weChatPayCancelService.refund(this.getPayment(), weChatPayment, this.getRefundModeParam().getAmount(),
this.weChatPayConfig);
weChatPaymentService.updatePayRefund(weChatPayment, this.getRefundModeParam().getAmount());
paymentService.updateRefundSuccess(this.getPayment(), this.getRefundModeParam().getAmount(), PayChannelEnum.WECHAT); paymentService.updateRefundSuccess(this.getPayment(), this.getRefundModeParam().getAmount(), PayChannelEnum.WECHAT);
} }
/**
* 初始化微信支付
*/
private void initWeChatPayConfig(String appCode) {
// 检查并获取微信支付配置
}
} }

View File

@@ -17,6 +17,9 @@ import java.math.BigDecimal;
@Schema(title = "可退款信息") @Schema(title = "可退款信息")
public class RefundableInfo { public class RefundableInfo {
/**
* @see cn.bootx.platform.daxpay.code.pay.PayChannelEnum
*/
@Schema(description = "支付通道") @Schema(description = "支付通道")
private String payChannel; private String payChannel;