package com.wechat.pay.java.service.partnerpayments.nativepay;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.CloseOrderRequest;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.PrepayResponse;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.QueryOrderByIdRequest;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.QueryOrderByOutTradeNoRequest;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.Transaction;
/** NativePayService使用示例 */
public class NativePayServiceExample {
/** 商户号 */
public static String merchantId = "190000****";
/** 商户API私钥路径 */
public static String privateKeyPath = "/Users/yourname/your/path/apiclient_key.pem";
/** 商户证书序列号 */
public static String merchantSerialNumber = "5157F09EFDC096DE15EBE81A47057A72********";
/** 商户APIV3密钥 */
public static String apiV3Key = "...";
public static NativePayService service;
public static void main(String[] args) {
// 初始化商户配置
Config config =
new RSAAutoCertificateConfig.Builder()
.merchantId(merchantId)
// 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
.privateKeyFromPath(privateKeyPath)
.merchantSerialNumber(merchantSerialNumber)
.apiV3Key(apiV3Key)
.build();
// 初始化服务
service = new NativePayService.Builder().config(config).build();
// ... 调用接口
try {
closeOrder();
} catch (HttpException e) { // 发送HTTP请求失败
// 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
} catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
// 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
} catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
// 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
}
}
/** 关闭订单 */
public static void closeOrder() {
CloseOrderRequest request = new CloseOrderRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
service.closeOrder(request);
}
/** Native支付预下单 */
public static PrepayResponse prepay() {
PrepayRequest request = new PrepayRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.prepay(request);
}
/** 微信支付订单号查询订单 */
public static Transaction queryOrderById() {
QueryOrderByIdRequest request = new QueryOrderByIdRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.queryOrderById(request);
}
/** 商户订单号查询订单 */
public static Transaction queryOrderByOutTradeNo() {
QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
return service.queryOrderByOutTradeNo(request);
}
}
Exception in thread "main" com.wechat.pay.java.core.exception.ServiceException: Wrong HttpStatusCode[500]
httpResponseBody[{"code":"SYSTEM_ERROR","message":"系统繁忙,请稍后重试"}] HttpRequest[{"http_method":"GET","url":"https://api.mch.weixin.qq.com/v3/certificates?algorithm_type=RSA","uri":"https://api.mch.weixin.qq.com/v3/certificates?algorithm_type=RSA","headers":{"headers":{"Authorization":"WECHATPAY2-SHA256-RSA2048 mchid=\"XXXXXXX\",nonce_str=\"nX14DfLsl9MRehi4OfZgNIYjjABYPGsy\",timestamp=\"1729827433\",serial_no=\"XXXXXXXXXX\",signature=\"4L+S+55cBFVJthUMdnujHHyvhfylySUBuEwWAfC0EEThIQG+2fArKNdTO9Wct0/Cak9Zi84x3ZLlIld2jf9XgmI6xp5zt/AViqut1r/JPF0wgKo42Z4txbdlBgLICoRNQBL0jWU1Nc2SvIbahd0/CJ1WZonoEHG8NaYzmvRWVsy5bOPKNvvvP6i31zmAQws88HoGq8n3hJBv9lwYVc14ZumggBY8oMqa/HXdj4gmamOC++aYrhgHtlVxmKeMX/NQMQZDo38PVbvCPuSGz6rgvFDEy8qYIcWQQz03l59iLdByJUT44jg+hs52wa7CZfhm8TKbiriA0cv9jjExxxhh2g==\"","Accept":" */*","User-Agent":"WechatPay-Java/0.2.14 (Windows 11/10.0) Java/17.0.11 Credential/WechatPay2Credential Validator/ okhttp3/null","Content-Type":"application/json"}}}]
at com.wechat.pay.java.core.http.AbstractHttpClient.validateResponse(AbstractHttpClient.java:74)
at com.wechat.pay.java.core.http.AbstractHttpClient.execute(AbstractHttpClient.java:45)
at com.wechat.pay.java.core.certificate.CertificateDownloader.download(CertificateDownloader.java:82)
at com.wechat.pay.java.core.certificate.AutoCertificateService.lambda$register$0(AutoCertificateService.java:66)
新商户需要用公钥模式,不会签发平台证书,不能使用RSAAutoCertificateConfig