Skip to content

Commit

Permalink
[/api/v1/card/charge] (#19)
Browse files Browse the repository at this point in the history
* card-charge: update README

* card-charge: done
  • Loading branch information
mysiar authored May 20, 2022
1 parent d688968 commit ce5c58e
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
| /api/v1/transaction/by/sessionId | purchaseInfo |
| /api/v1/card/info | cardInfo |
| /api/v1/card/pay | cardPay |
| /api/v1/card/charge | cardCharge |

## Install

Expand Down
10 changes: 10 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Omnipay\Common\AbstractGateway;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Przelewy24\Message\CardChargeRequest;
use Omnipay\Przelewy24\Message\CardInfoRequest;
use Omnipay\Przelewy24\Message\CardPayRequest;
use Omnipay\Przelewy24\Message\CompletePurchaseRequest;
Expand Down Expand Up @@ -182,4 +183,13 @@ public function cardPay(array $options): CardPayRequest
{
return $this->createRequest(CardPayRequest::class, $options);
}

/**
* @param string[] $options
* @return AbstractRequest|CardChargeRequest
*/
public function cardCharge(array $options): CardChargeRequest
{
return $this->createRequest(CardChargeRequest::class, $options);
}
}
26 changes: 26 additions & 0 deletions src/Message/CardChargeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Omnipay\Przelewy24\Message;

class CardChargeRequest extends AbstractRequest
{
public function getData(): array
{
$this->validate('token');

return [
'token' => $this->getToken(),
];
}

public function sendData($data): CardChargeResponse
{
$httpResponse = $this->sendRequest('POST', 'card/charge', $data);

$responseData = json_decode($httpResponse->getBody()->getContents(), true);

return $this->response = new CardChargeResponse($this, $responseData);
}
}
30 changes: 30 additions & 0 deletions src/Message/CardChargeResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Omnipay\Przelewy24\Message;

use Omnipay\Common\Message\RequestInterface;

class CardChargeResponse extends AbstractResponse
{
/**
* @var null|string
*/
private $transactionId = null;

public function __construct(RequestInterface $request, $data)
{
parent::__construct($request, $data);
if (isset($data['data'])) {
if (isset($data['data']['orderId'])) {
$this->transactionId = (string) $data['data']['orderId'];
}
}
}

public function getTransactionId(): ?string
{
return $this->transactionId;
}
}
10 changes: 10 additions & 0 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Omnipay\Przelewy24\Gateway;
use Omnipay\Przelewy24\Message\CardChargeRequest;
use Omnipay\Przelewy24\Message\CardInfoRequest;
use Omnipay\Przelewy24\Message\CardPayRequest;
use Omnipay\Przelewy24\Message\CompletePurchaseRequest;
Expand Down Expand Up @@ -235,6 +236,15 @@ public function it_should_create_card_pay()
$this->assertInstanceOf(CardPayRequest::class, $request);
}

/**
* @test
*/
public function it_should_create_card_charge()
{
$request = $this->gateway->cardCharge([]);
$this->assertInstanceOf(CardChargeRequest::class, $request);
}

public function refund_data_provider(): array
{
return [
Expand Down
66 changes: 66 additions & 0 deletions tests/Message/CardChargeRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Message;

use Omnipay\Przelewy24\Message\CardChargeRequest;
use Omnipay\Przelewy24\Message\CardChargeResponse;
use Omnipay\Tests\TestCase;
use Symfony\Component\HttpFoundation\Response;

class CardChargeRequestTest extends TestCase
{
/**
* @var CardChargeRequest
*/
private $request;

public function setUp()
{
$this->request = new CardChargeRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->initialize([
"token" => "29fa01f8-6bb8-4187-9fb0-ec6e1a62a731",
]);
}

public function testGetData(): void
{
$data = $this->request->getData();

$this->assertSame('29fa01f8-6bb8-4187-9fb0-ec6e1a62a731', $data['token']);
}

public function testSendSuccess()
{
$this->setMockHttpResponse('CardChargeSuccess.txt');
/** @var CardChargeResponse $response */
$response = $this->request->send();

$this->assertInstanceOf(CardChargeResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame('1234567890', $response->getTransactionId());
}

public function testSendAuthFailure()
{
$this->setMockHttpResponse('CardChargeAuthFailure.txt');
$response = $this->request->send();

$this->assertInstanceOf(CardChargeResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getCode());
$this->assertSame('Incorrect authentication', $response->getMessage());
}

public function testSendInvalidDataFailure()
{
$this->setMockHttpResponse('CardChargeInvalidDataFailure.txt');
$response = $this->request->send();

$this->assertInstanceOf(CardChargeResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getCode());
$this->assertSame('Invalid input data', $response->getMessage());
}
}
16 changes: 16 additions & 0 deletions tests/Mock/CardChargeAuthFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
HTTP/1.1 422 Unprocessable Entity
Server: nginx/1.4.4
Date: Sat, 20 May 2022 07:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 101
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Max-Age: 300
Cache-Control: no-cache, no-store
Strict-Transport-Security: max-age=31556926; includeSubDomains

{
"error": "Incorrect authentication",
"code": 401
}
16 changes: 16 additions & 0 deletions tests/Mock/CardChargeInvalidDataFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
HTTP/1.1 422 Unprocessable Entity
Server: nginx/1.4.4
Date: Sat, 20 May 2022 07:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 101
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Max-Age: 300
Cache-Control: no-cache, no-store
Strict-Transport-Security: max-age=31556926; includeSubDomains

{
"error": "Invalid input data",
"code": 400
}
18 changes: 18 additions & 0 deletions tests/Mock/CardChargeSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
HTTP/1.1 422 Unprocessable Entity
Server: nginx/1.4.4
Date: Sat, 20 May 2022 07:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 101
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Max-Age: 300
Cache-Control: no-cache, no-store
Strict-Transport-Security: max-age=31556926; includeSubDomains

{
"data": {
"orderId": 1234567890
},
"responseCode": 0
}

0 comments on commit ce5c58e

Please sign in to comment.