From 376c8f7ec1f9ebdba20ab633242cd74518dfe471 Mon Sep 17 00:00:00 2001 From: Nuryagdy Mustapayev Date: Mon, 1 Apr 2024 13:11:10 +0200 Subject: [PATCH] test coverage and refactor exception throws --- src/Gateways/InterPos.php | 4 +- src/Gateways/KuveytPos.php | 11 +- src/Gateways/PayFlexCPV4Pos.php | 10 +- src/Gateways/PayForPos.php | 4 +- src/Gateways/ToslaPos.php | 12 +- src/Serializer/PayFlexV4PosSerializer.php | 3 +- tests/Unit/Crypt/PayFlexCP4CryptTest.php | 3 - .../GarantiPosRequestDataMapperTest.php | 2 +- .../PayFlexV4PosResponseDataMapperTest.php | 6 +- tests/Unit/Gateways/EstPosTest.php | 225 +++++++- tests/Unit/Gateways/GarantiPosTest.php | 419 +++++++++++++++ tests/Unit/Gateways/InterPosTest.php | 389 ++++++++++++++ tests/Unit/Gateways/KuveytPosTest.php | 60 ++- tests/Unit/Gateways/PayFlexCPV4PosTest.php | 242 +++++++++ tests/Unit/Gateways/PayFlexV4PosTest.php | 423 +++++++++++++++ tests/Unit/Gateways/PayForTest.php | 493 +++++++++++++++++- tests/Unit/Gateways/PosNetTest.php | 341 +++++++++++- tests/Unit/Gateways/PosNetV1PosTest.php | 342 ++++++++++++ tests/Unit/Gateways/ToslaPosTest.php | 61 ++- 19 files changed, 3007 insertions(+), 43 deletions(-) diff --git a/src/Gateways/InterPos.php b/src/Gateways/InterPos.php index b9f2aa69..b9208762 100644 --- a/src/Gateways/InterPos.php +++ b/src/Gateways/InterPos.php @@ -119,7 +119,9 @@ public function make3DPayPayment(Request $request, array $order, string $txType) */ public function make3DHostPayment(Request $request, array $order, string $txType): PosInterface { - return $this->make3DPayPayment($request, $order, $txType); + $this->response = $this->responseDataMapper->map3DHostResponseData($request->request->all(), $txType, $order); + + return $this; } /** diff --git a/src/Gateways/KuveytPos.php b/src/Gateways/KuveytPos.php index e7d9568b..7d3a3e6f 100644 --- a/src/Gateways/KuveytPos.php +++ b/src/Gateways/KuveytPos.php @@ -17,7 +17,6 @@ use Mews\Pos\Entity\Card\CreditCardInterface; use Mews\Pos\Event\RequestDataPreparedEvent; use Mews\Pos\Exceptions\HashMismatchException; -use Mews\Pos\Exceptions\NotImplementedException; use Mews\Pos\Exceptions\UnsupportedPaymentModelException; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\PosInterface; @@ -114,7 +113,15 @@ public function get3DFormData(array $order, string $paymentModel, string $txType */ public function makeRegularPayment(array $order, CreditCardInterface $creditCard, string $txType): PosInterface { - throw new NotImplementedException(); + throw new UnsupportedPaymentModelException(); + } + + /** + * @inheritDoc + */ + public function makeRegularPostPayment(array $order): PosInterface + { + throw new UnsupportedPaymentModelException(); } /** diff --git a/src/Gateways/PayFlexCPV4Pos.php b/src/Gateways/PayFlexCPV4Pos.php index 97b22697..cca01dd1 100644 --- a/src/Gateways/PayFlexCPV4Pos.php +++ b/src/Gateways/PayFlexCPV4Pos.php @@ -128,12 +128,20 @@ public function make3DHostPayment(Request $request, array $order, string $txType return $this->make3DPayPayment($request, $order, $txType); } + /** + * @inheritDoc + */ + public function status(array $order): PosInterface + { + throw new UnsupportedTransactionTypeException(); + } + /** * @inheritDoc */ public function history(array $data): PosInterface { - throw new UnsupportedPaymentModelException(); + throw new UnsupportedTransactionTypeException(); } /** diff --git a/src/Gateways/PayForPos.php b/src/Gateways/PayForPos.php index e3d30879..129d66d3 100644 --- a/src/Gateways/PayForPos.php +++ b/src/Gateways/PayForPos.php @@ -112,7 +112,9 @@ public function make3DPayPayment(Request $request, array $order, string $txType) */ public function make3DHostPayment(Request $request, array $order, string $txType): PosInterface { - return $this->make3DPayPayment($request, $order, $txType); + $this->response = $this->responseDataMapper->map3DHostResponseData($request->request->all(), $txType, $order); + + return $this; } /** diff --git a/src/Gateways/ToslaPos.php b/src/Gateways/ToslaPos.php index 333e87b8..281c3db3 100644 --- a/src/Gateways/ToslaPos.php +++ b/src/Gateways/ToslaPos.php @@ -114,7 +114,17 @@ public function make3DPayPayment(Request $request, array $order, string $txType) */ public function make3DHostPayment(Request $request, array $order, string $txType): PosInterface { - return $this->make3DPayPayment($request, $order, $txType); + $request = $request->request; + + if ($this->is3DAuthSuccess($request->all()) && !$this->requestDataMapper->getCrypt()->check3DHash($this->account, $request->all())) { + throw new HashMismatchException(); + } + + $this->response = $this->responseDataMapper->map3DHostResponseData($request->all(), $txType, $order); + + $this->logger->debug('finished 3D payment', ['mapped_response' => $this->response]); + + return $this; } /** diff --git a/src/Serializer/PayFlexV4PosSerializer.php b/src/Serializer/PayFlexV4PosSerializer.php index 30f64c2c..3f7071b0 100644 --- a/src/Serializer/PayFlexV4PosSerializer.php +++ b/src/Serializer/PayFlexV4PosSerializer.php @@ -12,7 +12,6 @@ use Symfony\Component\Serializer\Encoder\XmlEncoder; use Symfony\Component\Serializer\Exception\NotEncodableValueException; use Symfony\Component\Serializer\Serializer; -use function sprintf; use function strip_tags; class PayFlexV4PosSerializer implements SerializerInterface @@ -46,7 +45,7 @@ public static function supports(string $gatewayClass): bool public function encode(array $data, string $txType): string { if (PosInterface::TX_TYPE_HISTORY === $txType || PosInterface::TX_TYPE_ORDER_HISTORY === $txType) { - throw new DomainException(sprintf('Serialization of the transaction %s is not supported', $txType)); + throw new DomainException(\sprintf('Serialization of the transaction %s is not supported', $txType)); } if (PosInterface::TX_TYPE_STATUS === $txType) { diff --git a/tests/Unit/Crypt/PayFlexCP4CryptTest.php b/tests/Unit/Crypt/PayFlexCP4CryptTest.php index 1d844f58..4c5ed83c 100644 --- a/tests/Unit/Crypt/PayFlexCP4CryptTest.php +++ b/tests/Unit/Crypt/PayFlexCP4CryptTest.php @@ -17,9 +17,6 @@ */ class PayFlexCP4CryptTest extends TestCase { - /** @var array|array */ - public array $order = []; - public PayFlexCPV4Crypt $crypt; private PayFlexAccount $account; diff --git a/tests/Unit/DataMapper/RequestDataMapper/GarantiPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/GarantiPosRequestDataMapperTest.php index 7bc3b5de..50317c48 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/GarantiPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/GarantiPosRequestDataMapperTest.php @@ -31,7 +31,7 @@ class GarantiPosRequestDataMapperTest extends TestCase private GarantiPosRequestDataMapper $requestDataMapper; - private $order; + private array $order; private $config; diff --git a/tests/Unit/DataMapper/ResponseDataMapper/PayFlexV4PosResponseDataMapperTest.php b/tests/Unit/DataMapper/ResponseDataMapper/PayFlexV4PosResponseDataMapperTest.php index 1587c8c8..3b963e0e 100644 --- a/tests/Unit/DataMapper/ResponseDataMapper/PayFlexV4PosResponseDataMapperTest.php +++ b/tests/Unit/DataMapper/ResponseDataMapper/PayFlexV4PosResponseDataMapperTest.php @@ -470,7 +470,7 @@ public static function paymentDataProvider(): iterable public static function threeDPaymentDataProvider(): array { return [ - 'authFail1' => [ + '3d_auth_fail' => [ 'order' => [], 'txType' => PosInterface::TX_TYPE_PAY_AUTH, 'threeDResponseData' => [ @@ -517,7 +517,7 @@ public static function threeDPaymentDataProvider(): array 'installment_count' => 0, ], ], - 'auth_success_payment_fail' => [ + '3d_auth_success_payment_fail' => [ 'order' => [], 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, 'threeDResponseData' => [ @@ -584,7 +584,7 @@ public static function threeDPaymentDataProvider(): array 'installment_count' => 0, ], ], - 'success1' => [ + 'success1' => [ 'order' => [], 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, 'threeDResponseData' => [ diff --git a/tests/Unit/Gateways/EstPosTest.php b/tests/Unit/Gateways/EstPosTest.php index bf8d53e6..f6baaeb0 100644 --- a/tests/Unit/Gateways/EstPosTest.php +++ b/tests/Unit/Gateways/EstPosTest.php @@ -11,6 +11,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\EstPosAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\EstPos; @@ -26,6 +27,8 @@ /** * @covers \Mews\Pos\Gateways\EstPos + * + * @uses \Mews\Pos\Gateways\AbstractGateway */ class EstPosTest extends TestCase { @@ -145,6 +148,34 @@ public function testInit(): void $this->assertSame($this->account, $this->pos->getAccount()); } + /** + * @testWith [true] + * [false] + */ + public function testGet3DFormData( + bool $isWithCard + ): void { + $card = $isWithCard ? $this->card : null; + $order = ['id' => '124']; + $paymentModel = PosInterface::MODEL_3D_SECURE; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('create3DFormData') + ->with( + $this->pos->getAccount(), + $order, + $paymentModel, + $txType, + 'https://entegrasyon.asseco-see.com.tr/fim/est3Dgate', + $card + ) + ->willReturn(['formData']); + + $actual = $this->pos->get3DFormData($order, $paymentModel, $txType, $card); + + $this->assertSame(['formData'], $actual); + } /** * @return void @@ -245,6 +276,12 @@ public function testStatus(array $bankResponse, array $expectedData, bool $isSuc $this->assertSame($isSuccess, $this->pos->isSuccess()); } + public function testHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->history([]); + } + /** * @dataProvider orderHistoryDataProvider */ @@ -455,13 +492,95 @@ public function testMake3DPayment( $this->assertSame($isSuccess, $this->pos->isSuccess()); } + /** + * @dataProvider makeRegularPaymentDataProvider + */ + public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $card = $this->card; + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePaymentRequestData') + ->with($account, $order, $txType, $card) + ->willReturn(['createNonSecurePaymentRequestData']); + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPayment($order, $card, $txType); + } + + /** + * @dataProvider makeRegularPostAuthPaymentDataProvider + */ + public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePostAuthPaymentRequestData') + ->with($account, $order) + ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPostPayment($order); + } + public static function make3DPaymentDataProvider(): array { return [ 'auth_fail' => [ 'order' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['order'], 'txType' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['txType'], - 'request' => Request::create('', 'POST', EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['threeDResponseData']), + 'request' => Request::create( + '', + 'POST', + EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['threeDResponseData'] + ), 'paymentResponse' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['paymentData'], 'expected' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['expectedData'], 'is3DSuccess' => false, @@ -470,7 +589,11 @@ public static function make3DPaymentDataProvider(): array '3d_auth_success_payment_fail' => [ 'order' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['order'], 'txType' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['txType'], - 'request' => Request::create('', 'POST', EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['threeDResponseData']), + 'request' => Request::create( + '', + 'POST', + EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['threeDResponseData'] + ), 'paymentResponse' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['paymentData'], 'expected' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['expectedData'], 'is3DSuccess' => true, @@ -479,7 +602,11 @@ public static function make3DPaymentDataProvider(): array 'success' => [ 'order' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['order'], 'txType' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['txType'], - 'request' => Request::create('', 'POST', EstPosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['threeDResponseData']), + 'request' => Request::create( + '', + 'POST', + EstPosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['threeDResponseData'] + ), 'paymentResponse' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['paymentData'], 'expected' => EstPosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['expectedData'], 'is3DSuccess' => true, @@ -542,4 +669,96 @@ public static function orderHistoryDataProvider(): iterable 'isSuccess' => false, ]; } + + public static function makeRegularPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'api_url' => 'https://entegrasyon.asseco-see.com.tr/fim/api', + ], + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, + 'api_url' => 'https://entegrasyon.asseco-see.com.tr/fim/api', + ], + ]; + } + + public static function makeRegularPostAuthPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://entegrasyon.asseco-see.com.tr/fim/api', + ], + ]; + } + + public static function statusRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function cancelRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function refundRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function historyRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function orderHistoryRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } } diff --git a/tests/Unit/Gateways/GarantiPosTest.php b/tests/Unit/Gateways/GarantiPosTest.php index 14920ffd..51deea5f 100644 --- a/tests/Unit/Gateways/GarantiPosTest.php +++ b/tests/Unit/Gateways/GarantiPosTest.php @@ -10,7 +10,11 @@ use Mews\Pos\DataMapper\RequestDataMapper\RequestDataMapperInterface; use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\GarantiPosAccount; +use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Exceptions\UnsupportedPaymentModelException; +use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; +use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\GarantiPos; use Mews\Pos\PosInterface; use Mews\Pos\Serializer\SerializerInterface; @@ -24,6 +28,8 @@ /** * @covers \Mews\Pos\Gateways\GarantiPos + * + * @uses \Mews\Pos\Gateways\AbstractGateway */ class GarantiPosTest extends TestCase { @@ -57,6 +63,8 @@ class GarantiPosTest extends TestCase /** @var GarantiPos */ private PosInterface $pos; + private CreditCardInterface $card; + protected function setUp(): void { parent::setUp(); @@ -106,6 +114,16 @@ protected function setUp(): void ); $this->pos->setTestMode(true); + + $this->card = CreditCardFactory::createForGateway( + $this->pos, + '5555444433332222', + '21', + '12', + '122', + 'ahmet', + CreditCardInterface::CARD_TYPE_VISA + ); } /** @@ -121,6 +139,35 @@ public function testInit(): void $this->assertSame($this->account, $this->pos->getAccount()); } + /** + * @testWith [true] + * [false] + */ + public function testGet3DFormData( + bool $isWithCard + ): void { + $card = $isWithCard ? $this->card : null; + $order = ['id' => '124']; + $paymentModel = PosInterface::MODEL_3D_SECURE; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('create3DFormData') + ->with( + $this->pos->getAccount(), + $order, + $paymentModel, + $txType, + 'https://sanalposprovtest.garantibbva.com.tr/servlet/gt3dengine', + $card + ) + ->willReturn(['formData']); + + $actual = $this->pos->get3DFormData($order, $paymentModel, $txType, $card); + + $this->assertSame(['formData'], $actual); + } + /** * @dataProvider make3DPaymentDataProvider */ @@ -202,6 +249,286 @@ public function testMake3DPayment( $this->assertSame($isSuccess, $this->pos->isSuccess()); } + public function testMake3DHostPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DHostPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + + /** + * @return void + */ + public function testMake3DPayPayment(): void + { + $this->cryptMock->expects(self::never()) + ->method('check3DHash'); + + $responseData = ['$responseData']; + $request = Request::create('', 'POST', $responseData); + $order = ['id' => '123']; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->responseMapperMock->expects(self::once()) + ->method('map3DPayResponseData') + ->with($request->request->all(), $txType, $order) + ->willReturn(['status' => 'approved']); + + $pos = $this->pos; + + $pos->make3DPayPayment($request, $order, $txType); + + $result = $pos->getResponse(); + $this->assertSame(['status' => 'approved'], $result); + $this->assertTrue($pos->isSuccess()); + } + + /** + * @dataProvider makeRegularPaymentDataProvider + */ + public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $card = $this->card; + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePaymentRequestData') + ->with($account, $order, $txType, $card) + ->willReturn(['createNonSecurePaymentRequestData']); + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPayment($order, $card, $txType); + } + + /** + * @dataProvider makeRegularPostAuthPaymentDataProvider + */ + public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePostAuthPaymentRequestData') + ->with($account, $order) + ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPostPayment($order); + } + + /** + * @dataProvider statusRequestDataProvider + */ + public function testStatusRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + + $this->requestMapperMock->expects(self::once()) + ->method('createStatusRequestData') + ->with($account, $order) + ->willReturn(['createStatusRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createStatusRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapStatusResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->status($order); + } + + /** + * @dataProvider cancelRequestDataProvider + */ + public function testCancelRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + + $this->requestMapperMock->expects(self::once()) + ->method('createCancelRequestData') + ->with($account, $order) + ->willReturn(['createCancelRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createCancelRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapCancelResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->cancel($order); + } + + /** + * @dataProvider refundRequestDataProvider + */ + public function testRefundRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + + $this->requestMapperMock->expects(self::once()) + ->method('createRefundRequestData') + ->with($account, $order) + ->willReturn(['createRefundRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createRefundRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapRefundResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->refund($order); + } + + public function testHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->history([]); + } + + /** + * @dataProvider orderHistoryRequestDataProvider + */ + public function testOrderHistoryRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + + $this->requestMapperMock->expects(self::once()) + ->method('createOrderHistoryRequestData') + ->with($account, $order) + ->willReturn(['createOrderHistoryRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createOrderHistoryRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapOrderHistoryResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->orderHistory($order); + } + public static function make3DPaymentDataProvider(): array { return [ @@ -225,4 +552,96 @@ public static function make3DPaymentDataProvider(): array ], ]; } + + public static function makeRegularPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function makeRegularPostAuthPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function statusRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function cancelRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function refundRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function historyRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } + + public static function orderHistoryRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + ], + ]; + } } diff --git a/tests/Unit/Gateways/InterPosTest.php b/tests/Unit/Gateways/InterPosTest.php index c3191987..cf0591de 100644 --- a/tests/Unit/Gateways/InterPosTest.php +++ b/tests/Unit/Gateways/InterPosTest.php @@ -10,6 +10,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\InterPosAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\InterPos; @@ -142,6 +143,35 @@ public function testInit(): void $this->assertSame($this->account, $this->pos->getAccount()); } + /** + * @testWith [true] + * [false] + */ + public function testGet3DFormData( + bool $isWithCard + ): void { + $card = $isWithCard ? $this->card : null; + $order = ['id' => '124']; + $paymentModel = PosInterface::MODEL_3D_SECURE; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('create3DFormData') + ->with( + $this->pos->getAccount(), + $order, + $paymentModel, + $txType, + 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + $card + ) + ->willReturn(['formData']); + + $actual = $this->pos->get3DFormData($order, $paymentModel, $txType, $card); + + $this->assertSame(['formData'], $actual); + } + /** * @dataProvider make3DPaymentDataProvider */ @@ -212,6 +242,272 @@ public function testMake3DPayment( $this->assertSame($isSuccess, $this->pos->isSuccess()); } + /** + * @return void + */ + public function testMake3DPayPayment(): void + { + $this->cryptMock->expects(self::never()) + ->method('check3DHash'); + + $responseData = ['$responseData']; + $request = Request::create('', 'POST', $responseData); + $order = ['id' => '123']; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->responseMapperMock->expects(self::once()) + ->method('map3DPayResponseData') + ->with($request->request->all(), $txType, $order) + ->willReturn(['status' => 'approved']); + + $pos = $this->pos; + + $pos->make3DPayPayment($request, $order, $txType); + + $result = $pos->getResponse(); + $this->assertSame(['status' => 'approved'], $result); + $this->assertTrue($pos->isSuccess()); + } + + /** + * @return void + */ + public function testMake3DHostPayment(): void + { + $this->cryptMock->expects(self::never()) + ->method('check3DHash'); + + $responseData = ['$responseData']; + $request = Request::create('', 'POST', $responseData); + $order = ['id' => '123']; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->responseMapperMock->expects(self::once()) + ->method('map3DHostResponseData') + ->with($request->request->all(), $txType, $order) + ->willReturn(['status' => 'approved']); + + $pos = $this->pos; + + $pos->make3DHostPayment($request, $order, $txType); + + $result = $pos->getResponse(); + $this->assertSame(['status' => 'approved'], $result); + $this->assertTrue($pos->isSuccess()); + } + + public function testHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->history([]); + } + + public function testOrderHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->orderHistory([]); + } + + + /** + * @dataProvider makeRegularPaymentDataProvider + */ + public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $card = $this->card; + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePaymentRequestData') + ->with($account, $order, $txType, $card) + ->willReturn(['createNonSecurePaymentRequestData']); + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePaymentRequestData'], $txType) + ->willReturn(['request-body']); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPayment($order, $card, $txType); + } + + /** + * @dataProvider makeRegularPostAuthPaymentDataProvider + */ + public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePostAuthPaymentRequestData') + ->with($account, $order) + ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) + ->willReturn(['request-body']); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPostPayment($order); + } + + + /** + * @dataProvider statusRequestDataProvider + */ + public function testStatusRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + + $this->requestMapperMock->expects(self::once()) + ->method('createStatusRequestData') + ->with($account, $order) + ->willReturn(['createStatusRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createStatusRequestData'], $txType) + ->willReturn(['request-body']); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapStatusResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->status($order); + } + + /** + * @dataProvider cancelRequestDataProvider + */ + public function testCancelRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + + $this->requestMapperMock->expects(self::once()) + ->method('createCancelRequestData') + ->with($account, $order) + ->willReturn(['createCancelRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createCancelRequestData'], $txType) + ->willReturn(['request-body']); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapCancelResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->cancel($order); + } + + /** + * @dataProvider refundRequestDataProvider + */ + public function testRefundRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + + $this->requestMapperMock->expects(self::once()) + ->method('createRefundRequestData') + ->with($account, $order) + ->willReturn(['createRefundRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createRefundRequestData'], $txType) + ->willReturn(['request-body']); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapRefundResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->refund($order); + } + public static function make3DPaymentDataProvider(): array { return [ @@ -227,4 +523,97 @@ public static function make3DPaymentDataProvider(): array ], ]; } + + + public static function makeRegularPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + ], + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, + 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + ], + ]; + } + + public static function makeRegularPostAuthPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + ], + ]; + } + + public static function statusRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + ], + ]; + } + + public static function cancelRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + ], + ]; + } + + public static function refundRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + ], + ]; + } + + public static function historyRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + ], + ]; + } + + public static function orderHistoryRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + ], + ]; + } } diff --git a/tests/Unit/Gateways/KuveytPosTest.php b/tests/Unit/Gateways/KuveytPosTest.php index 5628d63a..e5935e0f 100644 --- a/tests/Unit/Gateways/KuveytPosTest.php +++ b/tests/Unit/Gateways/KuveytPosTest.php @@ -11,7 +11,8 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\KuveytPosAccount; use Mews\Pos\Entity\Card\CreditCardInterface; -use Mews\Pos\Exceptions\NotImplementedException; +use Mews\Pos\Exceptions\UnsupportedPaymentModelException; +use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\KuveytPos; @@ -325,10 +326,45 @@ public function testMake3DPayment( public function testMakeRegularPayment(): void { - $this->expectException(NotImplementedException::class); + $this->expectException(UnsupportedPaymentModelException::class); $this->pos->makeRegularPayment([], $this->card, PosInterface::TX_TYPE_PAY_AUTH); } + public function testMakeRegularPostAuthPayment(): void + { + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->makeRegularPostPayment([]); + } + + public function testHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->history([]); + } + + public function testOrderHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->orderHistory([]); + } + + public function testMake3DHostPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DHostPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + + public function testMake3DPayPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DPayPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + + public static function make3DPaymentDataProvider(): array { return [ @@ -376,4 +412,24 @@ public static function make3DPaymentDataProvider(): array ], ]; } + + public static function makeRegularPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'api_url' => 'https://boa.vakifkatilim.com.tr/VirtualPOS.Gateway/Home/Non3DPayGate', + ], + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, + 'api_url' => 'https://boa.vakifkatilim.com.tr/VirtualPOS.Gateway/Home/PreAuthorizaten', + ], + ]; + } } diff --git a/tests/Unit/Gateways/PayFlexCPV4PosTest.php b/tests/Unit/Gateways/PayFlexCPV4PosTest.php index 74bf256b..158aad82 100644 --- a/tests/Unit/Gateways/PayFlexCPV4PosTest.php +++ b/tests/Unit/Gateways/PayFlexCPV4PosTest.php @@ -14,6 +14,8 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PayFlexAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Exceptions\UnsupportedPaymentModelException; +use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\PayFlexCPV4Pos; @@ -249,6 +251,14 @@ public function testGet3DFormDataEnrollmentFail(): void $this->pos->get3DFormData($order, $paymentModel, $txType, $card); } + public function testMake3DPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + /** * @dataProvider make3DPayPaymentDataProvider */ @@ -322,6 +332,182 @@ public function testMake3DPayPayment( $this->assertSame($isSuccess, $this->pos->isSuccess()); } + /** + * @dataProvider makeRegularPaymentDataProvider + */ + public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $card = $this->card; + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePaymentRequestData') + ->with($account, $order, $txType, $card) + ->willReturn(['createNonSecurePaymentRequestData']); + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPayment($order, $card, $txType); + } + + /** + * @dataProvider makeRegularPostAuthPaymentDataProvider + */ + public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePostAuthPaymentRequestData') + ->with($account, $order) + ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPostPayment($order); + } + + public function testStatusRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->status([]); + } + + /** + * @dataProvider cancelRequestDataProvider + */ + public function testCancelRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + + $this->requestMapperMock->expects(self::once()) + ->method('createCancelRequestData') + ->with($account, $order) + ->willReturn(['createCancelRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createCancelRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapCancelResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->cancel($order); + } + + /** + * @dataProvider refundRequestDataProvider + */ + public function testRefundRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + + $this->requestMapperMock->expects(self::once()) + ->method('createRefundRequestData') + ->with($account, $order) + ->willReturn(['createRefundRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createRefundRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapRefundResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->refund($order); + } + + public function testHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->history([]); + } + + public function testOrderHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->orderHistory([]); + } + public static function make3DPayPaymentDataProvider(): array { $testData = iterator_to_array( @@ -357,4 +543,60 @@ public static function make3DPayPaymentDataProvider(): array ], ]; } + + public static function makeRegularPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'api_url' => 'https://cptest.vakifbank.com.tr/CommonPayment/api/RegisterTransaction', + ], + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, + 'api_url' => 'https://cptest.vakifbank.com.tr/CommonPayment/api/RegisterTransaction', + ], + ]; + } + + public static function makeRegularPostAuthPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://cptest.vakifbank.com.tr/CommonPayment/api/RegisterTransaction', + ], + ]; + } + + public static function cancelRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://cptest.vakifbank.com.tr/CommonPayment/api/RegisterTransaction', + ], + ]; + } + + public static function refundRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://cptest.vakifbank.com.tr/CommonPayment/api/RegisterTransaction', + ], + ]; + } } diff --git a/tests/Unit/Gateways/PayFlexV4PosTest.php b/tests/Unit/Gateways/PayFlexV4PosTest.php index 749aa552..0f0d3dcc 100644 --- a/tests/Unit/Gateways/PayFlexV4PosTest.php +++ b/tests/Unit/Gateways/PayFlexV4PosTest.php @@ -13,17 +13,21 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PayFlexAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Exceptions\UnsupportedPaymentModelException; +use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\PayFlexV4Pos; use Mews\Pos\PosInterface; use Mews\Pos\Serializer\SerializerInterface; use Mews\Pos\Tests\Unit\DataMapper\RequestDataMapper\PayFlexV4PosRequestDataMapperTest; +use Mews\Pos\Tests\Unit\DataMapper\ResponseDataMapper\PayFlexV4PosResponseDataMapperTest; use Mews\Pos\Tests\Unit\HttpClientTestTrait; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; +use Symfony\Component\HttpFoundation\Request; /** * @covers \Mews\Pos\Gateways\PayFlexV4Pos @@ -223,6 +227,311 @@ public function testGet3DFormDataSuccess(): void $this->assertSame(['3d-form-data'], $result); } + /** + * @dataProvider make3DPaymentDataProvider + */ + public function testMake3DPayment( + array $order, + string $txType, + Request $request, + array $paymentResponse, + array $expectedResponse, + bool $is3DSuccess, + bool $isSuccess + ): void + { + if ($is3DSuccess) { + $this->cryptMock->expects(self::never()) + ->method('check3DHash'); + } + + $this->responseMapperMock->expects(self::once()) + ->method('extractMdStatus') + ->with($request->request->all()) + ->willReturn('3d-status'); + + $this->responseMapperMock->expects(self::once()) + ->method('is3dAuthSuccess') + ->with('3d-status') + ->willReturn($is3DSuccess); + + $create3DPaymentRequestData = [ + 'create3DPaymentRequestData', + ]; + if ($is3DSuccess) { + $this->requestMapperMock->expects(self::once()) + ->method('create3DPaymentRequestData') + ->with($this->account, $order, $txType, $request->request->all()) + ->willReturn($create3DPaymentRequestData); + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $this->config['gateway_endpoints']['payment_api'], + [ + 'form_params' => ['prmstr' => 'request-body'], + ], + ); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($create3DPaymentRequestData, $txType) + ->willReturn('request-body'); + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn($paymentResponse); + + $this->responseMapperMock->expects(self::once()) + ->method('map3DPaymentData') + ->with($request->request->all(), $paymentResponse, $txType, $order) + ->willReturn($expectedResponse); + } else { + $this->responseMapperMock->expects(self::once()) + ->method('map3DPaymentData') + ->with($request->request->all(), null, $txType, $order) + ->willReturn($expectedResponse); + $this->requestMapperMock->expects(self::never()) + ->method('create3DPaymentRequestData'); + $this->serializerMock->expects(self::never()) + ->method('encode'); + $this->serializerMock->expects(self::never()) + ->method('decode'); + } + + $this->pos->make3DPayment($request, $order, $txType); + + $result = $this->pos->getResponse(); + $this->assertSame($expectedResponse, $result); + $this->assertSame($isSuccess, $this->pos->isSuccess()); + } + + public function testMake3DHostPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DHostPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + + public function testMake3DPayPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DPayPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + + /** + * @dataProvider makeRegularPaymentDataProvider + */ + public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $card = $this->card; + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePaymentRequestData') + ->with($account, $order, $txType, $card) + ->willReturn(['createNonSecurePaymentRequestData']); + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['prmstr' => 'request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPayment($order, $card, $txType); + } + + /** + * @dataProvider makeRegularPostAuthPaymentDataProvider + */ + public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePostAuthPaymentRequestData') + ->with($account, $order) + ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['prmstr' => 'request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPostPayment($order); + } + + + /** + * @dataProvider statusRequestDataProvider + */ + public function testStatusRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + + $this->requestMapperMock->expects(self::once()) + ->method('createStatusRequestData') + ->with($account, $order) + ->willReturn(['createStatusRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createStatusRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['prmstr' => 'request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapStatusResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->status($order); + } + + /** + * @dataProvider cancelRequestDataProvider + */ + public function testCancelRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + + $this->requestMapperMock->expects(self::once()) + ->method('createCancelRequestData') + ->with($account, $order) + ->willReturn(['createCancelRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createCancelRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['prmstr' => 'request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapCancelResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->cancel($order); + } + + /** + * @dataProvider refundRequestDataProvider + */ + public function testRefundRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + + $this->requestMapperMock->expects(self::once()) + ->method('createRefundRequestData') + ->with($account, $order) + ->willReturn(['createRefundRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createRefundRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'form_params' => ['prmstr' => 'request-body'], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapRefundResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->refund($order); + } + + public function testHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->history([]); + } + + public function testOrderHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->orderHistory([]); + } + public static function getSampleEnrollmentFailResponseDataProvider(): array { return [ @@ -236,4 +545,118 @@ public static function getSampleEnrollmentFailResponseDataProvider(): array 'ErrorMessage' => 'Merchant cannot be found for this bank', ]; } + + + + + public static function makeRegularPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'api_url' => 'https://onlineodemetest.vakifbank.com.tr:4443/VposService/v3/Vposreq.aspx', + ], + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, + 'api_url' => 'https://onlineodemetest.vakifbank.com.tr:4443/VposService/v3/Vposreq.aspx', + ], + ]; + } + + public static function makeRegularPostAuthPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://onlineodemetest.vakifbank.com.tr:4443/VposService/v3/Vposreq.aspx', + ], + ]; + } + + public static function statusRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://sanalpos.vakifbank.com.tr/v4/UIWebService/Search.aspx', + ], + ]; + } + + public static function cancelRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://onlineodemetest.vakifbank.com.tr:4443/VposService/v3/Vposreq.aspx', + ], + ]; + } + + public static function refundRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://onlineodemetest.vakifbank.com.tr:4443/VposService/v3/Vposreq.aspx', + ], + ]; + } + + public static function make3DPaymentDataProvider(): array + { + return [ + 'auth_fail' => [ + 'order' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['order'], + 'txType' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['txType'], + 'request' => Request::create( + '', + 'POST', + PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['threeDResponseData'] + ), + 'paymentResponse' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['paymentData'], + 'expected' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_fail']['expectedData'], + 'is3DSuccess' => false, + 'isSuccess' => false, + ], + '3d_auth_success_payment_fail' => [ + 'order' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['order'], + 'txType' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['txType'], + 'request' => Request::create( + '', + 'POST', + PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['threeDResponseData'] + ), 'paymentResponse' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['paymentData'], + 'expected' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['expectedData'], + 'is3DSuccess' => true, + 'isSuccess' => false, + ], + 'success' => [ + 'order' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['order'], + 'txType' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['txType'], + 'request' => Request::create( + '', + 'POST', + PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['3d_auth_success_payment_fail']['threeDResponseData'] + ), 'paymentResponse' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['paymentData'], + 'expected' => PayFlexV4PosResponseDataMapperTest::threeDPaymentDataProvider()['success1']['expectedData'], + 'is3DSuccess' => true, + 'isSuccess' => true, + ], + ]; + } } diff --git a/tests/Unit/Gateways/PayForTest.php b/tests/Unit/Gateways/PayForTest.php index ff9ed37a..a7688c53 100644 --- a/tests/Unit/Gateways/PayForTest.php +++ b/tests/Unit/Gateways/PayForTest.php @@ -10,7 +10,9 @@ use Mews\Pos\DataMapper\RequestDataMapper\RequestDataMapperInterface; use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PayForAccount; +use Mews\Pos\Entity\Card\CreditCardInterface; use Mews\Pos\Factory\AccountFactory; +use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\PayForPos; use Mews\Pos\PosInterface; use Mews\Pos\Serializer\SerializerInterface; @@ -27,7 +29,6 @@ */ class PayForTest extends TestCase { - use HttpClientTestTrait; private PayForAccount $account; @@ -58,6 +59,8 @@ class PayForTest extends TestCase /** @var SerializerInterface & MockObject */ private MockObject $serializerMock; + private CreditCardInterface $card; + protected function setUp(): void { parent::setUp(); @@ -105,6 +108,16 @@ protected function setUp(): void ); $this->pos->setTestMode(true); + + $this->card = CreditCardFactory::createForGateway( + $this->pos, + '5555444433332222', + '21', + '12', + '122', + 'ahmet', + CreditCardInterface::CARD_TYPE_VISA + ); } /** @@ -134,6 +147,37 @@ public function testSetTestMode(): void $this->assertTrue($this->pos->isTestMode()); } + /** + * @testWith [true, "3d", "https://vpostest.qnbfinansbank.com/Gateway/Default.aspx"] + * [false, "3d", "https://vpostest.qnbfinansbank.com/Gateway/Default.aspx"] + * [false, "3d_host", "https://vpostest.qnbfinansbank.com/Gateway/3DHost.aspx"] + */ + public function testGet3DFormData( + bool $isWithCard, + string $paymentModel, + string $gatewayUrl + ): void { + $card = $isWithCard ? $this->card : null; + $order = ['id' => '124']; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('create3DFormData') + ->with( + $this->pos->getAccount(), + $order, + $paymentModel, + $txType, + $gatewayUrl, + $card + ) + ->willReturn(['formData']); + + $actual = $this->pos->get3DFormData($order, $paymentModel, $txType, $card); + + $this->assertSame(['formData'], $actual); + } + /** * @dataProvider make3DPaymentDataProvider */ @@ -219,6 +263,361 @@ public function testMake3DPayment( $this->assertSame($isSuccess, $this->pos->isSuccess()); } + /** + * @return void + */ + public function testMake3DPayPayment(): void + { + $this->cryptMock->expects(self::never()) + ->method('check3DHash'); + + $responseData = ['$responseData']; + $request = Request::create('', 'POST', $responseData); + $order = ['id' => '123']; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->responseMapperMock->expects(self::once()) + ->method('map3DPayResponseData') + ->with($request->request->all(), $txType, $order) + ->willReturn(['status' => 'approved']); + + $pos = $this->pos; + + $pos->make3DPayPayment($request, $order, $txType); + + $result = $pos->getResponse(); + $this->assertSame(['status' => 'approved'], $result); + $this->assertTrue($pos->isSuccess()); + } + + /** + * @return void + */ + public function testMake3DHostPayment(): void + { + $this->cryptMock->expects(self::never()) + ->method('check3DHash'); + + $responseData = ['$responseData']; + $request = Request::create('', 'POST', $responseData); + $order = ['id' => '123']; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->responseMapperMock->expects(self::once()) + ->method('map3DHostResponseData') + ->with($request->request->all(), $txType, $order) + ->willReturn(['status' => 'approved']); + + $pos = $this->pos; + + $pos->make3DHostPayment($request, $order, $txType); + + $result = $pos->getResponse(); + $this->assertSame(['status' => 'approved'], $result); + $this->assertTrue($pos->isSuccess()); + } + + /** + * @dataProvider makeRegularPaymentDataProvider + */ + public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $card = $this->card; + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePaymentRequestData') + ->with($account, $order, $txType, $card) + ->willReturn(['createNonSecurePaymentRequestData']); + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPayment($order, $card, $txType); + } + + /** + * @dataProvider makeRegularPostAuthPaymentDataProvider + */ + public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePostAuthPaymentRequestData') + ->with($account, $order) + ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPostPayment($order); + } + + + /** + * @dataProvider statusRequestDataProvider + */ + public function testStatusRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + + $this->requestMapperMock->expects(self::once()) + ->method('createStatusRequestData') + ->with($account, $order) + ->willReturn(['createStatusRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createStatusRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + ] + ); + + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapStatusResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->status($order); + } + + /** + * @dataProvider cancelRequestDataProvider + */ + public function testCancelRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + + $this->requestMapperMock->expects(self::once()) + ->method('createCancelRequestData') + ->with($account, $order) + ->willReturn(['createCancelRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createCancelRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapCancelResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->cancel($order); + } + + /** + * @dataProvider refundRequestDataProvider + */ + public function testRefundRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + + $this->requestMapperMock->expects(self::once()) + ->method('createRefundRequestData') + ->with($account, $order) + ->willReturn(['createRefundRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createRefundRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapRefundResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->refund($order); + } + + /** + * @dataProvider historyRequestDataProvider + */ + public function testHistoryRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_HISTORY; + + $this->requestMapperMock->expects(self::once()) + ->method('createHistoryRequestData') + ->with($account, $order) + ->willReturn(['createHistoryRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createHistoryRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapHistoryResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->history($order); + } + + /** + * @dataProvider orderHistoryRequestDataProvider + */ + public function testOrderHistoryRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + + $this->requestMapperMock->expects(self::once()) + ->method('createOrderHistoryRequestData') + ->with($account, $order) + ->willReturn(['createOrderHistoryRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createOrderHistoryRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapOrderHistoryResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->orderHistory($order); + } + public static function make3DPaymentDataProvider(): array { return [ @@ -251,4 +650,96 @@ public static function make3DPaymentDataProvider(): array ], ]; } + + public static function makeRegularPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'api_url' => 'https://vpostest.qnbfinansbank.com/Gateway/XMLGate.aspx', + ], + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, + 'api_url' => 'https://vpostest.qnbfinansbank.com/Gateway/XMLGate.aspx', + ], + ]; + } + + public static function makeRegularPostAuthPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://vpostest.qnbfinansbank.com/Gateway/XMLGate.aspx', + ], + ]; + } + + public static function statusRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://vpostest.qnbfinansbank.com/Gateway/XMLGate.aspx', + ], + ]; + } + + public static function cancelRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://vpostest.qnbfinansbank.com/Gateway/XMLGate.aspx', + ], + ]; + } + + public static function refundRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://vpostest.qnbfinansbank.com/Gateway/XMLGate.aspx', + ], + ]; + } + + public static function historyRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://vpostest.qnbfinansbank.com/Gateway/XMLGate.aspx', + ], + ]; + } + + public static function orderHistoryRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://vpostest.qnbfinansbank.com/Gateway/XMLGate.aspx', + ], + ]; + } } diff --git a/tests/Unit/Gateways/PosNetTest.php b/tests/Unit/Gateways/PosNetTest.php index bd6a230a..2b1f2f82 100644 --- a/tests/Unit/Gateways/PosNetTest.php +++ b/tests/Unit/Gateways/PosNetTest.php @@ -14,6 +14,8 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PosNetAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Exceptions\UnsupportedPaymentModelException; +use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\PosNet; @@ -334,6 +336,249 @@ public function testMake3DPayment( $this->assertSame($isSuccess, $this->pos->isSuccess()); } + public function testMake3DHostPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DHostPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + + public function testMake3DPayPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DPayPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + + /** + * @dataProvider makeRegularPaymentDataProvider + */ + public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $card = $this->card; + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePaymentRequestData') + ->with($account, $order, $txType, $card) + ->willReturn(['createNonSecurePaymentRequestData']); + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'xmldata=request-body', + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + ], + ); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPayment($order, $card, $txType); + } + + /** + * @dataProvider makeRegularPostAuthPaymentDataProvider + */ + public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePostAuthPaymentRequestData') + ->with($account, $order) + ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'xmldata=request-body', + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + ], + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPostPayment($order); + } + + + /** + * @dataProvider statusRequestDataProvider + */ + public function testStatusRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + + $this->requestMapperMock->expects(self::once()) + ->method('createStatusRequestData') + ->with($account, $order) + ->willReturn(['createStatusRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createStatusRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'xmldata=request-body', + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + ], + ); + + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapStatusResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->status($order); + } + + /** + * @dataProvider cancelRequestDataProvider + */ + public function testCancelRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + + $this->requestMapperMock->expects(self::once()) + ->method('createCancelRequestData') + ->with($account, $order) + ->willReturn(['createCancelRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createCancelRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'xmldata=request-body', + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + ], + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapCancelResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->cancel($order); + } + + /** + * @dataProvider refundRequestDataProvider + */ + public function testRefundRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + + $this->requestMapperMock->expects(self::once()) + ->method('createRefundRequestData') + ->with($account, $order) + ->willReturn(['createRefundRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createRefundRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'xmldata=request-body', + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + ], + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapRefundResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->refund($order); + } + + public function testHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->history([]); + } + + public function testOrderHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->orderHistory([]); + } + public static function make3DPaymentDataProvider(): array { $resolveMerchantResponseData = [ @@ -343,7 +588,7 @@ public static function make3DPaymentDataProvider(): array ]; return [ - 'auth_fail' => [ + 'auth_fail' => [ 'order' => PosNetResponseDataMapperTest::threeDPaymentDataProvider()['auth_fail1']['order'], 'txType' => PosNetResponseDataMapperTest::threeDPaymentDataProvider()['auth_fail1']['txType'], 'request' => Request::create('', 'POST', $resolveMerchantResponseData), @@ -363,7 +608,7 @@ public static function make3DPaymentDataProvider(): array 'is3DSuccess' => false, 'isSuccess' => false, ], - 'success' => [ + 'success' => [ 'order' => PosNetResponseDataMapperTest::threeDPaymentDataProvider()['success1']['order'], 'txType' => PosNetResponseDataMapperTest::threeDPaymentDataProvider()['success1']['txType'], 'request' => Request::create('', 'POST', $resolveMerchantResponseData), @@ -375,4 +620,96 @@ public static function make3DPaymentDataProvider(): array ], ]; } + + public static function makeRegularPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', + ], + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, + 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', + ], + ]; + } + + public static function makeRegularPostAuthPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', + ], + ]; + } + + public static function statusRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', + ], + ]; + } + + public static function cancelRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', + ], + ]; + } + + public static function refundRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', + ], + ]; + } + + public static function historyRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', + ], + ]; + } + + public static function orderHistoryRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', + ], + ]; + } } diff --git a/tests/Unit/Gateways/PosNetV1PosTest.php b/tests/Unit/Gateways/PosNetV1PosTest.php index bbfd3be0..3f117055 100644 --- a/tests/Unit/Gateways/PosNetV1PosTest.php +++ b/tests/Unit/Gateways/PosNetV1PosTest.php @@ -11,6 +11,8 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PosNetAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Exceptions\UnsupportedPaymentModelException; +use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\PosNetV1Pos; @@ -137,6 +139,35 @@ public function testGetApiURL(string $txType, string $mappedTxType, string $expe $this->assertSame($expected, $this->pos->getApiURL($txType)); } + /** + * @testWith [true] + * [false] + */ + public function testGet3DFormData( + bool $isWithCard + ): void { + $card = $isWithCard ? $this->card : null; + $order = ['id' => '124']; + $paymentModel = PosInterface::MODEL_3D_SECURE; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('create3DFormData') + ->with( + $this->pos->getAccount(), + $order, + $paymentModel, + $txType, + 'https://epostest.albarakaturk.com.tr/ALBSecurePaymentUI/SecureProcess/SecureVerification.aspx', + $card + ) + ->willReturn(['formData']); + + $actual = $this->pos->get3DFormData($order, $paymentModel, $txType, $card); + + $this->assertSame(['formData'], $actual); + } + /** * @dataProvider make3DPaymentDataProvider */ @@ -220,6 +251,249 @@ public function testMake3DPayment( $this->assertSame($isSuccess, $this->pos->isSuccess()); } + public function testMake3DHostPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DHostPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + + public function testMake3DPayPayment(): void + { + $request = Request::create('', 'POST'); + + $this->expectException(UnsupportedPaymentModelException::class); + $this->pos->make3DPayPayment($request, [], PosInterface::TX_TYPE_PAY_AUTH); + } + + /** + * @dataProvider makeRegularPaymentDataProvider + */ + public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $card = $this->card; + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePaymentRequestData') + ->with($account, $order, $txType, $card) + ->willReturn(['createNonSecurePaymentRequestData']); + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'application/json', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPayment($order, $card, $txType); + } + + /** + * @dataProvider makeRegularPostAuthPaymentDataProvider + */ + public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + + $this->requestMapperMock->expects(self::once()) + ->method('createNonSecurePostAuthPaymentRequestData') + ->with($account, $order) + ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'application/json', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['paymentResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapPaymentResponse') + ->with(['paymentResponse'], $txType, $order) + ->willReturn(['result']); + + $this->pos->makeRegularPostPayment($order); + } + + + /** + * @dataProvider statusRequestDataProvider + */ + public function testStatusRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + + $this->requestMapperMock->expects(self::once()) + ->method('createStatusRequestData') + ->with($account, $order) + ->willReturn(['createStatusRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createStatusRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'application/json', + ], + ] + ); + + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapStatusResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->status($order); + } + + /** + * @dataProvider cancelRequestDataProvider + */ + public function testCancelRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + + $this->requestMapperMock->expects(self::once()) + ->method('createCancelRequestData') + ->with($account, $order) + ->willReturn(['createCancelRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createCancelRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'application/json', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapCancelResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->cancel($order); + } + + /** + * @dataProvider refundRequestDataProvider + */ + public function testRefundRequest(array $order, string $apiUrl): void + { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + + $this->requestMapperMock->expects(self::once()) + ->method('createRefundRequestData') + ->with($account, $order) + ->willReturn(['createRefundRequestData']); + + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with(['createRefundRequestData'], $txType) + ->willReturn('request-body'); + + $this->prepareClient( + $this->httpClientMock, + 'response-body', + $apiUrl, + [ + 'body' => 'request-body', + 'headers' => [ + 'Content-Type' => 'application/json', + ], + ] + ); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with('response-body', $txType) + ->willReturn(['decodedResponse']); + + $this->responseMapperMock->expects(self::once()) + ->method('mapRefundResponse') + ->with(['decodedResponse']) + ->willReturn(['result']); + + $this->pos->refund($order); + } + + public function testHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->history([]); + } + + public function testOrderHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->orderHistory([]); + } + public static function make3dPaymentTestProvider(): iterable { $dataSamples = iterator_to_array(PosNetV1PosResponseDataMapperTest::threeDPaymentDataProvider()); @@ -295,4 +569,72 @@ public static function make3DPaymentDataProvider(): array ], ]; } + + public static function makeRegularPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'api_url' => 'https://epostest.albarakaturk.com.tr/ALBMerchantService/MerchantJSONAPI.svc', + ], + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'txType' => PosInterface::TX_TYPE_PAY_PRE_AUTH, + 'api_url' => 'https://epostest.albarakaturk.com.tr/ALBMerchantService/MerchantJSONAPI.svc', + ], + ]; + } + + public static function makeRegularPostAuthPaymentDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://epostest.albarakaturk.com.tr/ALBMerchantService/MerchantJSONAPI.svc', + ], + ]; + } + + public static function statusRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://epostest.albarakaturk.com.tr/ALBMerchantService/MerchantJSONAPI.svc', + ], + ]; + } + + public static function cancelRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://epostest.albarakaturk.com.tr/ALBMerchantService/MerchantJSONAPI.svc', + ], + ]; + } + + public static function refundRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'id' => '2020110828BC', + ], + 'api_url' => 'https://epostest.albarakaturk.com.tr/ALBMerchantService/MerchantJSONAPI.svc', + ], + ]; + } } diff --git a/tests/Unit/Gateways/ToslaPosTest.php b/tests/Unit/Gateways/ToslaPosTest.php index 2793d7fa..1a98b632 100644 --- a/tests/Unit/Gateways/ToslaPosTest.php +++ b/tests/Unit/Gateways/ToslaPosTest.php @@ -13,6 +13,7 @@ use Mews\Pos\Entity\Card\CreditCardInterface; use Mews\Pos\Event\RequestDataPreparedEvent; use Mews\Pos\Exceptions\UnsupportedPaymentModelException; +use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\ToslaPos; @@ -157,13 +158,14 @@ public function testGet3DHostGatewayURL(): void * @dataProvider make3DPayPaymentDataProvider */ public function testMake3DPayPayment( - array $order, - string $txType, + array $order, + string $txType, Request $request, - array $expectedResponse, - bool $is3DSuccess, - bool $isSuccess - ): void { + array $expectedResponse, + bool $is3DSuccess, + bool $isSuccess + ): void + { if ($is3DSuccess) { $this->cryptMock->expects(self::once()) ->method('check3DHash') @@ -195,12 +197,12 @@ public function testMake3DPayPayment( * @dataProvider make3DPayPaymentDataProvider */ public function testMake3DHostPayment( - array $order, - string $txType, + array $order, + string $txType, Request $request, - array $expectedResponse, - bool $is3DSuccess, - bool $isSuccess + array $expectedResponse, + bool $is3DSuccess, + bool $isSuccess ): void { if ($is3DSuccess) { @@ -221,7 +223,7 @@ public function testMake3DHostPayment( ->willReturn($is3DSuccess); $this->responseMapperMock->expects(self::once()) - ->method('map3DPayResponseData') + ->method('map3DHostResponseData') ->willReturn($expectedResponse); $this->pos->make3DHostPayment($request, $order, $txType); @@ -252,7 +254,8 @@ public function testGet3DFormData( string $encodedRequestData, string $responseData, array $decodedResponseData, - array $formData + array $formData, + string $gatewayUrl ): void { @@ -278,7 +281,7 @@ public function testGet3DFormData( $decodedResponseData, $paymentModel, $txType, - 'https://ent.akodepos.com/api/Payment/ProcessCardForm', + $gatewayUrl, $card ) ->willReturn($formData); @@ -418,6 +421,11 @@ public function testRefund( $this->assertSame($result, $mappedResponse); } + public function testHistoryRequest(): void + { + $this->expectException(UnsupportedTransactionTypeException::class); + $this->pos->history([]); + } /** * @dataProvider orderHistoryDataProvider @@ -516,7 +524,7 @@ public static function orderHistoryDataProvider(): iterable public static function threeDFormDataProvider(): iterable { - yield [ + yield '3d_pay' => [ 'order' => ToslaPosRequestDataMapperTest::paymentRegisterRequestDataProvider()[0]['order'], 'paymentModel' => PosInterface::MODEL_3D_PAY, 'txType' => PosInterface::TX_TYPE_PAY_AUTH, @@ -526,6 +534,20 @@ public static function threeDFormDataProvider(): iterable 'responseData' => ToslaPosSerializerTest::decodeDataProvider()['payment_register']['input'], 'decodedResponseData' => ToslaPosSerializerTest::decodeDataProvider()['payment_register']['decoded'], 'formData' => ToslaPosRequestDataMapperTest::threeDFormDataProvider()['3d_pay_form_data']['expected'], + 'gateway_url' => 'https://ent.akodepos.com/api/Payment/ProcessCardForm', + ]; + + yield '3d_host' => [ + 'order' => ToslaPosRequestDataMapperTest::paymentRegisterRequestDataProvider()[0]['order'], + 'paymentModel' => PosInterface::MODEL_3D_HOST, + 'txType' => PosInterface::TX_TYPE_PAY_AUTH, + 'isWithCard' => false, + 'requestData' => ToslaPosRequestDataMapperTest::paymentRegisterRequestDataProvider()[0]['expected'], + 'encodedRequestData' => \json_encode(ToslaPosRequestDataMapperTest::statusRequestDataProvider()[0]['expected'], JSON_THROW_ON_ERROR), + 'responseData' => ToslaPosSerializerTest::decodeDataProvider()['payment_register']['input'], + 'decodedResponseData' => ToslaPosSerializerTest::decodeDataProvider()['payment_register']['decoded'], + 'formData' => ToslaPosRequestDataMapperTest::threeDFormDataProvider()['3d_pay_form_data']['expected'], + 'gateway_url' => 'https://ent.akodepos.com/api/Payment/threeDSecure/PA49E341381C94587AB4CB196DAC10DC02E509578520E4471A3EEE2BB4830AE4F', ]; } @@ -643,7 +665,7 @@ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): vo public function testStatusRequest(array $order, string $apiUrl): void { $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_STATUS; + $txType = PosInterface::TX_TYPE_STATUS; $this->requestMapperMock->expects(self::once()) ->method('createStatusRequestData') @@ -687,7 +709,7 @@ public function testStatusRequest(array $order, string $apiUrl): void public function testCancelRequest(array $order, string $apiUrl): void { $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_CANCEL; + $txType = PosInterface::TX_TYPE_CANCEL; $this->requestMapperMock->expects(self::once()) ->method('createCancelRequestData') @@ -730,7 +752,7 @@ public function testCancelRequest(array $order, string $apiUrl): void public function testRefundRequest(array $order, string $apiUrl): void { $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_REFUND; + $txType = PosInterface::TX_TYPE_REFUND; $this->requestMapperMock->expects(self::once()) ->method('createRefundRequestData') @@ -773,7 +795,7 @@ public function testRefundRequest(array $order, string $apiUrl): void public function testOrderHistoryRequest(array $order, string $apiUrl): void { $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $txType = PosInterface::TX_TYPE_ORDER_HISTORY; $this->requestMapperMock->expects(self::once()) ->method('createOrderHistoryRequestData') @@ -867,7 +889,6 @@ public static function getApiUrlDataProvider(): array } - public static function makeRegularPaymentDataProvider(): array { return [