微信支付分(四)--------完结微信支付分订单以及签名

在此讲明,此调微信支付分接口文档,可通用于微信支付分的所有调用支付分接口,在此就只写一次为例!!!!!!!!

一.在做完结支付分订单我们需要了解以下要求

1.商户系统对于完结微信支付分订单一定要做签名,不然会报错:未签名。(签名)

一.后代代码

 /**
     * 完结支付分订单
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/completeOrder.do", method = RequestMethod.POST)
    public Map<String, Object> CompleteOrder(@RequestBody Map<String, Object> params, HttpServletRequest request,
                                          HttpServletResponse response) throws Exception {
        setup();
        String url ="https://api.mch.weixin.qq.com/v3/payscore/serviceorder/%OUTORDERNO%/complete";
        url = url .replaceAll("%OUTORDERNO%", "自己的商户订单号");

        //签名
        String result2 = HttpRequest.post(url)
                .header(Header.CONTENT_TYPE,"application/json")
                .header(Header.ACCEPT,"application/json")
                .header("Authorization", "WECHATPAY2-SHA256-RSA2048"+" "
                        + Sign.getToken("POST",url,"验签的参数",商户MCHID,商户MCHSERIALNO,“商户私钥路径”))//头信息,多个头信息多次调用此方法即可
                .body(reqdata)
                .execute().body();
        System.out.println("result2 = " + result2);
        Map<String,Object> jsonToMap = JSON.parseObject(result2,Map.class);
        after();
        return jsonToMap;
    }


    public void setup() throws IOException {  
        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
                new ByteArrayInputStream(你的商户私钥.getBytes("utf-8")));
        X509Certificate wechatpayCertificate = PemUtil.loadCertificate(
                new ByteArrayInputStream(你的微信支付平台证书.getBytes("utf-8")));

        ArrayList<X509Certificate> listCertificates = new ArrayList<>();
        listCertificates.add(wechatpayCertificate);

        httpClient = WechatPayHttpClientBuilder.create()
                .withMerchant(WX_MCHID, WX_MCHSERIALNO, merchantPrivateKey)
                .withWechatpay(listCertificates)
                .build();
    }

    //	@After
    public void after() throws IOException {
        httpClient.close();
    }

  

//method(请求类型GET、POST url(请求url) body(请求body,GET请求时body传"",POST请求时body为请求参数的json串)  merchantId(商户号) certSerialNo(API证书序列号) keyPath(API证书路径)
    public static String getToken(String method, String url, String body, String merchantId, String certSerialNo, String keyPath) throws Exception {
        String signStr = "";
        HttpUrl httpurl = HttpUrl.parse(url);
//        String nonceStr = getNonceStr();
        String nonceStr = "随机字符串";
        long timestamp = System.currentTimeMillis() / 1000;
        if (StringUtils.isEmpty(body)) {
            body = "";
        }
        String message = buildMessage(method, httpurl, timestamp, nonceStr, body);
        String signature = sign(message.getBytes("utf-8"), keyPath);
        signStr = "mchid=\"" + merchantId
                + "\",nonce_str=\"" + nonceStr
                + "\",timestamp=\"" + timestamp
                + "\",serial_no=\"" + certSerialNo
                + "\",signature=\"" + signature + "\"";
        LOGGER.info("Authorization Token:" + signStr);
        System.out.println("signStr:--" + signStr);
        return signStr;
    }


    public static String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body) {
        String canonicalUrl = url.encodedPath();
        if (url.encodedQuery() != null) {
            canonicalUrl += "?" + url.encodedQuery();
        }
        return method + "\n"
                + canonicalUrl + "\n"
                + timestamp + "\n"
                + nonceStr + "\n"
                + body + "\n";
    }

    public static String sign(byte[] message, String keyPath) throws Exception {
        Signature sign = Signature.getInstance("SHA256withRSA");
        sign.initSign(getPrivateKey(keyPath));
        sign.update(message);
        return Base64.encodeBase64String(sign.sign());
    }

  

// 你的商户私钥

猜你喜欢

转载自www.cnblogs.com/wyrlzy/p/12699081.html