Skip to content

Commit

Permalink
Merge pull request #10 from NextmediaMa/DOT-4698
Browse files Browse the repository at this point in the history
DOT-4698: Check YouCan Pay keys on save gateway
  • Loading branch information
5baddi authored Dec 13, 2021
2 parents 87f7b18 + 88deda2 commit 0592cfa
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ use YouCan\Pay\YouCanPay;

$youCanPay = YouCanPay::instance()->useKeys('my-private-key', 'my-public-key');

// check keys are valid
YouCanPay::instance()->checkKeys('private-key', 'public-key');
// or using current instance
$youCanPay->keys->check('private-key', 'public-key');

// generate a token for a new payment
$token = $youCanPay->token->create("order-id", "2000", "USD", "123.123.123.123");
var_dump($token->getToken(), $token->getRedirectURL());
Expand Down
61 changes: 61 additions & 0 deletions src/API/Endpoints/KeysEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace YouCan\Pay\API\Endpoints;

use YouCan\Pay\API\Exceptions\InvalidResponseException;
use YouCan\Pay\API\Response;

class KeysEndpoint extends Endpoint
{
private const BASE_ENDPOINT = 'keys';

/**
* @param string|null $privateKey
* @param string|null $publicKey
* @return bool
* @throws InvalidResponseException
*/
public function check(?string $privateKey = null, ?string $publicKey = null): bool
{
if ($privateKey === null && $publicKey === null) {
return false;
}

$response = $this->apiService->post(
sprintf('%s/check', self::BASE_ENDPOINT),
[
'pri_key' => $privateKey,
'pub_key' => $publicKey,
]
);

return $this->assertResponse($response);
}

protected function endpoint(): string
{
return self::BASE_ENDPOINT;
}

/**
* @param Response $response
* @return bool
* @throws InvalidResponseException
*/
private function assertResponse(Response $response): bool
{
if ($response->getStatusCode() === 200) {
return true;
}

if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
throw new InvalidResponseException(
$response->getStatusCode(),
json_encode($response->getResponse()),
'internal error from server. Support has been notified. Please try again!'
);
}

return false;
}
}
14 changes: 14 additions & 0 deletions src/YouCanPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use YouCan\Pay\API\APIService;
use YouCan\Pay\API\APIServiceInterface;
use YouCan\Pay\API\Endpoints\KeysEndpoint;
use YouCan\Pay\API\Endpoints\TokenEndpoint;
use YouCan\Pay\API\Endpoints\TransactionEndpoint;
use YouCan\Pay\API\HTTPAdapter\HTTPAdapterPicker;
use YouCan\Pay\API\Exceptions\InvalidResponseException;

class YouCanPay
{
Expand All @@ -16,6 +18,9 @@ class YouCanPay
/** @var TokenEndpoint */
public $token;

/** @var KeysEndpoint */
public $keys;

public function __construct(APIServiceInterface $apiService)
{
$this->initializeEndpoints($apiService);
Expand All @@ -38,6 +43,7 @@ private function initializeEndpoints(APIServiceInterface $apiService): void
{
$this->transaction = new TransactionEndpoint($apiService);
$this->token = new TokenEndpoint($apiService);
$this->keys = new KeysEndpoint($apiService);
}

public static function setIsSandboxMode(bool $isSandboxMode): void
Expand All @@ -49,4 +55,12 @@ public static function instance(): self
{
return new self(new APIService(new HTTPAdapterPicker()));
}

/**
* @throws InvalidResponseException
*/
public function checkKeys(?string $privateKey = null, ?string $publicKey = null): bool
{
return $this->keys->check($privateKey, $publicKey);
}
}
50 changes: 50 additions & 0 deletions tests/API/Endpoints/KeysEndpointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Tests\API\Endpoints;

use Tests\API\FakeAPIService;
use Tests\BaseTestCase;
use YouCan\Pay\API\Endpoints\KeysEndpoint;
use YouCan\Pay\API\Response;

class KeysEndpointTest extends BaseTestCase
{
public function test_check_keys_success()
{
$response = new Response(200, []);
$fakeAPIService = new FakeAPIService($response);

$keysEndpoint = new KeysEndpoint($fakeAPIService);
$result = $keysEndpoint->check("pri_key_123", "pub_key_123");

$this->assertTrue($result);
}

public function test_check_not_found_response_when_keys_not_correct()
{
$response = new Response(404, []);

$fakeAPIService = new FakeAPIService($response);

$keysEndpoint = new KeysEndpoint($fakeAPIService);
$result = $keysEndpoint->check("pri_key_123", "pub_key_123");

$this->assertFalse($result);
}

public function test_check_not_found_response_when_no_keys_passed()
{
$response = new Response(404, []);
$fakeAPIService = new FakeAPIService($response);

$keysEndpoint = new KeysEndpoint($fakeAPIService);
$result = $keysEndpoint->check();

$this->assertFalse($result);
}

protected function setUp()
{
parent::setUp();
}
}

0 comments on commit 0592cfa

Please sign in to comment.