From 118bab84213d9fde9733c157bb1687017e7f1b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Wojtyczka?= Date: Thu, 15 Feb 2024 08:41:17 +0100 Subject: [PATCH] API url override --- src/Api/ApiAction.php | 29 ++++++++++++++++++++++++++--- src/Api/TpayApi.php | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/Api/ApiAction.php b/src/Api/ApiAction.php index 05c91e5..8e845e0 100644 --- a/src/Api/ApiAction.php +++ b/src/Api/ApiAction.php @@ -2,6 +2,7 @@ namespace Tpay\OpenApi\Api; +use RuntimeException; use Tpay\OpenApi\Curl\Curl; use Tpay\OpenApi\Dictionary\HttpCodesDictionary; use Tpay\OpenApi\Manager\Manager; @@ -24,6 +25,9 @@ class ApiAction /** @var Token */ protected $Token; + /** @var string */ + protected $apiUrl; + /** @var bool */ private $productionMode; @@ -39,7 +43,26 @@ public function __construct($Token, $productionMode) $this->Token = $Token; $this->Curl = new Curl(); $this->Manager = new Manager(); - $this->clientName = 'tpay-com/tpay-openapi-php:1.6.6|PHP:'.phpversion(); + $this->clientName = 'tpay-com/tpay-openapi-php:1.6.8|PHP:'.phpversion(); + $this->apiUrl = true === $this->productionMode + ? ApiAction::TPAY_API_URL_PRODUCTION + : ApiAction::TPAY_API_URL_SANDBOX; + } + + /** + * @param string $apiUrl + * + * @return self + */ + public function overrideApiUrl($apiUrl) + { + if (!filter_var($apiUrl, FILTER_VALIDATE_URL)) { + throw new RuntimeException(sprintf('Invalid URL provided: %s', $apiUrl)); + } + + $this->apiUrl = $apiUrl; + + return $this; } public function run($requestMethod, $apiMethod, $fields = [], $requestBody = null, $headers = []) @@ -65,7 +88,7 @@ public function download($requestMethod, $apiMethod, $fields = [], $requestBody $requestUrl = sprintf( '%s%s', - true === $this->productionMode ? static::TPAY_API_URL_PRODUCTION : static::TPAY_API_URL_SANDBOX, + $this->apiUrl, $apiMethod ); if (is_string($this->Token->access_token->getValue()) && '/oauth/auth' !== $apiMethod) { @@ -135,7 +158,7 @@ protected function sendRequest($apiMethod, $requestMethod, $fields = [], $header { $requestUrl = sprintf( '%s%s', - true === $this->productionMode ? static::TPAY_API_URL_PRODUCTION : static::TPAY_API_URL_SANDBOX, + $this->apiUrl, $apiMethod ); if (is_string($this->Token->access_token->getValue()) && '/oauth/auth' !== $apiMethod) { diff --git a/src/Api/TpayApi.php b/src/Api/TpayApi.php index 7c71c9a..003a963 100644 --- a/src/Api/TpayApi.php +++ b/src/Api/TpayApi.php @@ -52,18 +52,31 @@ class TpayApi /** @var string */ private $scope; + /** @var string */ + private $apiUrl; + /** - * @param string $clientId - * @param string $clientSecret - * @param bool $productionMode - * @param string $scope + * @param string $clientId + * @param string $clientSecret + * @param bool $productionMode + * @param string $scope + * @param null|string $apiUrlOverride */ - public function __construct($clientId, $clientSecret, $productionMode = false, $scope = 'read') + public function __construct($clientId, $clientSecret, $productionMode = false, $scope = 'read', $apiUrlOverride = null) { $this->clientId = $clientId; $this->clientSecret = $clientSecret; $this->productionMode = $productionMode; $this->scope = $scope; + $this->apiUrl = true === $this->productionMode + ? ApiAction::TPAY_API_URL_PRODUCTION + : ApiAction::TPAY_API_URL_SANDBOX; + if (null !== $apiUrlOverride) { + if (!filter_var($apiUrlOverride, FILTER_VALIDATE_URL)) { + throw new RuntimeException(sprintf('Invalid URL provided: %s', $apiUrlOverride)); + } + $this->apiUrl = $apiUrlOverride; + } } /** @@ -103,7 +116,8 @@ public function accounts() { $this->authorize(); if (null === $this->accounts) { - $this->accounts = new AccountsApi($this->token, $this->productionMode); + $this->accounts = (new AccountsApi($this->token, $this->productionMode)) + ->overrideApiUrl($this->apiUrl); } return $this->accounts; @@ -114,7 +128,8 @@ public function authorization() { $this->authorize(); if (null === $this->authorization) { - $this->authorization = new AuthorizationApi($this->token, $this->productionMode); + $this->authorization = (new AuthorizationApi($this->token, $this->productionMode)) + ->overrideApiUrl($this->apiUrl); } return $this->authorization; @@ -125,7 +140,8 @@ public function refunds() { $this->authorize(); if (null === $this->refunds) { - $this->refunds = new RefundsApi($this->token, $this->productionMode); + $this->refunds = (new RefundsApi($this->token, $this->productionMode)) + ->overrideApiUrl($this->apiUrl); } return $this->refunds; @@ -136,7 +152,8 @@ public function reports() { $this->authorize(); if (null === $this->reports) { - $this->reports = new ReportsApi($this->token, $this->productionMode); + $this->reports = (new ReportsApi($this->token, $this->productionMode)) + ->overrideApiUrl($this->apiUrl); } return $this->reports; @@ -147,7 +164,8 @@ public function transactions() { $this->authorize(); if (null === $this->transactions) { - $this->transactions = new TransactionsApi($this->token, $this->productionMode); + $this->transactions = (new TransactionsApi($this->token, $this->productionMode)) + ->overrideApiUrl($this->apiUrl); } return $this->transactions; @@ -162,7 +180,7 @@ private function authorize() return; } - $authApi = new AuthorizationApi(new Token(), $this->productionMode); + $authApi = (new AuthorizationApi(new Token(), $this->productionMode))->overrideApiUrl($this->apiUrl); $fields = [ 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret,