From 0d07a1443f684d3757d8b052523e4715b38e1a87 Mon Sep 17 00:00:00 2001 From: Nordine Charafi Date: Thu, 26 Aug 2021 14:40:09 +0100 Subject: [PATCH] DOT-3808: Adapt base url for sandbox mode (#7) --- README.md | 16 ++++++++++------ src/API/APIService.php | 11 ++++++----- src/YouCanPay.php | 9 ++++++--- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c2e8b11..7fe06b8 100755 --- a/README.md +++ b/README.md @@ -7,17 +7,17 @@ This package allows the developer to interact easily with the YouCan Pay API. ```php use YouCan\Pay\YouCanPay; -$youcanPay = YouCanPay::instance(); +$youCanPay = YouCanPay::instance(); // get your key from your account settings -$youcanPay->setPrivateKey("my-key-here"); +$youCanPay->setPrivateKey("my-key-here"); // generate a token for a new payment -$token = $youcanPay->generateToken("order-id", "20.50", "USD", "123.123.123.123"); +$token = $youCanPay->generateToken("order-id", "20.50", "USD", "123.123.123.123"); var_dump($token->getToken(), $token->getRedirectURL()); // get details of a transaction -$transaction = $youcanPay->getTransaction('transaction-id'); +$transaction = $youCanPay->getTransaction('transaction-id'); var_dump($transaction->getAmount(), $transaction->getCurrency()); ``` @@ -29,8 +29,12 @@ use YouCan\Pay\API\APIService; use YouCan\Pay\YouCanPay; // prod environment -$youcanPay = YouCanPay::instance(APIService::PROD_ENVIRONMENT); +$youCanPay = YouCanPay::instance(APIService::PROD_ENVIRONMENT); // test environment -$youcanPay = YouCanPay::instance(APIService::TEST_ENVIRONMENT); +$youCanPay = YouCanPay::instance(APIService::TEST_ENVIRONMENT); + +// enable sandbox mode +$youCanPay->setIsSandboxMode(true); + ``` diff --git a/src/API/APIService.php b/src/API/APIService.php index 4846c3a..26173d6 100755 --- a/src/API/APIService.php +++ b/src/API/APIService.php @@ -78,8 +78,7 @@ private function initHttpClient(): void 'base_uri' => $this->getBaseUrl(), RequestOptions::HTTP_ERRORS => false, RequestOptions::HEADERS => [ - 'Content-Type' => 'application/json', - 'X-IS-SANDBOX-MODE' => self::$isSandboxMode + 'Content-Type' => 'application/json' ] ]); } @@ -96,14 +95,16 @@ public static function setIsSandboxMode(bool $isSandboxMode): void private function getBaseUrl(): string { + $sandboxUrlPrefix = self::$isSandboxMode ? 'sandbox/' : ''; + if (static::$environment === self::DEV_ENVIRONMENT) { - return "http://pay.dotshop.com/api/"; + return sprintf("http://pay.dotshop.com/%sapi/", $sandboxUrlPrefix); } if (static::$environment === self::TEST_ENVIRONMENT) { - return "https://pay.testyoucan.shop/api/"; + return sprintf("https://pay.testyoucan.shop/%sapi/", $sandboxUrlPrefix); } - return "https://pay.youcan.shop/api/"; + return sprintf("https://pay.youcan.shop/%sapi/", $sandboxUrlPrefix); } } diff --git a/src/YouCanPay.php b/src/YouCanPay.php index e3cbb9d..e305370 100755 --- a/src/YouCanPay.php +++ b/src/YouCanPay.php @@ -26,14 +26,12 @@ class YouCanPay public function __construct( TokenizationService $tokenizationService, TransactionService $transactionService, - int $environment = APIService::PROD_ENVIRONMENT, - bool $isSandboxMode = false + int $environment = APIService::PROD_ENVIRONMENT ) { $this->tokenizationService = $tokenizationService; $this->transactionService = $transactionService; APIService::setEnvironment($environment); - APIService::setIsSandboxMode($isSandboxMode); } public function generateToken(string $orderId, string $amount, string $currency, string $customerIP, array $customerInfo = null): ?Token @@ -95,6 +93,11 @@ private function assertPublicKeyIsSet(): void } } + public static function setIsSandboxMode(bool $isSandboxMode): void + { + APIService::setIsSandboxMode($isSandboxMode); + } + public static function instance(int $environment = APIService::PROD_ENVIRONMENT): self { return new self(