puzhibing
2025-02-05 74b0e0814e37d640596f44ec86d20fa9ecce9ed6
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.ruoyi.payment.ali.v2;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.ruoyi.payment.ali.config.AliProperties;
import com.ruoyi.payment.ali.config.SignType;
import com.ruoyi.payment.api.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
 
 
/**
 * 支付宝小程序支付
 * @author zhibing.pu
 * @Date 2024/8/23 16:05
 */
@Component
public class AppletPayUtil {
    private static Logger log = LoggerFactory.getLogger(AppletPayUtil.class);
    
    @Resource
    private AliProperties aliProperties;
    
    
    /**
     * 创建统一收单交易
     * @param pojo
     */
    public PaymentResp payment(PaymentReq pojo){
        try {
            // 初始化SDK
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
            // 构造请求参数以调用接口
            AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
            AlipayTradeCreateModel model = new AlipayTradeCreateModel();
            // 设置商户订单号
            model.setOutTradeNo(pojo.getOutTradeNo());
            // 设置产品码
            model.setProductCode("JSAPI_PAY");
            // 设置小程序支付中
            model.setOpAppId(aliProperties.getAppId());
            // 设置订单总金额
            model.setTotalAmount(pojo.getTotalAmount());
            // 设置订单标题
            model.setSubject(pojo.getSubject());
            // 设置订单附加信息
            model.setBody(pojo.getBody());
            // 设置买家支付宝用户唯一标识
            model.setBuyerOpenId(pojo.getBuyerOpenId());
            //超时相对时间
            model.setTimeoutExpress(pojo.getTimeoutExpress());
            //异步返回参数
            model.setPassbackParams(pojo.getPassbackParams());
            request.setBizModel(model);
            request.setNotifyUrl(aliProperties.getNotifyUrl() + pojo.getNotifyUrl());
            
            AlipayTradeCreateResponse response = alipayClient.execute(request);
            log.info("-----调起支付宝支付-----");
            log.info("请求参数:{}", JSON.toJSONString(request));
            log.info("返回结果:{}", response.getBody());
            if (response.isSuccess()) {
                return PaymentResp.build(response.getOutTradeNo(), response.getTradeNo());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    
    /**
     * 查询支付订单
     * @param outTradeNo 业务流水号
     * @return
     */
    public QueryResp query(String outTradeNo){
        try {
            // 初始化SDK
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
            // 构造请求参数以调用接口
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            AlipayTradeQueryModel model = new AlipayTradeQueryModel();
            // 设置订单支付时传入的商户订单号
            model.setOutTradeNo(outTradeNo);
            request.setBizModel(model);
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            log.info("-----查询支付宝支付-----");
            log.info("请求参数:{}", outTradeNo);
            log.info("返回结果:{}", response.getBody());
            if (response.isSuccess()) {
                return JSON.parseObject(response.getBody(), QueryResp.class);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    
    /**
     * 交易退款
     * @param req
     * @return
     */
    public RefundResp refund(RefundReq req){
        try {
            // 初始化SDK
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
            // 构造请求参数以调用接口
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            AlipayTradeRefundModel model = new AlipayTradeRefundModel();
            // 设置商户订单号
            model.setOutTradeNo(req.getOutTradeNo());
            // 设置退款商户订单号
            model.setOutRequestNo(req.getOutRequestNo());
            // 设置退款金额
            model.setRefundAmount(req.getRefundAmount());
            // 设置退款原因说明
            model.setRefundReason(req.getRefundReason());
            request.setBizModel(model);
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            log.info("-----支付宝退款-----");
            log.info("请求参数:{}", req);
            log.info("返回结果:{}", response.getBody());
            if (response.isSuccess()) {
                JSONObject jsonObject = JSON.parseObject(response.getBody());
                RefundResp alipay_trade_refund_response = jsonObject.getObject("alipay_trade_refund_response", RefundResp.class);
                return alipay_trade_refund_response;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 退款查询
     * @param req
     * @return
     */
    public QueryRefundResp queryRefund(QueryRefundReq req){
        try {
            // 初始化SDK
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
            // 构造请求参数以调用接口
            AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
            AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
            // 设置商户订单号
            model.setOutTradeNo(req.getOutTradeNo());
            model.setOutRequestNo(req.getOutRequestNo());
            request.setBizModel(model);
            AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
            log.info("-----查询支付宝退款-----");
            log.info("请求参数:{}", req);
            log.info("返回结果:{}", response.getBody());
            if (response.isSuccess()) {
                return JSON.parseObject(response.getBody(), QueryRefundResp.class);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 支付宝关闭订单
     * @param outTradeNo 业务流水号
     * @return
     */
    public boolean close(String outTradeNo){
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
            AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
            AlipayTradeCloseModel model = new AlipayTradeCloseModel();
            model.setOutTradeNo(outTradeNo);
            request.setBizModel(model);
            AlipayTradeCloseResponse response = alipayClient.execute(request);
            log.info("-----关闭支付宝支付订单-----");
            log.info("请求参数:{}", outTradeNo);
            log.info("返回结果:{}", response.getBody());
            if(response.isSuccess()){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    
    
    
    /**
     * 构建配置数据
     * @return
     */
    private AlipayConfig getAlipayConfig() throws Exception {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(aliProperties.getV2Path());
        alipayConfig.setAppId(aliProperties.getAppId());
        alipayConfig.setFormat("json");
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType(SignType.RSA2.getType());
        //判断加签方式
        String signType = aliProperties.getSignType();
        if(SignType.SECRET_KEY.getType().equals(signType)){
            alipayConfig.setPrivateKey(aliProperties.getPrivateKey());
            alipayConfig.setAlipayPublicKey(aliProperties.getAlipayPublicKey());
            return alipayConfig;
        }
        if(SignType.CERT.getType().equals(signType)){
            alipayConfig.setAppCertPath(aliProperties.getAppCertPath());
            alipayConfig.setAlipayPublicCertPath(aliProperties.getAlipayPublicCertPath());
            alipayConfig.setRootCertPath(aliProperties.getRootCertPath());
            return alipayConfig;
        }
        throw new RuntimeException("构建配置失败");
    }
    
    
    
    
    
}