• 微信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

    1. <dependency>
    2. <groupId>com.github.wechatpay-apiv3</groupId>
    3. <artifactId>wechatpay-apache-httpclient</artifactId>
    4. <version>0.2.2</version>
    5. </dependency>

    6.测试示例

    1. import com.fasterxml.jackson.databind.ObjectMapper;
    2. import com.fasterxml.jackson.databind.node.ObjectNode;
    3. import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
    4. import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
    5. import org.apache.http.client.methods.CloseableHttpResponse;
    6. import org.apache.http.client.methods.HttpPost;
    7. import org.apache.http.entity.StringEntity;
    8. import org.apache.http.impl.client.CloseableHttpClient;
    9. import org.apache.http.util.EntityUtils;
    10. import java.io.ByteArrayOutputStream;
    11. import java.io.FileInputStream;
    12. import java.security.cert.X509Certificate;
    13. import java.util.ArrayList;
    14. import java.util.List;
    15. public class test {
    16. public static void main(String[] args) throws Exception {
    17. String apiV3key = "SYKJ20234545weasdq";
    18. // 商户号
    19. String mchId = "150923821451";
    20. // 商户证书序列号
    21. String mchSerialNo = "79D9A85662A93453457720657B19";
    22. // 商户私钥
    23. String mchPrivateKeyFilePath = "D:\\可删除\\apiclient_cert.pem";
    24. // 微信支付平台证书
    25. String wechatpayCertificateFilePath = "E:\\allrun2\\微信支付证书工具\\wechatpay_38023669B5DBFED58B2B16B3D38CA15DB983E002.pem";
    26. //下载成功后保存证书的路径
    27. String outputFilePath = "E:\\allrun2\\微信支付证书工具";
    28. //native统一下单
    29. String nativePay = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";
    30. WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
    31. .withMerchant(mchId, mchSerialNo, PemUtil.loadPrivateKey(new FileInputStream(mchPrivateKeyFilePath)));
    32. if (wechatpayCertificateFilePath == null) {
    33. //不做验签
    34. builder.withValidator(response -> true);
    35. } else {
    36. List certs = new ArrayList<>();
    37. certs.add(PemUtil.loadCertificate(new FileInputStream(wechatpayCertificateFilePath)));
    38. builder.withWechatpay(certs);
    39. }
    40. /*HttpGet httpGet = new HttpGet(CertDownloadPath);
    41. httpGet.addHeader("Accept", "application/json");
    42. try (CloseableHttpClient client = builder.build()) {
    43. CloseableHttpResponse response = client.execute(httpGet);
    44. int statusCode = response.getStatusLine().getStatusCode();
    45. String body = EntityUtils.toString(response.getEntity());
    46. if (statusCode == 200) {
    47. System.out.println("body:" + body);
    48. } else {
    49. System.out.println("download failed,resp code=" + statusCode + ",body=" + body);
    50. throw new IOException("request failed");
    51. }
    52. }*/
    53. HttpPost httpPost = new HttpPost(nativePay);
    54. httpPost.addHeader("Accept", "application/json");
    55. httpPost.addHeader("Content-type","application/json; charset=utf-8");
    56. ObjectMapper objectMapper = new ObjectMapper();
    57. ByteArrayOutputStream bos = new ByteArrayOutputStream();
    58. ObjectNode rootNode = objectMapper.createObjectNode();
    59. rootNode.put("mchid",mchId)
    60. .put("appid", "wxf3152dbae1f27a61")
    61. .put("description", "Ima5677公仔")
    62. .put("notify_url", "http://2j9c780335.zicp.vip/siyu-cloud-pay-biz/pms/wechatPay/callback")
    63. .put("out_trade_no", "515414244");
    64. rootNode.putObject("amount")
    65. .put("total", 1);
    66. objectMapper.writeValue(bos, rootNode);
    67. httpPost.setEntity(new StringEntity(bos.toString("UTF-8")));
    68. try (CloseableHttpClient httpClient = builder.build()) {
    69. CloseableHttpResponse response = httpClient.execute(httpPost);
    70. String bodyAsString = EntityUtils.toString(response.getEntity());
    71. System.out.println(bodyAsString);
    72. }
    73. }
    74. }
  • 相关阅读:
    番外 1 : Java 环境下的 selenium 搭建
    【网络编程实例】C++11实现基于TCP的回射服务器和客户端通信
    STM32——STM32F103时钟解析(正点原子资料+HAL库代码分析)
    Qt QWidget 简约美观的加载动画 第五季 - 小方块风格
    分布式事务解决方案
    69-Java常用API:Object、Objects、StringBuilder、Math、System、BigDecimal
    【力扣题解】石子游戏
    Node 使用 WebStorm 打开文件
    软考知识汇总--结构化开发方法
    对话李彦宏:AI 大模型时代,应用开发机会比移动互联网大十
  • 原文地址:https://blog.csdn.net/qq_36617310/article/details/133738803