Skip to content

Commit

Permalink
[Refund] api/v1/transaction/refund (#4)
Browse files Browse the repository at this point in the history
* transaction-register: in progress

* Refund save work [refund]

* ECS fix [refund]

* tests for refund [refund]

* Remove junk, remove amount auto-calc [refund]

* Remove junk, fix tests [refund]

* Typing [refund]

* fix mistake [refund]

* Remove VarDumper [refund]

* Composer changes [refund]

* bump authors [refund]

* refund: merged dev

Co-authored-by: Piotr Synowiec <[email protected]>
  • Loading branch information
Nykilor and mysiar authored May 18, 2022
1 parent 42ccef4 commit 64d22c0
Show file tree
Hide file tree
Showing 19 changed files with 517 additions and 30 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
| /api/v1/transaction/verify | completePurchase |
| /api/v1/transaction/by/sessionId | purchaseInfo |
| /api/v1/card/info | cardInfo |
| /api/v1/transaction/refund | refund |

## Install

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

* Przelewy24

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

## Example

```php
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
{
"name": "Piotr Synowiec",
"email": "[email protected]"
},
{
"name": "Adam Migacz",
"email": "[email protected]"
}
],
"autoload": {
Expand All @@ -26,8 +30,9 @@
},
"require": {
"omnipay/common": "^3",
"ext-json": "*",
"ext-bcmath": "*",
"ext-json": "*"
"php": "^7.2|^8.0"
},
"require-dev": {
"omnipay/tests": "^3",
Expand Down
10 changes: 5 additions & 5 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
'syntax' => 'short',
]]);

$ecsConfig->import(SetList::SPACES);
$ecsConfig->import(SetList::ARRAY);
$ecsConfig->import(SetList::DOCBLOCK);
$ecsConfig->import(SetList::PSR_12);
$ecsConfig->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);
};
10 changes: 10 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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
Expand Down Expand Up @@ -162,4 +163,13 @@ public function cardInfo(string $transactionId): CardInfoRequest
'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
Expand Up @@ -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)
);
}

Expand Down
20 changes: 10 additions & 10 deletions src/Message/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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'];
Expand All @@ -43,7 +41,9 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion src/Message/PurchaseInfoResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getCode(): int
return $this->data['code'];
}

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
115 changes: 115 additions & 0 deletions src/Message/RefundsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Omnipay\Przelewy24\Message;

use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Message\ResponseInterface;

class RefundsRequest extends AbstractRequest
{
public function setRequestId(string $requestId): self
{
return $this->setParameter('requestId', $requestId);
}

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

public function setRefunds(array $refunds): self
{
return $this->setParameter('refunds', $refunds);
}

public function getRefunds(): array
{
return $this->getParameter('refunds');
}

public function setRefundsUuid(string $refundsUuid): self
{
return $this->setParameter('refundsUuid', $refundsUuid);
}

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

public function setUrlStatus(?string $urlStatus): self
{
return $this->setParameter('urlStatus', $urlStatus);
}

public function getUrlStatus(): ?string
{
return $this->getParameter('urlStatus');
}

public function getData(): array
{
$this->validate('requestId', 'refunds', 'refundsUuid');
$this->validateRefundsArray();
$this->transformRefundsAmount();

$data = [
'requestId' => $this->getRequestId(),
'refunds' => $this->getRefunds(),
'refundsUuid' => $this->getRefundsUuid(),
'urlStatus' => $this->getUrlStatus(),
];

return array_filter($data);
}

/**
* @param array $data
* @return ResponseInterface|MethodsResponse
*/
public function sendData($data): ResponseInterface
{
$httpResponse = $this->sendRequest('GET', 'transaction/refund', $data);

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

return $this->response = new RefundsResponse($this, $responseData, $this->getEndpoint());
}

/**
* @throws InvalidRequestException
*/
private function validateRefundsArray(): void
{
$refunds = $this->getParameter('refunds');
if (is_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");
}
foreach ($requiredFields as $requiredField) {
if (! array_key_exists($requiredField, $refund)) {
throw new InvalidRequestException("The $requiredField parameter is required in index $key of `refunds` array");
}
}
}
}

/**
* @throws InvalidRequestException
*/
private function transformRefundsAmount(): void
{
$refunds = $this->getParameter('refunds');
foreach ($refunds as $key => $refund) {
$refunds[$key]['amount'] = $this->internalAmountValue($refund['amount']);
}

$this->setParameter('refunds', $refunds);
}
}
42 changes: 42 additions & 0 deletions src/Message/RefundsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Omnipay\Przelewy24\Message;

use Omnipay\Common\Message\RequestInterface;

class RefundsResponse extends AbstractResponse
{
private $refunds;

private $responseCode;

public function __construct(RequestInterface $request, $data, $endpoint)
{
parent::__construct($request, $data, $endpoint);
$this->refunds = $data['data'] ?? null;
if (isset($data['responseCode'])) {
$this->responseCode = (int) $data['responseCode'];
}
}

public function getRefunds(): array
{
return $this->refunds;
}

public function getResponseCode(): ?int
{
return $this->responseCode;
}

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

return parent::getCode();
}
}
9 changes: 4 additions & 5 deletions tests-api/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace API;

use Omnipay\Omnipay;
use Omnipay\Przelewy24\Message\AbstractResponse;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\VarDumper\VarDumper;
Expand All @@ -32,22 +31,22 @@ public function setUp()
public function testAccess(): void
{
$response = $this->gateway->testAccess()->send();
$this->assertSame(AbstractResponse::HTTP_OK, $response->getCode());
$this->assertSame(Response::HTTP_OK, $response->getCode());
$this->assertSame('', $response->getMessage());
$this->assertTrue($response->isSuccessful());

$this->gateway->setReportKey('dummy');
$response = $this->gateway->testAccess()->send();

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

public function testMethods(): void
{
$response = $this->gateway->methods()->send();
$this->assertSame(AbstractResponse::HTTP_OK, $response->getCode());
$this->assertSame(Response::HTTP_OK, $response->getCode());
$this->assertSame('', $response->getMessage());
$this->assertTrue($response->isSuccessful());
$this->assertGreaterThan(0, count($response->getMethods()));
Expand All @@ -72,7 +71,7 @@ public function testPurchase(): void
VarDumper::dump($response->getRedirectUrl());
VarDumper::dump($response->getMessage());

$this->assertSame(AbstractResponse::HTTP_OK, $response->getCode());
$this->assertSame(Response::HTTP_OK, $response->getCode());
$this->assertSame('', $response->getMessage());
$this->assertTrue($response->isSuccessful());
$this->assertContains('https://sandbox.przelewy24.pl/trnRequest/', $response->getRedirectUrl());
Expand Down
Loading

0 comments on commit 64d22c0

Please sign in to comment.