-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refund] api/v1/transaction/refund (#4)
* 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
Showing
19 changed files
with
517 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,10 @@ | |
{ | ||
"name": "Piotr Synowiec", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Adam Migacz", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
|
@@ -26,8 +30,9 @@ | |
}, | ||
"require": { | ||
"omnipay/common": "^3", | ||
"ext-json": "*", | ||
"ext-bcmath": "*", | ||
"ext-json": "*" | ||
"php": "^7.2|^8.0" | ||
}, | ||
"require-dev": { | ||
"omnipay/tests": "^3", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.