# native模式的统一下单接口
https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml
<!-- 微信支付 -->
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.2.2</version>
</dependency>
package com.shenmazong.shenmacodeserver.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.*;
import java.security.PrivateKey;
/**
* @author 军哥
* @version 1.0
* @description: TODO
* @date 2021/8/23 20:42
*/
@Configuration
public class WeiXinPayConfig {
/**
* 商户号
*/
@Value("${weixin.merchantId}")
private String merchantId;
/**
* 商户API私钥
*/
@Value("${weixin.merchantSerialNumber}")
private String merchantSerialNumber;
/**
* 微信支付平台证书文件存储路径
*/
@Value("${weixin.privateKeyPath}")
private String privateKeyPath;
/**
* apiV3 的密码
*/
@Value("${weixin.apiV3Key}")
private String apiV3Key;
/**
* APP ID
*/
@Value("${weixin.appId}")
private String appId;
/**
* 回调URL
*/
@Value("${weixin.notifyUrl}")
private String notifyUrl;
@Bean
public WeiXinPayConfig weiXinPayUtil() {
return new WeiXinPayConfig();
}
public String prepayIdByOrderInfo(String orderId, String product, Long amount, String openId) throws IOException {
//--1 读取证书
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
new FileInputStream(privateKeyPath));
//--2 构建请求
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(merchantId, new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
apiV3Key.getBytes("utf-8"));
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier));
//--3 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签,并进行证书自动更新
HttpClient httpClient = builder.build();
//--4 调用统一下单接口
// https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type","application/json; charset=utf-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode rootNode = objectMapper.createObjectNode();
rootNode.put("mchid",merchantId)
.put("appid", appId)
.put("description", product)
.put("notify_url", notifyUrl)
.put("out_trade_no", orderId);
rootNode.putObject("amount")
.put("total", amount);
rootNode.putObject("payer")
.put("openid", openId);
objectMapper.writeValue(bos, rootNode);
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(httpPost);
String bodyAsString = EntityUtils.toString(response.getEntity());
System.out.println(bodyAsString);
return bodyAsString;
}
}
步骤说明:当用户完成支付,微信会把相关支付结果将通过异步回调的方式通知商户,商户需要接收处理,并按文档规范返回应答
参考文档:
https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_2.shtml