2021-06-07 22:18:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Yansongda\Pay\Tests\Plugin\Alipay;
|
|
|
|
|
2022-09-03 13:39:21 +08:00
|
|
|
use Yansongda\Pay\Contract\ConfigInterface;
|
|
|
|
use Yansongda\Pay\Exception\Exception;
|
|
|
|
use Yansongda\Pay\Exception\InvalidConfigException;
|
|
|
|
use Yansongda\Pay\Pay;
|
2021-06-07 22:18:15 +08:00
|
|
|
use Yansongda\Pay\Plugin\Alipay\PreparePlugin;
|
|
|
|
use Yansongda\Pay\Rocket;
|
2021-07-17 15:37:43 +08:00
|
|
|
use Yansongda\Pay\Tests\TestCase;
|
2022-09-03 13:39:21 +08:00
|
|
|
use Yansongda\Supports\Config;
|
2021-06-07 22:18:15 +08:00
|
|
|
|
|
|
|
class PreparePluginTest extends TestCase
|
|
|
|
{
|
2022-09-03 13:39:21 +08:00
|
|
|
protected $plugin;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->plugin = new PreparePlugin();
|
|
|
|
}
|
|
|
|
|
2021-06-07 22:18:15 +08:00
|
|
|
public function testNormal()
|
|
|
|
{
|
|
|
|
$rocket = new Rocket();
|
|
|
|
$rocket->setParams([]);
|
|
|
|
|
2022-09-03 13:39:21 +08:00
|
|
|
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
2022-04-12 21:15:14 +08:00
|
|
|
$payload = $result->getPayload();
|
2021-06-07 22:18:15 +08:00
|
|
|
|
2022-04-12 21:15:14 +08:00
|
|
|
self::assertTrue($payload->has('app_cert_sn'));
|
|
|
|
self::assertEquals('fb5e86cfb784de936dd3594e32381cf8', $payload->get('app_cert_sn'));
|
|
|
|
self::assertEquals('687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6', $payload->get('alipay_root_cert_sn'));
|
|
|
|
self::assertEquals('yansongda_token', $payload->get('app_auth_token'));
|
2021-06-07 22:18:15 +08:00
|
|
|
}
|
2021-07-25 15:21:49 +08:00
|
|
|
|
2021-09-18 11:11:50 +08:00
|
|
|
public function testGlobalBcscale()
|
|
|
|
{
|
|
|
|
bcscale(2);
|
|
|
|
|
|
|
|
$rocket = new Rocket();
|
|
|
|
$rocket->setParams([]);
|
|
|
|
|
2022-09-03 13:39:21 +08:00
|
|
|
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
2021-09-18 11:11:50 +08:00
|
|
|
|
|
|
|
self::assertEquals('687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6', $result->getPayload()->get('alipay_root_cert_sn'));
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:21:49 +08:00
|
|
|
public function testCustomizedReturnUrl()
|
|
|
|
{
|
|
|
|
$rocket = new Rocket();
|
|
|
|
$rocket->setParams([
|
|
|
|
'_return_url' => 'https://yansongda.cn',
|
|
|
|
]);
|
|
|
|
|
2022-09-03 13:39:21 +08:00
|
|
|
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
2021-07-25 15:21:49 +08:00
|
|
|
|
|
|
|
self::assertEquals('', $result->getPayload()->get('notify_url'));
|
|
|
|
self::assertEquals('https://yansongda.cn', $result->getPayload()->get('return_url'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCustomizedNotifyUrl()
|
|
|
|
{
|
|
|
|
$rocket = new Rocket();
|
|
|
|
$rocket->setParams([
|
|
|
|
'_notify_url' => 'https://yansongda.cn',
|
|
|
|
]);
|
|
|
|
|
2022-09-03 13:39:21 +08:00
|
|
|
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
2021-07-25 15:21:49 +08:00
|
|
|
|
|
|
|
self::assertEquals('', $result->getPayload()->get('return_url'));
|
|
|
|
self::assertEquals('https://yansongda.cn', $result->getPayload()->get('notify_url'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCustomizedReturnNotifyUrl()
|
|
|
|
{
|
|
|
|
$rocket = new Rocket();
|
|
|
|
$rocket->setParams([
|
|
|
|
'_return_url' => 'https://yansongda.cn',
|
|
|
|
'_notify_url' => 'https://yansongda.cn',
|
|
|
|
]);
|
|
|
|
|
2022-09-03 13:39:21 +08:00
|
|
|
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
2021-07-25 15:21:49 +08:00
|
|
|
|
|
|
|
self::assertEquals('https://yansongda.cn', $result->getPayload()->get('return_url'));
|
|
|
|
self::assertEquals('https://yansongda.cn', $result->getPayload()->get('notify_url'));
|
|
|
|
}
|
2022-04-12 21:15:14 +08:00
|
|
|
|
|
|
|
public function testCustomizedAppAuthToken()
|
|
|
|
{
|
|
|
|
$rocket = new Rocket();
|
|
|
|
$rocket->setParams([
|
|
|
|
'_app_auth_token' => 'yansongda.cn',
|
|
|
|
]);
|
|
|
|
|
2022-09-03 13:39:21 +08:00
|
|
|
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
2022-04-12 21:15:14 +08:00
|
|
|
|
|
|
|
self::assertEquals('yansongda.cn', $result->getPayload()->get('app_auth_token'));
|
|
|
|
}
|
2022-09-03 13:39:21 +08:00
|
|
|
|
|
|
|
public function testMissingAppPublicCertPath()
|
|
|
|
{
|
|
|
|
$rocket = new Rocket();
|
|
|
|
|
|
|
|
Pay::set(ConfigInterface::class, new Config());
|
|
|
|
|
|
|
|
self::expectException(InvalidConfigException::class);
|
|
|
|
self::expectExceptionCode(Exception::ALIPAY_CONFIG_ERROR);
|
|
|
|
self::expectExceptionMessage('Missing Alipay Config -- [app_public_cert_path]');
|
|
|
|
|
|
|
|
$this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWrongAppPublicCertPath()
|
|
|
|
{
|
|
|
|
$rocket = new Rocket();
|
|
|
|
$config = Pay::get(ConfigInterface::class);
|
|
|
|
$config->set('alipay.default.app_public_cert_path', __DIR__.'/../../Cert/foo');
|
|
|
|
|
|
|
|
Pay::set(ConfigInterface::class, $config);
|
|
|
|
|
|
|
|
self::expectException(InvalidConfigException::class);
|
|
|
|
self::expectExceptionCode(Exception::ALIPAY_CONFIG_ERROR);
|
|
|
|
self::expectExceptionMessage('Parse `app_public_cert_path` Error');
|
|
|
|
|
|
|
|
$this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMissingAlipayRootPath()
|
|
|
|
{
|
|
|
|
$rocket = new Rocket();
|
|
|
|
$config = Pay::get(ConfigInterface::class);
|
|
|
|
|
|
|
|
$config->set('alipay.default.alipay_root_cert_path', null);
|
|
|
|
|
|
|
|
self::expectException(InvalidConfigException::class);
|
|
|
|
self::expectExceptionCode(Exception::ALIPAY_CONFIG_ERROR);
|
|
|
|
self::expectExceptionMessage('Missing Alipay Config -- [alipay_root_cert_path]');
|
|
|
|
|
|
|
|
$this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWrongAlipayRootPath()
|
|
|
|
{
|
|
|
|
$rocket = new Rocket();
|
|
|
|
$config = Pay::get(ConfigInterface::class);
|
|
|
|
|
|
|
|
$config->set('alipay.default.alipay_root_cert_path', __DIR__.'/../../Cert/foo');
|
|
|
|
|
|
|
|
self::expectException(InvalidConfigException::class);
|
|
|
|
self::expectExceptionCode(Exception::ALIPAY_CONFIG_ERROR);
|
|
|
|
self::expectExceptionMessage('Invalid alipay_root_cert');
|
|
|
|
|
|
|
|
$this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
|
|
|
|
}
|
2022-09-04 12:19:33 +08:00
|
|
|
|
|
|
|
public function testAppCertSnCached()
|
|
|
|
{
|
|
|
|
$result = $this->plugin->assembly(new Rocket(), function ($rocket) { return $rocket; });
|
|
|
|
$payload = $result->getPayload();
|
|
|
|
|
|
|
|
self::assertEquals('fb5e86cfb784de936dd3594e32381cf8', $payload->get('app_cert_sn'));
|
|
|
|
|
|
|
|
$config = Pay::get(ConfigInterface::class);
|
|
|
|
$config->set('alipay.default.app_public_cert_path', null);
|
|
|
|
|
|
|
|
$result = $this->plugin->assembly(new Rocket(), function ($rocket) { return $rocket; });
|
|
|
|
$payload = $result->getPayload();
|
|
|
|
|
|
|
|
self::assertEquals('fb5e86cfb784de936dd3594e32381cf8', $payload->get('app_cert_sn'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAlipayRootCertSnCached()
|
|
|
|
{
|
|
|
|
$result = $this->plugin->assembly(new Rocket(), function ($rocket) { return $rocket; });
|
|
|
|
$payload = $result->getPayload();
|
|
|
|
|
|
|
|
self::assertEquals('687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6', $payload->get('alipay_root_cert_sn'));
|
|
|
|
|
|
|
|
$config = Pay::get(ConfigInterface::class);
|
|
|
|
$config->set('alipay.default.alipay_root_cert_path', null);
|
|
|
|
|
|
|
|
$result = $this->plugin->assembly(new Rocket(), function ($rocket) { return $rocket; });
|
|
|
|
$payload = $result->getPayload();
|
|
|
|
|
|
|
|
self::assertEquals('687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6', $payload->get('alipay_root_cert_sn'));
|
|
|
|
}
|
2021-06-07 22:18:15 +08:00
|
|
|
}
|