request('alipay.trade.query', ['out_trade_no' => 'P202605040001']); self::assertSame('TRADE202605040001', $data['trade_no']); } /** * 测试支付宝异步通知验签。 */ public function testVerifyNotify(): void { [$privateKey, $publicKey] = self::keyPair(); $params = [ 'notify_time' => '2026-05-04 12:00:00', 'app_id' => 'ali_app', 'trade_status' => 'TRADE_SUCCESS', 'out_trade_no' => 'P202605040001', 'sign_type' => 'RSA2', ]; $params['sign'] = self::sign('app_id=ali_app¬ify_time=2026-05-04 12:00:00&out_trade_no=P202605040001&trade_status=TRADE_SUCCESS', $privateKey); $client = new AlipayPlatformClient(new AlipayPlatformConfig('ali_app', $privateKey, $publicKey)); self::assertTrue($client->verifyNotify($params)); } /** * 生成测试使用的 RSA 密钥对。 * * @return array{0:string,1:string} */ private static function keyPair(): array { $resource = openssl_pkey_new(['private_key_bits' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA]); self::assertNotFalse($resource); openssl_pkey_export($resource, $privateKey); $details = openssl_pkey_get_details($resource); self::assertIsArray($details); return [$privateKey, (string)$details['key']]; } /** * 使用测试私钥生成签名。 */ private static function sign(string $source, string $privateKey): string { $ok = openssl_sign($source, $signature, $privateKey, OPENSSL_ALGO_SHA256); self::assertTrue($ok); return base64_encode($signature); } } /** * 返回固定网关响应的支付宝测试 HTTP 客户端。 */ final class AlipayFakeHttpClient implements ClientInterface { /** * 创建固定响应测试 HTTP 客户端。 */ public function __construct(private readonly string $body) {} /** * 实现测试 HTTP 客户端同步发送接口。 */ public function send(RequestInterface $request, array $options = []): ResponseInterface { return $this->response(); } /** * 实现测试 HTTP 客户端异步发送接口。 */ public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface { return Create::rejectionFor(new \RuntimeException('sendAsync is not used in this test')); } /** * 实现测试 HTTP 客户端请求接口或记录请求。 */ public function request(string $method, $uri = '', array $options = []): ResponseInterface { return $this->response(); } /** * 实现测试 HTTP 客户端异步请求接口。 */ public function requestAsync(string $method, $uri = '', array $options = []): PromiseInterface { return Create::rejectionFor(new \RuntimeException('requestAsync is not used in this test')); } /** * 返回测试 HTTP 客户端配置。 */ public function getConfig(?string $option = null): mixed { return null; } /** * 构造测试 HTTP 响应对象。 */ private function response(): ResponseInterface { return new Response(200, [], $this->body); } }