xiaoyong931011
2022-08-23 8392493854d03fbcc8df911c3f5e7b1430579853
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
package cc.mrbird.febs.pay.service.impl;
 
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.pay.model.UnipayDto;
import cc.mrbird.febs.pay.service.UnipayService;
import cc.mrbird.febs.pay.util.HttpRequester;
import cc.mrbird.febs.pay.util.HttpRespons;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.util.*;
 
@Slf4j
@Service
public class UnipayServiceImpl implements UnipayService {
 
    public static final String notifyUrl = "http://47.111.90.145:8800/api/unipay/unipayCallBack";
 
    @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", "888118000001971");/** 商户编号 */
        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);
 
    }
 
}