Skip to content

Commit

Permalink
[/api/v1/transaction/registerOffline] (#23)
Browse files Browse the repository at this point in the history
* transaction-registerOffline: update README

* transaction-registerOffline: request, object, tests & some code clean up
  • Loading branch information
mysiar authored May 21, 2022
1 parent 3f64083 commit 14e7dba
Show file tree
Hide file tree
Showing 32 changed files with 358 additions and 108 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@

## API endpoints implemented

| API endpoint | Gateway method |
|:---------------------------------|:-----------------|
| /api/v1/testAccess | testAccess |
| /api/v1/payment/methods | methods |
| /api/v1/transaction/register | purchase |
| /api/v1/transaction/verify | completePurchase |
| /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 |
| /api/v1/card/chargeWith3ds | cardCharge3ds |
| API endpoint | Gateway method |
|:------------------------------------|:-----------------|
| /api/v1/testAccess | testAccess |
| /api/v1/payment/methods | methods |
| /api/v1/transaction/register | purchase |
| /api/v1/transaction/verify | completePurchase |
| /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 |
| /api/v1/card/chargeWith3ds | cardCharge3ds |
| /api/v1/transaction/registerOffline | purchaseOffline |

## Install

Expand All @@ -40,7 +41,7 @@ The following gateways are provided by this package:

* Przelewy24

Reference official documentation https://developers.przelewy24.pl/index.php
Reference official documentation https://developers.przelewy24.pl/index.php?en

## Example

Expand Down
1 change: 1 addition & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
]]);

$ecsConfig->import(SetList::SPACES);
$ecsConfig->import(SetList::STRICT);
$ecsConfig->import(SetList::ARRAY);
$ecsConfig->import(SetList::DOCBLOCK);
$ecsConfig->import(SetList::PSR_12);
Expand Down
16 changes: 16 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Omnipay\Przelewy24\Message\CompletePurchaseRequest;
use Omnipay\Przelewy24\Message\MethodsRequest;
use Omnipay\Przelewy24\Message\PurchaseInfoRequest;
use Omnipay\Przelewy24\Message\PurchaseOfflineRequest;
use Omnipay\Przelewy24\Message\PurchaseRequest;
use Omnipay\Przelewy24\Message\RefundsRequest;
use Omnipay\Przelewy24\Message\TestAccessRequest;
Expand Down Expand Up @@ -130,6 +131,7 @@ public function methods(string $lang = 'en'): MethodsRequest

/**
* @param string[] $options
*
* @return AbstractRequest|PurchaseRequest
*/
public function purchase(array $options = []): PurchaseRequest
Expand All @@ -139,6 +141,7 @@ public function purchase(array $options = []): PurchaseRequest

/**
* @param string[] $options
*
* @return AbstractRequest|CompletePurchaseRequest
*/
public function completePurchase(array $options = []): CompletePurchaseRequest
Expand Down Expand Up @@ -168,6 +171,7 @@ public function cardInfo(string $transactionId): CardInfoRequest

/**
* @param string[] $options
*
* @return AbstractRequest|RefundsRequest
*/
public function refund(array $options = []): RefundsRequest
Expand All @@ -177,6 +181,7 @@ public function refund(array $options = []): RefundsRequest

/**
* @param string[] $options
*
* @return AbstractRequest|CardPayRequest
*/
public function cardPay(array $options): CardPayRequest
Expand All @@ -186,10 +191,21 @@ public function cardPay(array $options): CardPayRequest

/**
* @param string[] $options
*
* @return AbstractRequest|CardChargeRequest
*/
public function cardCharge(array $options): CardChargeRequest
{
return $this->createRequest(CardChargeRequest::class, $options);
}

/**
* @param string[] $options
*
* @return AbstractRequest|PurchaseOfflineRequest
*/
public function purchaseOffline(array $options): PurchaseOfflineRequest
{
return $this->createRequest(PurchaseOfflineRequest::class, $options);
}
}
5 changes: 3 additions & 2 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

abstract class AbstractRequest extends BaseAbstractRequest
{
protected const SIGN_ALGO = 'sha384';

protected $liveEndpoint = 'https://secure.przelewy24.pl/api/v1/';

protected $testEndpoint = 'https://sandbox.przelewy24.pl/api/v1/';
Expand All @@ -17,8 +19,6 @@ abstract class AbstractRequest extends BaseAbstractRequest

protected $testRedirectEndpoint = 'https://sandbox.przelewy24.pl/';

protected const SIGN_ALGO = 'sha384';

public function getMerchantId()
{
return $this->getParameter('merchantId');
Expand Down Expand Up @@ -111,6 +111,7 @@ protected function sendRequest(string $method, string $endpoint, $data): Respons

/**
* @param mixed $value
*
* @throws
*/
protected function internalAmountValue($value = null): int
Expand Down
5 changes: 3 additions & 2 deletions src/Message/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getCode(): int
}

/**
* @return array|string|null
* @return null|array|string
*/
public function getMessage()
{
Expand All @@ -59,7 +59,7 @@ public function isSuccessful(): bool
{
$code = $this->getCode();

return in_array($code, [Response::HTTP_CREATED, Response::HTTP_OK]);
return in_array($code, [Response::HTTP_CREATED, Response::HTTP_OK], true);
}

protected function getAmountFromInternal(int $amount): string
Expand All @@ -69,6 +69,7 @@ protected function getAmountFromInternal(int $amount): string

/**
* @param string[] $data
*
* @return string[]
*/
protected function replaceInfoKeys(array $data, string $oldKey, string $newKey): array
Expand Down
2 changes: 1 addition & 1 deletion src/Message/CardChargeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CardChargeResponse extends AbstractResponse
/**
* @var null|string
*/
private $transactionId = null;
private $transactionId;

public function __construct(RequestInterface $request, $data)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Message/CardInfoResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function getInfo(): array

/**
* @param string[] $data
*
* @return string[]
*/
private function formatInfo(array $data): array
Expand All @@ -39,8 +40,7 @@ private function formatInfo(array $data): array

// replace keys
$formatted = $this->replaceInfoKeys($formatted, 'cardType', 'brand');
$formatted = $this->replaceInfoKeys($formatted, 'cardDate', 'expiry');

return $formatted;
return $this->replaceInfoKeys($formatted, 'cardDate', 'expiry');
}
}
4 changes: 2 additions & 2 deletions src/Message/CardPayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class CardPayResponse extends AbstractResponse
/**
* @var null|string
*/
private $transactionId = null;
private $transactionId;

/**
* @var null
*/
private $redirectUrl = null;
private $redirectUrl;

public function __construct(RequestInterface $request, $data)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Message/MethodsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function getData(): array

/**
* @param string[] $data
* @return ResponseInterface|MethodsResponse
*
* @return MethodsResponse|ResponseInterface
*/
public function sendData($data): ResponseInterface
{
Expand Down
6 changes: 3 additions & 3 deletions src/Message/PurchaseInfoResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getInfo(): array

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

Expand All @@ -45,6 +45,7 @@ public function getCode(): int

/**
* @param string[] $data
*
* @return string[]
*/
private function formatInfo(array $data): array
Expand All @@ -61,8 +62,7 @@ private function formatInfo(array $data): array
$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;
return $this->replaceInfoKeys($formatted, 'clientPostcode', 'postcode');
}
}
26 changes: 26 additions & 0 deletions src/Message/PurchaseOfflineRequest.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 PurchaseOfflineRequest extends AbstractRequest
{
public function getData(): array
{
$this->validate('token');

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

public function sendData($data): PurchaseOfflineResponse
{
$httpResponse = $this->sendRequest('POST', 'transaction/registerOffline', $data);

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

return $this->response = new PurchaseOfflineResponse($this, $responseData);
}
}
30 changes: 30 additions & 0 deletions src/Message/PurchaseOfflineResponse.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 PurchaseOfflineResponse extends AbstractResponse
{
/**
* @var string[]
*/
private $info = [];

public function __construct(RequestInterface $request, $data)
{
parent::__construct($request, $data);

if (isset($this->data['data'])) {
$this->info = $this->data['data'];
$this->info = $this->replaceInfoKeys($this->info, 'orderId', 'transactionId');
}
}

public function getInfo(): array
{
return $this->info;
}
}
2 changes: 1 addition & 1 deletion src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function getData()
];

return array_filter($data, function ($val) {
return ! is_null($val);
return null !== $val;
});
}

Expand Down
13 changes: 8 additions & 5 deletions src/Message/RefundsRequest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Omnipay\Przelewy24\Message;

use Omnipay\Common\Exception\InvalidRequestException;
Expand Down Expand Up @@ -65,7 +67,8 @@ public function getData(): array

/**
* @param array $data
* @return ResponseInterface|MethodsResponse
*
* @return MethodsResponse|ResponseInterface
*/
public function sendData($data): ResponseInterface
{
Expand All @@ -82,19 +85,19 @@ public function sendData($data): ResponseInterface
private function validateRefundsArray(): void
{
$refunds = $this->getParameter('refunds');
if (is_null($refunds) || empty($refunds)) {
throw new InvalidRequestException("The parameter `refunds` can not be empty");
if (null === $refunds || empty($refunds)) {
throw new InvalidRequestException('The parameter `refunds` can not be empty');
}

$requiredFields = ['orderId', 'sessionId', 'amount'];

foreach ($refunds as $key => $refund) {
if (! is_array($refund)) {
throw new InvalidRequestException("Values in `refunds` need to be an array");
throw new InvalidRequestException('Values in `refunds` need to be an array');
}
foreach ($requiredFields as $requiredField) {
if (! array_key_exists($requiredField, $refund)) {
throw new InvalidRequestException("The $requiredField parameter is required in index $key of `refunds` array");
throw new InvalidRequestException("The {$requiredField} parameter is required in index {$key} of `refunds` array");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Message/RefundsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getResponseCode(): ?int

public function getCode(): int
{
if (! is_null($this->getResponseCode())) {
if (null !== $this->getResponseCode()) {
return $this->getResponseCode();
}

Expand Down
1 change: 1 addition & 0 deletions src/Message/TestAccessRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function getData(): array

/**
* @param string[] $data
*
* @return ResponseInterface|TestAccessResponse
*/
public function sendData($data): ResponseInterface
Expand Down
7 changes: 4 additions & 3 deletions tests-api/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp()
'testMode' => true,
];

$this->gateway = Omnipay::create("Przelewy24");
$this->gateway = Omnipay::create('Przelewy24');
$this->gateway->initialize($settings);
}

Expand All @@ -40,7 +40,7 @@ public function testAccess(): void

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

public function testMethods(): void
Expand Down Expand Up @@ -165,9 +165,10 @@ private function randomString(int $length = 15): string
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
for ($i = 0; $i < $length; ++$i) {
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}

return $randomString;
}
}
Loading

0 comments on commit 14e7dba

Please sign in to comment.