Skip to content

Commit

Permalink
Merge pull request #20 from mysiar/dev
Browse files Browse the repository at this point in the history
cardPay & cardCharge
  • Loading branch information
mysiar authored May 20, 2022
2 parents 72254b8 + ce5c58e commit a54b540
Show file tree
Hide file tree
Showing 16 changed files with 505 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
| /api/v1/transaction/refund | refund |
| /api/v1/transaction/by/sessionId | purchaseInfo |
| /api/v1/card/info | cardInfo |
| /api/v1/card/pay | cardPay |
| /api/v1/card/charge | cardCharge |

## Install

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

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;
use Omnipay\Przelewy24\Message\MethodsRequest;
use Omnipay\Przelewy24\Message\PurchaseInfoRequest;
Expand Down Expand Up @@ -172,4 +174,22 @@ public function refund(array $options = []): RefundsRequest
{
return $this->createRequest(RefundsRequest::class, $options);
}

/**
* @param string[] $options
* @return AbstractRequest|CardPayRequest
*/
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;
}
}
70 changes: 70 additions & 0 deletions src/Message/CardPayRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Omnipay\Przelewy24\Message;

class CardPayRequest extends AbstractRequest
{
public function getNumber(): string
{
return $this->getParameter('number');
}

public function setNumber(string $value): self
{
return $this->setParameter('number', $value);
}

public function getExpiry(): string
{
return $this->getParameter('expiry');
}

public function setExpiry(string $value): self
{
return $this->setParameter('expiry', $value);
}

public function getCvv(): string
{
return $this->getParameter('cvv');
}

public function setCvv(string $value): self
{
return $this->setParameter('cvv', $value);
}

public function getName(): string
{
return $this->getParameter('name');
}

public function setName(string $value): self
{
return $this->setParameter('name', $value);
}

public function getData(): array
{
$this->validate('transactionId', 'number', 'expiry', 'cvv', 'name');

return [
'transactionToken' => $this->getTransactionId(),
'cardNumber' => $this->getNumber(),
'cardDate' => $this->getExpiry(),
'cvv' => $this->getCvv(),
'clientName' => $this->getName(),
];
}

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

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

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

declare(strict_types=1);

namespace Omnipay\Przelewy24\Message;

use Omnipay\Common\Message\RequestInterface;
use Symfony\Component\HttpFoundation\Response;

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

/**
* @var null
*/
private $redirectUrl = 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'];
}
if (isset($data['data']['redirectUrl'])) {
$this->redirectUrl = $data['data']['redirectUrl'];
}
}
}

public function getCode(): int
{
if (
isset($this->data['code'])
&& 0 === $this->data['code']
&& isset($this->data['error'])
&& strlen($this->data['error']) > 0
) {
return Response::HTTP_CONFLICT;
}

if (isset($this->data['code'])) {
return $this->data['code'];
}

return Response::HTTP_OK;
}

public function getTransactionId(): ?string
{
return $this->transactionId;
}

public function getRedirectUrl(): ?string
{
return $this->redirectUrl;
}

public function isRedirect(): bool
{
return $this->isSuccessful() && null !== $this->getRedirectUrl();
}
}
20 changes: 20 additions & 0 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
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;
use Omnipay\Przelewy24\Message\MethodsRequest;
use Omnipay\Przelewy24\Message\PurchaseInfoRequest;
Expand Down Expand Up @@ -225,6 +227,24 @@ public function it_should_create_card_info()
$this->assertInstanceOf(CardInfoRequest::class, $request);
}

/**
* @test
*/
public function it_should_create_card_pay()
{
$request = $this->gateway->cardPay([]);
$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());
}
}
Loading

0 comments on commit a54b540

Please sign in to comment.