xiaoyong931011
2022-08-25 5dbeaad1fd5f326b4b661523b703317f8129a5fe
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package cc.mrbird.febs.pay.service.impl;
 
import cc.mrbird.febs.mall.entity.MallOrderInfo;
import cc.mrbird.febs.mall.mapper.MallOrderInfoMapper;
import cc.mrbird.febs.pay.model.*;
import cc.mrbird.febs.pay.service.UnipayService;
import cc.mrbird.febs.pay.util.*;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
 
@Slf4j
@Service
public class UnipayServiceImpl implements UnipayService {
 
    //汇聚支付平台公钥
    public static final String platformPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwSAEXsiC0IYh" +
            "a6a94imKq8VfOkk7WjDRAQWMBRnoKOZeEUeMrHYiblcrqeMYXGpV13288iUOkuyKwkPXkYXyIQK8emvJIbQOhtB5bS" +
            "lAbodsPgncM9Ney1GFiz+7ogBxyt58mP8AA9UHtMw7u78zZoQ1+dUWwUUowVXml3Q0cVQIDAQAB";
    //本地生成的公钥
    public static final String paySecretKey =
            "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJSsoLHAy4P+H76l4i6IMZ/JKKTVphvrJBSB777N8h" +
                    "gzN38M+66IqazRQLNjjyn2UZxm4eSfuJ4OBxCTGLJgDOw8B9l9lVo+0bzhNxsy5ae20EOlQa3h8xfCjCCU" +
                    "Xb8VP8yknG43Wk1WtwLX5VYWmJFTwA1I0dWQR4RlbHH2q2aVAgMBAAECgYBZNyoK4JV/tFwaTHLo12Nn7g" +
                    "9MssRGFpmFEN/sEKuJKBrSS9kvx+SBxuPbgg/j72LTxs0hI1NmzSYiJRL53zeBFM0wM2/D2zW0pZfogUPN" +
                    "7Mb3FkFOoE0CxFwn7pjjBkvAsZQJS2fZFY6cf/WYhVI+XCMzjiQWJSQUXKEfWzUflQJBAMpLzinyAsBHKe" +
                    "6lmAkSh/yaQX76cnxNZGmfc/gj62qM5YDzpt3+Za5Y4NSq5kYu36NAgorf2ipJPRwpgS13fK8CQQC8JKVP" +
                    "2YgZ39fa+xVJu3yTT48k9EnYIEfsv/Bc7AVdpaZKDeEBuGpBb0JQzuIg3Zd6yNQCzH3oBqp8l6l/fgn7Ak" +
                    "EAtFluvHB4yWjoVk0lRPlTaP0w5P5ssKrimVPBtPh4+a4RMayHGKSjjBLKpm6SCwHg+Q8bEqpNOqO+qmvK" +
                    "MXm0GwJAZd4Bk8ZYJopIOUyRLibRQIFnI78Q7HAuAUW7QtSX4yh5bMcu+Nt8zIkNAuvBC8Ju7hAmmo1V7n" +
                    "cNgAAtydXYWQJALLOFCjCkRgeRVL8YE8bVi4U16b8ltAN1DlbWEzui6VFy2vIga3IryesNVAOOdornyAwf" +
                    "1huqB2lYfuQwtrIBKg==";
    public static final String notifyUrl = "http://47.111.90.145:8800/api/unipay/unipayCallBack";
    public static final String agreementPayNotifyUrl = "http://47.111.90.145:8800/api/unipay/agreeMentPayCallBack";
    public static final String p1MerchantNo = "888118000001971";/** 商户编号 */
    public static final String aesKey = "1234567891234567";/** 商户编号 */
 
    @Autowired
    private MallOrderInfoMapper orderInfoMapper;
 
    @Override
    @Transactional
    public String unipay(UnipayDto unipayDto) {
        String key = "2e95f6a3e11e47fa8a4386d6aefe1735";/** md5密钥商户后台-商户中心-商户设置-密钥管理获取 必填!*/
        Map<String, String> map = new HashMap<String, String>();
        map.put("p0_Version", "1.0");/** 版本号 */
        map.put("p1_MerchantNo", p1MerchantNo);/** 商户编号 */
        map.put("p2_OrderNo", unipayDto.getOrderNo()); /**商户订单号*/
        map.put("p3_Amount", unipayDto.getAmount().toString());/**订单金额*/
        map.put("p4_Cur", "1"); /**交易币种 */
        map.put("p5_ProductName", unipayDto.getProductName()); /** 商品名称 */
        map.put("p9_NotifyUrl", notifyUrl); /** 服务器异步通知地址 */
        map.put("q1_FrpCode", unipayDto.getFrpCode()); /** 交易类型*/
        map.put("qa_TradeMerchantNo",unipayDto.getTradeMerchantNo()); /** 777开头的报备商户号   必填!*/
        map.put("q7_AppId", "wx42ce0fbc27965fe9"); /** 777开头的报备商户号   必填!*/
 
        String Strmap = createLinkStringByGet(map);
        // 签名
        String sign = "";
        sign = SignByMD5(Strmap, key);
        map.put("hmac", sign);/** 签名数据 */
        System.out.println("发送:" + JSON.toJSONString(map).toString());
 
        // post请求参数内容
        HttpRequester hr = new HttpRequester();
        HttpRespons HP = null;
        try {
            HP = hr.sendPost("https://www.joinpay.com/trade/uniPayApi.action", map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("接收返回参数:" + HP.getContent());
        Boolean result = false;
        try {
            result = nosign(HP.getContent(), key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(result){
            return HP.getContent();
        }else{
            return "fail";
        }
    }
 
 
    //参数的拼接整理
    private String createLinkStringByGet(Map<String, String> params){
        // TODO Auto-generated method stub
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String str1 ="";
        for(int i=0;i<keys.size();i++) {
            String key = keys.get(i);
 
            Object value = params.get(key);//(String) 强制类型转换
            if(value instanceof Integer) {
                value = (Integer)value;
            }
            if(i==keys.size()-1) {
 
                str1 = str1+value.toString();
            }else {
 
                str1 = str1+value;
            }
        }
        System.out.println("整理"+str1);
        return str1;
    }
 
    /**
     * MD5签名
     *
     * @param requestSign 请求签名串
     * @param merchantKey 商户秘钥
     */
    private String SignByMD5(String requestSign, String merchantKey) {
 
        String reqHmac = "";
        try {
            reqHmac = DigestUtils.md5Hex(requestSign + merchantKey).toUpperCase();
        } catch (Exception e) {}
 
        return reqHmac;
    }
 
    /**
     * 验证返回参数
     * @param hp
     * @param key
     * @return
     * @throws Exception
     */
    private boolean nosign(String hp, String key){
        System.out.println("接收到:" + hp);
        JSONObject myJson = JSONObject.parseObject(hp);
        Map m = myJson;
 
        // 返回hmac
        String returnHmac = (String) m.remove("hmac");
        System.out.println(m.toString());
        String Strmap = createLinkStringByGet(m);
 
        // 返回参数组装hmac
        String hmac1 = SignByMD5(Strmap, key);
        return hmac1.equalsIgnoreCase(returnHmac);
 
    }
 
 
 
    @Override
    @Transactional
    public String agreementPay(AgreementPayDto agreementPayDto) {
 
        String secretKey =paySecretKey;
        String key = aesKey;//敏感信息加密key
 
        Map<String, Object> map = new HashMap<>();
        RequestParam requestParam = new RequestParam();
        requestParam.setMch_no(p1MerchantNo);// 商户编号
        requestParam.setMethod("fastPay.agreement.pay");// 固定方法名
        requestParam.setVersion("1.0");// 版本号
        requestParam.setRand_str("12345678901234567890123456789012");// 随机字符串
        requestParam.setSign_type("2");// 签名类型
        try {
            requestParam.setSec_key(RSAUtil.encryptByPublicKey(platformPublicKey,aesKey));//加密密钥
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        map.put("mch_order_no",agreementPayDto.getOrderNo()); // 商户订单号
        map.put("order_amount", agreementPayDto.getOrderAmount()); // 订单金额;金额保留两位小数
        map.put("mch_req_time", agreementPayDto.getOrderTime()); // 订单时间
        map.put("order_desc", agreementPayDto.getOrderDesc()); // 商品名称
        map.put("callback_url", agreementPayNotifyUrl); // 异步通知地址
        map.put("bank_card_no",  AESUtil.Aes256Encode(agreementPayDto.getBankNo(),aesKey)); // 签约银行卡号
 
        requestParam.setData(JsonUtil.toString(map));
 
        String signStr = null;
        try {
            signStr = getSortedString(requestParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("待签名字符串:"+signStr);// 待签名字符串
 
        // 签名
        if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
            try {
                signStr =(RSAUtil.sign(signStr, secretKey, false));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            signStr = "";
            System.out.println("未预期的签名类型:" + requestParam.getSign_type());
        }
        requestParam.setSign(signStr);
 
        // Map转json字符串
        String reqBodyJson = JSON.toJSONString(requestParam);
        System.out.println(reqBodyJson);
        String httpResponseJson = null;
        try {
            httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(httpResponseJson);
 
        if(StrUtil.isNotBlank(httpResponseJson)){
            return httpResponseJson;
        }else{
            return "fail";
        }
    }
 
    public static void main(String[] args) {
//        BigDecimal value = new BigDecimal("0.10858").setScale(2,BigDecimal.ROUND_HALF_UP);
        BigDecimal value = new BigDecimal("1231.1").setScale(2, BigDecimal.ROUND_DOWN);
        DecimalFormat decimalFormat = new DecimalFormat("0.00#");
        String strVal = decimalFormat.format(value);
        System.out.println(strVal);
    }
 
    @Override
    public String getAgreeMentPaySms(AgreeMentPaySmsDto agreeMentPaySmsDto) {
        String secretKey = paySecretKey;
 
        Map<String, Object> map = new HashMap<>();
        RequestParam requestParam = new RequestParam();
        requestParam.setMch_no(p1MerchantNo);// 商户编号
        requestParam.setMethod("fastPay.agreement.signSms");// 固定方法名
        requestParam.setVersion("1.0");// 版本号
        requestParam.setRand_str("12345678901234567890123456789012");// 随机字符串
        requestParam.setSign_type("2");// 签名类型
        try {
            requestParam.setSec_key(RSAUtil.encryptByPublicKey(platformPublicKey,aesKey));//加密密钥
        } catch (Exception e) {
            e.printStackTrace();
        }
 
 
        MallOrderInfo orderInfo = orderInfoMapper.selectByOrderNo(agreeMentPaySmsDto.getOrderNo());
        map.put("mch_order_no", "NO_"+orderInfo.getOrderNo()); // 商户订单号
 
        BigDecimal value = orderInfo.getAmount().setScale(2, BigDecimal.ROUND_DOWN);
        DecimalFormat decimalFormat = new DecimalFormat("0.00#");
        String strVal = decimalFormat.format(value);
        map.put("order_amount", strVal); // 订单金额
        map.put("mch_req_time", DateUtil.now()); // 下单时间
        map.put("payer_name",AESUtil.Aes256Encode(agreeMentPaySmsDto.getName(),aesKey)); // 姓名
        map.put("id_type", agreeMentPaySmsDto.getIdType()); // 证件类型
        map.put("id_no", AESUtil.Aes256Encode(agreeMentPaySmsDto.getIdCardNum(),aesKey)); // 证件号码
        map.put("bank_card_no", AESUtil.Aes256Encode(agreeMentPaySmsDto.getBankNo(),aesKey)); // 银行卡号
        map.put("mobile_no", AESUtil.Aes256Encode(agreeMentPaySmsDto.getPhone(), aesKey)); // 手机号
 
        requestParam.setData(JsonUtil.toString(map));
 
        String signStr = null;
        try {
            signStr = getSortedString(requestParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("待签名字符串:"+signStr);// 待签名字符串
 
        // 签名
        if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
            try {
                signStr =(RSAUtil.sign(signStr, secretKey, false));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            signStr = "";
            System.out.println("未预期的签名类型:" + requestParam.getSign_type());
        }
        requestParam.setSign(signStr);
 
 
        // Map转json字符串
        String reqBodyJson = JSON.toJSONString(requestParam);
        System.out.println(reqBodyJson);
        String httpResponseJson = null;
        try {
            httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(httpResponseJson);
        if(StrUtil.isNotBlank(httpResponseJson)){
            cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(httpResponseJson);
            System.out.println(jsonObject);
            String biz_code = jsonObject.get("biz_code").toString();
            if("JS000000" == biz_code){
                return httpResponseJson;
            }
            return "fail";
        }else{
            return "fail";
        }
    }
 
    @Override
    public String agreementSign(AgreementSignDto agreementSignDto) {
        String secretKey = paySecretKey;
 
        Map<String, Object> map = new HashMap<>();
        RequestParam requestParam = new RequestParam();
        requestParam.setMch_no(p1MerchantNo);// 商户编号
        requestParam.setMethod("fastPay.agreement.smsSign");// 固定方法名
        requestParam.setVersion("1.0");// 版本号
        requestParam.setRand_str("12345678901234567890123456789012");// 随机字符串
        requestParam.setSign_type("2");// 签名类型
        try {
            requestParam.setSec_key(RSAUtil.encryptByPublicKey(platformPublicKey,aesKey));
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        map.put("mch_order_no",  "NO_"+agreementSignDto.getOrderNo()); // 商户订单号
        map.put("sms_code", agreementSignDto.getSmsCode()); // 签约短信验证码
 
        requestParam.setData(JsonUtil.toString(map));
 
        String signStr = null;
        try {
            signStr = getSortedString(requestParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(signStr);// 待签名字符串
 
        // 签名
        if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) {
            try {
                signStr =(RSAUtil.sign(signStr, secretKey, false));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            signStr = "";
            System.out.println("未预期的签名类型:" + requestParam.getSign_type());
        }
        requestParam.setSign(signStr);
 
 
        // Map转json字符串
        String reqBodyJson = JSON.toJSONString(requestParam);
        System.out.println(reqBodyJson);
        String httpResponseJson = null;
        try {
            httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(httpResponseJson);
 
        if(StrUtil.isNotBlank(httpResponseJson)){
            return httpResponseJson;
        }else{
            return "fail";
        }
    }
 
    protected static String getSortedString(Object obj) throws Exception {
        Field[] fields = obj.getClass().getDeclaredFields();
 
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < fields.length; i++) {
            Field filed = fields[i];
            String name = filed.getName();
            if (SignUtil.NOT_SIGN_PARAM.contains(name)) {// 不参与签名或验签的参数直接跳过
                continue;
            }
            filed.setAccessible(true);
            map.put(name, String.valueOf(filed.get(obj)));
        }
        StringBuffer content = new StringBuffer();
        List<String> keys = new ArrayList(map.keySet());
        Collections.sort(keys); // 排序map
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = map.get(key);
 
            if (i != 0) {
                content.append(SignUtil.SIGN_SEPARATOR);
            }
            content.append(key).append(SignUtil.SIGN_EQUAL).append(value);
        }
        return content.toString();
    }
 
}