2024年Web前端最全微信小程序JSAPI支付 v3 版本 详解 附带源码_微信支付v3后端源码,2024前端大厂面试真题

最后

由于篇幅限制,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

v2与v3的区别

V2版接口和V3版接口实际上是基于两种接口标准设计的两套接口。 目前大部分接口已升级为V3接口,其余V2接口后续也将逐步升级为V3接口

在这里插入图片描述

总的来说,v3支付是对v2版本的升级,不过目前来看,v3的支付对接和实现对开发者更加友好,并且支付更加安全。

微信支付官方文档

请大家花点时间认真看下文档。指引文档,更新日志,sdk,api字典等,这些都很重要,避免在开发过程中,遇到很多不必要的麻烦。

v2的微信小程序支付我之前已经实现过一次了,可以看看 微信小程序支付详解

文章。

交互图

在这里插入图片描述

https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_4&index=3 请看交互图,其实交互是一样的,只是v3的支付接口不一致、大致的实现流程就这些,后台的大致流程为:

  1. 请求微信统一下单接口,获取预支付交易会话标识(prepay_id)
  2. 拿prepay_id 拼接参数,生成签名,给到前端拉起支付(JSAPI调起支付API)
  3. 支付成功后,处理支付回调方法

这里就是后端需要做处理的流程,中间会涉及到 数字证书,秘钥,签名等加密解密出来,以及自己业务处理。

代码实现

官方提供了工具和SDK。先获取官方的SDK,java版本的来做项目开发。

https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay6_0.shtml

wechatpay-apache-httpclient,适用于使用Apache HttpClient处理HTTP的Java开发者。
wechatpay-php(推荐)、wechatpay-guzzle-middleware,适用于PHP开发者。
wechatpay-go,适用于Go开发者

大家根据自己的语言去下载对应的SDK。并且好好看看sdk的使用方式。

maven依赖
   <dependency>
      <groupId>com.github.wechatpay-apiv3</groupId>
      <artifactId>wechatpay-apache-httpclient</artifactId>
      <version>0.2.2</version>
    </dependency>

配置类:
    @Bean
    public HttpClient wxHttpClient(
            @Value("${mchid}") String mchid,
            @Value("${mchSerialNo}") String mchSerialNo,
            PrivateKey merchantPrivateKey,
            AutoUpdateCertificatesVerifier autoUpdateCertificatesVerifier) {
        return WechatPayHttpClientBuilder.create()
                .withMerchant(mchid, mchSerialNo, merchantPrivateKey)
                .withValidator(new WechatPay2Validator(autoUpdateCertificatesVerifier)).build();
    }

    /**
     * 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
     */
    @Bean
    public AutoUpdateCertificatesVerifier autoUpdateCertificatesVerifier(
            PrivateKey merchantPrivateKey,
            @Value("${mchid}") String mchid,
            @Value("${mchSerialNo}") String mchSerialNo,
            @Value("${v3Key}") String v3Key) throws UnsupportedEncodingException {
        return new AutoUpdateCertificatesVerifier(
                new WechatPay2Credentials(mchid, new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),
                v3Key.getBytes("utf-8"));
    }

    /**
     * 加载商户私钥(privateKey:私钥字符串)
     */
    @Bean
    public PrivateKey merchantPrivateKey(@Value("${privateKey}") String privateKey)
            throws UnsupportedEncodingException {
        return PemUtil.loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8")));
    }

  static final int KEY_LENGTH_BYTE = 32;
  static final int TAG_LENGTH_BIT = 128;

  @Value("${v3Key}")
  private String weChatV3Key;

  public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
          throws GeneralSecurityException, IOException {
    try {
      Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");

      SecretKeySpec key = new SecretKeySpec(weChatV3Key.getBytes(StandardCharsets.UTF_8), "AES");
      GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);

      cipher.init(Cipher.DECRYPT_MODE, key, spec);
      cipher.updateAAD(associatedData);

      return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
      throw new IllegalStateException(e);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
      throw new IllegalArgumentException(e);
    }
  }

请求下单
  /**
     * 请求统一下单逻辑处理:
     */
    private WxPayOrderResult unifiedOrder(WxPayOrderRequest wxPayOrderRequest) throws IOException {
        HttpPost httpPost = new HttpPost(PAYUNIFIEDORDERURL);
        // 请求body参数
        StringEntity entity = new StringEntity(new Gson().toJson(wxPayOrderRequest), "UTF-8");
        entity.setContentType("application/json;charset=UTF-8");
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json;charset=UTF-8");
        HttpResponse response = httpClient.execute(httpPost);
        int code = response.getStatusLine().getStatusCode();
        String body = response.getEntity() == null ? null : EntityUtils.toString(response.getEntity());
        if (code != 200) {
            log.error("请求微信统一下单异常: code={}, body={}", code, body);


### 最后

本人分享一下这次字节跳动、美团、头条等大厂的面试真题涉及到的知识点,以及我个人的学习方法、学习路线等,当然也整理了一些学习文档资料出来是给大家的。知识点涉及比较全面,包括但不限于**前端基础,HTML,CSS,JavaScript,Vue,ES6,HTTP,浏览器,算法等等**

>**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/topics/618166371)**

![](https://img-blog.csdnimg.cn/img_convert/f78bc1778941b561829478a7b1d9f169.webp?x-oss-process=image/format,png)

**前端视频资料:**
![](https://img-blog.csdnimg.cn/img_convert/566eebc62aab3fdebc52b49a6cb73dab.webp?x-oss-process=image/format,png)

等**

>**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/topics/618166371)**

[外链图片转存中...(img-CpjFuqly-1714847321805)]

**前端视频资料:**
[外链图片转存中...(img-7j2iTbTh-1714847321806)]

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值