mitao
2024-06-06 3d2b51ea4520533de5e78f88dddf5b5c7dce4247
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.sinata.core.util;
 
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayFundTransToaccountTransferModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.core.enums.SignType;
import com.ijpay.core.kit.IpKit;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.WxPayApiConfig;
import com.ijpay.wxpay.WxPayApiConfigKit;
import com.ijpay.wxpay.model.RefundModel;
import com.ijpay.wxpay.model.TransferModel;
import com.sinata.core.common.exception.BizExceptionEnum;
import com.sinata.core.exception.GunsException;
import com.sinata.core.support.HttpKit;
import com.sinata.core.support.StrKit;
import lombok.extern.slf4j.Slf4j;
 
import java.math.BigDecimal;
import java.util.Map;
 
/**
 * 支付工具
 *
 * @author frankevil
 * @create 2023-03-06 00:41
 **/
@Slf4j
public class PayUtil {
 
    public static void aliRefund(String transactionNo, String orderNo, BigDecimal amount, String reason) {
        AlipayTradeRefundResponse result = null;
        try {
            if (StrUtil.isBlank(transactionNo) && StrUtil.isBlank(orderNo)) {
                log.error("支付交易号为空 请联系管理员");
                throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "支付交易号为空 请联系管理员");
            }
            AlipayTradeRefundModel model = new AlipayTradeRefundModel();
            if (StrUtil.isNotBlank(transactionNo)) {
                model.setTradeNo(transactionNo);
            }
            if (StrUtil.isNotBlank(orderNo)) {
                model.setOutTradeNo(orderNo);
            }
            model.setRefundAmount(amount.toString());
            model.setRefundReason(reason);
            result = AliPayApi.tradeRefundToResponse(model);
        } catch (AlipayApiException e) {
            log.error("调用退款失败,", e);
            throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "申退款失败,请联系管理员");
        }
        if (!result.isSuccess()) {
            log.error("申请提现失败," + result.getMsg());
            throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "申退款失败," + result.getMsg());
        }
    }
 
    public static void wechatRefund(String refundNo, String transactionNo, String orderNo, BigDecimal totalAmount, BigDecimal refundAmount, String reason, String notifyUrl) {
        String refundStr = "";
        try {
            if (StrUtil.isBlank(transactionNo) && StrUtil.isBlank(orderNo)) {
                log.error("支付交易号为空 请联系管理员");
                throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "支付交易号为空 请联系管理员");
            }
            WxPayApiConfig wxPayApiConfig = WxPayApiConfigKit.getWxPayApiConfig();
            Map<String, String> params = RefundModel.builder()
                    .appid(wxPayApiConfig.getAppId())
                    .mch_id(wxPayApiConfig.getMchId())
                    .nonce_str(WxPayKit.generateStr())
                    .transaction_id(transactionNo)
                    .out_trade_no(orderNo)
                    .out_refund_no(refundNo)
                    .total_fee(totalAmount.longValue() + "")
                    .refund_fee(refundAmount.longValue() + "")
                    .refund_desc(reason)
                    .notify_url(wxPayApiConfig.getDomain() + notifyUrl)
                    .build()
                    .createSign(wxPayApiConfig.getPartnerKey(), SignType.MD5);
            refundStr = WxPayApi.orderRefund(false, params, wxPayApiConfig.getCertPath(), wxPayApiConfig.getMchId());
        } catch (Exception e) {
            log.error("调用退款失败,", e);
            throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "申退款失败,请联系管理员");
        }
 
        Map<String, String> result = WxPayKit.xmlToMap(refundStr);
        String returnCode = result.get("return_code");
        if (!WxPayKit.codeIsOk(returnCode)) {
            log.error("申请提现退款," + result);
            throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "申退款失败," + result.get("return_msg"));
        }
        String resultCode = result.get("result_code");
        if (!WxPayKit.codeIsOk(resultCode)) {
            log.error("申请提现退款," + result);
            throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "申退款失败," + result.get("err_code_des"));
        }
    }
 
    public static void weChatWithdraw(String transactionFlow, String openid, BigDecimal amount, String desc) {
        String ip = IpKit.getRealIp(HttpKit.getRequest());
        if (StrKit.isBlank(ip)) {
            ip = "127.0.0.1";
        }
        WxPayApiConfig wxPayApiConfig = WxPayApiConfigKit.getWxPayApiConfig();
        Map<String, String> params = TransferModel.builder()
                .mch_appid(wxPayApiConfig.getAppId())
                .mchid(wxPayApiConfig.getMchId())
                .nonce_str(WxPayKit.generateStr())
                .partner_trade_no(transactionFlow)
                .openid(openid)
                .check_name("NO_CHECK")
                .amount(NumberUtil.mul(amount, 100).stripTrailingZeros().toPlainString())
                .desc(desc)
                .spbill_create_ip(ip)
                .build()
                .createSign(wxPayApiConfig.getPartnerKey(), SignType.MD5, false);
 
        // 提现
        String transfers = WxPayApi.transfers(params, wxPayApiConfig.getCertPath(), wxPayApiConfig.getMchId());
        Map<String, String> map = WxPayKit.xmlToMap(transfers);
        String returnCode = map.get("return_code");
        String resultCode = map.get("result_code");
        if (!WxPayKit.codeIsOk(returnCode) || !WxPayKit.codeIsOk(resultCode)) {
            log.warn("提现失败" + map.get("return_msg") + ":" + map.get("err_code_des"));
            throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "提现失败" + map.get("return_msg") + ":" + map.get("err_code_des"));
        }
    }
 
    public static void aliWithdraw(String transactionFlow, String account, BigDecimal amount, String desc) {
        AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();
        model.setOutBizNo(transactionFlow);
        model.setPayeeType("ALIPAY_LOGONID");
        model.setPayeeAccount(account);
        model.setAmount(amount.toString());
        model.setRemark(desc);
        AlipayFundTransToaccountTransferResponse response = null;
        try {
            response = AliPayApi.transferToResponse(model);
        } catch (Exception e) {
            log.warn("支付宝提现失败:" + e);
            throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "支付宝提现失败,请联系管理员");
        }
 
        if (!response.isSuccess()) {
            log.warn("支付宝提现失败:" + response);
            throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "支付宝提现失败:" + response.getSubMsg());
        }
    }
}