三级全黄做爰电影_夫妻之间那些事_啊啊啊啊好爽视频_国产精品xxxx喷水欧美_色一情一乱一伦麻豆_国产精品美乳一区二区免费_亚洲综合av在线在线播放_人与禽伦性了_黄色软件色多多_av在线免费观看网址

微信支付api v3服務(wù)商開(kāi)發(fā),Native支付,支付通知API,php實(shí)現(xiàn)
發(fā)表人:金馬 | 2022-03-10

微信支付api v3服務(wù)商開(kāi)發(fā)接口文檔參數(shù)解密說(shuō)明確實(shí)寫(xiě)得不清楚,官方的介紹是:

1、用商戶平臺(tái)上設(shè)置的APIv3密鑰【微信商戶平臺(tái)—>賬戶設(shè)置—>API安全—>設(shè)置APIv3密鑰】,記為key;

2、針對(duì)resource.algorithm中描述的算法(目前為AEAD_AES_256_GCM),取得對(duì)應(yīng)的參數(shù)nonce和associated_data;

3、使用key、nonce和associated_data,對(duì)數(shù)據(jù)密文resource.ciphertext進(jìn)行解密,得到JSON形式的資源對(duì)象;

注: AEAD_AES_256_GCM算法的接口細(xì)節(jié),請(qǐng)參考rfc5116。微信支付使用的密鑰key長(zhǎng)度為32個(gè)字節(jié),隨機(jī)串nonce長(zhǎng)度12個(gè)字節(jié),associated_data長(zhǎng)度小于16個(gè)字節(jié)并可能為空字符串。

這里的rfc5116參考:https://datatracker.ietf.org/doc/html/rfc5116

然取到三個(gè)參數(shù)基本不知如何去解,在百度查了很久,基本找不到什么相關(guān)信息,好在最后還是在一個(gè)開(kāi)源文檔里找到了接口,現(xiàn)分享出來(lái),也供大家作為參考:

首先建個(gè)文檔wxapire.php,內(nèi)容如下:

<?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('無(wú)效的ApiV3Key,長(zhǎng)度應(yīng)為32個(gè)字節(jié)');

        }

        $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');

    }

}

?>

在通知頁(yè)引用類頁(yè),如下代碼:

<?php 

require('wxapire.php');

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

//參數(shù)說(shuō)明:$key=》apiv3密鑰,$associated_data,$nonce,$ciphertext這三個(gè)是按返回來(lái)的字段信息,可以按名稱對(duì)應(yīng)即可

//$data_array就是解密后的數(shù)組,大家可以跟據(jù)實(shí)際情況使用到場(chǎng)景中

?>

實(shí)際界面效果:

首先生成支付二維碼image.png

支付后,后端接收到通知,改變訂單狀態(tài)

生成的二維碼付款頁(yè)間隔幾秒就去分析是否訂單狀態(tài),如改變就提示,如下圖:

image.png

至此微信支付服務(wù)商開(kāi)發(fā)api v3 "Native支付"支付和通知的業(yè)務(wù)場(chǎng)景完成。

我來(lái)說(shuō)兩句(0)
:zui: :wink: :twisted: :roll: :oops: :mrgreen: :love: :lol: :jidong: :idea: :han:
發(fā)表評(píng)論(Ctrl+Enter)
—— 金馬科技公眾號(hào) ——
十二年 行業(yè)積累

砥礪前行路上,有您關(guān)注,相聚相研共話成長(zhǎng)!

如有意向,請(qǐng)與我們聯(lián)系

辦公電話:0774-3838278 / QQ:154727262 / 微信:wztmma