Skip to content

Commit

Permalink
Merge pull request #11 from mysiar/dev
Browse files Browse the repository at this point in the history
ver 0.3.0
  • Loading branch information
mysiar authored May 18, 2022
2 parents 0ff6152 + 64d22c0 commit 6045940
Showing 30 changed files with 818 additions and 55 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:
@@ -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
@@ -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
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

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

## Install

@@ -28,6 +36,8 @@ The following gateways are provided by this package:

* Przelewy24

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

## Example

```php
@@ -61,7 +71,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.

10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -17,6 +17,10 @@
{
"name": "Piotr Synowiec",
"email": "psynowiec@gmail.com"
},
{
"name": "Adam Migacz",
"email": "smigi1997@gmail.com"
}
],
"autoload": {
@@ -26,15 +30,13 @@
},
"require": {
"omnipay/common": "^3",
"ext-json": "*",
"ext-bcmath": "*",
"ext-json": "*"
"php": "^7.2|^8.0"
},
"require-dev": {
"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
@@ -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
@@ -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>
21 changes: 21 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
@@ -6,10 +6,12 @@

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;
use Omnipay\Przelewy24\Message\PurchaseRequest;
use Omnipay\Przelewy24\Message\RefundsRequest;
use Omnipay\Przelewy24\Message\TestAccessRequest;

class Gateway extends AbstractGateway
@@ -151,4 +153,23 @@ public function purchaseInfo(string $sessionId): PurchaseInfoRequest
'sessionId' => $sessionId,
]);
}

/**
* @return AbstractRequest|CardInfoRequest
*/
public function cardInfo(string $transactionId): CardInfoRequest
{
return $this->createRequest(CardInfoRequest::class, [
'transactionId' => $transactionId,
]);
}

/**
* @param string[] $options
* @return AbstractRequest|RefundsRequest
*/
public function refund(array $options = []): RefundsRequest
{
return $this->createRequest(RefundsRequest::class, $options);
}
}
2 changes: 1 addition & 1 deletion src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ protected function sendRequest(string $method, string $endpoint, $data): Respons
base64_encode(sprintf('%s:%s', $this->getMerchantId(), $this->getReportKey()))
),
],
empty($data) ? null : json_encode($data)
empty($data) ? null : json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
}

34 changes: 24 additions & 10 deletions src/Message/AbstractResponse.php
Original file line number Diff line number Diff line change
@@ -6,15 +6,10 @@

use Omnipay\Common\Message\AbstractResponse as BaseAbstractResponse;
use Omnipay\Common\Message\RequestInterface;
use Symfony\Component\HttpFoundation\Response;

abstract class AbstractResponse extends BaseAbstractResponse
{
public const HTTP_OK = 200;

public const HTTP_BAD_REQUEST = 400;

public const HTTP_UNAUTHORIZED = 401;

/**
* @param string[] $data
*/
@@ -26,13 +21,16 @@ public function __construct(RequestInterface $request, $data)
public function getCode(): int
{
if (isset($this->data['code'])) {
return $this->data['code'];
return (int) $this->data['code'];
}

return self::HTTP_OK;
return Response::HTTP_OK;
}

public function getMessage(): string
/**
* @return array|string|null
*/
public function getMessage()
{
if (isset($this->data['error'])) {
return $this->data['error'];
@@ -43,11 +41,27 @@ public function getMessage(): string

public function isSuccessful(): bool
{
return self::HTTP_OK === $this->getCode();
$code = $this->getCode();

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

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;
}
}
16 changes: 1 addition & 15 deletions src/Message/PurchaseInfoResponse.php
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ public function getCode(): int
return $this->data['code'];
}

return self::HTTP_OK;
return Response::HTTP_OK;
}

/**
@@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
@@ -133,7 +133,7 @@ public function getShipping()

public function setShipping($value): self
{
return $this->setParameter('regulationAccept', $value);
return $this->setParameter('shipping', $value);
}

public function getTransactionLabel(): ?string
Loading

0 comments on commit 6045940

Please sign in to comment.