This commit is contained in:
yansongda 2021-06-07 22:18:15 +08:00
parent a1e511ab55
commit 19a7711381
6 changed files with 382 additions and 0 deletions

View File

@ -82,6 +82,10 @@ class Rocket implements JsonSerializableInterface, SerializableInterface, ArrayA
public function mergePayload(array $payload): Rocket
{
if (empty($this->payload)) {
$this->payload = new Collection();
}
$this->payload = $this->payload->merge($payload);
return $this;

View File

@ -0,0 +1,43 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Alipay;
use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Yansongda\Pay\Plugin\Alipay\HtmlResponsePlugin;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Collection;
class HtmlResponsePluginTest extends TestCase
{
public function testRedirect()
{
$rocket = new Rocket();
$rocket->setRadar(new Request('GET', 'https://yansongda.cn'))
->setPayload(new Collection(['name' => 'yansongda']));
$plugin = new HtmlResponsePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
self::assertInstanceOf(ResponseInterface::class, $result->getDestination());
self::assertArrayHasKey('Location', $result->getDestination()->getHeaders());
self::assertEquals('https://yansongda.cn?name=yansongda', $result->getDestination()->getHeaderLine('Location'));
}
public function testHtml()
{
$rocket = new Rocket();
$rocket->setRadar(new Request('POST', 'https://yansongda.cn'))
->setPayload(new Collection(['name' => 'yansongda']));
$plugin = new HtmlResponsePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$contents = $result->getDestination()->getBody()->getContents();
self::assertInstanceOf(ResponseInterface::class, $result->getDestination());
self::assertStringContainsString('alipay_submit', $contents);
self::assertStringContainsString('yansongda', $contents);
}
}

View File

@ -0,0 +1,141 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Alipay;
use PHPUnit\Framework\TestCase;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Parser\NoHttpRequestParser;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Alipay\LaunchPlugin;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Collection;
class LaunchPluginTest extends TestCase
{
protected function setUp(): void
{
$config = [
'alipay' => [
'default' => [
'alipay_public_cert_path' => __DIR__.'/../../Stubs/cert/alipayCertPublicKey_RSA2.crt'
],
]
];
Pay::config($config);
}
protected function tearDown(): void
{
Pay::clear();
}
public function testNoHttpRequest()
{
$rocket = new Rocket();
$rocket->setDirection(NoHttpRequestParser::class);
$plugin = new LaunchPlugin();
self::assertSame($rocket, $plugin->assembly($rocket, function ($rocket) { return $rocket; }));
}
public function testNormal()
{
$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==",
];
$rocket = new Rocket();
$rocket->setPayload(new Collection(['method' => 'alipay.trade.query']))
->setDestination(new Collection($response))
->setParams([]);
$plugin = new LaunchPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
self::assertEqualsCanonicalizing($response['alipay_trade_query_response'], $result->getDestination()->all());
}
public function testCodeError()
{
$response = [
'alipay_trade_query_response' => [
"code" => "40004",
"msg" => "Business Failed",
"sub_code" => "ACQ.TRADE_NOT_EXIST",
"sub_msg" => "交易不存在",
"buyer_pay_amount" => "0.00",
"invoice_amount" => "0.00",
"out_trade_no" => "1622819957",
"point_amount" => "0.00",
"receipt_amount" => "0.00",
],
'alipay_cert_sn' => 'a359aaadd01ceca03dbc07537da539b9',
'sign' => 'OaQiIXuxZeMWccI/gV0/f0YFKmR0zUsUSA+pOUghMJjsbL7W+mNw4Wvk8NFJzlk0EcwV+BpvT/NFl5oSPN2NTn4JbHheVkN9DvYDK8UacvUjnDLO4vZ2Z828he8CF77ktieTjrzxo5b6dguMnOFeew+YAzSCZaiV2sSUSc6K42yiSC290B80jBUbNKE10sUDWR8OKPYqHxMlbtPyGv2jSxNoDIIP7VIGKNzU8i7dbNOYCrAviBXcDrR/m9ncYfIJfhn1yHPtLCGUUcJKToPsvE0+4Q3gS4n+wMHhCcbq02qnwhPSRbmsPS0E7D5JNqVmiXIc2XeEffKYFy1kQKvGGQ==',
];
$rocket = new Rocket();
$rocket->setPayload(new Collection(['method' => 'alipay.trade.query']))
->setDestination(new Collection($response))
->setParams([]);
$plugin = new LaunchPlugin();
self::expectException(InvalidResponseException::class);
self::expectExceptionCode(InvalidResponseException::INVALID_RESPONSE_CODE);
$plugin->assembly($rocket, function ($rocket) { return $rocket; });
}
public function testWrongSing()
{
$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" => "pp1M3pwUFJ19Tx/D+40RZstXr3VSZzGxPB1Qfj1e837UkGxOJxFFK6EZ288SeEh06dPFd4qJ7BHfP/7mvkRqF1/mezBGvhBz03XTXfDn/O6IkoA+cVwpfm+i8MFvzC/ZQB0dgtZppu5qfzVyFaaNu8ct3L/NSQCMR1RXg2lH3HiwfxmIF35+LmCoL7ZPvTxB/epm7A/XNhAjLpK5GlJffPA0qwhhtQwaIZ7DHMXo06z03fbgxlBu2eEclQUm6Fobgj3JEERWLA0MDQiV1EYNWuHSSlHCMrIxWHba+Euu0jVkKKe0IFKsU8xJQbc7GTJXx/o0NfHqGwwq8hMvtgBkg==",
];
$rocket = new Rocket();
$rocket->setPayload(new Collection(['method' => 'alipay.trade.query']))
->setDestination(new Collection($response))
->setParams([]);
$plugin = new LaunchPlugin();
self::expectException(InvalidResponseException::class);
self::expectExceptionCode(InvalidResponseException::INVALID_RESPONSE_SIGN);
$plugin->assembly($rocket, function ($rocket) { return $rocket; });
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Alipay;
use PHPUnit\Framework\TestCase;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Parser\NoHttpRequestParser;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Alipay\LaunchPlugin;
use Yansongda\Pay\Plugin\Alipay\PreparePlugin;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Collection;
class PreparePluginTest extends TestCase
{
protected function setUp(): void
{
$config = [
'alipay' => [
'default' => [
'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 testNormal()
{
$rocket = new Rocket();
$rocket->setParams([]);
$plugin = new PreparePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
self::assertTrue($result->getPayload()->has('app_cert_sn'));
self::assertEquals('fb5e86cfb784de936dd3594e32381cf8', $result->getPayload()->get('app_cert_sn'));
self::assertEquals('687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6', $result->getPayload()->get('alipay_root_cert_sn'));
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Alipay;
use PHPUnit\Framework\TestCase;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Parser\NoHttpRequestParser;
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\Rocket;
use Yansongda\Supports\Collection;
class RadarPluginTest extends TestCase
{
protected function setUp(): void
{
$config = [
'alipay' => [
'default' => [
'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 testPostNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection(['name' => 'yansongda']));
$plugin = new RadarPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
self::assertEquals('name=yansongda', $result->getRadar()->getBody()->getContents());
self::assertEquals('POST', $result->getRadar()->getMethod());
}
public function testGetNormal()
{
$rocket = new Rocket();
$rocket->setParams(['_method' => 'get'])->setPayload(new Collection(['name' => 'yansongda']));
$plugin = new RadarPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
self::assertEquals('GET', $result->getRadar()->getMethod());
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Alipay;
use PHPUnit\Framework\TestCase;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Alipay\SignPlugin;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Collection;
class SignPluginTest extends TestCase
{
protected function setUp(): void
{
$config = [
'alipay' => [
'default' => [
'app_secret_cert' => 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCDRjOg5DnX+8L+rB8d2MbrQ30Z7JPM4hiDhawHSwQCQ7RlmQNpl6b/N6IrPLcPFC1uii179U5Il5xTZynfjkUyJjnHusqnmHskftLJDKkmGbSUFMAlOv+NlpUWMJ2A+VUopl+9FLyqcV+XgbaWizxU3LsTtt64v89iZ2iC16H6/6a3YcP+hDZUjiNGQx9cuwi9eJyykvcwhDkFPxeBxHbfwppsul+DYUyTCcl0Ltbga/mUechk5BksW6yPPwprYHQBXyM16Jc3q5HbNxh3660FyvUBFLuVWIBs6RtR2gZCa6b8rOtCkPQKhUKvzRMlgheOowXsWdk99GjxGQDK5W4XAgMBAAECggEAYPKnjlr+nRPBnnNfR5ugzH67FToyrU0M7ZT6xygPfdyijaXDb2ggXLupeGUOjIRKSSijDrjLZ7EQMkguFHvtfmvcoDTDFaL2zq0a3oALK6gwRGxOuzAnK1naINkmeOmqiqrUab+21emEv098mRGbLNEXGCgltCtz7SiRdo/pgIPZ1wHj4MH0b0K2bFG3xwr51EyaLXKYH4j6w9YAXXsTdvzcJ+eRE0Yq4uGPfkziqg8d0xXSEt90HmCGHKo4O2eh1w1IlBcHfK0F6vkeUAtrtAV01MU2bNoRU147vKFxjDOVBlY1nIZY/drsbiPMuAfSsodL0hJxGSYivbKTX4CWgQKBgQDd0MkF5AIPPdFC+fhWdNclePRw4gUkBwPTIUljMP4o+MhJNrHp0sEy0sr1mzYsOT4J20hsbw/qTnMKGdgy784bySf6/CC7lv2hHp0wyS3Es0DRJuN+aTyyONOKGvQqd8gvuQtuYJy+hkIoHygjvC3TKndX1v66f9vCr/7TS0QPywKBgQCXgVHERHP+CarSAEDG6bzI878/5yqyJVlUeVMG5OXdlwCl0GAAl4mDvfqweUawSVFE7qiSqy3Eaok8KHkYcoRlQmAefHg/C8t2PNFfNrANDdDB99f7UhqhXTdBA6DPyW02eKIaBcXjZ7jEXZzA41a/zxZydKgHvz4pUq1BdbU5ZQKBgHyqGCDgaavpQVAUL1df6X8dALzkuqDp9GNXxOgjo+ShFefX/pv8oCqRQBJTflnSfiSKAqU2skosdwlJRzIxhrQlFPxBcaAcl0VTcGL33mo7mIU0Bw2H1d4QhAuNZIbttSvlIyCQ2edWi54DDMswusyAhHxwz88/huJfiad1GLaLAoGASIweMVNuD5lleMWyPw2x3rAJRnpVUZTc37xw6340LBWgs8XCEsZ9jN4t6s9H8CZLiiyWABWEBufU6z+eLPy5NRvBlxeXJOlq9iVNRMCVMMsKybb6b1fzdI2EZdds69LSPyEozjkxdyE1sqH468xwv8xUPV5rD7qd83+pgwzwSJkCgYBrRV0OZmicfVJ7RqbWyneBG03r7ziA0WTcLdRWDnOujQ9orhrkm+EY2evhLEkkF6TOYv4QFBGSHfGJ0SwD7ghbCQC/8oBvNvuQiPWI8B+00LwyxXNrkFOxy7UfIUdUmLoLc1s/VdBHku+JEd0YmEY+p4sjmcRnlu4AlzLxkWUTTg==',
'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 testNormal()
{
$payload = [
"app_id" => "2016082000295641",
"method" => "alipay.trade.query",
"format" => "JSON",
"return_url" => "http://127.0.0.1:8000/alipay/verify",
"charset" => "utf-8",
"sign_type" => "RSA2",
"timestamp" => "2021-06-07 21:54:50",
"version" => "1.0",
"app_cert_sn" => "fb5e86cfb784de936dd3594e32381cf8",
"alipay_root_cert_sn" => "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6",
"biz_content" => ['out_trade_no' => "yansongda-1622986519"],
];
$sign = "QMh6CzKWIt5yIYCrYrMdC2/Mt+4lTNEaPN0biIZPuiWzgTS7pyIYFOmb+dEi70X5q9UaCBlejwwwTEzRtfIjudPu/mIrlpnwsN8mEhDjyZihmgb/wCZy+kR0OIwvZjTd/3AuALIcwDbhZqDwssZAOTlco4eE7WosEdsob52OfCBAn0ZEf/9zZk5+FSbL8xbwO9hTlspl5ArgFBf9RryBxAviC09Nr5eSNdwYBIyOUdLtEVHBuHHvwa4UfiCFe0SyDFVQODgoz3Mjcs5d4RmJqKNLorkN8dHzjzlAnCR07EHsMzV4ivNG703hReHMyazPDuaWBg11/spMJUNUF/tEBQ==";
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection($payload));
$plugin = new SignPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
self::assertSame($sign, $result->getPayload()->get('sign'));
}
public function testUnderlineParams()
{
$payload = [
"app_id" => "2016082000295641",
"method" => "alipay.trade.query",
"format" => "JSON",
"return_url" => "http://127.0.0.1:8000/alipay/verify",
"charset" => "utf-8",
"sign_type" => "RSA2",
"timestamp" => "2021-06-07 21:54:50",
"version" => "1.0",
"app_cert_sn" => "fb5e86cfb784de936dd3594e32381cf8",
"alipay_root_cert_sn" => "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6",
"biz_content" => ['out_trade_no' => "yansongda-1622986519", '_method' => 'get', '_ignore' => true],
];
$sign = "QMh6CzKWIt5yIYCrYrMdC2/Mt+4lTNEaPN0biIZPuiWzgTS7pyIYFOmb+dEi70X5q9UaCBlejwwwTEzRtfIjudPu/mIrlpnwsN8mEhDjyZihmgb/wCZy+kR0OIwvZjTd/3AuALIcwDbhZqDwssZAOTlco4eE7WosEdsob52OfCBAn0ZEf/9zZk5+FSbL8xbwO9hTlspl5ArgFBf9RryBxAviC09Nr5eSNdwYBIyOUdLtEVHBuHHvwa4UfiCFe0SyDFVQODgoz3Mjcs5d4RmJqKNLorkN8dHzjzlAnCR07EHsMzV4ivNG703hReHMyazPDuaWBg11/spMJUNUF/tEBQ==";
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection($payload));
$plugin = new SignPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
self::assertSame($sign, $result->getPayload()->get('sign'));
}
}