WeChatDeveloper/tests/PaymentCryptoTest.php
Anyon e8cd998852 feat(security): 强化 token 密钥证书格式校验
- 新增 CredentialValidator,集中校验微信 Token、EncodingAESKey、APIv3 Key、RSA 密钥和证书。

- 配置对象构造时提前验证微信、微信支付、支付宝关键凭证格式。

- 消息加解密和支付通知解密复用统一校验,减少运行期隐性错误。

- 补充无效 Token、密钥、证书和解密场景测试,提升凭证错误可诊断性。
2026-05-08 11:32:58 +08:00

45 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
/**
* This file is part of HyperfAdmin.
*
* @Link https://thinkadmin.top
* @Author Anyon<zoujingli@qq.com>
*/
namespace We\Tests;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use We\Support\PaymentCrypto;
/**
* 微信支付 APIv3 通知 resource 解密测试用例。
* @internal
*/
#[CoversClass(PaymentCrypto::class)]
final class PaymentCryptoTest extends TestCase
{
/**
* 测试微信支付回调资源解密。
*/
public function testDecryptResource(): void
{
$key = str_repeat('k', 32);
$nonce = '123456789012';
$aad = 'transaction';
$plain = '{"out_trade_no":"T202605010001","trade_state":"SUCCESS"}';
$cipher = openssl_encrypt($plain, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $tag, $aad);
$data = PaymentCrypto::decryptResource($key, [
'ciphertext' => base64_encode($cipher . $tag),
'nonce' => $nonce,
'associated_data' => $aad,
]);
$this->assertSame('T202605010001', $data['out_trade_no']);
$this->assertSame('SUCCESS', $data['trade_state']);
}
}