Browse Source

app兼容退款

xw 1 week ago
parent
commit
82c3e9fa85

+ 45 - 3
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreOrderScrmServiceImpl.java

@@ -143,6 +143,7 @@ import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
 import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
 import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
 import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult;
+import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryV3Result;
 import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
 import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
 import com.github.binarywang.wxpay.config.WxPayConfig;
@@ -204,6 +205,7 @@ import static com.fs.hisStore.constants.StoreConstants.DELIVERY;
 public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
 
     Logger logger = LoggerFactory.getLogger(getClass());
+    private static final String SPECIAL_APP_REFUND_APP_ID = "wxfefe26a6c1ff71ba";
     @Autowired
     private CompanyMoneyLogsMapper moneyLogsMapper;
 
@@ -2639,12 +2641,41 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
                     if (StringUtils.isBlank(payment.getAppId())) {
                         throw new IllegalArgumentException("appId不能为空");
                     }
+                    boolean specialAppRefund = SPECIAL_APP_REFUND_APP_ID.equals(payment.getAppId());
                     String payType = payment.getPayMode();
                     if ("wxApp".equals(payment.getPayMode())){
                         payType = "wx";
                     }
 
-                    if (payment.getPayMode() == null || payment.getPayMode().equals("wx")) {
+                    if (specialAppRefund) {
+                        WxPayService appWxPayService = getWxPayService();
+                        WxPayRefundV3Request refundRequest = new WxPayRefundV3Request();
+                        refundRequest.setOutTradeNo("store-" + payment.getPayCode());
+                        refundRequest.setOutRefundNo("store-" + payment.getPayCode());
+                        WxPayRefundV3Request.Amount amount = new WxPayRefundV3Request.Amount();
+                        Integer money = WxPayUnifiedOrderRequest.yuanToFen(payment.getPayMoney().toString());
+                        amount.setRefund(money);
+                        amount.setTotal(money);
+                        amount.setCurrency("CNY");
+                        refundRequest.setAmount(amount);
+                        try {
+                            WxPayRefundV3Result refundResult = appWxPayService.refundV3(refundRequest);
+                            WxPayRefundQueryV3Result refundQueryResult = appWxPayService.refundQueryV3(refundResult.getOutRefundNo());
+                            if (refundQueryResult != null && "SUCCESS".equals(refundQueryResult.getStatus())) {
+                                FsStorePaymentScrm paymentMap = new FsStorePaymentScrm();
+                                paymentMap.setPaymentId(payment.getPaymentId());
+                                paymentMap.setStatus(-1);
+                                paymentMap.setRefundTime(DateUtils.getNowDate());
+                                paymentMap.setRefundMoney(payment.getPayMoney());
+                                paymentService.updateFsStorePayment(paymentMap);
+                            } else {
+                                String errMsg = refundQueryResult == null ? "退款查询为空" : refundQueryResult.getStatus();
+                                throw new CustomException("退款请求失败" + errMsg);
+                            }
+                        } catch (WxPayException e) {
+                            throw new CustomException("退款请求失败" + e.getCustomErrorMsg());
+                        }
+                    } else if (payment.getPayMode() == null || payment.getPayMode().equals("wx")) {
 
                         MerchantAppConfig merchantAppConfig = merchantAppConfigMapper.selectMerchantAppConfigByAppId(payment.getAppId(),payType);
                         FsPayConfig fsPayConfig = JSON.parseObject(merchantAppConfig.getDataJson(), FsPayConfig.class);
@@ -2653,8 +2684,11 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
                         payConfig.setMchId(fsPayConfig.getWxMchId());
                         payConfig.setMchKey(fsPayConfig.getWxMchKey());
                         payConfig.setKeyPath(fsPayConfig.getKeyPath());
-                        payConfig.setPublicKeyId(fsPayConfig.getPublicKeyId());
-                        payConfig.setPublicKeyPath(fsPayConfig.getPublicKeyPath());
+                        if (StringUtils.isNotBlank(fsPayConfig.getPublicKeyId())
+                                && StringUtils.isNotBlank(fsPayConfig.getPublicKeyPath())) {
+                            payConfig.setPublicKeyId(fsPayConfig.getPublicKeyId());
+                            payConfig.setPublicKeyPath(fsPayConfig.getPublicKeyPath());
+                        }
                         payConfig.setApiV3Key(fsPayConfig.getWxApiV3Key());
                         payConfig.setPrivateKeyPath(fsPayConfig.getPrivateKeyPath());
                         payConfig.setPrivateCertPath(fsPayConfig.getPrivateCertPath());
@@ -2877,6 +2911,14 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
         payConfig.setSubAppId(StringUtils.trimToNull(null));
         payConfig.setSubMchId(StringUtils.trimToNull(null));
         payConfig.setKeyPath(payConfig1.getKeyPath());
+        if (StringUtils.isNotBlank(payConfig1.getPublicKeyId())
+                && StringUtils.isNotBlank(payConfig1.getPublicKeyPath())) {
+            payConfig.setPublicKeyId(payConfig1.getPublicKeyId());
+            payConfig.setPublicKeyPath(payConfig1.getPublicKeyPath());
+        }
+        payConfig.setApiV3Key(payConfig1.getWxApiV3Key());
+        payConfig.setPrivateKeyPath(payConfig1.getPrivateKeyPath());
+        payConfig.setPrivateCertPath(payConfig1.getPrivateCertPath());
         payConfig.setNotifyUrl(payConfig1.getNotifyUrlScrm());
         WxPayServiceImpl payService = new WxPayServiceImpl();
         payService.setConfig(payConfig);