huliguo
2 天以前 5d7b65670282a4fad015e37d567cfa171b162052
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.ruoyi.errand.utils;
 
import cn.hutool.http.*;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
 
 
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
 
import java.util.*;
 
@Slf4j
public class PaymentUtil {
    
    //微信公众号、微信小程序、微信 APP+/H5、云微小程序支付
    private static final String appId = "wxdeed472c98e42a54";
    /**
     * 商户密钥
     */
    private static final String key = "925899fcc374430f9e4b4ba3db05b448";
    /**
     * 商户号
     */
    private static final String merchantNo = "888122600004175";
    /**
     * 平台-报备商户号
     */
    private static final String sysTradeMerchantNo = "777168500885852";
    /**
     * 支付回调地址
     */
    private static final String callbackUrl = "https://www.qijisheng.top";
    
    
    /**
     * 支付
     * @param orderNo           商户订单号
     * @param amount            订单金额
     * @param productName       商品名称
     * @param productDesc       商品描述
     * @param mp                公用回传参数
     * @param notifyUrl         服务器异步通知地址
     * @param openId            微信 Openid
     * @param tradeMerchantNo   报备商户号
     * @return
     */
    public static UniPayResult uniPay(String orderNo, Double amount, String productName, String productDesc, String mp, String notifyUrl, String openId, String tradeMerchantNo){
        String url = "https://trade.joinpay.com/tradeRt/uniPay";
        HttpRequest post = HttpUtil.createPost(url);
        post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
        JSONObject body = new JSONObject();
        //版本号
        body.put("p0_Version", "2.5");
        //商户编号
        body.put("p1_MerchantNo", merchantNo);
        //商户订单号
        body.put("p2_OrderNo", orderNo);
        //订单金额
        body.put("p3_Amount", amount);
        //交易币种
        body.put("p4_Cur", "1");
        //商品名称
        body.put("p5_ProductName", productName);
        //商品描述
        body.put("p6_ProductDesc", productDesc);
        //公用回传参数
        body.put("p7_Mp", mp);
        //服务器异步通知地址
        body.put("p9_NotifyUrl", callbackUrl + notifyUrl);
        //交易类型
        body.put("q1_FrpCode", FrpCodeEnum.WEIXIN_XCX.getCode());
        //微信 Openid
        body.put("q5_OpenId", openId);
        //APPID
        body.put("q7_AppId", appId);
        //报备商户号
        body.put("qa_TradeMerchantNo", StringUtils.isNotEmpty(tradeMerchantNo) ? tradeMerchantNo : sysTradeMerchantNo);
        String sign = null;
        try {
            sign = sign(body);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        body.put("hmac", sign);
        post.form(body);
        log.info("支付接口请求参数:" + body);
        HttpResponse execute = post.execute();
        log.info("支付接口请求响应:" + execute.body());
        if(200 != execute.getStatus()){
            log.error("支付接口异常:" + execute.body());
            return null;
        }
        UniPayResult uniPayResult = JSON.parseObject(execute.body(), UniPayResult.class);
        return uniPayResult;
    }
    
    
    /**
     * 查询支付订单
     * @param orderNo   订单号
     * @return
     */
    public static QueryOrderResult queryOrder(String orderNo){
        String url = "https://trade.joinpay.com/tradeRt/queryOrder";
        HttpRequest post = HttpUtil.createPost(url);
        post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
        JSONObject body = new JSONObject();
        //版本号
        body.put("p0_Version", "2.5");
        //商户编号
        body.put("p1_MerchantNo", merchantNo);
        //商户订单号
        body.put("p2_OrderNo", orderNo);
        String sign = null;
        try {
            sign = sign(body);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        body.put("hmac", sign);
        post.form(body);
        log.info("查询支付接口请求参数:" + body);
        HttpResponse execute = post.execute();
        log.info("查询支付接口请求响应:" + execute.body());
        if(200 != execute.getStatus()){
            log.error("查询支付接口异常:" + execute.body());
            return null;
        }
        QueryOrderResult uniPayResult = JSON.parseObject(execute.body(), QueryOrderResult.class);
        return uniPayResult;
    }
    
    
    /**
     * 退款
     * @param orderNo           支付订单号
     * @param refundOrderNo     退款订单号
     * @param refundAmount      退款金额
     * @param notifyUrl         异步通知地址
     * @return
     */
    public static RefundResult refund(String orderNo, String refundOrderNo, Double refundAmount, String notifyUrl){
        String url = "https://trade.joinpay.com/tradeRt/refund";
        HttpRequest post = HttpUtil.createPost(url);
        post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
        JSONObject body = new JSONObject();
        //版本号
        body.put("p0_Version", "2.3");
        //商户编号
        body.put("p1_MerchantNo", merchantNo);
        //商户订单号
        body.put("p2_OrderNo", orderNo);
        //商户退款订单号
        body.put("p3_RefundOrderNo", refundOrderNo);
        //退款金额
        body.put("p4_RefundAmount", refundAmount);
        //服务器异步通知地址
        body.put("p6_NotifyUrl", callbackUrl + notifyUrl);
        String sign = null;
        try {
            sign = sign(body);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        body.put("hmac", sign);
        post.form(body);
        log.info("退款接口请求参数:" + body);
        HttpResponse execute = post.execute();
        log.info("退款接口请求响应:" + execute.body());
        if(200 != execute.getStatus()){
            log.error("退款接口异常:" + execute.body());
            return null;
        }
        RefundResult uniPayResult = JSON.parseObject(execute.body(), RefundResult.class);
        return uniPayResult;
    }
    
    
    /**
     * 查询退款订单
     * @param refundOrderNo 退款订单号
     * @return
     */
    public static QueryRefundResult queryRefund(String refundOrderNo){
        String url = "https://trade.joinpay.com/tradeRt/refund";
        HttpRequest post = HttpUtil.createPost(url);
        post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
        JSONObject body = new JSONObject();
        //版本号
        body.put("p0_Version", "2.3");
        //商户编号
        body.put("p1_MerchantNo", merchantNo);
        //商户退款订单号
        body.put("p2_RefundOrderNo", refundOrderNo);
        String sign = null;
        try {
            sign = sign(body);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        body.put("hmac", sign);
        post.form(body);
        log.info("退款接口请求参数:" + body);
        HttpResponse execute = post.execute();
        log.info("退款接口请求响应:" + execute.body());
        if(200 != execute.getStatus()){
            log.error("退款接口异常:" + execute.body());
            return null;
        }
        QueryRefundResult uniPayResult = JSON.parseObject(execute.body(), QueryRefundResult.class);
        return uniPayResult;
    }
    
    
    /**
     * 关闭订单(仅支持微信和支付宝)
     * @param orderNo   订单号
     * @return
     */
    public static CloseOrderResult closeOrder(String orderNo){
        String url = "https://www.joinpay.com/trade/closeOrder.action";
        HttpRequest post = HttpUtil.createPost(url);
        post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
        JSONObject body = new JSONObject();
        //商户编号
        body.put("p1_MerchantNo", merchantNo);
        //商户订单号
        body.put("p2_OrderNo", orderNo);
        //交易类型
        body.put("p3_FrpCode", FrpCodeEnum.WEIXIN_XCX.getCode());
        String sign = null;
        try {
            sign = sign(body);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        body.put("hmac", sign);
        post.form(body);
        log.info("关闭订单接口请求参数:" + body);
        HttpResponse execute = post.execute();
        log.info("关闭订单接口请求响应:" + execute.body());
        if(200 != execute.getStatus()){
            log.error("关闭订单接口异常:" + execute.body());
            return null;
        }
        CloseOrderResult uniPayResult = JSON.parseObject(execute.body(), CloseOrderResult.class);
        return uniPayResult;
    }
    
    
    
    public static String sign(JSONObject body) {
        Set<Map.Entry<String, Object>> entries = body.entrySet();
        List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(entries);
        // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
        Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
            public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
                return (o1.getKey()).compareTo(o2.getKey());
            }
        });
        // 构造签名键值对的格式
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Object> item : infoIds) {
            if (item.getKey() != null || item.getKey() != "") {
                Object val = item.getValue();
                if (!(val == "" || val == null)) {
                    sb.append(val);
                }
            }
        }
        sb.append(key);
        log.info("待签名串:{}", sb.toString());
        return MD5AndKL.MD5(sb.toString());
    }
    
    
    public static void main(String[] args) {
//        UniPayResult uniPayResult = PaymentUtil.uniPay("852963742", 0.01D, "测试商品", "这是用于对接支付测试的商品描述",
//                "", "/order/shopping-cart/shoppingCartPaymentCallback", "ooOrs64zHLuInkZ_GF0LpIN9_Rxc", "777168500885852");
//        PaymentUtil.queryOrder("852963742");
//        PaymentUtil.closeOrder("852963742");
        
    }
}