The two most important keys in WeChat Mini Programs: Mini Program Secret Key, Merchant Payment API Secret Key

There are two very important keys in the WeChat applet:

The first one : AppSecret , which is used when the applet uses WeChat to log in. This secret key accesses the fixed interface to obtain the user's openid.

/**
*微信登录
*/
@PostMapping("/wxLogin")
    public WenJSONResult wxLogin(@RequestBody Users users, String code) throws Exception {
      
        // 该接口用来获取用户的openid
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, String> param = new HashMap<>();
        param.put("appid", WxConfig.appid);

        param.put("secret", WxConfig.key);
        param.put("js_code", code);
        param.put("grant_type", "authorization_code");
        String wxResult = HttpClientUtil.doGet(url, param);
        WXSessionModel model = JsonUtils.jsonToPojo(wxResult, WXSessionModel.class);
        .....................................
        ...........自己的业务代码.............
        .....................................
    }

The second one : WeChat Pay, merchant platform, API key . When the merchant integrates WeChat payment, it is used to call up the secret key of the unified ordering interface. The two secret keys cannot be reversed, otherwise the WeChat Pay will always report an error when signing for the first time: signature error.

    /**
     * @Description: 微信支付
     */
    @RequestMapping("pay")
    public WenJSONResult pay(String openid, HttpServletRequest request) {

        try {
            Map<String, String> packageParams = new HashMap<>();
            // 生成随机字符串,保证每一次的请求生成的签名都不一样
            String nonce_str = GenerateUUID.getUUID();
            // 商品名称
            String body = "测试商品";
            // 获取本机的ip地址
            String spbill_create_ip = IpUtils.getIpAddr(request);
            // 订单号
            String orderNo = sid.nextShort();
            // 支付金额,单位:分,这边需要转成字符串类型,否则后面的签名会失败
            String money = "1";

            packageParams.put("appid", WxPayConfig.appid);
            packageParams.put("mch_id", WxPayConfig.mch_id);
            packageParams.put("nonce_str", nonce_str);
            packageParams.put("body", body);
            packageParams.put("out_trade_no", orderNo);
            packageParams.put("total_fee", money);
            packageParams.put("spbill_create_ip", spbill_create_ip);
            packageParams.put("notify_url", WxPayConfig.notify_url);
            packageParams.put("trade_type", WxPayConfig.TRADETYPE);
            packageParams.put("openid", openid);

            // 第一次签名,用于统一下单接口
            String sign = WXPayUtil.generateSignature(packageParams, WxPayConfig.payKey, WXPayConstants.SignType.MD5);
            packageParams.put("sign", sign);
            // 拼接统一下单接口使用的xml数据,要将上一步生成的签名一起拼接进去
            String xml = WXPayUtil.generateSignedXml(packageParams, WxPayConfig.payKey, WXPayConstants.SignType.MD5);

            // 调起统一支付接口,返回预下单订单号prepay_id
            String result = PayUtil.httpRequest(WxPayConfig.pay_url, "POST", xml);

            // 将请求结果存储在map中
            Map map = PayUtil.doXMLParse(result);

            // 返回状态码
            String return_code = (String) map.get("return_code");

            // 给移动端返回调起支付需要的参数
            Map<String, Object> res = new HashMap<>();
            if (return_code == "SUCCESS" || return_code.equals(return_code)) {

                // 业务结果,返回的预付单信息
                String prepay_id = (String) map.get("prepay_id");
                res.put("nonceStr", nonce_str);
                res.put("package", "prepay_id=" + prepay_id);

                Long timeStamp = System.currentTimeMillis() / 1000;
                // 要求将时间戳转化成字符串
                res.put("timeStamp", timeStamp + "");

                String stringSignTemp = "appId=" + WxPayConfig.appid + "&nonceStr="
                        + nonce_str + "&package=prepay_id=" + prepay_id + "&signType="
                        + WxPayConfig.SIGNTYPE + "&timeStamp=" + timeStamp;
                // 第二次签名,将小程序支付需要的参数传回小程序。小程序调用wx.requesetPayment方法
                String paySign = PayUtil.sign(stringSignTemp, WxPayConfig.payKey, "utf-8").toUpperCase();

                res.put("paySign", paySign);

                //更新订单信息
                //业务逻辑代码
            }

            res.put("appid", WxPayConfig.appid);

            return WenJSONResult.ok(res);
        } catch (Exception e) {
            e.printStackTrace();
            return WenJSONResult.errorMsg("支付失败");
        }
    }

 

Guess you like

Origin blog.csdn.net/qq_29644709/article/details/97433967