微信支付api v3服务商开发,Native支付,支付通知API,php实现
发表人:金马 | 2022-03-10

微信支付api v3服务商开发接口文档参数解密说明确实写得不清楚,官方的介绍是:

1、用商户平台上设置的APIv3密钥【微信商户平台—>账户设置—>API安全—>设置APIv3密钥】,记为key;

2、针对resource.algorithm中描述的算法(目前为AEAD_AES_256_GCM),取得对应的参数nonce和associated_data;

3、使用key、nonce和associated_data,对数据密文resource.ciphertext进行解密,得到JSON形式的资源对象;

注: AEAD_AES_256_GCM算法的接口细节,请参考rfc5116。微信支付使用的密钥key长度为32个字节,随机串nonce长度12个字节,associated_data长度小于16个字节并可能为空字符串。

这里的rfc5116参考:https://datatracker.ietf.org/doc/html/rfc5116

然取到三个参数基本不知如何去解,在百度查了很久,基本找不到什么相关信息,好在最后还是在一个开源文档里找到了接口,现分享出来,也供大家作为参考:

首先建个文档wxapire.php,内容如下:

<?php 

class AesUtil

{

    /**

     * AES key

     *

     * @var string

     */

    private $aesKey;


    const KEY_LENGTH_BYTE = 32;

    const AUTH_TAG_LENGTH_BYTE = 16;


    /**

     * Constructor

     */

    public function __construct($aesKey)

    {

        if (strlen($aesKey) != self::KEY_LENGTH_BYTE) {

            throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');

        }

        $this->aesKey = $aesKey;

    }


    /**

     * Decrypt AEAD_AES_256_GCM ciphertext

     *

     * @param string    $associatedData     AES GCM additional authentication data

     * @param string    $nonceStr           AES GCM nonce

     * @param string    $ciphertext         AES GCM cipher text

     *

     * @return string|bool      Decrypted string on success or FALSE on failure

     */

    public function decryptToString($associatedData, $nonceStr, $ciphertext)

    {

        $ciphertext = \base64_decode($ciphertext);

        if (strlen($ciphertext) <= self::AUTH_TAG_LENGTH_BYTE) {

            return false;

        }


        // ext-sodium (default installed on >= PHP 7.2)

        if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&

            \sodium_crypto_aead_aes256gcm_is_available()) {

            return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $this->aesKey);

        }


        // ext-libsodium (need install libsodium-php 1.x via pecl)

        if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&

            \Sodium\crypto_aead_aes256gcm_is_available()) {

            return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $this->aesKey);

        }


        // openssl (PHP >= 7.1 support AEAD)

        if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {

            $ctext = substr($ciphertext, 0, -self::AUTH_TAG_LENGTH_BYTE);

            $authTag = substr($ciphertext, -self::AUTH_TAG_LENGTH_BYTE);


            return \openssl_decrypt($ctext, 'aes-256-gcm', $this->aesKey, \OPENSSL_RAW_DATA, $nonceStr,

                $authTag, $associatedData);

        }


        throw new \RuntimeException('AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php');

    }

}

?>

在通知页引用类页,如下代码:

<?php 

require('wxapire.php');

$data_array=json_decode((new AesUtil($key))->decryptToString($associated_data,$nonce,$ciphertext),true);

//参数说明:$key=》apiv3密钥,$associated_data,$nonce,$ciphertext这三个是按返回来的字段信息,可以按名称对应即可

//$data_array就是解密后的数组,大家可以跟据实际情况使用到场景中

?>

实际界面效果:

首先生成支付二维码image.png

支付后,后端接收到通知,改变订单状态

生成的二维码付款页间隔几秒就去分析是否订单状态,如改变就提示,如下图:

image.png

至此微信支付服务商开发api v3 "Native支付"支付和通知的业务场景完成。

我来说两句(0)
:zui: :wink: :twisted: :roll: :oops: :mrgreen: :love: :lol: :jidong: :idea: :han:
发表评论(Ctrl+Enter)
—— 金马科技公众号 ——
十二年 行业积累

砥砺前行路上,有您关注,相聚相研共话成长!

如有意向,请与我们联系

办公电话:0774-3838278 / QQ:154727262 / 微信:wztmma