-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from NextmediaMa/DOT-4698
DOT-4698: Check YouCan Pay keys on save gateway
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |