微信小程序支付V3版本java实现

本文介绍了微信支付小程序支付V3版本在Java项目中的实现,包括商户平台配置秘钥的步骤,导入微信支付SDK,设置常量配置,使用RSA签名工具类以及创建预付订单的详细过程。
摘要由CSDN通过智能技术生成

微信支付小程序支付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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值