admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
package com.yeshi.fanli.controller.wxpay;
 
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.wx.WXUtil;
 
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("pay")
public class WXPayController {
    // 板栗快省
    private WXAPPInfo app = new WXAPPInfo("wxa99686bb65a9f466", "57390718ddedaa1591f6876cdcf96f43", "1520950211",
            "XYJkJ2018FAfaodCCx899mLl138rfGVd");
 
    @RequestMapping("login")
    public String login() {
        return "wxpay/login";
    }
 
    @RequestMapping("loginCallBack")
    public void loginCallBack(String code, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("code:" + code);
        String url = String.format(
                "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
                app.getAppId(), app.getAppSecret(), code);
        String result = HttpUtil.get(url);
        System.out.println("result:" + result);
        JSONObject json = JSONObject.fromObject(result);
        String openId = json.optString("openid");
        String orderNo = System.currentTimeMillis() + "";
 
        double d = 0.1 + 0.5 * Math.random();
        BigDecimal money = new BigDecimal((int) (d * 100)).divide(new BigDecimal(100));
        Map<String, String> resultMap = produceOrder(orderNo, money, openId, "板栗快省支付", app);
        if (resultMap != null) {
            System.out.println("统一下单成功:" + resultMap.get("prepay_id"));
            Map<String, String> payParams = new HashMap<>();
            payParams.put("appId", app.getAppId());
            payParams.put("timeStamp", System.currentTimeMillis() / 1000 + "");
            payParams.put("nonceStr", StringUtil.getRandomCode(32));
            payParams.put("package", "prepay_id=" + resultMap.get("prepay_id"));
            payParams.put("signType", "MD5");
            payParams.put("paySign", WXUtil.getSignMD5(payParams, app.getMchKey()));
            JSONObject jaon = new JSONObject();
            for (Iterator<String> its = payParams.keySet().iterator(); its.hasNext();) {
                String key = its.next();
                jaon.put(key, payParams.get(key));
            }
 
            try {
                response.sendRedirect("pay?data=" + URLEncoder.encode(jaon.toString(), "UTF-8"));
            } catch (Exception e) {
            }
        }
    }
 
    @RequestMapping("pay")
    public ModelAndView pay(String data) {
        JSONObject jsonObject = JSONObject.fromObject(data);
        ModelAndView modelAndView = new ModelAndView("wxpay/pay");
        for (Iterator<String> its = jsonObject.keySet().iterator(); its.hasNext();) {
            String key = its.next();
            modelAndView.addObject(key, jsonObject.optString(key));
        }
        return modelAndView;
    }
 
    @RequestMapping("test")
    public ModelAndView test() {
        ModelAndView modelAndView = new ModelAndView("test");
        WXAPPInfo wxappInfo = new WXAPPInfo();
        wxappInfo.setAppId("appId");
        modelAndView.addObject(wxappInfo);
        // modelAndView.addObject("test", 123123);
        return modelAndView;
    }
 
    private static Map<String, String> produceOrder(String orderNo, BigDecimal fee, String openId, String body,
            WXAPPInfo info) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appid", info.getAppId());
        map.put("mch_id", info.getMchId());
        map.put("nonce_str", StringUtil.getRandomCode(32));
        map.put("body", body);
        map.put("out_trade_no", orderNo);
        map.put("total_fee", "" + fee.multiply(new BigDecimal(100)).intValue());
        map.put("spbill_create_ip", "119.85.112.210");
        map.put("notify_url", "https://banli.xiaoxiangyingji.com/pay/paySuccess");
        map.put("trade_type", "JSAPI");
        map.put("openid", openId);
        map.put("sign", WXUtil.getSignMD5(map, info.getMchKey()));
 
        String entity = WXUtil.loadWXMessage(map);
 
        String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/unifiedorder", entity);
        System.out.println("统一下单结果:" + result);
        Map<String, String> resultMap = WXUtil.parseXML(result);
 
        return resultMap;
    }
 
}