直接请求微信H5下单成功,再此基础上通过qq小程序代理请求微信H5下单时,报错 401 Unauthorized。
代码如下:
//获取商户API私钥
merchantPrivateKey = PemUtil.loadPrivateKey(
new FileInputStream(privateKeyLocation));
//自动更新证书
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(h5OrderReq.getMchid(), new PrivateKeySigner(serialNumber, merchantPrivateKey)),
privateKey.getBytes("utf-8"));
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(h5OrderReq.getMchid(),serialNumber,merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier));
//通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签,并进行证书自动更新
HttpClient httpClient = builder.build();
h5OrderReq.setNotify_url(h5OrderReq.getNotify_url()+h5OrderReq.getOut_trade_no());
String accessToken = getAccessToken();
//直连微信
//HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/h5");
//qq代理
HttpPost httpPost = new HttpPost("https://api.q.qq.com/wxpay/v3/pay/transactions/h5?appid=" + appId + "&access_token=" + accessToken);
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type","application/json; charset=utf-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(bos, h5OrderReq);
httpPost.setEntity(new StringEntity(bos.toString("utf-8"),"utf-8"));
HttpResponse response = httpClient.execute(httpPost);
String stringResult = EntityUtils.toString(response.getEntity());
h5OrderRes.setH5_url(stringResult);