Skip to content

Commit

Permalink
DOT-3808: Adapt base url for sandbox mode (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nordine Charafi authored Aug 26, 2021
1 parent 0362f25 commit 0d07a14
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
```

Expand All @@ -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);

```
11 changes: 6 additions & 5 deletions src/API/APIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
]);
}
Expand All @@ -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);
}
}
9 changes: 6 additions & 3 deletions src/YouCanPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0d07a14

Please sign in to comment.