Skip to content

Commit

Permalink
[Card Info] (#9)
Browse files Browse the repository at this point in the history
* card-info: method request & response

* card-info: ecs config update

* card-info: code coverage

* card-info: replaced some response keys
  • Loading branch information
mysiar authored May 18, 2022
1 parent 7a03232 commit 42ccef4
Show file tree
Hide file tree
Showing 18 changed files with 306 additions and 30 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test Omnipay Przelewy24 API V1
name: CI
on:
push:
branches:
Expand Down Expand Up @@ -26,5 +26,13 @@ jobs:
run: composer install
- name: Coding standard
run: php${{ matrix.php }} ./vendor/bin/ecs check src tests
- name: PHPUnit
- name: PHPUnit tests
run: php${{ matrix.php }} ./vendor/bin/phpunit --testdox --verbose --stop-on-failure
- name: Code coverage
run: php${{ matrix.php }} -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-clover=var/coverage.xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./var/coverage.xml
fail_ci_if_error: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
tests-api/.env
var/
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ cs-fix:

changelog:
npx auto-changelog -o CHANGELOG.md

test:
./vendor/bin/phpunit --no-coverage

coverage:
php -d xdebug.mode=coverage ./vendor/bin/phpunit
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

**Przelewy24 API V1 gateway for the Omnipay PHP payment processing library**

[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) ![example workflow](https://github.com/mysiar/omnipay-przelewy24v1/actions/workflows/tests.yml/badge.svg)
[![CI](https://github.com/mysiar/omnipay-przelewy24v1/actions/workflows/tests.yml/badge.svg)](https://github.com/mysiar/omnipay-przelewy24v1/actions)
[![codecov](https://codecov.io/gh/mysiar/omnipay-przelewy24v1/branch/main/graph/badge.svg?token=gW4QFlc4lw)](https://codecov.io/gh/mysiar/omnipay-przelewy24v1)
[![Latest Version](https://img.shields.io/github/release/mysiar/omnipay-przelewy24v1.svg)](https://github.com/mysiar/omnipay-przelewy24v1/releases)
[![Total downloads](https://img.shields.io/packagist/dt/mysiar/omnipay-przelewy24v1.svg)](https://packagist.org/packages/mysiar/omnipay-przelewy24v1)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style)](LICENSE)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmysiar%2Fomnipay-przelewy24v1.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmysiar%2Fomnipay-przelewy24v1?ref=badge_shield)
[![PHP Version Require](http://poser.pugx.org/mysiar/omnipay-przelewy24v1/require/php)](https://packagist.org/packages/mysiar/omnipay-przelewy24v1)

## API endpoints implemented

Expand All @@ -13,6 +19,7 @@
| /api/v1/transaction/register | purchase |
| /api/v1/transaction/verify | completePurchase |
| /api/v1/transaction/by/sessionId | purchaseInfo |
| /api/v1/card/info | cardInfo |

## Install

Expand Down Expand Up @@ -61,7 +68,9 @@ $params = [
$response = $gateway->purchase($params)->send();
```

For more exmples check `tests-api/GatewayTest.php`
For more examples check
* [tests-api/GatewayTest.php](tests-api/GatewayTest.php)
* [tests/Message/](tests/Message/)

Optionally you can specify the payment channels.

Expand Down
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,5 @@
"omnipay/tests": "^3",
"symplify/easy-coding-standard": "^10.2",
"symfony/var-dumper": "^5.4"
},
"scripts": {
"test": "phpunit"
}
}
19 changes: 10 additions & 9 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
declare(strict_types=1);

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Option;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
return static function (ECSConfig $ecsConfig): void {
$parameters = $ecsConfig->parameters();
$parameters->set(Option::PATHS, [
__DIR__ . '/src',
__DIR__ . '/tests',
__DIR__ . '/tests-api',
]);

$services = $containerConfigurator->services();
$services = $ecsConfig->services();
$services->set(ArraySyntaxFixer::class)
->call('configure', [[
'syntax' => 'short',
]]);

$containerConfigurator->import(SetList::SPACES);
$containerConfigurator->import(SetList::ARRAY);
$containerConfigurator->import(SetList::DOCBLOCK);
$containerConfigurator->import(SetList::PSR_12);
$containerConfigurator->import(SetList::CLEAN_CODE);
$ecsConfig->import(SetList::SPACES);
$ecsConfig->import(SetList::ARRAY);
$ecsConfig->import(SetList::DOCBLOCK);
$ecsConfig->import(SetList::PSR_12);
$ecsConfig->import(SetList::CLEAN_CODE);
};
9 changes: 9 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="var/phpunit" lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="var/coverage.xml"/>
</logging>
</phpunit>
11 changes: 11 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\CardInfoRequest;
use Omnipay\Przelewy24\Message\CompletePurchaseRequest;
use Omnipay\Przelewy24\Message\MethodsRequest;
use Omnipay\Przelewy24\Message\PurchaseInfoRequest;
Expand Down Expand Up @@ -151,4 +152,14 @@ public function purchaseInfo(string $sessionId): PurchaseInfoRequest
'sessionId' => $sessionId,
]);
}

/**
* @return AbstractRequest|CardInfoRequest
*/
public function cardInfo(string $transactionId): CardInfoRequest
{
return $this->createRequest(CardInfoRequest::class, [
'transactionId' => $transactionId,
]);
}
}
14 changes: 14 additions & 0 deletions src/Message/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ protected function getAmountFromInternal(int $amount): string
{
return bcdiv((string) $amount, '100', 2);
}

/**
* @param string[] $data
* @return string[]
*/
protected function replaceInfoKeys(array $data, string $oldKey, string $newKey): array
{
if (isset($data[$oldKey])) {
$data[$newKey] = $data[$oldKey];
unset($data[$oldKey]);
}

return $data;
}
}
26 changes: 26 additions & 0 deletions src/Message/CardInfoRequest.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 CardInfoRequest extends AbstractRequest
{
public function getData()
{
$this->validate('transactionId');

return [
'transactionId' => $this->getTransactionId(),
];
}

public function sendData($data): CardInfoResponse
{
$httpResponse = $this->sendRequest('GET', sprintf('/api/v1/card/info/%s', $data['transactionId']), []);

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

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

declare(strict_types=1);

namespace Omnipay\Przelewy24\Message;

use Omnipay\Common\Message\RequestInterface;

class CardInfoResponse 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;
}

/**
* @param string[] $data
* @return string[]
*/
private function formatInfo(array $data): array
{
$formatted = $data;

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

return $formatted;
}
}
14 changes: 0 additions & 14 deletions src/Message/PurchaseInfoResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,4 @@ private function formatInfo(array $data): array

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;
}
}
7 changes: 7 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\CardInfoRequest;
use Omnipay\Przelewy24\Message\CompletePurchaseRequest;
use Omnipay\Przelewy24\Message\MethodsRequest;
use Omnipay\Przelewy24\Message\PurchaseInfoRequest;
Expand Down Expand Up @@ -180,4 +181,10 @@ public function it_should_create_a_purchase_info()
$this->assertInstanceOf(PurchaseInfoRequest::class, $request);
$this->assertSame('session-id', $request->getSessionId());
}

public function it_should_create_card_info()
{
$request = $this->gateway->cardInfo('transaction-id');
$this->assertInstanceOf(CardInfoRequest::class, $request);
}
}
84 changes: 84 additions & 0 deletions tests/Message/CardInfoRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Message;

use Omnipay\Przelewy24\Message\CardInfoRequest;
use Omnipay\Przelewy24\Message\CardInfoResponse;
use Omnipay\Tests\TestCase;
use Symfony\Component\HttpFoundation\Response;

class CardInfoRequestTest extends TestCase
{
/**
* @var CardInfoRequest
*/
private $request;

public function setUp()
{
$this->request = new CardInfoRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->initialize([
'transactionId' => 1234567890,
]);
}

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

$this->assertSame(1234567890, $data['transactionId']);
}

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

$this->assertInstanceOf(CardInfoResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getCode());
$this->assertSame('Wrong input data', $response->getMessage());
}

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

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

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

$this->assertInstanceOf(CardInfoResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertSame(Response::HTTP_NOT_FOUND, $response->getCode());
$this->assertSame('Transaction not exists', $response->getMessage());
}

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

$this->assertInstanceOf(CardInfoResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame(Response::HTTP_OK, $response->getCode());
$this->assertSame('', $response->getMessage());

$this->assertSame('ref-id', $response->getInfo()['refId']);
$this->assertSame(0, $response->getInfo()['bin']);
$this->assertSame('xxxx-xxxx-xxxx-1234', $response->getInfo()['mask']);
$this->assertSame('VISA', $response->getInfo()['brand']);
$this->assertSame('202505', $response->getInfo()['expiry']);
$this->assertSame('e0dfeab6-39c2-42cb-b96e-fb0d6d48d503', $response->getInfo()['hash']);
}
}
16 changes: 16 additions & 0 deletions tests/Mock/CardInfoAuthFailure.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, 17 May 2022 17: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
}
Loading

0 comments on commit 42ccef4

Please sign in to comment.