-
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.
Merge pull request #20 from mysiar/dev
cardPay & cardCharge
- Loading branch information
Showing
16 changed files
with
505 additions
and
0 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
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Omnipay\Przelewy24\Message; | ||
|
||
class CardChargeRequest extends AbstractRequest | ||
{ | ||
public function getData(): array | ||
{ | ||
$this->validate('token'); | ||
|
||
return [ | ||
'token' => $this->getToken(), | ||
]; | ||
} | ||
|
||
public function sendData($data): CardChargeResponse | ||
{ | ||
$httpResponse = $this->sendRequest('POST', 'card/charge', $data); | ||
|
||
$responseData = json_decode($httpResponse->getBody()->getContents(), true); | ||
|
||
return $this->response = new CardChargeResponse($this, $responseData); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Omnipay\Przelewy24\Message; | ||
|
||
use Omnipay\Common\Message\RequestInterface; | ||
|
||
class CardChargeResponse extends AbstractResponse | ||
{ | ||
/** | ||
* @var null|string | ||
*/ | ||
private $transactionId = null; | ||
|
||
public function __construct(RequestInterface $request, $data) | ||
{ | ||
parent::__construct($request, $data); | ||
if (isset($data['data'])) { | ||
if (isset($data['data']['orderId'])) { | ||
$this->transactionId = (string) $data['data']['orderId']; | ||
} | ||
} | ||
} | ||
|
||
public function getTransactionId(): ?string | ||
{ | ||
return $this->transactionId; | ||
} | ||
} |
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Omnipay\Przelewy24\Message; | ||
|
||
class CardPayRequest extends AbstractRequest | ||
{ | ||
public function getNumber(): string | ||
{ | ||
return $this->getParameter('number'); | ||
} | ||
|
||
public function setNumber(string $value): self | ||
{ | ||
return $this->setParameter('number', $value); | ||
} | ||
|
||
public function getExpiry(): string | ||
{ | ||
return $this->getParameter('expiry'); | ||
} | ||
|
||
public function setExpiry(string $value): self | ||
{ | ||
return $this->setParameter('expiry', $value); | ||
} | ||
|
||
public function getCvv(): string | ||
{ | ||
return $this->getParameter('cvv'); | ||
} | ||
|
||
public function setCvv(string $value): self | ||
{ | ||
return $this->setParameter('cvv', $value); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->getParameter('name'); | ||
} | ||
|
||
public function setName(string $value): self | ||
{ | ||
return $this->setParameter('name', $value); | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
$this->validate('transactionId', 'number', 'expiry', 'cvv', 'name'); | ||
|
||
return [ | ||
'transactionToken' => $this->getTransactionId(), | ||
'cardNumber' => $this->getNumber(), | ||
'cardDate' => $this->getExpiry(), | ||
'cvv' => $this->getCvv(), | ||
'clientName' => $this->getName(), | ||
]; | ||
} | ||
|
||
public function sendData($data): CardPayResponse | ||
{ | ||
$httpResponse = $this->sendRequest('POST', 'card/pay', $data); | ||
|
||
$responseData = json_decode($httpResponse->getBody()->getContents(), true); | ||
|
||
return $this->response = new CardPayResponse($this, $responseData); | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Omnipay\Przelewy24\Message; | ||
|
||
use Omnipay\Common\Message\RequestInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class CardPayResponse extends AbstractResponse | ||
{ | ||
/** | ||
* @var null|string | ||
*/ | ||
private $transactionId = null; | ||
|
||
/** | ||
* @var null | ||
*/ | ||
private $redirectUrl = null; | ||
|
||
public function __construct(RequestInterface $request, $data) | ||
{ | ||
parent::__construct($request, $data); | ||
if (isset($data['data'])) { | ||
if (isset($data['data']['orderId'])) { | ||
$this->transactionId = (string) $data['data']['orderId']; | ||
} | ||
if (isset($data['data']['redirectUrl'])) { | ||
$this->redirectUrl = $data['data']['redirectUrl']; | ||
} | ||
} | ||
} | ||
|
||
public function getCode(): int | ||
{ | ||
if ( | ||
isset($this->data['code']) | ||
&& 0 === $this->data['code'] | ||
&& isset($this->data['error']) | ||
&& strlen($this->data['error']) > 0 | ||
) { | ||
return Response::HTTP_CONFLICT; | ||
} | ||
|
||
if (isset($this->data['code'])) { | ||
return $this->data['code']; | ||
} | ||
|
||
return Response::HTTP_OK; | ||
} | ||
|
||
public function getTransactionId(): ?string | ||
{ | ||
return $this->transactionId; | ||
} | ||
|
||
public function getRedirectUrl(): ?string | ||
{ | ||
return $this->redirectUrl; | ||
} | ||
|
||
public function isRedirect(): bool | ||
{ | ||
return $this->isSuccessful() && null !== $this->getRedirectUrl(); | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Message; | ||
|
||
use Omnipay\Przelewy24\Message\CardChargeRequest; | ||
use Omnipay\Przelewy24\Message\CardChargeResponse; | ||
use Omnipay\Tests\TestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class CardChargeRequestTest extends TestCase | ||
{ | ||
/** | ||
* @var CardChargeRequest | ||
*/ | ||
private $request; | ||
|
||
public function setUp() | ||
{ | ||
$this->request = new CardChargeRequest($this->getHttpClient(), $this->getHttpRequest()); | ||
$this->request->initialize([ | ||
"token" => "29fa01f8-6bb8-4187-9fb0-ec6e1a62a731", | ||
]); | ||
} | ||
|
||
public function testGetData(): void | ||
{ | ||
$data = $this->request->getData(); | ||
|
||
$this->assertSame('29fa01f8-6bb8-4187-9fb0-ec6e1a62a731', $data['token']); | ||
} | ||
|
||
public function testSendSuccess() | ||
{ | ||
$this->setMockHttpResponse('CardChargeSuccess.txt'); | ||
/** @var CardChargeResponse $response */ | ||
$response = $this->request->send(); | ||
|
||
$this->assertInstanceOf(CardChargeResponse::class, $response); | ||
$this->assertTrue($response->isSuccessful()); | ||
$this->assertSame('1234567890', $response->getTransactionId()); | ||
} | ||
|
||
public function testSendAuthFailure() | ||
{ | ||
$this->setMockHttpResponse('CardChargeAuthFailure.txt'); | ||
$response = $this->request->send(); | ||
|
||
$this->assertInstanceOf(CardChargeResponse::class, $response); | ||
$this->assertFalse($response->isSuccessful()); | ||
$this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getCode()); | ||
$this->assertSame('Incorrect authentication', $response->getMessage()); | ||
} | ||
|
||
public function testSendInvalidDataFailure() | ||
{ | ||
$this->setMockHttpResponse('CardChargeInvalidDataFailure.txt'); | ||
$response = $this->request->send(); | ||
|
||
$this->assertInstanceOf(CardChargeResponse::class, $response); | ||
$this->assertFalse($response->isSuccessful()); | ||
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getCode()); | ||
$this->assertSame('Invalid input data', $response->getMessage()); | ||
} | ||
} |
Oops, something went wrong.