pay/tests/Provider/AlipayTest.php

122 lines
6.2 KiB
PHP
Raw Normal View History

2021-06-06 21:39:26 +08:00
<?php
namespace Yansongda\Pay\Tests\Provider;
2021-06-07 23:55:38 +08:00
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
2021-06-07 21:15:52 +08:00
use GuzzleHttp\Psr7\ServerRequest;
2021-06-07 23:55:38 +08:00
use Mockery;
2021-06-06 21:39:26 +08:00
use PHPUnit\Framework\TestCase;
2021-06-07 23:48:55 +08:00
use Yansongda\Pay\Exception\InvalidParamsException;
2021-06-07 21:15:52 +08:00
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Alipay\LaunchPlugin;
use Yansongda\Pay\Plugin\Alipay\PreparePlugin;
use Yansongda\Pay\Plugin\Alipay\RadarPlugin;
use Yansongda\Pay\Plugin\Alipay\SignPlugin;
use Yansongda\Pay\Plugin\ParserPlugin;
2021-06-06 21:39:26 +08:00
class AlipayTest extends TestCase
{
2021-06-07 23:48:55 +08:00
protected function setUp(): void
{
$config = [
'alipay' => [
'default' => [
2021-06-07 23:55:38 +08:00
'app_id' => '2016082000295641',
'app_secret_cert' => 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCDRjOg5DnX+8L+rB8d2MbrQ30Z7JPM4hiDhawHSwQCQ7RlmQNpl6b/N6IrPLcPFC1uii179U5Il5xTZynfjkUyJjnHusqnmHskftLJDKkmGbSUFMAlOv+NlpUWMJ2A+VUopl+9FLyqcV+XgbaWizxU3LsTtt64v89iZ2iC16H6/6a3YcP+hDZUjiNGQx9cuwi9eJyykvcwhDkFPxeBxHbfwppsul+DYUyTCcl0Ltbga/mUechk5BksW6yPPwprYHQBXyM16Jc3q5HbNxh3660FyvUBFLuVWIBs6RtR2gZCa6b8rOtCkPQKhUKvzRMlgheOowXsWdk99GjxGQDK5W4XAgMBAAECggEAYPKnjlr+nRPBnnNfR5ugzH67FToyrU0M7ZT6xygPfdyijaXDb2ggXLupeGU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LwyxXNrkFOxy7UfIUdUmLoLc1s/VdBHku+JEd0YmEY+p4sjmcRnlu4AlzLxkWUTTg==',
2021-06-07 23:48:55 +08:00
'app_public_cert_path' => __DIR__.'/../Stubs/cert/appCertPublicKey_2016082000295641.crt',
'alipay_public_cert_path' => __DIR__.'/../Stubs/cert/alipayCertPublicKey_RSA2.crt',
'alipay_root_cert_path' => __DIR__.'/../Stubs/cert/alipayRootCert.crt',
],
]
];
Pay::config($config);
}
protected function tearDown(): void
{
Pay::clear();
}
public function testShortcutNotFound()
{
self::expectException(InvalidParamsException::class);
self::expectExceptionCode(InvalidParamsException::SHORTCUT_NOT_FOUND);
Pay::alipay()->foo();
}
public function testShortcutIncompatible()
{
self::expectException(InvalidParamsException::class);
self::expectExceptionCode(InvalidParamsException::SHORTCUT_NOT_FOUND);
Pay::alipay()->foo();
}
2021-06-07 23:55:38 +08:00
public function testFindDefault()
2021-06-06 21:42:29 +08:00
{
2021-06-07 23:55:38 +08:00
$response = [
"alipay_trade_query_response" => [
"code" => "10000",
"msg" => "Success",
"buyer_logon_id" => "ghd***@sandbox.com",
"buyer_pay_amount" => "0.00",
"buyer_user_id" => "2088102174698127",
"buyer_user_type" => "PRIVATE",
"invoice_amount" => "0.00",
"out_trade_no" => "yansongda-1622986519",
"point_amount" => "0.00",
"receipt_amount" => "0.00",
"send_pay_date" => "2021-06-06 21:35:40",
"total_amount" => "0.01",
"trade_no" => "2021060622001498120501382075",
"trade_status" => "TRADE_SUCCESS",
],
"alipay_cert_sn" => "a359aaadd01ceca03dbc07537da539b9",
"sign" => "Ipp1M3pwUFJ19Tx/D+40RZstXr3VSZzGxPB1Qfj1e837UkGxOJxFFK6EZ288SeEh06dPFd4qJ7BHfP/7mvkRqF1/mezBGvhBz03XTXfDn/O6IkoA+cVwpfm+i8MFvzC/ZQB0dgtZppu5qfzVyFaaNu8ct3L/NSQCMR1RXg2lH3HiwfxmIF35+LmCoL7ZPvTxB/epm7A/XNhAjLpK5GlJffPA0qwhhtQwaIZ7DHMXo06z03fbgxlBu2eEclQUm6Fobgj3JEERWLA0MDQiV1EYNWuHSSlHCMrIxWHba+Euu0jVkKKe0IFKsU8xJQbc7GTJXx/o0NfHqGwwq8hMvtgBkg==",
2021-06-07 21:15:52 +08:00
];
2021-06-07 23:55:38 +08:00
$http = Mockery::mock(Client::class);
$http->shouldReceive('sendRequest')->andReturn(new Response(200, [], json_encode($response)));
$result = Pay::alipay()->find('yansongda-1622986519');
self::assertEqualsCanonicalizing($response['alipay_trade_query_response'], $result->all());
}
public function testVerifyResponse()
{
2021-06-07 21:15:52 +08:00
$url = 'http://127.0.0.1:8000/alipay/verify?charset=utf-8&out_trade_no=yansongda-1622986519&method=alipay.trade.page.pay.return&total_amount=0.01&sign=oSazH3ZnzPQBGfJ8piYuri0E683D7bEKtd1NPcuYctvCEiRWP1QBVWma3hwoTLc19KdXbMGGZcOS5UvtlWwIvcK3oqkuRkFOwcRRmyF0UScmdHrTEPO9VwcaEWPK9Hy%2BTSlYrlnfCae1zlDo4vvNojFZf%2BduaaYCGS2L4Q55atloeztOPsZTNSYI7Jy0rrQcOaAWL7F9aJNqFPW6WkWL31w6HwDHcRSEQzD9C9YTsRkQ7khPHFEw8CHSYp5h8XOq%2BfE0yRDAEEw2pxYYC5QhCtbqVjLdfFXp792cTRd31IB6iAznnDvOATZVgulpC0Z6MV0k0MInL2CarbuO5SZfRg%3D%3D&trade_no=2021060622001498120501382075&auth_app_id=2016082000295641&version=1.0&app_id=2016082000295641&sign_type=RSA2&seller_id=2088102172237210&timestamp=2021-06-06+21%3A35%3A50';
parse_str(parse_url($url)['query'], $query);
$request = new ServerRequest('GET', $url);
$request = $request->withQueryParams($query);
$result = Pay::alipay()->verify($request);
self::assertNotEmpty($result->all());
$result = Pay::alipay()->verify($query);
self::assertNotEmpty($result->all());
}
public function testMergeCommonPlugins()
{
Pay::config([]);
$plugins = [FooPluginStub::class];
self::assertEquals(array_merge(
[PreparePlugin::class],
$plugins,
[SignPlugin::class, RadarPlugin::class],
[LaunchPlugin::class, ParserPlugin::class],
), Pay::alipay()->mergeCommonPlugins($plugins));
2021-06-06 21:42:29 +08:00
}
2021-06-06 21:39:26 +08:00
}
2021-06-07 23:12:02 +08:00
class FooPluginStub
{
}
2021-06-07 23:48:55 +08:00
class FooShortcut
{
}