微信native-v3版支付对接流程及demo

1.将p12证书转为pem证书,得到商户私钥

openssl pkcs12 -in apiclient_cert.p12 -out apiclient_cert.pem -nodes

密码是:商户id

2.将获取到的apiclient_cert.pem证书,复制出这一块内容,其他的不要

3.下载这个工具包

https://github.com/wechatpay-apiv3/CertificateDownloader

4.在test包中运行

生成证书

5.引入腾讯的sdk

https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient

<dependency>

<groupId>com.github.wechatpay-apiv3</groupId>

<artifactId>wechatpay-apache-httpclient</artifactId>

<version>0.2.2</version>

</dependency>

6.测试示例

java 复制代码
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.util.PemUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

public class test {


    public static void main(String[] args) throws Exception {
        String apiV3key = "SYKJ20234545weasdq";
        // 商户号
        String mchId = "150923821451";
        // 商户证书序列号
        String mchSerialNo = "79D9A85662A93453457720657B19";
        // 商户私钥
        String mchPrivateKeyFilePath = "D:\\可删除\\apiclient_cert.pem";
        // 微信支付平台证书
        String wechatpayCertificateFilePath = "E:\\allrun2\\微信支付证书工具\\wechatpay_38023669B5DBFED58B2B16B3D38CA15DB983E002.pem";
        //下载成功后保存证书的路径
        String outputFilePath = "E:\\allrun2\\微信支付证书工具";

        

        //native统一下单
        String nativePay = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";

        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                .withMerchant(mchId, mchSerialNo, PemUtil.loadPrivateKey(new FileInputStream(mchPrivateKeyFilePath)));

        if (wechatpayCertificateFilePath == null) {
            //不做验签
            builder.withValidator(response -> true);
        } else {
            List<X509Certificate> certs = new ArrayList<>();
            certs.add(PemUtil.loadCertificate(new FileInputStream(wechatpayCertificateFilePath)));
            builder.withWechatpay(certs);
        }

        /*HttpGet httpGet = new HttpGet(CertDownloadPath);
        httpGet.addHeader("Accept", "application/json");

        try (CloseableHttpClient client = builder.build()) {
            CloseableHttpResponse response = client.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            String body = EntityUtils.toString(response.getEntity());
            if (statusCode == 200) {
                System.out.println("body:" + body);
            } else {
                System.out.println("download failed,resp code=" + statusCode + ",body=" + body);
                throw new IOException("request failed");
            }
        }*/
        HttpPost httpPost = new HttpPost(nativePay);
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-type","application/json; charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectNode rootNode = objectMapper.createObjectNode();
        rootNode.put("mchid",mchId)
                .put("appid", "wxf3152dbae1f27a61")
                .put("description", "Ima5677公仔")
                .put("notify_url", "http://2j9c780335.zicp.vip/siyu-cloud-pay-biz/pms/wechatPay/callback")
                .put("out_trade_no", "515414244");
        rootNode.putObject("amount")
                .put("total", 1);

        objectMapper.writeValue(bos, rootNode);
        httpPost.setEntity(new StringEntity(bos.toString("UTF-8")));
        try (CloseableHttpClient httpClient = builder.build()) {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            String bodyAsString = EntityUtils.toString(response.getEntity());
            System.out.println(bodyAsString);
        }




    }
}
相关推荐
2401_8459375312 小时前
PHP一键约课高效健身智能健身管理系统小程序源码
微信·微信小程序·小程序·微信公众平台·微信开放平台
CHEtuzki14 小时前
微信这样回复客户高效又方便
微信·自动回复
2401_856654512 天前
员工疯狂打CALL!解锁企业微信新玩法,2024年必学秘籍来啦!
安全·微服务·微信·电脑·企业微信
龙哥·三年风水2 天前
微信支付开发-前端api实现
微信·php·支付系统
鲸天千流4 天前
鲸天科技外卖会员卡系统更专业
网络·科技·微信·小程序
2401_845937534 天前
PHP即刻送达同城派送小程序系统
微信·微信小程序·小程序·微信公众平台·微信开放平台
2401_845936455 天前
PHP一键约课高效健身智能健身管理系统小程序源码
微信·微信小程序·小程序·微信公众平台·微信开放平台
2401_845936145 天前
PHP即刻送达同城派送小程序系统
微信·微信小程序·小程序·微信公众平台·微信开放平台
2401_844139035 天前
PHP全程可视化防伪溯源一体化管理系统小程序源码
微信·微信小程序·小程序·微信公众平台·微信开放平台·防伪溯源一体化
2401_844137955 天前
PHP悦读随行一键借阅图书小程序
微信·微信小程序·小程序·微信公众平台·微信开放平台·图书馆借阅小程序