2021与蓝度共同重构项目,服务端
liuhaonan
2022-03-22 db79eca1d06cc0f3cfb5aae710262aedfb95577e
ximon-admin/src/main/java/com/sandu/ximon/admin/pay/wx/WxFastPayService.java
ÎļþÃû´Ó sandu-common/src/main/java/com/sandu/common/third/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;
    }
}