Skip to content

Commit

Permalink
Merge branch 'release/v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sunaoka committed Aug 6, 2024
2 parents 0a92991 + e7d3f97 commit 7a39442
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
13 changes: 13 additions & 0 deletions config/paypay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
];
26 changes: 13 additions & 13 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ class Client extends \PayPay\OpenPaymentAPI\Client

/**
* @param array{API_KEY: string, API_SECRET: string, MERCHANT_ID: string} $auth
* @param bool $productionmode
* @param GuzzleHttpClient|false $requestHandler
*
* @throws ClientException
*/
public function __construct($auth = null, $productionmode = false, $requestHandler = false)
{
public function __construct(
array $auth,
bool $productionmode = false,
GuzzleHttpClient|false $requestHandler = false,
private readonly array $options = []
) {
$this->fakeResponse = null;

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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/PayPayServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], false, $config['options']);
});

$this->app->alias('PayPay', Client::class);
Expand Down
30 changes: 29 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,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');

Expand Down Expand Up @@ -120,4 +121,31 @@ 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',
RequestOptions::CONNECT_TIMEOUT => 3.14,
];
$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[RequestOptions::PROXY]);
self::assertSame($options[RequestOptions::CONNECT_TIMEOUT], $config[RequestOptions::CONNECT_TIMEOUT]);
}
}
2 changes: 1 addition & 1 deletion tests/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected function defineEnvironment($app): void
'api_secret' => 'api_secret',
'merchant_id' => 'merchant_id',
'production_mode' => false,
'options' => [],
]);
});
}
Expand Down

0 comments on commit 7a39442

Please sign in to comment.