From 0b78e952ad7d47500ac2c93724c46eee91da2f2e Mon Sep 17 00:00:00 2001 From: Marmolada Date: Thu, 4 Aug 2022 11:15:20 +0200 Subject: [PATCH] Blik request handles for Przelewy24 (#32) * Blik request handlers [blik] * Add blank line to gitignore [blik] Co-authored-by: adammigaczheal --- .gitignore | 3 + src/Gateway.php | 44 ++++++++ .../BlikAliasesByEmailCustomRequest.php | 37 +++++++ .../BlikAliasesByEmailCustomResponse.php | 28 +++++ src/Message/BlikAliasesByEmailRequest.php | 37 +++++++ src/Message/BlikAliasesByEmailResponse.php | 28 +++++ src/Message/BlikChargeByAliasRequest.php | 82 ++++++++++++++ src/Message/BlikChargeByAliasResponse.php | 39 +++++++ src/Message/BlikChargeByCodeRequest.php | 59 ++++++++++ src/Message/BlikChargeByCodeResponse.php | 39 +++++++ tests/GatewayTest.php | 102 ++++++++++++++---- .../BlikAliasesByEmailCustomRequestTest.php | 77 +++++++++++++ .../Message/BlikAliasesByEmailRequestTest.php | 77 +++++++++++++ .../Message/BlikChargeByAliasRequestTest.php | 100 +++++++++++++++++ tests/Message/BlikChargeByCodeRequestTest.php | 84 +++++++++++++++ tests/Mock/BlikAliasesByEmailAuthFailure.txt | 16 +++ tests/Mock/BlikAliasesByEmailFailure.txt | 16 +++ tests/Mock/BlikAliasesByEmailNotFound.txt | 16 +++ tests/Mock/BlikAliasesByEmailSuccess.txt | 19 ++++ ...likChargeByAliasAlternativeKeysFailure.txt | 28 +++++ tests/Mock/BlikChargeByCodeAuthFailure.txt | 16 +++ tests/Mock/BlikChargeByCodeFailure.txt | 16 +++ tests/Mock/BlikChargeByCodeSuccess.txt | 19 ++++ tests/Mock/BlikChargeByCodeUndefinedError.txt | 16 +++ tests/Mock/CardInfoSuccess.txt | 2 +- 25 files changed, 979 insertions(+), 21 deletions(-) create mode 100644 src/Message/BlikAliasesByEmailCustomRequest.php create mode 100644 src/Message/BlikAliasesByEmailCustomResponse.php create mode 100644 src/Message/BlikAliasesByEmailRequest.php create mode 100644 src/Message/BlikAliasesByEmailResponse.php create mode 100644 src/Message/BlikChargeByAliasRequest.php create mode 100644 src/Message/BlikChargeByAliasResponse.php create mode 100644 src/Message/BlikChargeByCodeRequest.php create mode 100644 src/Message/BlikChargeByCodeResponse.php create mode 100644 tests/Message/BlikAliasesByEmailCustomRequestTest.php create mode 100644 tests/Message/BlikAliasesByEmailRequestTest.php create mode 100644 tests/Message/BlikChargeByAliasRequestTest.php create mode 100644 tests/Message/BlikChargeByCodeRequestTest.php create mode 100644 tests/Mock/BlikAliasesByEmailAuthFailure.txt create mode 100644 tests/Mock/BlikAliasesByEmailFailure.txt create mode 100644 tests/Mock/BlikAliasesByEmailNotFound.txt create mode 100644 tests/Mock/BlikAliasesByEmailSuccess.txt create mode 100644 tests/Mock/BlikChargeByAliasAlternativeKeysFailure.txt create mode 100644 tests/Mock/BlikChargeByCodeAuthFailure.txt create mode 100644 tests/Mock/BlikChargeByCodeFailure.txt create mode 100644 tests/Mock/BlikChargeByCodeSuccess.txt create mode 100644 tests/Mock/BlikChargeByCodeUndefinedError.txt diff --git a/.gitignore b/.gitignore index f3a104c..461c67d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ composer.lock tests-api/.env var/ .phpunit.result.cache +.docker +index.php +.idea diff --git a/src/Gateway.php b/src/Gateway.php index c153de1..838efcb 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -6,6 +6,10 @@ use Omnipay\Common\AbstractGateway; use Omnipay\Common\Message\AbstractRequest; +use Omnipay\Przelewy24\Message\BlikAliasesByEmailCustomRequest; +use Omnipay\Przelewy24\Message\BlikAliasesByEmailRequest; +use Omnipay\Przelewy24\Message\BlikChargeByAliasRequest; +use Omnipay\Przelewy24\Message\BlikChargeByCodeRequest; use Omnipay\Przelewy24\Message\CardChargeRequest; use Omnipay\Przelewy24\Message\CardInfoRequest; use Omnipay\Przelewy24\Message\CardPayRequest; @@ -204,4 +208,44 @@ public function purchaseOffline(array $options = []): PurchaseOfflineRequest { return $this->createRequest(PurchaseOfflineRequest::class, $options); } + + /** + * @param string[] $options + * + * @return AbstractRequest|BlikAliasesByEmailRequest + */ + public function blikGetAliasesByEmail(array $options = []): BlikAliasesByEmailRequest + { + return $this->createRequest(BlikAliasesByEmailRequest::class, $options); + } + + /** + * @param string[] $options + * + * @return AbstractRequest|BlikAliasesByEmailCustomRequest + */ + public function blikGetAliasesByEmailCustom(array $options = []): BlikAliasesByEmailCustomRequest + { + return $this->createRequest(BlikAliasesByEmailCustomRequest::class, $options); + } + + /** + * @param string[] $options + * + * @return AbstractRequest|BlikChargeByCodeRequest + */ + public function blikChargeByCode(array $options = []): BlikChargeByCodeRequest + { + return $this->createRequest(BlikChargeByCodeRequest::class, $options); + } + + /** + * @param string[] $options + * + * @return AbstractRequest|BlikChargeByAliasRequest + */ + public function blikChargeByAlias(array $options = []): BlikChargeByAliasRequest + { + return $this->createRequest(BlikChargeByAliasRequest::class, $options); + } } diff --git a/src/Message/BlikAliasesByEmailCustomRequest.php b/src/Message/BlikAliasesByEmailCustomRequest.php new file mode 100644 index 0000000..704e396 --- /dev/null +++ b/src/Message/BlikAliasesByEmailCustomRequest.php @@ -0,0 +1,37 @@ +getParameter('email'); + } + + public function setEmail(string $email): self + { + return $this->setParameter('email', $email); + } + + public function getData(): array + { + $this->validate('email'); + + return [ + 'email' => $this->getEmail(), + ]; + } + + public function sendData($data): BlikAliasesByEmailCustomResponse + { + $email = $data['email']; + $httpResponse = $this->sendRequest('GET', sprintf('paymentMethod/blik/getAliasesByEmail/%s/custom', $email), []); + + $responseData = json_decode($httpResponse->getBody()->getContents(), true); + + return $this->response = new BlikAliasesByEmailCustomResponse($this, $responseData); + } +} diff --git a/src/Message/BlikAliasesByEmailCustomResponse.php b/src/Message/BlikAliasesByEmailCustomResponse.php new file mode 100644 index 0000000..8d44a25 --- /dev/null +++ b/src/Message/BlikAliasesByEmailCustomResponse.php @@ -0,0 +1,28 @@ +aliases = $data['data']; + } + } + + public function getAliases(): array + { + return $this->aliases; + } +} diff --git a/src/Message/BlikAliasesByEmailRequest.php b/src/Message/BlikAliasesByEmailRequest.php new file mode 100644 index 0000000..030329b --- /dev/null +++ b/src/Message/BlikAliasesByEmailRequest.php @@ -0,0 +1,37 @@ +getParameter('email'); + } + + public function setEmail(string $email): self + { + return $this->setParameter('email', $email); + } + + public function getData(): array + { + $this->validate('email'); + + return [ + 'email' => $this->getEmail(), + ]; + } + + public function sendData($data): BlikAliasesByEmailResponse + { + $email = $data['email']; + $httpResponse = $this->sendRequest('GET', sprintf('paymentMethod/blik/getAliasesByEmail/%s', $email), []); + + $responseData = json_decode($httpResponse->getBody()->getContents(), true); + + return $this->response = new BlikAliasesByEmailResponse($this, $responseData); + } +} diff --git a/src/Message/BlikAliasesByEmailResponse.php b/src/Message/BlikAliasesByEmailResponse.php new file mode 100644 index 0000000..3de1f7f --- /dev/null +++ b/src/Message/BlikAliasesByEmailResponse.php @@ -0,0 +1,28 @@ +aliases = $data['data']; + } + } + + public function getAliases(): array + { + return $this->aliases; + } +} diff --git a/src/Message/BlikChargeByAliasRequest.php b/src/Message/BlikChargeByAliasRequest.php new file mode 100644 index 0000000..c2e142a --- /dev/null +++ b/src/Message/BlikChargeByAliasRequest.php @@ -0,0 +1,82 @@ +getParameter('alternativeKey'); + } + + public function setAlternativeKey(string $type): self + { + return $this->setParameter('alternativeKey', $type); + } + + public function getType(): string + { + return $this->getParameter('type'); + } + + public function setType(string $type): self + { + return $this->setParameter('type', $type); + } + + public function getAliasValue(): string + { + return $this->getParameter('aliasValue'); + } + + public function setAliasValue(string $aliasValue): self + { + return $this->setParameter('aliasValue', $aliasValue); + } + + public function getAliasLabel(): string + { + return $this->getParameter('aliasLabel'); + } + + public function setAliasLabel(string $label): self + { + return $this->setParameter('aliasLabel', $label); + } + + public function getData(): array + { + $this->validate('token', 'type'); + + if (! in_array($this->getType(), ['alias', 'alternativeKey'], true)) { + throw new InvalidRequestException( + "The `type` parameter is required, and allows only 'alias', 'alternativeKey' values" + ); + } + + if ($this->getType() === 'alternativeKey') { + $this->validate('alternativeKey'); + } + + return [ + 'token' => $this->getToken(), + 'type' => $this->getType(), + 'aliasLabel' => $this->getAliasLabel(), + 'aliasValue' => $this->getAliasValue(), + 'alternativeKey' => $this->getAlternativeKey(), + ]; + } + + public function sendData($data): BlikChargeByAliasResponse + { + $httpResponse = $this->sendRequest('POST', 'paymentMethod/blik/chargeByAlias', $data); + + $responseData = json_decode($httpResponse->getBody()->getContents(), true); + + return $this->response = new BlikChargeByAliasResponse($this, $responseData); + } +} diff --git a/src/Message/BlikChargeByAliasResponse.php b/src/Message/BlikChargeByAliasResponse.php new file mode 100644 index 0000000..218b79e --- /dev/null +++ b/src/Message/BlikChargeByAliasResponse.php @@ -0,0 +1,39 @@ +orderId = $data['data']['orderId']; + $this->chargeMessage = $data['data']['message']; + } + } + + public function getOrderId(): string + { + return $this->orderId; + } + + public function getChargeMessage(): string + { + return $this->chargeMessage; + } +} diff --git a/src/Message/BlikChargeByCodeRequest.php b/src/Message/BlikChargeByCodeRequest.php new file mode 100644 index 0000000..f9e2516 --- /dev/null +++ b/src/Message/BlikChargeByCodeRequest.php @@ -0,0 +1,59 @@ +getParameter('blikCode'); + } + + public function setBlikCode(string $code): self + { + return $this->setParameter('blikCode', $code); + } + + public function getAliasValue(): string + { + return $this->getParameter('aliasValue'); + } + + public function setAliasValue(string $aliasValue): self + { + return $this->setParameter('aliasValue', $aliasValue); + } + + public function getAliasLabel(): string + { + return $this->getParameter('aliasLabel'); + } + + public function setAliasLabel(string $label): self + { + return $this->setParameter('aliasLabel', $label); + } + + public function getData(): array + { + $this->validate('token', 'blikCode'); + + return [ + 'token' => $this->getToken(), + 'blikCode' => $this->getBlikCode(), + 'aliasLabel' => $this->getAliasLabel(), + 'aliasValue' => $this->getAliasValue(), + ]; + } + + public function sendData($data): BlikChargeByCodeResponse + { + $httpResponse = $this->sendRequest('POST', 'paymentMethod/blik/chargeByCode', $data); + + $responseData = json_decode($httpResponse->getBody()->getContents(), true); + + return $this->response = new BlikChargeByCodeResponse($this, $responseData); + } +} diff --git a/src/Message/BlikChargeByCodeResponse.php b/src/Message/BlikChargeByCodeResponse.php new file mode 100644 index 0000000..c087fb6 --- /dev/null +++ b/src/Message/BlikChargeByCodeResponse.php @@ -0,0 +1,39 @@ +orderId = $data['data']['orderId']; + $this->chargeMessage = $data['data']['message']; + } + } + + public function getOrderId(): string + { + return $this->orderId; + } + + public function getChargeMessage(): string + { + return $this->chargeMessage; + } +} diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index f40e857..5a6d9d4 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -3,6 +3,10 @@ declare(strict_types=1); use Omnipay\Przelewy24\Gateway; +use Omnipay\Przelewy24\Message\BlikAliasesByEmailCustomRequest; +use Omnipay\Przelewy24\Message\BlikAliasesByEmailRequest; +use Omnipay\Przelewy24\Message\BlikChargeByAliasRequest; +use Omnipay\Przelewy24\Message\BlikChargeByCodeRequest; use Omnipay\Przelewy24\Message\CardChargeRequest; use Omnipay\Przelewy24\Message\CardInfoRequest; use Omnipay\Przelewy24\Message\CardPayRequest; @@ -32,7 +36,7 @@ public function setUp(): void /** * @test */ - public function itShouldReturnTheName() + public function itShouldReturnTheName(): void { $this->assertSame('Przelewy24', $this->gateway->getName()); } @@ -40,7 +44,7 @@ public function itShouldReturnTheName() /** * @test */ - public function itShouldReturnDefaultParameters() + public function itShouldReturnDefaultParameters(): void { $defaultParameters = $this->gateway->getDefaultParameters(); $this->assertSame('', $defaultParameters['merchantId']); @@ -54,7 +58,7 @@ public function itShouldReturnDefaultParameters() /** * @test */ - public function itShouldSetAndGetMerchantId() + public function itShouldSetAndGetMerchantId(): void { $merchantId = '42'; @@ -65,7 +69,7 @@ public function itShouldSetAndGetMerchantId() /** * @test */ - public function itShouldSetAndGetPosId() + public function itShouldSetAndGetPosId(): void { $posId = '13'; @@ -76,7 +80,7 @@ public function itShouldSetAndGetPosId() /** * @test */ - public function itShouldSetAndGetCrc() + public function itShouldSetAndGetCrc(): void { $crc = '1288348798'; @@ -87,7 +91,7 @@ public function itShouldSetAndGetCrc() /** * @test */ - public function itShouldSetAndGetReportKey() + public function itShouldSetAndGetReportKey(): void { $crc = '6c2f6d86-3091-4291-b1e4-2568492605e4'; @@ -98,7 +102,7 @@ public function itShouldSetAndGetReportKey() /** * @test */ - public function itShouldSetAndGetChannel() + public function itShouldSetAndGetChannel(): void { $channel = 32; @@ -109,7 +113,7 @@ public function itShouldSetAndGetChannel() /** * @test */ - public function itShouldSetAndGetLanguage() + public function itShouldSetAndGetLanguage(): void { $language = 'pl'; $this->gateway->setLanguage($language); @@ -119,7 +123,7 @@ public function itShouldSetAndGetLanguage() /** * @test */ - public function itShouldCreateATestAccess() + public function itShouldCreateATestAccess(): void { $request = $this->gateway->testAccess(); $this->assertInstanceOf(TestAccessRequest::class, $request); @@ -128,7 +132,7 @@ public function itShouldCreateATestAccess() /** * @test */ - public function itShouldCreateAMethods() + public function itShouldCreateAMethods(): void { $request = $this->gateway->methods(); $this->assertInstanceOf(MethodsRequest::class, $request); @@ -138,7 +142,7 @@ public function itShouldCreateAMethods() /** * @test */ - public function itShouldCreateAPurchase() + public function itShouldCreateAPurchase(): void { $request = $this->gateway->purchase([ 'amount' => '10.00', @@ -150,7 +154,7 @@ public function itShouldCreateAPurchase() /** * @test */ - public function itShouldSetAndGetAmountOnPurchase() + public function itShouldSetAndGetAmountOnPurchase(): void { $request = $this->gateway->purchase([ 'amount' => '1000', @@ -162,7 +166,7 @@ public function itShouldSetAndGetAmountOnPurchase() /** * @test */ - public function itShouldSetAndGetShippingOnPurchase() + public function itShouldSetAndGetShippingOnPurchase(): void { $request = $this->gateway->purchase([ 'amount' => '1000', @@ -174,7 +178,7 @@ public function itShouldSetAndGetShippingOnPurchase() /** * @test */ - public function itShouldCreateACompletePurchase() + public function itShouldCreateACompletePurchase(): void { $request = $this->gateway->completePurchase([ 'amount' => '10.00', @@ -192,7 +196,7 @@ public function itShouldCreateARefund( array $refunds, string $refundsUuid, ?string $urlStatus - ) { + ): void { $data = [ 'requestId' => $requestId, 'refunds' => $refunds, @@ -213,7 +217,7 @@ public function itShouldCreateARefund( /** * @test */ - public function itShouldCreateAPurchaseInfo() + public function itShouldCreateAPurchaseInfo(): void { $request = $this->gateway->purchaseInfo([ 'sessionId' => 'session-id', @@ -225,7 +229,7 @@ public function itShouldCreateAPurchaseInfo() /** * @test */ - public function itShouldCreateCardInfo() + public function itShouldCreateCardInfo(): void { $request = $this->gateway->cardInfo([ 'transactionId' => 'transaction-id', @@ -237,7 +241,7 @@ public function itShouldCreateCardInfo() /** * @test */ - public function itShouldCreateCardPay() + public function itShouldCreateCardPay(): void { $request = $this->gateway->cardPay([]); $this->assertInstanceOf(CardPayRequest::class, $request); @@ -246,7 +250,7 @@ public function itShouldCreateCardPay() /** * @test */ - public function itShouldCreateCardCharge() + public function itShouldCreateCardCharge(): void { $request = $this->gateway->cardCharge([]); $this->assertInstanceOf(CardChargeRequest::class, $request); @@ -255,7 +259,7 @@ public function itShouldCreateCardCharge() /** * @test */ - public function itShouldCreatePurchaseOffline() + public function itShouldCreatePurchaseOffline(): void { $request = $this->gateway->purchaseOffline([ 'token' => 'token-abc', @@ -264,6 +268,64 @@ public function itShouldCreatePurchaseOffline() $this->assertSame('token-abc', $request->getToken()); } + /** + * @test + */ + public function itShouldCreateBlikAliasesByEmail(): void + { + $request = $this->gateway->blikGetAliasesByEmail([ + 'email' => 'test', + ]); + $this->assertInstanceOf(BlikAliasesByEmailRequest::class, $request); + $this->assertSame('test', $request->getEmail()); + } + + /** + * @test + */ + public function itShouldCreateBlikAliasesByEmailCustom(): void + { + $request = $this->gateway->blikGetAliasesByEmailCustom([ + 'email' => 'test', + ]); + $this->assertInstanceOf(BlikAliasesByEmailCustomRequest::class, $request); + $this->assertSame('test', $request->getEmail()); + } + + /** + * @test + */ + public function itShouldCreateBlikChargeByCode(): void + { + $request = $this->gateway->blikChargeByCode([ + 'token' => 'test', + 'blikCode' => '123456', + ]); + $this->assertInstanceOf(BlikChargeByCodeRequest::class, $request); + $this->assertSame('test', $request->getToken()); + $this->assertSame('123456', $request->getBlikCode()); + } + + /** + * @test + */ + public function itShouldCreateBlikChargeByAlias(): void + { + $request = $this->gateway->blikChargeByAlias([ + 'token' => 'test', + 'type' => 'alias', + 'aliasLabel' => 'test3', + 'aliasValue' => 'test4', + 'alternativeKey' => 'test5', + ]); + $this->assertInstanceOf(BlikChargeByAliasRequest::class, $request); + $this->assertSame('test', $request->getToken()); + $this->assertSame('alias', $request->getType()); + $this->assertSame('test3', $request->getAliasLabel()); + $this->assertSame('test4', $request->getAliasValue()); + $this->assertSame('test5', $request->getAlternativeKey()); + } + public function refund_data_provider(): array { return [ diff --git a/tests/Message/BlikAliasesByEmailCustomRequestTest.php b/tests/Message/BlikAliasesByEmailCustomRequestTest.php new file mode 100644 index 0000000..2170f5d --- /dev/null +++ b/tests/Message/BlikAliasesByEmailCustomRequestTest.php @@ -0,0 +1,77 @@ +request = new BlikAliasesByEmailCustomRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->initialize([ + "email" => "adam@migacz.pl", + ]); + } + + public function testGetData(): void + { + $data = $this->request->getData(); + + $this->assertSame('adam@migacz.pl', $data['email']); + } + + public function testSendSuccess(): void + { + $this->setMockHttpResponse('BlikAliasesByEmailSuccess.txt'); + /** @var BlikAliasesByEmailCustomResponse $response */ + $response = $this->request->send(); + + $this->assertInstanceOf(BlikAliasesByEmailCustomResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + $this->assertCount(2, $response->getAliases()); + } + + public function testSendFailure(): void + { + $this->setMockHttpResponse('BlikAliasesByEmailFailure.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikAliasesByEmailCustomResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getCode()); + $this->assertSame('Invalid input data', $response->getMessage()); + } + + public function testSendAuthFailure(): void + { + $this->setMockHttpResponse('BlikAliasesByEmailAuthFailure.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikAliasesByEmailCustomResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getCode()); + $this->assertSame('Incorrect authentication', $response->getMessage()); + } + + public function testSendNotFound(): void + { + $this->setMockHttpResponse('BlikAliasesByEmailNotFound.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikAliasesByEmailCustomResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_NOT_FOUND, $response->getCode()); + $this->assertSame("Transaction not found", $response->getMessage()); + } +} diff --git a/tests/Message/BlikAliasesByEmailRequestTest.php b/tests/Message/BlikAliasesByEmailRequestTest.php new file mode 100644 index 0000000..c02d70d --- /dev/null +++ b/tests/Message/BlikAliasesByEmailRequestTest.php @@ -0,0 +1,77 @@ +request = new BlikAliasesByEmailRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->initialize([ + "email" => "adam@migacz.pl", + ]); + } + + public function testGetData(): void + { + $data = $this->request->getData(); + + $this->assertSame('adam@migacz.pl', $data['email']); + } + + public function testSendSuccess(): void + { + $this->setMockHttpResponse('BlikAliasesByEmailSuccess.txt'); + /** @var BlikAliasesByEmailResponse $response */ + $response = $this->request->send(); + + $this->assertInstanceOf(BlikAliasesByEmailResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + $this->assertCount(2, $response->getAliases()); + } + + public function testSendFailure(): void + { + $this->setMockHttpResponse('BlikAliasesByEmailFailure.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikAliasesByEmailResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getCode()); + $this->assertSame('Invalid input data', $response->getMessage()); + } + + public function testSendAuthFailure(): void + { + $this->setMockHttpResponse('BlikAliasesByEmailAuthFailure.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikAliasesByEmailResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getCode()); + $this->assertSame('Incorrect authentication', $response->getMessage()); + } + + public function testSendNotFound(): void + { + $this->setMockHttpResponse('BlikAliasesByEmailNotFound.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikAliasesByEmailResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_NOT_FOUND, $response->getCode()); + $this->assertSame("Transaction not found", $response->getMessage()); + } +} diff --git a/tests/Message/BlikChargeByAliasRequestTest.php b/tests/Message/BlikChargeByAliasRequestTest.php new file mode 100644 index 0000000..7adc727 --- /dev/null +++ b/tests/Message/BlikChargeByAliasRequestTest.php @@ -0,0 +1,100 @@ +request = new BlikChargeByAliasRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->initialize([ + 'token' => 'test1', + 'type' => 'alias', + 'aliasLabel' => 'test3', + 'aliasValue' => 'test4', + 'alternativeKey' => 'test5', + ]); + } + + public function testGetData(): void + { + $data = $this->request->getData(); + + $this->assertSame('test1', $data['token']); + $this->assertSame('alias', $data['type']); + $this->assertSame('test3', $data['aliasLabel']); + $this->assertSame('test4', $data['aliasValue']); + $this->assertSame('test5', $data['alternativeKey']); + } + + public function testSendSuccess(): void + { + $this->setMockHttpResponse('BlikChargeByCodeSuccess.txt'); + /** @var BlikChargeByAliasResponse $response */ + $response = $this->request->send(); + + $this->assertInstanceOf(BlikChargeByAliasResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + $this->assertSame("test123", $response->getOrderId()); + $this->assertSame("success", $response->getChargeMessage()); + } + + public function testSendFailure(): void + { + $this->setMockHttpResponse('BlikChargeByCodeFailure.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikChargeByAliasResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getCode()); + $this->assertSame('Invalid input data', $response->getMessage()); + } + + public function testSendAuthFailure(): void + { + $this->setMockHttpResponse('BlikChargeByCodeAuthFailure.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikChargeByAliasResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getCode()); + $this->assertSame('Incorrect authentication', $response->getMessage()); + } + + public function testUndefinedError(): void + { + $this->setMockHttpResponse('BlikChargeByCodeUndefinedError.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikChargeByAliasResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getCode()); + $this->assertSame("Undefined error", $response->getMessage()); + } + + public function testAlternativeKeys(): void + { + $this->setMockHttpResponse('BlikChargeByAliasAlternativeKeysFailure.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikChargeByAliasResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_CONFLICT, $response->getCode()); + $this->assertIsArray($response->getMessage()); + $this->assertArrayHasKey('alternativeKeys', $response->getMessage()); + $this->assertCount(2, $response->getMessage()['alternativeKeys']); + } +} diff --git a/tests/Message/BlikChargeByCodeRequestTest.php b/tests/Message/BlikChargeByCodeRequestTest.php new file mode 100644 index 0000000..5ab7d72 --- /dev/null +++ b/tests/Message/BlikChargeByCodeRequestTest.php @@ -0,0 +1,84 @@ +request = new BlikChargeByCodeRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->initialize([ + "token" => "test1", + "blikCode" => "test2", + "aliasValue" => "test3", + "aliasLabel" => "test4", + ]); + } + + public function testGetData(): void + { + $data = $this->request->getData(); + + $this->assertSame('test1', $data['token']); + $this->assertSame('test2', $data['blikCode']); + $this->assertSame('test3', $data['aliasValue']); + $this->assertSame('test4', $data['aliasLabel']); + } + + public function testSendSuccess(): void + { + $this->setMockHttpResponse('BlikChargeByCodeSuccess.txt'); + /** @var BlikChargeByCodeResponse $response */ + $response = $this->request->send(); + + $this->assertInstanceOf(BlikChargeByCodeResponse::class, $response); + $this->assertTrue($response->isSuccessful()); + $this->assertSame("test123", $response->getOrderId()); + $this->assertSame("success", $response->getChargeMessage()); + } + + public function testSendFailure(): void + { + $this->setMockHttpResponse('BlikChargeByCodeFailure.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikChargeByCodeResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getCode()); + $this->assertSame('Invalid input data', $response->getMessage()); + } + + public function testSendAuthFailure(): void + { + $this->setMockHttpResponse('BlikChargeByCodeAuthFailure.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikChargeByCodeResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getCode()); + $this->assertSame('Incorrect authentication', $response->getMessage()); + } + + public function testUndefinedError(): void + { + $this->setMockHttpResponse('BlikChargeByCodeUndefinedError.txt'); + $response = $this->request->send(); + + $this->assertInstanceOf(BlikChargeByCodeResponse::class, $response); + $this->assertFalse($response->isSuccessful()); + $this->assertSame(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getCode()); + $this->assertSame("Undefined error", $response->getMessage()); + } +} diff --git a/tests/Mock/BlikAliasesByEmailAuthFailure.txt b/tests/Mock/BlikAliasesByEmailAuthFailure.txt new file mode 100644 index 0000000..304aeda --- /dev/null +++ b/tests/Mock/BlikAliasesByEmailAuthFailure.txt @@ -0,0 +1,16 @@ +HTTP/1.1 401 Unprocessable Entity +Server: nginx/1.4.4 +Date: Tue, 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", + "responseCode": 401 +} diff --git a/tests/Mock/BlikAliasesByEmailFailure.txt b/tests/Mock/BlikAliasesByEmailFailure.txt new file mode 100644 index 0000000..118a62d --- /dev/null +++ b/tests/Mock/BlikAliasesByEmailFailure.txt @@ -0,0 +1,16 @@ +HTTP/1.1 400 Unprocessable Entity +Server: nginx/1.4.4 +Date: Tue, 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": "Invalid input data", + "responseCode": 400 +} diff --git a/tests/Mock/BlikAliasesByEmailNotFound.txt b/tests/Mock/BlikAliasesByEmailNotFound.txt new file mode 100644 index 0000000..92bb3b2 --- /dev/null +++ b/tests/Mock/BlikAliasesByEmailNotFound.txt @@ -0,0 +1,16 @@ +HTTP/1.1 404 Unprocessable Entity +Server: nginx/1.4.4 +Date: Tue, 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": "Transaction not found", + "responseCode": 404 +} diff --git a/tests/Mock/BlikAliasesByEmailSuccess.txt b/tests/Mock/BlikAliasesByEmailSuccess.txt new file mode 100644 index 0000000..fae6f75 --- /dev/null +++ b/tests/Mock/BlikAliasesByEmailSuccess.txt @@ -0,0 +1,19 @@ +HTTP/1.1 200 Unprocessable Entity +Server: nginx/1.4.4 +Date: Tue, 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 + +{ + "data": [ + {"type": "blik200"}, + {"type": "blik200"} + ], + "responseCode": 200 +} diff --git a/tests/Mock/BlikChargeByAliasAlternativeKeysFailure.txt b/tests/Mock/BlikChargeByAliasAlternativeKeysFailure.txt new file mode 100644 index 0000000..b781108 --- /dev/null +++ b/tests/Mock/BlikChargeByAliasAlternativeKeysFailure.txt @@ -0,0 +1,28 @@ +HTTP/1.1 409 Unprocessable Entity +Server: nginx/1.4.4 +Date: Tue, 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": { + "message": "Some error", + "alternativeKeys": [ + { + "label": "test2", + "alias": "test2" + }, + { + "label": "test3", + "alias": "test3" + } + ] + }, + "responseCode": 409 +} diff --git a/tests/Mock/BlikChargeByCodeAuthFailure.txt b/tests/Mock/BlikChargeByCodeAuthFailure.txt new file mode 100644 index 0000000..304aeda --- /dev/null +++ b/tests/Mock/BlikChargeByCodeAuthFailure.txt @@ -0,0 +1,16 @@ +HTTP/1.1 401 Unprocessable Entity +Server: nginx/1.4.4 +Date: Tue, 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", + "responseCode": 401 +} diff --git a/tests/Mock/BlikChargeByCodeFailure.txt b/tests/Mock/BlikChargeByCodeFailure.txt new file mode 100644 index 0000000..118a62d --- /dev/null +++ b/tests/Mock/BlikChargeByCodeFailure.txt @@ -0,0 +1,16 @@ +HTTP/1.1 400 Unprocessable Entity +Server: nginx/1.4.4 +Date: Tue, 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": "Invalid input data", + "responseCode": 400 +} diff --git a/tests/Mock/BlikChargeByCodeSuccess.txt b/tests/Mock/BlikChargeByCodeSuccess.txt new file mode 100644 index 0000000..dbee474 --- /dev/null +++ b/tests/Mock/BlikChargeByCodeSuccess.txt @@ -0,0 +1,19 @@ +HTTP/1.1 200 Unprocessable Entity +Server: nginx/1.4.4 +Date: Tue, 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 + +{ + "data": { + "orderId": "test123", + "message": "success" + }, + "responseCode": 200 +} diff --git a/tests/Mock/BlikChargeByCodeUndefinedError.txt b/tests/Mock/BlikChargeByCodeUndefinedError.txt new file mode 100644 index 0000000..e543455 --- /dev/null +++ b/tests/Mock/BlikChargeByCodeUndefinedError.txt @@ -0,0 +1,16 @@ +HTTP/1.1 500 Unprocessable Entity +Server: nginx/1.4.4 +Date: Tue, 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": "Undefined error", + "responseCode": 500 +} diff --git a/tests/Mock/CardInfoSuccess.txt b/tests/Mock/CardInfoSuccess.txt index e1bbc59..5ad0f06 100644 --- a/tests/Mock/CardInfoSuccess.txt +++ b/tests/Mock/CardInfoSuccess.txt @@ -1,4 +1,4 @@ -HTTP/1.1 422 Unprocessable Entity +HTTP/1.1 200 Unprocessable Entity Server: nginx/1.4.4 Date: Tue, 17 May 2022 17:00:00 GMT Content-Type: application/json; charset=utf-8