From db79eca1d06cc0f3cfb5aae710262aedfb95577e Mon Sep 17 00:00:00 2001
From: liuhaonan <konodioda2333@vip.qq.com>
Date: 星期二, 22 三月 2022 14:35:22 +0800
Subject: [PATCH] c3m退款实现

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/pay/wx/WxFastPayService.java |   67 +++++++++++++++++++++++++++++----
 1 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/sandu-common/src/main/java/com/sandu/common/third/pay/wx/WxFastPayService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/pay/wx/WxFastPayService.java
similarity index 74%
rename from sandu-common/src/main/java/com/sandu/common/third/pay/wx/WxFastPayService.java
rename to ximon-admin/src/main/java/com/sandu/ximon/admin/pay/wx/WxFastPayService.java
index b78b3a5..eedfb8c 100644
--- a/sandu-common/src/main/java/com/sandu/common/third/pay/wx/WxFastPayService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/pay/wx/WxFastPayService.java
@@ -1,4 +1,4 @@
-package com.sandu.common.third.pay.wx;
+package com.sandu.ximon.admin.pay.wx;
 
 import cn.hutool.core.codec.Base64;
 import cn.hutool.crypto.SecureUtil;
@@ -13,15 +13,15 @@
 import com.github.binarywang.wxpay.service.WxPayService;
 import com.sandu.common.execption.BusinessException;
 import com.sandu.common.util.IpUtil;
-import com.sandu.common.util.ResponseUtil;
+import com.sandu.ximon.admin.pay.wxpay.UsrWxPayConfigService;
+import com.sandu.ximon.dao.domain.WxConfigEntity;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedMap;
-import java.util.TreeMap;
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 /**
  * @author chenjiantian
@@ -32,6 +32,9 @@
 @AllArgsConstructor
 public class WxFastPayService {
 
+    private final com.github.binarywang.wxpay.service.WxPayService wxPayService;
+    //private final WxPayProperties wxPayProperties;
+    public static final String WECHAT_ORDER_PAY_CALLBACK_URL = "http://49.235.239.118:20012/callback/pay/wechatCallback";
 
     /**
      * app鏀粯
@@ -121,7 +124,7 @@
      * @param callbackUrl
      * @return
      */
-    public boolean refundOrder(WxPayService wxPayService, String orderId,String refundSn, Integer refundFee, String callbackUrl) {
+    public boolean refundOrder(WxPayService wxPayService, String orderId, String refundSn, Integer refundFee, String callbackUrl) {
         WxPayRefundRequest refundRequest = new WxPayRefundRequest();
         refundRequest.setOutTradeNo(orderId);
         refundRequest.setOutRefundNo(refundSn);
@@ -142,11 +145,12 @@
         }
     }
 
+
     /**
      * 鐢熸垚浜岀淮鐮佷粯娆�
      */
     public String generateQRCodePay(WxPayService wxPayService, String body,
-                                                String orderId, int totalFee, String callbackUrl) {
+                                    String orderId, int totalFee, String callbackUrl) {
         WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest();
         wxPayUnifiedOrderRequest.setBody(body);
         wxPayUnifiedOrderRequest.setOutTradeNo(orderId);
@@ -173,4 +177,51 @@
             throw new BusinessException("寰俊鏀粯澶辫触锛�" + e.getErrCodeDes());
         }
     }
+
+    /**
+     * 閫�娆�
+     * @param totalAmount
+     * @param refundAmount
+     * @param outTradeNo
+     * @param poleId
+     * @return
+     */
+    public boolean refund(Double totalAmount, Double refundAmount, String outTradeNo, Long poleId) {
+        WxConfigEntity wxConfig = UsrWxPayConfigService.getBean().getConfigByPoleId(poleId);
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
+        String refundNo = sdf.format(new Date());
+        WxPayRefundRequest refundRequest = new WxPayRefundRequest();
+        refundRequest.setOutTradeNo(outTradeNo);
+        refundRequest.setOutRefundNo(refundNo);
+        refundRequest.setTotalFee(Integer.valueOf(parseWxAmount((totalAmount + ""))));
+        refundRequest.setRefundFee(Integer.valueOf(parseWxAmount((refundAmount + ""))));
+        try {
+            WxPayRefundResult wxPayRefundResult = wxPayService.refund(refundRequest);
+            if (WxPayConstants.ResultCode.SUCCESS.equals(wxPayRefundResult.getResultCode())) {
+                return true;
+            } else {
+                log.error("寰俊閫�娆惧け璐ワ紝{}", wxPayRefundResult.getErrCodeDes());
+                throw new BusinessException("寰俊閫�娆惧け璐ワ紝" + wxPayRefundResult.getErrCodeDes());
+            }
+        } catch (WxPayException e) {
+            log.error("寰俊閫�娆惧け璐ワ紝{}", e.getErrCodeDes());
+            throw new BusinessException("寰俊閫�娆惧け璐ワ紝" + e.getErrCodeDes());
+        }
+    }
+
+    /**
+     * 杞崲涓哄井淇¢噾棰�(鍒�)
+     *
+     * @param normalAmount 0.01鍏�
+     * @return
+     */
+    public static String parseWxAmount(String normalAmount) {
+        //  淇濈暀2浣嶅皬鏁�
+        double totalAmountDouble = Double.parseDouble(new DecimalFormat("#.00").format(
+                Double.parseDouble(normalAmount)));
+        String amount = (totalAmountDouble * 100) + "";
+        amount = amount.substring(0, amount.indexOf("."));
+        return amount;
+    }
+
 }

--
Gitblit v1.9.3