From da47be42f9e53b90965ef6b71466476f55836ba1 Mon Sep 17 00:00:00 2001 From: "autumn.g@foxmail.com" Date: Sat, 26 Aug 2023 18:38:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A4=E6=98=93=E6=92=A4=E9=94=80=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../operation/PaymentReverseOperation.java | 32 ++++++++++++ .../jsowell/adapay/service/AdapayService.java | 24 ++++++++- .../impl/OrderBasicInfoServiceImpl.java | 51 +++++++++++++++---- 3 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 jsowell-pile/src/main/java/com/jsowell/adapay/operation/PaymentReverseOperation.java diff --git a/jsowell-pile/src/main/java/com/jsowell/adapay/operation/PaymentReverseOperation.java b/jsowell-pile/src/main/java/com/jsowell/adapay/operation/PaymentReverseOperation.java new file mode 100644 index 000000000..fa2401c11 --- /dev/null +++ b/jsowell-pile/src/main/java/com/jsowell/adapay/operation/PaymentReverseOperation.java @@ -0,0 +1,32 @@ +package com.jsowell.adapay.operation; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.math.BigDecimal; + +/** + * 支付撤销操作对象 + */ +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class PaymentReverseOperation extends AdapayOperationInfo{ + // 支付id + private String paymentId; + + // 撤销金额 + private BigDecimal reverseAmt; + + // order/balance + private String scenarioType; + + // scenarioType = balance 会员id不为空 + private String memberId; + + // scenarioType = order 订单编号不为空 + private String orderCode; +} diff --git a/jsowell-pile/src/main/java/com/jsowell/adapay/service/AdapayService.java b/jsowell-pile/src/main/java/com/jsowell/adapay/service/AdapayService.java index ae1c3ee92..514f0e9f4 100644 --- a/jsowell-pile/src/main/java/com/jsowell/adapay/service/AdapayService.java +++ b/jsowell-pile/src/main/java/com/jsowell/adapay/service/AdapayService.java @@ -14,6 +14,7 @@ import com.jsowell.adapay.config.AbstractAdapayConfig; import com.jsowell.adapay.dto.*; import com.jsowell.adapay.factory.AdapayConfigFactory; import com.jsowell.adapay.operation.PaymentConfirmOperation; +import com.jsowell.adapay.operation.PaymentReverseOperation; import com.jsowell.adapay.response.*; import com.jsowell.adapay.vo.*; import com.jsowell.common.constant.CacheConstants; @@ -952,10 +953,27 @@ public class AdapayService { /** * 创建交易撤销请求 * 延迟分账未确认, 调交易撤销接口退款 - * + * delay模式的商户使用 + */ + public PaymentReverseResponse createPaymentReverseRequest(PaymentReverseOperation paymentReverseOperation) { + String wechatAppId = paymentReverseOperation.getMerchantKey(); + String paymentId = paymentReverseOperation.getPaymentId(); + BigDecimal reverseAmt = paymentReverseOperation.getReverseAmt(); + String memberId = paymentReverseOperation.getMemberId(); + String scenarioType = paymentReverseOperation.getScenarioType(); + String orderCode = paymentReverseOperation.getOrderCode(); + return createPaymentReverseRequest(paymentId, reverseAmt, wechatAppId, memberId, scenarioType, orderCode); + } + + /** + * 创建交易撤销请求 + * 延迟分账未确认, 调交易撤销接口退款 + * delay模式的商户使用 * @param wechatAppId 微信小程序appId */ - public PaymentReverseResponse createPaymentReverseRequest(String paymentId, BigDecimal reverseAmt, String wechatAppId, String memberId, String scenarioType, String orderCode) { + public PaymentReverseResponse createPaymentReverseRequest(String paymentId, BigDecimal reverseAmt, + String wechatAppId, String memberId, String scenarioType, + String orderCode) { AbstractAdapayConfig config = AdapayConfigFactory.getConfig(wechatAppId); if (config == null) { throw new BusinessException(ReturnCodeEnum.CODE_ADAPAY_CONFIG_IS_NULL_ERROR); @@ -967,6 +985,7 @@ public class AdapayService { reverseParams.put("reverse_amt", AdapayUtil.formatAmount(reverseAmt)); reverseParams.put("order_no", IdUtils.fastSimpleUUID()); reverseParams.put("notify_url", ADAPAY_CALLBACK_URL); + // expand 为扩展域 Map expendMap = Maps.newHashMap(); expendMap.put("memberId", memberId); @@ -976,6 +995,7 @@ public class AdapayService { } reverseParams.put("expand", expendMap); reverseParams.put("reason", expendMap); + Map paymentReverse = null; try { paymentReverse = PaymentReverse.create(reverseParams, config.getWechatAppId()); diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java index 0670a6bfb..a5f44eec3 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java @@ -9,6 +9,7 @@ import com.google.common.collect.Maps; import com.huifu.adapay.core.exception.BaseAdaPayException; import com.jsowell.adapay.dto.QueryPaymentConfirmDTO; import com.jsowell.adapay.operation.PaymentConfirmOperation; +import com.jsowell.adapay.operation.PaymentReverseOperation; import com.jsowell.adapay.response.PaymentConfirmResponse; import com.jsowell.adapay.response.PaymentReverseResponse; import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse; @@ -785,7 +786,7 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService { /** * 不延迟商家订单处理逻辑 * 例如:希晓 - * + * 相当于已经分账完成了 * @param orderBasicInfo 订单信息 * @param adapayMemberAccount 汇付用户信息 * @param wechatAppId 小程序appId @@ -1234,6 +1235,9 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService { BigDecimal deductionAmount = vo.getDeductionAmount(); // 延时分账,使用确认交易API + // PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, deductionAmount, orderCode, wechatAppId); + + // 换新的方法,逻辑不变,参数封装了 PaymentConfirmOperation operation = new PaymentConfirmOperation(); operation.setPaymentId(paymentId); operation.setConfirmAmt(deductionAmount); @@ -1242,7 +1246,6 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService { operation.setMerchantKey(wechatAppId); PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(operation); - // PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, deductionAmount, orderCode, wechatAppId); if (paymentConfirmResponse != null && paymentConfirmResponse.isNotFailed()) { confirmAmt = confirmAmt.add(new BigDecimal(paymentConfirmResponse.getConfirm_amt())); feeAmt = feeAmt.add(new BigDecimal(paymentConfirmResponse.getFee_amt())); @@ -2369,9 +2372,19 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService { // String payMode = "delay"; if (StringUtils.equalsIgnoreCase(payMode, Constants.ADAPAY_PAY_MODE_DELAY)) { // 延迟分账未确认调撤销调撤销接口退款 - PaymentReverseResponse response = adapayService.createPaymentReverseRequest( - paymentId, refundAmount, dto.getWechatAppId(), dto.getMemberId(), ScenarioEnum.ORDER.getValue(), - dto.getOrderCode()); + // PaymentReverseResponse response = adapayService.createPaymentReverseRequest( + // paymentId, refundAmount, dto.getWechatAppId(), dto.getMemberId(), ScenarioEnum.ORDER.getValue(), + // dto.getOrderCode()); + + PaymentReverseOperation operation = new PaymentReverseOperation(); + operation.setPaymentId(paymentId); + operation.setReverseAmt(refundAmount); + operation.setMerchantKey(dto.getWechatAppId()); + operation.setMemberId(dto.getMemberId()); + operation.setScenarioType(ScenarioEnum.ORDER.getValue()); + operation.setOrderCode(dto.getOrderCode()); + PaymentReverseResponse response = adapayService.createPaymentReverseRequest(operation); + if (response != null && response.isNotFailed()) { MemberAdapayRecord record = memberAdapayRecordService.selectByPaymentId(paymentId); BigDecimal reverseAmt = new BigDecimal(response.getReverse_amt()); @@ -2470,8 +2483,16 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService { BigDecimal deductionAmount = vo.getDeductionAmount(); // 调汇付的交易撤销接口 - PaymentReverseResponse paymentReverseRequest = adapayService.createPaymentReverseRequest(paymentId, deductionAmount, dto.getWechatAppId(), - dto.getMemberId(), ScenarioEnum.BALANCE.getValue(), null); + // PaymentReverseResponse paymentReverseRequest = adapayService.createPaymentReverseRequest(paymentId, deductionAmount, dto.getWechatAppId(), + // dto.getMemberId(), ScenarioEnum.BALANCE.getValue(), null); + + PaymentReverseOperation operation = new PaymentReverseOperation(); + operation.setPaymentId(paymentId); + operation.setReverseAmt(deductionAmount); + operation.setMerchantKey(dto.getWechatAppId()); + operation.setMemberId(dto.getMemberId()); + operation.setScenarioType(ScenarioEnum.BALANCE.getValue()); + PaymentReverseResponse paymentReverseRequest = adapayService.createPaymentReverseRequest(operation); if (paymentReverseRequest != null && paymentReverseRequest.isNotFailed()) { memberAdapayRecordService.updateRefundAmount(paymentId, deductionAmount); @@ -2865,9 +2886,19 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService { List paymentReverses = queryOrderAdapayRefund(orderBasicInfo); if (CollectionUtils.isEmpty(paymentReverses)) { // 如果没有退款过,重新执行一遍退款 - PaymentReverseResponse response = adapayService.createPaymentReverseRequest( - paymentId, refundAmount, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(), - orderCode); + // PaymentReverseResponse response = adapayService.createPaymentReverseRequest( + // paymentId, refundAmount, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(), + // orderCode); + + PaymentReverseOperation operation = new PaymentReverseOperation(); + operation.setPaymentId(paymentId); + operation.setReverseAmt(refundAmount); + operation.setMerchantKey(wechatAppId); + operation.setMemberId(memberId); + operation.setScenarioType(ScenarioEnum.ORDER.getValue()); + operation.setOrderCode(orderCode); + PaymentReverseResponse response = adapayService.createPaymentReverseRequest(operation); + logger.info("重试订单退款response:{}", JSON.toJSONString(response)); } else { BigDecimal reversedAmt = BigDecimal.ZERO;