From a36ca9ef229baf8cc8d96729cfe474e2c8677dd2 Mon Sep 17 00:00:00 2001 From: sunaoka Date: Tue, 6 Aug 2024 14:33:07 +0900 Subject: [PATCH 1/4] Added Guzzle request options setting. --- config/paypay.php | 13 +++++++++++++ src/Client.php | 22 ++++++++++++---------- src/Provider/PayPayServiceProvider.php | 2 +- tests/ClientTest.php | 26 ++++++++++++++++++++++++++ tests/TestCase.php | 1 + 5 files changed, 53 insertions(+), 11 deletions(-) diff --git a/config/paypay.php b/config/paypay.php index 7cf6991..babc643 100644 --- a/config/paypay.php +++ b/config/paypay.php @@ -5,4 +5,17 @@ 'api_secret' => env('PAYPAY_API_SECRET'), 'merchant_id' => env('PAYPAY_MERCHANT_ID'), 'production_mode' => (bool) env('PAYPAY_PRODUCTION_MODE', false), + + /* + |-------------------------------------------------------------------------- + | Guzzle Request Options + |-------------------------------------------------------------------------- + | + | You can set request options for Guzzle. However, "base_uri" is overridden + | by PayPay library. + | + | For more information on Request Options, please refer to the following. + | https://docs.guzzlephp.org/en/stable/request-options.html + */ + 'options' => [], ]; diff --git a/src/Client.php b/src/Client.php index bb4b2f0..bf996c4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -27,33 +27,35 @@ class Client extends \PayPay\OpenPaymentAPI\Client */ private ?array $fakeResponse; + private array $options; + /** * @param array{API_KEY: string, API_SECRET: string, MERCHANT_ID: string} $auth * @param bool $productionmode * @param GuzzleHttpClient|false $requestHandler + * @param array $options * * @throws ClientException */ - public function __construct($auth = null, $productionmode = false, $requestHandler = false) + public function __construct($auth = null, $productionmode = false, $requestHandler = false, $options = []) { $this->fakeResponse = null; + $this->options = $options; parent::__construct($auth, $productionmode, $requestHandler); } public function http(): GuzzleHttpClient { - if (empty($this->fakeResponse)) { - return parent::http(); - } + $options = $this->options; + $options['base_uri'] = $this->GetConfig('API_URL'); - $mockHandler = new MockHandler([array_shift($this->fakeResponse)]); - $handlerStack = HandlerStack::create($mockHandler); + if (! empty($this->fakeResponse)) { + $mockHandler = new MockHandler([array_shift($this->fakeResponse)]); + $options['handler'] = HandlerStack::create($mockHandler); + } - return new GuzzleHttpClient([ - 'base_uri' => $this->GetConfig('API_URL'), - 'handler' => $handlerStack, - ]); + return new GuzzleHttpClient($options); } /** diff --git a/src/Provider/PayPayServiceProvider.php b/src/Provider/PayPayServiceProvider.php index 174b0b7..8da7d41 100644 --- a/src/Provider/PayPayServiceProvider.php +++ b/src/Provider/PayPayServiceProvider.php @@ -27,7 +27,7 @@ public function register(): void 'API_KEY' => $config['api_key'], 'API_SECRET' => $config['api_secret'], 'MERCHANT_ID' => $config['merchant_id'], - ], $config['production_mode']); + ], $config['production_mode'], $config['options']); }); $this->app->alias('PayPay', Client::class); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 0da4dd8..934e9ac 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -5,6 +5,7 @@ namespace Tests; use GuzzleHttp\Psr7\Response; +use GuzzleHttp\RequestOptions; use PayPay\OpenPaymentAPI\ClientException; use PayPay\OpenPaymentAPI\Controller\CashBack; use PayPay\OpenPaymentAPI\Controller\ClientControllerException; @@ -120,4 +121,29 @@ public function test_accessor(): void // @phpstan-ignore method.notFound $client->foo(); } + + /** + * @throws ClientException + * @throws \ReflectionException + */ + public function test_http_with_options(): void + { + $options = [ + RequestOptions::PROXY => 'http://localhost:8125', + ]; + $client = new Client([ + 'API_KEY' => 'api_key', + 'API_SECRET' => 'api_secret', + 'MERCHANT_ID' => 'merchant_id', + ], options: $options); + + $http = $client->http(); + + $class = new \ReflectionObject($http); + $property = $class->getProperty('config'); + $config = $property->getValue($http); + + self::assertIsArray($config); + self::assertSame($options[RequestOptions::PROXY], $config['proxy']); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index af96f09..d2cb39b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -50,6 +50,7 @@ protected function defineEnvironment($app): void 'api_secret' => 'api_secret', 'merchant_id' => 'merchant_id', 'production_mode' => false, + 'options' => [], ]); }); } From 96fe7b61bec867b1f47d8297d02d57e874e88a16 Mon Sep 17 00:00:00 2001 From: sunaoka Date: Tue, 6 Aug 2024 14:38:33 +0900 Subject: [PATCH 2/4] Update Laravel Pint --- composer.json | 2 +- tests/ClientTest.php | 2 +- tests/FacadeTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index a9306f5..3bc8e7b 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "paypayopa/php-sdk": "^2.0" }, "require-dev": { - "laravel/pint": "^1.16", + "laravel/pint": "^1.17.0", "orchestra/testbench": "^8.23 || ^9.0", "phpstan/phpstan": "^1.11" }, diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 934e9ac..448ad4f 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -82,7 +82,7 @@ public function test_fake_successful(): void new Response(201, body: json_encode($fakeResponse, JSON_THROW_ON_ERROR)), ]); - $payload = new CreateQrCodePayload(); + $payload = new CreateQrCodePayload; $payload->setMerchantPaymentId('merchant_id'); $payload->setCodeType('ORDER_QR'); diff --git a/tests/FacadeTest.php b/tests/FacadeTest.php index 1ab4291..4e19a91 100644 --- a/tests/FacadeTest.php +++ b/tests/FacadeTest.php @@ -27,7 +27,7 @@ public function test_fake_successful(): void new Response(201, body: json_encode($fakeResponse, JSON_THROW_ON_ERROR)), ]); - $payload = new CreateQrCodePayload(); + $payload = new CreateQrCodePayload; $payload->setMerchantPaymentId('merchant_id'); $payload->setCodeType('ORDER_QR'); From 24d291b2a8d4ee05281e2ab3577b4e330bd6c837 Mon Sep 17 00:00:00 2001 From: sunaoka Date: Tue, 6 Aug 2024 14:50:03 +0900 Subject: [PATCH 3/4] Housekeeping --- src/Client.php | 14 ++++++-------- src/Provider/PayPayServiceProvider.php | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Client.php b/src/Client.php index bf996c4..94c1555 100644 --- a/src/Client.php +++ b/src/Client.php @@ -27,20 +27,18 @@ class Client extends \PayPay\OpenPaymentAPI\Client */ private ?array $fakeResponse; - private array $options; - /** * @param array{API_KEY: string, API_SECRET: string, MERCHANT_ID: string} $auth - * @param bool $productionmode - * @param GuzzleHttpClient|false $requestHandler - * @param array $options * * @throws ClientException */ - public function __construct($auth = null, $productionmode = false, $requestHandler = false, $options = []) - { + public function __construct( + array $auth, + bool $productionmode = false, + GuzzleHttpClient|false $requestHandler = false, + private readonly array $options = [] + ) { $this->fakeResponse = null; - $this->options = $options; parent::__construct($auth, $productionmode, $requestHandler); } diff --git a/src/Provider/PayPayServiceProvider.php b/src/Provider/PayPayServiceProvider.php index 8da7d41..83629e9 100644 --- a/src/Provider/PayPayServiceProvider.php +++ b/src/Provider/PayPayServiceProvider.php @@ -27,7 +27,7 @@ public function register(): void 'API_KEY' => $config['api_key'], 'API_SECRET' => $config['api_secret'], 'MERCHANT_ID' => $config['merchant_id'], - ], $config['production_mode'], $config['options']); + ], $config['production_mode'], false, $config['options']); }); $this->app->alias('PayPay', Client::class); From e7d3f9767e3cc5ae603e0ca048d2941a3f2b4315 Mon Sep 17 00:00:00 2001 From: sunaoka Date: Tue, 6 Aug 2024 14:50:13 +0900 Subject: [PATCH 4/4] Add tests --- tests/ClientTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 448ad4f..3673693 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -130,6 +130,7 @@ public function test_http_with_options(): void { $options = [ RequestOptions::PROXY => 'http://localhost:8125', + RequestOptions::CONNECT_TIMEOUT => 3.14, ]; $client = new Client([ 'API_KEY' => 'api_key', @@ -144,6 +145,7 @@ public function test_http_with_options(): void $config = $property->getValue($http); self::assertIsArray($config); - self::assertSame($options[RequestOptions::PROXY], $config['proxy']); + self::assertSame($options[RequestOptions::PROXY], $config[RequestOptions::PROXY]); + self::assertSame($options[RequestOptions::CONNECT_TIMEOUT], $config[RequestOptions::CONNECT_TIMEOUT]); } }