微信支付小程序支付V3版本Java实现
商户平台配置秘钥
配置流程参考官方文档
官方文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_1.shtml
JAVA项目实现支付
导入微信支付SDK依赖包
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.2.1</version>
</dependency>
常量配置类
public class WxPaySettingConstant {
public static final String mchId = "xxxxxxxxxxxxx"; // 商户号
public static final String mchSerialNo = "xxxxxxxxxxxxxxxxxxxx"; // 商户证书序列号
public static final String apiV3Key = "xxxxxxxxxxxxxxxx"; // api密钥
// 你的商户私钥
public static final String privateKey = "-----BEGIN PRIVATE KEY-----\n"+
"私钥内容"
+ "-----END PRIVATE KEY-----\n";
}
配置微信提供的发起请求Bean
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.PrivateKey;
import static com.yd.lhy.contents.LetAppContants.WECHAT_APPLET_MEETKEEPER_MEETING_ROOM_APPID;
import static org.junit.Assert.assertTrue;
@Slf4j
@Configuration
public class WxPayConfigSetting {
private CloseableHttpClient httpClient;
private AutoUpdateCertificatesVerifier verifier;
private static PrivateKey merchantPrivateKey;
static {
try {
merchantPrivateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(WxPaySettingConstant.privateKey.getBytes("utf-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@PostConstruct
public void setup() throws IOException {
//加载商户自己的私钥
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
new ByteArrayInputStream(WxPaySettingConstant.privateKey.getBytes("utf-8")));
//使用自动更新的签名验证器,不需要传入证书
verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(WxPaySettingConstant.mchId, new PrivateKeySigner(WxPaySettingConstant.mchSerialNo, merchantPrivateKey)),
WxPaySettingConstant.apiV3Key.getBytes("utf-8"));
httpClient = WechatPayHttpClientBuilder.create()
.withMerchant(WxPaySettingConstant.mchId, WxPaySettingConstant.mchSerialNo, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier))
.build();
}
@PreDestroy
public void after() throws IOException {
httpClient.close();
}
/**
* 发送微信预支付请求
* @param wxPayBean 参考官方文档地址生成实体类:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml
* @return
*/
public JSONObject sendReserveOrderRequest(WxPayBeanEntity wxPayBean)throws Exception{
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
StringEntity reqEntity = new StringEntity(JSON.toJSONString(wxPayBean), ContentTy