-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* transaction-by-session-id: update README * transaction-by-session-id: endpoint method, request, response & api test * transaction-by-session-id: tests in progress * transaction-by-session-id: PurchaseInfoRequest success test * transaction-by-session-id: finished PurchaseInfoRequest tests
- Loading branch information
Showing
13 changed files
with
347 additions
and
6 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
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Omnipay\Przelewy24\Message; | ||
|
||
class PurchaseInfoRequest extends AbstractRequest | ||
{ | ||
public function getSessionId(): string | ||
{ | ||
return $this->getParameter('sessionId'); | ||
} | ||
|
||
public function setSessionId(string $value): self | ||
{ | ||
return $this->setParameter('sessionId', $value); | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
$this->validate('sessionId'); | ||
|
||
return [ | ||
'sessionId' => $this->getSessionId(), | ||
]; | ||
} | ||
|
||
public function sendData($data) | ||
{ | ||
$httpResponse = $this->sendRequest('GET', sprintf('transaction/by/sessionId/%s', $data['sessionId']), []); | ||
|
||
$responseData = json_decode($httpResponse->getBody()->getContents(), true); | ||
|
||
return $this->response = new PurchaseInfoResponse($this, $responseData); | ||
} | ||
} |
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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Omnipay\Przelewy24\Message; | ||
|
||
use Omnipay\Common\Message\RequestInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class PurchaseInfoResponse extends AbstractResponse | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private $info = []; | ||
|
||
public function __construct(RequestInterface $request, $data) | ||
{ | ||
parent::__construct($request, $data); | ||
if (isset($data['data'])) { | ||
$this->info = $this->formatInfo($data['data']); | ||
} | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getInfo(): array | ||
{ | ||
return $this->info; | ||
} | ||
|
||
public function getCode(): int | ||
{ | ||
if (isset($this->data['responseCode']) && isset($this->data['error']) && strlen($this->data['error']) > 0) { | ||
return Response::HTTP_NOT_FOUND; | ||
} | ||
|
||
if (isset($this->data['code'])) { | ||
return $this->data['code']; | ||
} | ||
|
||
return self::HTTP_OK; | ||
} | ||
|
||
/** | ||
* @param string[] $data | ||
* @return string[] | ||
*/ | ||
private function formatInfo(array $data): array | ||
{ | ||
$formatted = $data; | ||
|
||
// format | ||
if (isset($formatted['amount'])) { | ||
$formatted['amount'] = $this->getAmountFromInternal((int) $formatted['amount']); | ||
} | ||
|
||
// replace keys | ||
$formatted = $this->replaceInfoKeys($formatted, 'clientEmail', 'email'); | ||
$formatted = $this->replaceInfoKeys($formatted, 'clientName', 'name'); | ||
$formatted = $this->replaceInfoKeys($formatted, 'clientAddress', 'address'); | ||
$formatted = $this->replaceInfoKeys($formatted, 'clientCity', 'city'); | ||
$formatted = $this->replaceInfoKeys($formatted, 'clientPostcode', 'postcode'); | ||
|
||
return $formatted; | ||
} | ||
|
||
/** | ||
* @param string[] $data | ||
* @return string[] | ||
*/ | ||
private function replaceInfoKeys(array $data, string $oldKey, string $newKey): array | ||
{ | ||
if (isset($data[$oldKey])) { | ||
$data[$newKey] = $data[$oldKey]; | ||
unset($data[$oldKey]); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
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
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,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Message; | ||
|
||
use Omnipay\Przelewy24\Message\PurchaseInfoRequest; | ||
use Omnipay\Przelewy24\Message\PurchaseInfoResponse; | ||
use Omnipay\Tests\TestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class PurchaseInfoRequestTest extends TestCase | ||
{ | ||
/** | ||
* @var PurchaseInfoRequest | ||
*/ | ||
private $request; | ||
|
||
public function setUp() | ||
{ | ||
$this->request = new PurchaseInfoRequest($this->getHttpClient(), $this->getHttpRequest()); | ||
$this->request->initialize([ | ||
'sessionId' => '1234567890', | ||
]); | ||
} | ||
|
||
public function testGetData(): void | ||
{ | ||
$data = $this->request->getData(); | ||
|
||
$this->assertSame('1234567890', $data['sessionId']); | ||
} | ||
|
||
public function testSendSuccess() | ||
{ | ||
$this->setMockHttpResponse('PurchaseInfoSuccess.txt'); | ||
$response = $this->request->send(); | ||
|
||
$this->assertInstanceOf(PurchaseInfoResponse::class, $response); | ||
$this->assertTrue($response->isSuccessful()); | ||
$this->assertSame(Response::HTTP_OK, $response->getCode()); | ||
$this->assertCount(18, $response->getInfo()); | ||
|
||
$this->assertSame(1234567890, $response->getInfo()['orderId']); | ||
$this->assertSame('20c62d6b-5ff0-46a0-97eb-eea0dd5b4a93', $response->getInfo()['sessionId']); | ||
$this->assertSame(1, $response->getInfo()['status']); | ||
$this->assertSame('12.34', $response->getInfo()['amount']); | ||
$this->assertSame('PLN', $response->getInfo()['currency']); | ||
$this->assertSame('202205161730', $response->getInfo()['date']); | ||
$this->assertSame('202205161730', $response->getInfo()['dateOfTransaction']); | ||
$this->assertSame(154, $response->getInfo()['paymentMethod']); | ||
$this->assertSame('transaction description', $response->getInfo()['description']); | ||
$this->assertSame(0, $response->getInfo()['batchId']); | ||
$this->assertSame("12", $response->getInfo()['fee']); | ||
$this->assertSame("P24-K12-B34-H56", $response->getInfo()['statement']); | ||
$this->assertSame('[email protected]', $response->getInfo()['email']); | ||
$this->assertSame('Franek Dolas', $response->getInfo()['name']); | ||
$this->assertSame('Kościuszki 12', $response->getInfo()['address']); | ||
$this->assertSame('Kraków', $response->getInfo()['city']); | ||
$this->assertSame('30-611', $response->getInfo()['postcode']); | ||
} | ||
|
||
public function testSendAuthFailure() | ||
{ | ||
$this->setMockHttpResponse('PurchaseInfoAuthFailure.txt'); | ||
$response = $this->request->send(); | ||
|
||
$this->assertInstanceOf(PurchaseInfoResponse::class, $response); | ||
$this->assertFalse($response->isSuccessful()); | ||
$this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getCode()); | ||
$this->assertSame('Incorrect authentication', $response->getMessage()); | ||
} | ||
|
||
public function testSendInvalidDataFailure() | ||
{ | ||
$this->setMockHttpResponse('PurchaseInfoInvalidDataFailure.txt'); | ||
$response = $this->request->send(); | ||
|
||
$this->assertInstanceOf(PurchaseInfoResponse::class, $response); | ||
$this->assertFalse($response->isSuccessful()); | ||
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getCode()); | ||
$this->assertSame('Invalid input data', $response->getMessage()); | ||
} | ||
|
||
public function testSendNotFoundFailure() | ||
{ | ||
$this->setMockHttpResponse('PurchaseInfoNotFoundFailure.txt'); | ||
$response = $this->request->send(); | ||
|
||
$this->assertInstanceOf(PurchaseInfoResponse::class, $response); | ||
$this->assertFalse($response->isSuccessful()); | ||
$this->assertSame(Response::HTTP_NOT_FOUND, $response->getCode()); | ||
$this->assertSame('Transaction not found', $response->getMessage()); | ||
} | ||
} |
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,12 @@ | ||
HTTP/1.1 201 Created | ||
Server: nginx/1.4.4 | ||
Date: Sat, 16 May 2022 17:30:00 GMT | ||
Content-Type: application/json; charset=utf-8 | ||
Connection: keep-alive | ||
Cache-Control: no-cache, no-store | ||
X-Request-Id: 53e398c2-1c9a-4e8b-a3a7-9dcf3cad90b1 | ||
|
||
{ | ||
"error": "Incorrect authentication", | ||
"code": 401 | ||
} |
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,12 @@ | ||
HTTP/1.1 201 Created | ||
Server: nginx/1.4.4 | ||
Date: Sat, 16 May 2022 17:30:00 GMT | ||
Content-Type: application/json; charset=utf-8 | ||
Connection: keep-alive | ||
Cache-Control: no-cache, no-store | ||
X-Request-Id: 53e398c2-1c9a-4e8b-a3a7-9dcf3cad90b1 | ||
|
||
{ | ||
"error": "Invalid input data", | ||
"code": 400 | ||
} |
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,12 @@ | ||
HTTP/1.1 201 Created | ||
Server: nginx/1.4.4 | ||
Date: Sat, 16 May 2022 17:30:00 GMT | ||
Content-Type: application/json; charset=utf-8 | ||
Connection: keep-alive | ||
Cache-Control: no-cache, no-store | ||
X-Request-Id: 53e398c2-1c9a-4e8b-a3a7-9dcf3cad90b1 | ||
|
||
{ | ||
"error": "Transaction not found", | ||
"responseCode": 0 | ||
} |
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,12 @@ | ||
HTTP/1.1 201 Created | ||
Server: nginx/1.4.4 | ||
Date: Sat, 16 May 2022 17:30:00 GMT | ||
Content-Type: application/json; charset=utf-8 | ||
Connection: keep-alive | ||
Cache-Control: no-cache, no-store | ||
X-Request-Id: 53e398c2-1c9a-4e8b-a3a7-9dcf3cad90b1 | ||
|
||
{ | ||
"error": "Transaction not found", | ||
"responseCode": 0 | ||
} |
Oops, something went wrong.