44323
2023-12-23 aa2c6d722ed799290285b6365f011aaa74373d73
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
286
287
288
289
290
291
292
293
package com.dsh.guns.modular.system.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;
/**
 * 第三方支付工具类
 */
@Component
public class PayMoneyUtil {
    private String aliAppid = "2021004105665036";//支付宝appid
 
    private String appPrivateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKk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pbgEcrzAHdLps769iGxkYQHXHFngpQZUwtTUcoNGqIKknd1jZFrv7gsD+XkgKG7PwimehRlkwmCX5ilxtLiVgJRzRt6+5U5AMVD90a0tHzXYP0z2yjj73fBJF5KtGl0a10KZxaYrQdm1UhB00CgYBZZgzx/k9rtHC8LAqIj1CYhHejT92G53c6Gkl3vyOqN4sgKhfGmSEySfrDGPRBPZxr8ZtbIPCd5mUdberH0osWGMYFaJI1UsCy7aQwvGpniz7MhZeN7dweaOjwDs8mgtjHQ96mL4XGCDhR0BZ/wIURvZ/6iaGdhbbu9unlsWj3uQKBgQCmZYdsbbZkd3ev6f8rwyvMz+DrCQyYpY44cegBYuJgrZiQnL2fJioeN7ixX0UM48SfwsZEIrzshP/LGAwnc2MdjxKUl4jLN8SEe0NAjXOnz9Zaw740+aOmLpXcLWdP4uM2gIhWsvW1tEkQZCXmm7c9s/RsU8Pmzv+YL3+fSijOzA==";//支付宝开发者应用私钥
 
    private String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAouYvZ1v4RiydwNOnGcU0Hb4hQu0x6XUooaE2Bi6/atNOobtFdunnATGP6OMOW7yF9DpP8qH5mbFXAiaQD721y/7qlayI50UcV4mngRU4ZcaAVE3bp721Eg2H85RISa+Tb1CiOh+pc9p4l5UBseKsvB2ruHHForfZDPI8FL7AVUKBYCQPsa4zL6KAO2C6KULaTg/lCa+bYQKU0n9ca569VtdsqJUyxB9eSZjVd+9nKl62FLqp2NELGj7cXqiVBgDnBnVS5ZUO3mrBM5z/AxQbw3RwE3JqdkhzUA1BFjejAlT2zIGNOjUFagF8ao0wGElYfuk0bum6Hz5qWAt02QdNNwIDAQAB";//支付宝应用公钥
 
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmu8n/4yTHWbn7VOrNc9OsLtDL1bEQ8gC1dHkj8Wy5z0mkaOsjJRIG/28ze12M0V8jdCKuuDr5Z1OPKiqf+XO3ypguEh+mYUVMBM/cZodDFQfTY1TKLWjvQCuaqlA+QUTCK6f7T7stsgyQ1o9Jj0rXZDz6PM4QHSTzjrLIBaeqM5WIBvH+fy/X+QG5Utd+/UT0kc0JyvuKhZ65yVUd/C9VcwJJAPliRsAQNrqYterwAJ9zvw9tF11wj9W0XgJ8Ccu4x3gR1vrlLRJJo/OA97RmxPQ+5hSacWQZCUd1dwiBq+YCrKVHGTj14izRHXrLc0yBlRXo7tBOIqcy3IsvKVthQIDAQAB";//支付宝支付公钥
 
    private String appid = "";//微信appid
 
    private String appletsAppid = "";//微信小程序appid
 
    private String mchId = "";//微信商户号
 
    private String key = "";//微信商户号
 
    private String callbackPath = "http://221.182.45.100:56666/account";//支付回调网关地址
 
    private String app_cert_path = "C:/cert/alipay/user/app_cert_path.crt";//应用公钥证书路径
 
    private String alipay_cert_path = "C:/cert/alipay/user/alipay_cert_path.crt";//支付宝公钥证书文件路径
 
    private String alipay_root_cert_path = "C:/cert/alipay/user/alipay_root_cert_path.crt";//支付宝CA根证书文件路径
 
    private String certPath = "C:\\cert\\1523106371_20211206_cert\\apiclient_cert.p12";//微信证书
 
    /**
     * 支付宝支付
     */
    public ResultUtil alipay(String body, String subject, String passbackParams, String outTradeNo, String amount, String notifyUrl) {
//        //构造client
//        CertAlipayRequest certAlipayRequest = new CertAlipayRequest ();
//        //设置网关地址
//        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
//        //设置应用Id
//        certAlipayRequest.setAppId(aliAppid);
//        //设置应用私钥
//        certAlipayRequest.setPrivateKey(appPrivateKey);
//        //设置请求格式,固定值json
//        certAlipayRequest.setFormat("json");
//        //设置字符集
//        certAlipayRequest.setCharset("UTF-8");
//        //设置签名类型
//        certAlipayRequest.setSignType("RSA2");
//        //设置应用公钥证书路径
//        certAlipayRequest.setCertPath(app_cert_path);
//        //设置支付宝公钥证书路径
//        certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);
//        //设置支付宝根证书路径
//        certAlipayRequest.setRootCertPath(alipay_root_cert_path);
//        //构造client
//        AlipayClient alipayClient = null;
//        try {
//            alipayClient = new DefaultAlipayClient(certAlipayRequest);
//        } catch (AlipayApiException e) {
//            e.printStackTrace();
//        }
//        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
//        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest ();
//        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
//        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel ();
//        model.setBody(body);
//        model.setSubject (subject);
//        model.setOutTradeNo (outTradeNo);
//        model.setTimeoutExpress ("30m" );
//        model.setTotalAmount (amount);
//        model.setProductCode ( "QUICK_MSECURITY_PAY" );
//        model.setPassbackParams(passbackParams);//自定义参数
//        request.setBizModel ( model );
//        request.setNotifyUrl (callbackPath + notifyUrl);
//        try  {
//            //这里和普通的接口调用不同,使用的是sdkExecute
//            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
//            Map<String, String> map = new HashMap<>();
//            map.put("orderString", response.getBody());
//            System.out.println(map);//就是orderString 可以直接给客户端请求,无需再做处理。
//            return ResultUtil.success(map);
//        }  catch (AlipayApiException e ) {
//            e.printStackTrace();
//        }
 
 
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(body);//对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。
        model.setSubject(subject);//商品的标题/交易标题/订单标题/订单关键字等。
        model.setOutTradeNo(outTradeNo);//商户网站唯一订单号
        model.setTimeoutExpress("30m");
        model.setTotalAmount(amount);//付款金额
        model.setProductCode("QUICK_MSECURITY_PAY");
        model.setPassbackParams(passbackParams);//自定义参数
 
 
        //分账
//        Integer    coursePackagePayments = coursePackageClient.queryByCode(outTradeNo);
//        Integer paymentCompetitions = competitionsClient.queryByCode(outTradeNo);
//        Integer siteBookings = siteClient.queryByCode(outTradeNo);
//        List<Integer> stores = new ArrayList<>();
//        stores.add(coursePackagePayments);
//        stores.add(paymentCompetitions);
//        stores.add(siteBookings);
//
//        OperatorUser operatorUser = siteClient.queryOperator(stores);
//
//        String alipayProportion = operatorUser.getAlipayProportion();
//        String alipayNum = operatorUser.getAlipayNum();
//
//        ExtendParams extendParams = new ExtendParams();
////        extendParams.setSysServiceProviderId("YOUR_SERVICE_PROVIDER_ID");
//        model.setExtendParams(extendParams);
//
//        RoyaltyInfo royaltyInfo = new RoyaltyInfo();
////        royaltyInfo.setRoyaltyType("transfer");
//
//
//        RoyaltyDetailInfos royaltyDetailInfo1 = new RoyaltyDetailInfos();
//        royaltyDetailInfo1.setTransOutType("userId");
//        royaltyDetailInfo1.setTransOut(aliAppid);
//        royaltyDetailInfo1.setTransInType("loginName");
//        royaltyDetailInfo1.setTransIn("18398968484");
//
//        royaltyDetailInfo1.setDesc("分账描述1");
//        royaltyDetailInfo1.setAmountPercentage(alipayProportion);
//        List<RoyaltyDetailInfos> royaltyDetailInfos = new ArrayList<>();
//
//
//
//        royaltyInfo.setRoyaltyDetailInfos(royaltyDetailInfos);
//        model.setRoyaltyInfo(royaltyInfo);
//        System.err.println("=================="+royaltyInfo);
        //
        request.setBizModel(model);
        request.setNotifyUrl(callbackPath + notifyUrl);
        try {
            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            Map<String, String> map = new HashMap<>();
            map.put("orderString", response.getBody());
            map.put("returnId", passbackParams);
            System.out.println(map);//就是orderString 可以直接给客户端请求,无需再做处理。
            return ResultUtil.success(map);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
 
 
    /**
     * 支付宝查询订单支付状态
     *
     * @param out_trade_no
     * @return
     * @throws Exception
     */
    public AlipayTradeQueryResponse queryALIOrder(String out_trade_no) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{" +
                "  \"out_trade_no\":\"" + out_trade_no + "\"" +
                "}");
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        return response;
 
    }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    /**
     * 获取请求内容
     *
     * @param request
     * @return
     * @throws IOException
     */
    private String getParam(HttpServletRequest request) throws IOException {
        // 读取参数
        InputStream inputStream;
        StringBuilder sb = new StringBuilder();
        inputStream = request.getInputStream();
        String s;
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        while ((s = in.readLine()) != null) {
            sb.append(s);
        }
        in.close();
        inputStream.close();
        return sb.toString();
    }
 
 
 
 
 
 
 
//    public static void main(String[] ages){
//        PayMoneyUtil payMoneyUtil = new PayMoneyUtil();
//        ResultUtil ce = payMoneyUtil.alipay("测试", "测试", "", "121456457", "10", "http://123.com");
//        System.err.println(ce);
//        ResultUtil resultUtil = null;
//        try {
//            resultUtil = payMoneyUtil.queryALIOrder("121456457");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.err.println(resultUtil);
//    }
 
 
    /**
     * xml转map
     *
     * @param xml
     * @param charset
     * @return
     * @throws UnsupportedEncodingException
     * @throws DocumentException
     */
    public static Map<String, String> xmlToMap(String xml, String charset) throws UnsupportedEncodingException, DocumentException {
 
        Map<String, String> respMap = new HashMap<String, String>();
 
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new ByteArrayInputStream(xml.getBytes(charset)));
        Element root = doc.getRootElement();
        xmlToMap(root, respMap);
        return respMap;
    }
 
    public static Map<String, String> xmlToMap(Element tmpElement, Map<String, String> respMap) {
        if (tmpElement.isTextOnly()) {
            respMap.put(tmpElement.getName(), tmpElement.getText());
            return respMap;
        }
 
        @SuppressWarnings("unchecked")
        Iterator<Element> eItor = tmpElement.elementIterator();
        while (eItor.hasNext()) {
            Element element = eItor.next();
            xmlToMap(element, respMap);
        }
        return respMap;
    }
}