微信支付V3 native支付简单示例
apache版本
导入sdk
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.4.9</version>
</dependency>
1、封装HttpClient请求工具
参数:merchantId(商户号)、merchantSerialNumber(商户证书序列号)、privateKey(商户私钥)、apiV3Key(商户32位key)
private static CloseableHttpClient httpClient;
// 初始化WechatPayHttpClient的方法,需要传入配置信息
public static CloseableHttpClient initHttpClient(String merchantId, String merchantSerialNumber, String privateKey, String apiV3Key) {
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(merchantId, new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
apiV3Key.getBytes(StandardCharsets.UTF_8)
);
httpClient = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier))
.build();
return httpClient;
}
2、native下单获取到code_url(二维码链接)
2.1调用微信支付接口
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/native");
2.2HTTP头参数
Authorization (httpClient会自动完成签名认证)
Accept
Content-Type
//设置请求头
httpPost.setHeader("Accept", "application/json");
2.3HTTP请求体参数
appid(公众号、小程序、网页的appid)
mchid(商户号)
description(商品描述)
out_trade_no(订单号自己定义)
notify_url(回调地址:需要外网可以访问的)
amount:total(integer)、currency(string)
// 请求body参数
String reqdata = "{"
+ ""time_expire":"2018-06-08T10:34:56+08:00","
+ ""amount": {"
+ ""total":100,"
+ ""currency":"CNY""
+ "},"
+ ""mchid":"1230000109","
+ ""description":"Image形象店-深圳腾大-QQ公仔","
+ ""notify_url":"https://www.weixin.qq.com/wxpay/pay.php","
+ ""out_trade_no":"1217752501201407033233368018","
+ ""goods_tag":"WXG","
+ ""appid":"wxd678efh567hg6787","
+ "}";
StringEntity entity = new StringEntity(reqdata,"utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
//设置请求头
httpPost.setHeader("Accept", "application/json");
//完成签名并执行请求
CloseableHttpResponse response = httpClient.execute(httpPost);
//微信返回的数据
EntityUtils.toString(response.getEntity())
返回示例
{
2 "code_url" : "weixin://wxpay/bizpayurl/up?pr=NwY5Mz9&groupid=00"
3}
将链接转成二维码即可调起微信支付
微信商户后台需要绑定公众号,开发配置中设置授权native域名