使用wechatpay-apache-httpclient调用统一下单接口,已经通过
httpPost.addHeader("Wechatpay-Serial", WECHATPAY_SERIAL_NO);
填写了支付证书的序列号,但调用
CloseableHttpResponse response = getWxpayClient().execute(httpPost);
控制台输出
[XNIO-1 task-1] WARN com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator - parameter error: empty Wechatpay-Serial, request-id=[08D8B0CEFC05108F0618A4B6C05520B6A70428FAA902-47001]
然后就跳到异常处理去了,异常提示返回信息校验失败。
第一次接触微信支付,请教该怎么处理?
谢谢!
@北望沣渭
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(crtFilePathStr + privateKeyFile));
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(MERCHANT_ID, new PrivateKeySigner(MERCHANT_SERIAL_NO, merchantPrivateKey)),
APIV3_KEY.getBytes("utf-8"));
WECHATPAY_SERIAL_NO = verifier.getValidCertificate().getSerialNumber().toString(16).toUpperCase();
_httpClient = WechatPayHttpClientBuilder.create()
.withMerchant(MERCHANT_ID, MERCHANT_SERIAL_NO, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier))
.build();
就是按照那个官方的例子里写的,这里边死活就是报
parameter error: empty Wechatpay-Serial
然后抛出去的异常信息里提示:
Caused by: org.apache.http.HttpException: 应答的微信支付签名验证失败
我请求接口使用的是如下内容
JSONObject parmJson = new JSONObject();
parmJson.put("appid", APP_ID);
parmJson.put("mch_id", MERCHANT_ID);
parmJson.put("sub_appid", "wxc6d45bc83c029ffe");
parmJson.put("sub_mch_id", "1603522998");
String nonceStr = UUID.randomUUID().toString().trim().replaceAll("-", "");
parmJson.put("nonce_str", nonceStr);
//parmJson.put("sign", ""); //签名,暂时屏蔽,据说v3接口会自动签名
parmJson.put("body", "营业收入");
parmJson.put("out_trade_no", "1234567890");
parmJson.put("total_fee", 100); //金额,单位分
parmJson.put("spbill_create_ip", "192.168.1.55");
parmJson.put("notify_url", "https://www.hui9.net/api/wxpay/payNotify");
parmJson.put("trade_type", "APP");
String parmStr = parmJson.toJSONString();
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder");
StringEntity reqEntity = new StringEntity(parmStr, ContentType.create("application/json", "utf-8"));
httpPost.setEntity(reqEntity);
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json");
httpPost.addHeader("Wechatpay-Serial", WECHATPAY_SERIAL_NO);
CloseableHttpResponse response = getWxpayClient().execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
String respHtml = EntityUtils.toString(response.getEntity());
关键代码就这么多。
麻烦指导一下,谢谢了!
建议翻一下 https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient 这个官方的牌子,里面有示例。