接口调用代码:
CloseableHttpClient httpClient = WxHttpClient.createHttpClient();
HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/business_code/" + businessCode);
httpGet.addHeader("Accept", "application/json");
httpGet.addHeader("Content-type", "application/json; charset=utf-8");
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
WxAuthStatusResponse res = JSONObject.parseObject(EntityUtils.toString(entity), WxAuthStatusResponse.class);
工具类代码:
public class WxHttpClient {
// 设置VX证书默认参数
// 服务商Id
private static final String merchantId = "163...";
// API V3密钥
private static final String apiV3Key = "zL3gprVKrr05...";
// 证书序列号
public static final String serialNumber = "2DD9DABDD05...";
public static CloseableHttpClient createHttpClient() {
PrivateKey privateKey = createPrivateKey();
Verifier verifier = createVerifier(privateKey);
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, serialNumber, privateKey)
.withValidator(new WechatPay2Validator(verifier));
return builder.build();
}
public static PrivateKey createPrivateKey() {
InputStream inputStream = ResourceUtil.getStream("wx_pay/apiclient_key.pem");
return PemUtil.loadPrivateKey(inputStream);
}
public static Verifier createVerifier(PrivateKey privateKey) {
try {
CertificatesManager instance = CertificatesManager.getInstance();
instance.putMerchant(merchantId, new WechatPay2Credentials(
merchantId, new PrivateKeySigner(serialNumber, privateKey)), apiV3Key.getBytes(StandardCharsets.UTF_8));
return instance.getVerifier(merchantId);
} catch (GeneralSecurityException | NotFoundException | IOException | HttpCodeException e) {
e.printStackTrace();
}
return null;
}
}
响应返回内容:
c.w.pay.contrib.apache.httpclient.SignatureExec > 应答的状态码不为200-299。status code[400] request headers[[Accept: application/json, Content-type: application/json; charset=utf-8, Authorization: WECHATPAY2-SHA256-RSA2048 mchid="1638262312",nonce_str="4X2TDkZuX94X55pxWoeaezI0rsSDvkZk",timestamp="1677857507",serial_no="2DD9DAB...",signature="tFEwJ6qmBCmmbADju/7VqtdL7vXsb8URSdsw69HwNVtRGyUcJASdyY391bOnjVB11SDR5t/a1uC+XhRalrwQN2IDJiJPWpL0OEZR7aKea5MV0YIgkmkldiDS3V8VgrBnuGAaTCDovgkHOrDibN6sVoy16zZmTAEGxCf5Vr/lUOsDa5IkfqdgsB7E25ZzCli4M4g7sKldOiGhUEVSynFEAbW9n3kgmjntlwKaoyU6D9ABSz5k8eGMGc4wO/EFGZQEnHj3SwLufB2xV1ub1ZkBrUv3fwau1BXiN1EwQ0Dxwmj/XVx6XXeJ7Tt4K9AKj1GJdnxBPPu1PbHdVZtL/jmPSQ==", Host: api.mch.weixin.qq.com, Connection: Keep-Alive, User-Agent: WechatPay-Apache-HttpClient/0.4.7 (Mac OS X/10.16) Java/1.8.0_322, Accept-Encoding: gzip,deflate]]
代码没有问题,实际返回的body:{"code":"PARAM_ERROR","message":"未能找到申请单"}
不是代码问题