Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API url override #60

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/Api/ApiAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,9 @@ class ApiAction
/** @var Token */
protected $Token;

/** @var string */
protected $apiUrl;

/** @var bool */
private $productionMode;

Expand All @@ -39,7 +43,22 @@ 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 */
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 = [])
Expand All @@ -65,7 +84,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) {
Expand Down Expand Up @@ -135,7 +154,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) {
Expand Down
40 changes: 29 additions & 11 deletions src/Api/TpayApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down
Loading