diff --git a/README.md b/README.md index e3766135..482009c5 100644 --- a/README.md +++ b/README.md @@ -257,26 +257,7 @@ $pos = \Mews\Pos\Factory\PosFactory::createPosGateway( ## Genel Kultur -### NonSecure, 3D Secure, 3DPay ve 3DHost ödeme modeller arasındaki farklar - -- **3DSecure** - Bankaya göre farklı isimler verilebilir, örn. 3D Full. - Gateway'den (3D şifre girdiginiz sayfadan) döndükten sonra ödemeyi tamamlamak - için - banka gateway'ne 1 istek daha (_provizyon_ isteği) gönderir. - Bu isteği göndermeden ödeme **tamamlanmaz**. -- **3DPay** - Bankaya göre farklı isimler verilebilir, örn. 3D Half. - Gateway'den (3D şifre girdiginiz sayfadan) döndükten sonra ödeme bitmiş - sayılır. - 3DSecure ödemede yapıldığı gibi ekstra provizyon istek gönderilmez. -- **3DHost** - Kredi kart girişi için kullanıcı bankanın sayfasına yönledirilir, - kredi kart bilgileri girdikten sonra bankanın 3D gateway sayfasına - yönlendirilir, - ordan da websitenize geri yönlendirilir. Yönlendirme sonucunda ödeme - tamanlanmış olur. -- **NonSecure** - Ödeme işlemi kullanıcı 3D onay işlemi yapmadan gerçekleşir. -- **NonSecure, 3DSecure ve 3DPay** - Ödemede kredi kart bilgisi websiteniz - tarafından alınır. - **3DHost** ödemede ise banka websayfasından alınır. +Ödeme modelleri hakkında bilgi edinmek istiyorsanız [bu makaleyi](https://medium.com/p/fa5cd016999c) inceleyebilirsiniz. ### Otorizasyon, Ön Otorizasyon, Ön Provizyon Kapama İşlemler arasındaki farklar diff --git a/docs/PRE-AUTH-POST-EXAMPLE.md b/docs/PRE-AUTH-POST-EXAMPLE.md index 58017fd7..2e84b65a 100644 --- a/docs/PRE-AUTH-POST-EXAMPLE.md +++ b/docs/PRE-AUTH-POST-EXAMPLE.md @@ -236,8 +236,8 @@ $order = createPostPayOrder( /** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */ $eventDispatcher->addListener( \Mews\Pos\Event\RequestDataPreparedEvent::class, - function (\Mews\Pos\Event\RequestDataPreparedEvent $event) use ($gatewayClass, $preAuthAmount, $postAuthAmount) { - if (\Mews\Pos\Gateways\EstPos::class === $gatewayClass || \Mews\Pos\Gateways\EstV3Pos::class === $gatewayClass) { + function (\Mews\Pos\Event\RequestDataPreparedEvent $event) use ($preAuthAmount, $postAuthAmount) { + if (\Mews\Pos\Gateways\EstPos::class === $event->getGatewayClass() || \Mews\Pos\Gateways\EstV3Pos::class === $event->getGatewayClass()) { // ($preAuthAmount < $postAuthAmount) durumda API isteğe ekstra değerler eklenmesi gerekiyor. if ($preAuthAmount < $postAuthAmount) { $requestData = $event->getRequestData(); diff --git a/docs/THREED-PAYMENT-EXAMPLE.md b/docs/THREED-PAYMENT-EXAMPLE.md index 0f69b01d..3ed6b27c 100644 --- a/docs/THREED-PAYMENT-EXAMPLE.md +++ b/docs/THREED-PAYMENT-EXAMPLE.md @@ -150,8 +150,8 @@ try { * Bu Event'i dinleyerek 3D formun hash verisi hesaplanmadan önce formun input array içireğini güncelleyebilirsiniz. * Eğer ekleyeceğiniz veri hash hesaplamada kullanılmıyorsa Form verisi oluştuktan sonra da güncelleyebilirsiniz. */ - $eventDispatcher->addListener(Before3DFormHashCalculatedEvent::class, function (Before3DFormHashCalculatedEvent $event) use ($pos): void { - if (get_class($pos) === \Mews\Pos\Gateways\EstPos::class || get_class($pos) === \Mews\Pos\Gateways\EstV3Pos::class) { + $eventDispatcher->addListener(Before3DFormHashCalculatedEvent::class, function (Before3DFormHashCalculatedEvent $event): void { + if ($event->getGatewayClass() === \Mews\Pos\Gateways\EstPos::class || $event->getGatewayClass() === \Mews\Pos\Gateways\EstV3Pos::class) { /** * Örnek 1: İşbank İmece Kart ile ödeme yaparken aşağıdaki verilerin eklenmesi gerekiyor: $supportedPaymentModels = [ @@ -179,8 +179,8 @@ try { // KuveytVos TDV2.0.0 icin ozel biri durum $eventDispatcher->addListener( RequestDataPreparedEvent::class, - function (RequestDataPreparedEvent $requestDataPreparedEvent) use ($pos): void { - if (get_class($pos) !== \Mews\Pos\Gateways\KuveytPos::class) { + function (RequestDataPreparedEvent $requestDataPreparedEvent): void { + if ($event->getGatewayClass() !== \Mews\Pos\Gateways\KuveytPos::class) { return; } // KuveytPos TDV2.0.0 icin zorunlu eklenmesi gereken ekstra alanlar: diff --git a/examples/_common-codes/3d/form.php b/examples/_common-codes/3d/form.php index 1d6e8eab..fe85ea77 100644 --- a/examples/_common-codes/3d/form.php +++ b/examples/_common-codes/3d/form.php @@ -69,8 +69,8 @@ // KuveytVos TDV2.0.0 icin ozel biri durum $eventDispatcher->addListener( RequestDataPreparedEvent::class, - function (RequestDataPreparedEvent $requestDataPreparedEvent) use ($pos): void { - if (get_class($pos) !== \Mews\Pos\Gateways\KuveytPos::class) { + function (RequestDataPreparedEvent $requestDataPreparedEvent): void { + if ($requestDataPreparedEvent->getGatewayClass() !== \Mews\Pos\Gateways\KuveytPos::class) { return; } // KuveytPos TDV2.0.0 icin zorunlu eklenmesi gereken ekstra alanlar: @@ -135,8 +135,8 @@ function (RequestDataPreparedEvent $requestDataPreparedEvent) use ($pos): void { * Bu Event'i dinleyerek 3D formun hash verisi hesaplanmadan önce formun input array içireğini güncelleyebilirsiniz. * Eger ekleyeceginiz veri hash hesaplamada kullanilmiyorsa form verisi olusturduktan sonra da ekleyebilirsiniz. */ - $eventDispatcher->addListener(Before3DFormHashCalculatedEvent::class, function (Before3DFormHashCalculatedEvent $event) use ($pos): void { - if (get_class($pos) === \Mews\Pos\Gateways\EstPos::class || get_class($pos) === \Mews\Pos\Gateways\EstV3Pos::class) { + $eventDispatcher->addListener(Before3DFormHashCalculatedEvent::class, function (Before3DFormHashCalculatedEvent $event): void { + if ($event->getGatewayClass() === \Mews\Pos\Gateways\EstPos::class || $event->getGatewayClass() === \Mews\Pos\Gateways\EstV3Pos::class) { //Örnek 1: İşbank İmece Kart ile ödeme yaparken aşağıdaki verilerin eklenmesi gerekiyor: // $supportedPaymentModels = [ // \Mews\Pos\PosInterface::MODEL_3D_PAY, @@ -150,7 +150,7 @@ function (RequestDataPreparedEvent $requestDataPreparedEvent) use ($pos): void { // $event->setFormInputs($formInputs); // } } - if (get_class($pos) === \Mews\Pos\Gateways\EstV3Pos::class) { + if ($event->getGatewayClass() === \Mews\Pos\Gateways\EstV3Pos::class) { // // Örnek 2: callbackUrl eklenmesi // $formInputs = $event->getFormInputs(); // $formInputs['callbackUrl'] = $formInputs['failUrl']; diff --git a/examples/_common-codes/regular/post-auth.php b/examples/_common-codes/regular/post-auth.php index 1f5fe00d..eca671a6 100644 --- a/examples/_common-codes/regular/post-auth.php +++ b/examples/_common-codes/regular/post-auth.php @@ -46,8 +46,8 @@ function createPostPayOrder(string $gatewayClass, array $lastResponse, string $i /** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */ $eventDispatcher->addListener( \Mews\Pos\Event\RequestDataPreparedEvent::class, - function (\Mews\Pos\Event\RequestDataPreparedEvent $event) use ($gatewayClass, $preAuthAmount, $postAuthAmount) { - if (\Mews\Pos\Gateways\EstPos::class === $gatewayClass || \Mews\Pos\Gateways\EstV3Pos::class === $gatewayClass) { + function (\Mews\Pos\Event\RequestDataPreparedEvent $event) use ($preAuthAmount, $postAuthAmount) { + if (\Mews\Pos\Gateways\EstPos::class === $event->getGatewayClass() || \Mews\Pos\Gateways\EstV3Pos::class === $event->getGatewayClass()) { if ($preAuthAmount < $postAuthAmount) { $requestData = $event->getRequestData(); $requestData['Extra']['PREAMT'] = $preAuthAmount; diff --git a/examples/_templates/_finish_non_secure_payment.php b/examples/_templates/_finish_non_secure_payment.php index e0957e2b..ffb83fac 100644 --- a/examples/_templates/_finish_non_secure_payment.php +++ b/examples/_templates/_finish_non_secure_payment.php @@ -22,7 +22,7 @@ // OZEL DURUMLAR ICIN KODLAR START // ============================================================================================ /** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */ -$eventDispatcher->addListener(RequestDataPreparedEvent::class, function (RequestDataPreparedEvent $event) use ($pos) { +$eventDispatcher->addListener(RequestDataPreparedEvent::class, function (RequestDataPreparedEvent $event) { // Burda istek banka API'na gonderilmeden once gonderilecek veriyi degistirebilirsiniz. // Ornek: // $data = $event->getRequestData(); @@ -38,13 +38,13 @@ * 5: Ekstre Erteleme * 6: Özel Vade Farkı */ - if ($pos instanceof \Mews\Pos\Gateways\PosNetV1Pos) { + if ($event->getGatewayClass() instanceof \Mews\Pos\Gateways\PosNetV1Pos) { // Albaraka PosNet KOICode ekleme // $data = $event->getRequestData(); // $data['KOICode'] = '1'; // $event->setRequestData($data); } - if ($pos instanceof \Mews\Pos\Gateways\PosNet) { + if ($event->getGatewayClass() instanceof \Mews\Pos\Gateways\PosNet) { // Yapikredi PosNet KOICode ekleme // $data = $event->getRequestData(); // $data['sale']['koiCode'] = '1'; diff --git a/examples/_templates/_payment_secure_response.php b/examples/_templates/_payment_secure_response.php index 80b80fd7..d57328bc 100644 --- a/examples/_templates/_payment_secure_response.php +++ b/examples/_templates/_payment_secure_response.php @@ -36,7 +36,7 @@ // OZEL DURUMLAR ICIN KODLAR START // ============================================================================================ /** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */ -$eventDispatcher->addListener(RequestDataPreparedEvent::class, function (RequestDataPreparedEvent $event) use ($pos) { +$eventDispatcher->addListener(RequestDataPreparedEvent::class, function (RequestDataPreparedEvent $event) { // Burda istek banka API'na gonderilmeden once gonderilecek veriyi degistirebilirsiniz. // Ornek: // $data = $event->getRequestData(); @@ -56,7 +56,7 @@ * 5: Ekstre Erteleme * 6: Özel Vade Farkı */ - if ($pos instanceof \Mews\Pos\Gateways\PosNetV1Pos && $event->getTxType() === PosInterface::TX_TYPE_PAY_AUTH) { + if ($event->getGatewayClass() instanceof \Mews\Pos\Gateways\PosNetV1Pos && $event->getTxType() === PosInterface::TX_TYPE_PAY_AUTH) { // Albaraka PosNet KOICode ekleme // $data = $event->getRequestData(); // $data['KOICode'] = '1'; diff --git a/src/DataMapper/RequestDataMapper/AkbankPosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/AkbankPosRequestDataMapper.php index c5edcd69..2d2fe6ed 100644 --- a/src/DataMapper/RequestDataMapper/AkbankPosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/AkbankPosRequestDataMapper.php @@ -11,6 +11,7 @@ use Mews\Pos\Entity\Card\CreditCardInterface; use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\NotImplementedException; +use Mews\Pos\Gateways\AkbankPos; use Mews\Pos\PosInterface; /** @@ -394,7 +395,8 @@ public function create3DFormData(AbstractPosAccount $posAccount, array $order, s $data['inputs'], $posAccount->getBank(), $txType, - $paymentModel + $paymentModel, + AkbankPos::class ); $this->eventDispatcher->dispatch($event); $data['inputs'] = $event->getFormInputs(); diff --git a/src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php index f7d951ea..04806f59 100644 --- a/src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php @@ -10,6 +10,7 @@ use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\NotImplementedException; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; +use Mews\Pos\Gateways\EstPos; use Mews\Pos\PosInterface; /** @@ -243,7 +244,13 @@ public function create3DFormData(AbstractPosAccount $posAccount, array $order, s $data = $this->create3DFormDataCommon($posAccount, $preparedOrder, $paymentModel, $txType, $gatewayURL, $creditCard); - $event = new Before3DFormHashCalculatedEvent($data['inputs'], $posAccount->getBank(), $txType, $paymentModel); + $event = new Before3DFormHashCalculatedEvent( + $data['inputs'], + $posAccount->getBank(), + $txType, + $paymentModel, + EstPos::class + ); $this->eventDispatcher->dispatch($event); $data['inputs'] = $event->getFormInputs(); diff --git a/src/DataMapper/RequestDataMapper/EstV3PosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/EstV3PosRequestDataMapper.php index bff77f61..2c61d03e 100644 --- a/src/DataMapper/RequestDataMapper/EstV3PosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/EstV3PosRequestDataMapper.php @@ -7,6 +7,7 @@ use Mews\Pos\Entity\Account\AbstractPosAccount; use Mews\Pos\Entity\Card\CreditCardInterface; use Mews\Pos\Event\Before3DFormHashCalculatedEvent; +use Mews\Pos\Gateways\EstV3Pos; /** * Creates request data for EstPos Gateway requests that supports v3 Hash algorithm @@ -27,7 +28,13 @@ public function create3DFormData(AbstractPosAccount $posAccount, array $order, s $data['inputs']['hashAlgorithm'] = 'ver3'; - $event = new Before3DFormHashCalculatedEvent($data['inputs'], $posAccount->getBank(), $txType, $paymentModel); + $event = new Before3DFormHashCalculatedEvent( + $data['inputs'], + $posAccount->getBank(), + $txType, + $paymentModel, + EstV3Pos::class + ); $this->eventDispatcher->dispatch($event); $data['inputs'] = $event->getFormInputs(); diff --git a/src/DataMapper/RequestDataMapper/GarantiPosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/GarantiPosRequestDataMapper.php index c14790b5..4d930394 100644 --- a/src/DataMapper/RequestDataMapper/GarantiPosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/GarantiPosRequestDataMapper.php @@ -12,6 +12,7 @@ use Mews\Pos\Entity\Card\CreditCardInterface; use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\NotImplementedException; +use Mews\Pos\Gateways\GarantiPos; use Mews\Pos\PosInterface; /** @@ -359,7 +360,13 @@ public function create3DFormData(AbstractPosAccount $posAccount, array $order, s $inputs['cardcvv2'] = $creditCard->getCvv(); } - $event = new Before3DFormHashCalculatedEvent($inputs, $posAccount->getBank(), $txType, $paymentModel); + $event = new Before3DFormHashCalculatedEvent( + $inputs, + $posAccount->getBank(), + $txType, + $paymentModel, + GarantiPos::class + ); $this->eventDispatcher->dispatch($event); $inputs = $event->getFormInputs(); diff --git a/src/DataMapper/RequestDataMapper/InterPosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/InterPosRequestDataMapper.php index cb2de011..b64e9c7b 100644 --- a/src/DataMapper/RequestDataMapper/InterPosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/InterPosRequestDataMapper.php @@ -9,6 +9,7 @@ use Mews\Pos\Entity\Card\CreditCardInterface; use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\NotImplementedException; +use Mews\Pos\Gateways\InterPos; use Mews\Pos\PosInterface; /** @@ -228,7 +229,13 @@ public function create3DFormData(AbstractPosAccount $posAccount, array $order, s $inputs['Cvv2'] = $creditCard->getCvv(); } - $event = new Before3DFormHashCalculatedEvent($inputs, $posAccount->getBank(), $txType, $paymentModel); + $event = new Before3DFormHashCalculatedEvent( + $inputs, + $posAccount->getBank(), + $txType, + $paymentModel, + InterPos::class + ); $this->eventDispatcher->dispatch($event); $inputs = $event->getFormInputs(); diff --git a/src/DataMapper/RequestDataMapper/PayForPosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/PayForPosRequestDataMapper.php index f95f2d5a..8d60dbcd 100644 --- a/src/DataMapper/RequestDataMapper/PayForPosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/PayForPosRequestDataMapper.php @@ -8,6 +8,7 @@ use Mews\Pos\Entity\Account\AbstractPosAccount; use Mews\Pos\Entity\Card\CreditCardInterface; use Mews\Pos\Event\Before3DFormHashCalculatedEvent; +use Mews\Pos\Gateways\PayForPos; use Mews\Pos\PosInterface; /** @@ -237,7 +238,13 @@ public function create3DFormData(AbstractPosAccount $posAccount, array $order, s $inputs['Cvv2'] = $creditCard->getCvv(); } - $event = new Before3DFormHashCalculatedEvent($inputs, $posAccount->getBank(), $txType, $paymentModel); + $event = new Before3DFormHashCalculatedEvent( + $inputs, + $posAccount->getBank(), + $txType, + $paymentModel, + PayForPos::class + ); $this->eventDispatcher->dispatch($event); $inputs = $event->getFormInputs(); diff --git a/src/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapper.php b/src/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapper.php index c087e039..3c98209e 100644 --- a/src/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapper.php +++ b/src/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapper.php @@ -12,6 +12,7 @@ use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\NotImplementedException; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; +use Mews\Pos\Gateways\PosNetV1Pos; use Mews\Pos\PosInterface; /** @@ -386,7 +387,13 @@ public function create3DFormData(AbstractPosAccount $posAccount, array $order, s $inputs += $cardData; - $event = new Before3DFormHashCalculatedEvent($inputs, $posAccount->getBank(), $txType, $paymentModel); + $event = new Before3DFormHashCalculatedEvent( + $inputs, + $posAccount->getBank(), + $txType, + $paymentModel, + PosNetV1Pos::class + ); $this->eventDispatcher->dispatch($event); $inputs = $event->getFormInputs(); diff --git a/src/DataMapper/ResponseDataMapper/PosNetResponseDataMapper.php b/src/DataMapper/ResponseDataMapper/PosNetResponseDataMapper.php index aefa6a36..bdeff45e 100644 --- a/src/DataMapper/ResponseDataMapper/PosNetResponseDataMapper.php +++ b/src/DataMapper/ResponseDataMapper/PosNetResponseDataMapper.php @@ -107,6 +107,7 @@ public function map3DPaymentData(array $raw3DAuthResponseData, ?array $rawPaymen $defaultResponse['proc_return_code'] = $procReturnCode; $defaultResponse['error_code'] = $raw3DAuthResponseData['respCode']; $defaultResponse['error_message'] = $raw3DAuthResponseData['respText']; + $defaultResponse['3d_all'] = $raw3DAuthResponseData; return $defaultResponse; } diff --git a/src/Event/Before3DFormHashCalculatedEvent.php b/src/Event/Before3DFormHashCalculatedEvent.php index a3ae195b..41276ba8 100644 --- a/src/Event/Before3DFormHashCalculatedEvent.php +++ b/src/Event/Before3DFormHashCalculatedEvent.php @@ -25,21 +25,32 @@ class Before3DFormHashCalculatedEvent /** @var PosInterface::MODEL_3D_* */ private string $paymentModel; + /** @var class-string */ + private string $gatewayClass; + /** * @phpstan-param PosInterface::TX_TYPE_PAY_* $txType * @phpstan-param PosInterface::MODEL_3D_* $paymentModel + * @phpstan-param class-string $gatewayClass * * @param array $formInputs * @param string $bank * @param string $txType * @param string $paymentModel + * @param string $gatewayClass */ - public function __construct(array $formInputs, string $bank, string $txType, string $paymentModel) - { + public function __construct( + array $formInputs, + string $bank, + string $txType, + string $paymentModel, + string $gatewayClass + ) { $this->formInputs = $formInputs; $this->bank = $bank; $this->txType = $txType; $this->paymentModel = $paymentModel; + $this->gatewayClass = $gatewayClass; } /** @@ -85,4 +96,12 @@ public function setFormInputs(array $formInputs): self return $this; } + + /** + * @return class-string + */ + public function getGatewayClass(): string + { + return $this->gatewayClass; + } } diff --git a/src/Event/RequestDataPreparedEvent.php b/src/Event/RequestDataPreparedEvent.php index c48ddfb8..717d197d 100644 --- a/src/Event/RequestDataPreparedEvent.php +++ b/src/Event/RequestDataPreparedEvent.php @@ -16,26 +16,46 @@ class RequestDataPreparedEvent /** @var array */ private array $requestData; + /** @var array */ + private array $order; + private string $bank; /** @var PosInterface::TX_TYPE_* */ private string $txType; + /** @var PosInterface::MODEL_* */ + private string $paymentModel; + + /** @var class-string */ + private string $gatewayClass; + /** - * @phpstan-param PosInterface::TX_TYPE_* $txType + * @phpstan-param PosInterface::TX_TYPE_* $txType + * @phpstan-param PosInterface::MODEL_* $paymentModel + * @phpstan-param class-string $gatewayClass * * @param array $requestData * @param string $bank * @param string $txType + * @param string $gatewayClass + * @param array $order + * @param string $paymentModel */ public function __construct( array $requestData, string $bank, - string $txType + string $txType, + string $gatewayClass, + array $order, + string $paymentModel ) { - $this->requestData = $requestData; - $this->bank = $bank; - $this->txType = $txType; + $this->requestData = $requestData; + $this->bank = $bank; + $this->txType = $txType; + $this->gatewayClass = $gatewayClass; + $this->order = $order; + $this->paymentModel = $paymentModel; } /** @@ -58,6 +78,14 @@ public function setRequestData(array $requestData): self return $this; } + /** + * @return array + */ + public function getOrder(): array + { + return $this->order; + } + /** * @return PosInterface::TX_TYPE_* */ @@ -66,6 +94,14 @@ public function getTxType(): string return $this->txType; } + /** + * @return PosInterface::MODEL_* + */ + public function getPaymentModel(): string + { + return $this->paymentModel; + } + /** * @return string */ @@ -73,4 +109,12 @@ public function getBank(): string { return $this->bank; } + + /** + * @return class-string + */ + public function getGatewayClass(): string + { + return $this->gatewayClass; + } } diff --git a/src/Gateways/AbstractGateway.php b/src/Gateways/AbstractGateway.php index 38746b75..03ccd7f8 100644 --- a/src/Gateways/AbstractGateway.php +++ b/src/Gateways/AbstractGateway.php @@ -230,7 +230,14 @@ public function makeRegularPayment(array $order, CreditCardInterface $creditCard throw new LogicException(\sprintf('Invalid transaction type "%s" provided', $txType)); } - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_NON_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -267,7 +274,14 @@ public function makeRegularPostPayment(array $order): PosInterface $requestData = $this->requestDataMapper->createNonSecurePostAuthPaymentRequestData($this->account, $order); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_NON_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -299,7 +313,14 @@ public function refund(array $order): PosInterface $txType = PosInterface::TX_TYPE_REFUND; $requestData = $this->requestDataMapper->createRefundRequestData($this->account, $order); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_NON_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -335,7 +356,14 @@ public function cancel(array $order): PosInterface $txType = PosInterface::TX_TYPE_CANCEL; $requestData = $this->requestDataMapper->createCancelRequestData($this->account, $order); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_NON_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -371,7 +399,14 @@ public function status(array $order): PosInterface $txType = PosInterface::TX_TYPE_STATUS; $requestData = $this->requestDataMapper->createStatusRequestData($this->account, $order); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_NON_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -403,7 +438,14 @@ public function history(array $data): PosInterface $txType = PosInterface::TX_TYPE_HISTORY; $requestData = $this->requestDataMapper->createHistoryRequestData($this->account, $data); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $data, + PosInterface::MODEL_NON_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -435,7 +477,14 @@ public function orderHistory(array $order): PosInterface $txType = PosInterface::TX_TYPE_ORDER_HISTORY; $requestData = $this->requestDataMapper->createOrderHistoryRequestData($this->account, $order); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_NON_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/AkbankPos.php b/src/Gateways/AkbankPos.php index 9bc5304e..d374bc7f 100644 --- a/src/Gateways/AkbankPos.php +++ b/src/Gateways/AkbankPos.php @@ -84,7 +84,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $request->all()); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/EstPos.php b/src/Gateways/EstPos.php index 4c9fbc0b..8f0b84c6 100644 --- a/src/Gateways/EstPos.php +++ b/src/Gateways/EstPos.php @@ -83,7 +83,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $request->all()); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/GarantiPos.php b/src/Gateways/GarantiPos.php index ce45401f..550ba002 100644 --- a/src/Gateways/GarantiPos.php +++ b/src/Gateways/GarantiPos.php @@ -77,7 +77,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $request->all()); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/InterPos.php b/src/Gateways/InterPos.php index 3371868e..b121a76f 100644 --- a/src/Gateways/InterPos.php +++ b/src/Gateways/InterPos.php @@ -83,7 +83,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $gatewayResponse); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/KuveytPos.php b/src/Gateways/KuveytPos.php index 6e7cfec0..d27a6429 100644 --- a/src/Gateways/KuveytPos.php +++ b/src/Gateways/KuveytPos.php @@ -168,7 +168,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $gatewayResponse); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -314,9 +321,22 @@ private function sendSoapRequest(array $contents, string $txType, string $url): */ private function getCommon3DFormData(KuveytPosAccount $kuveytPosAccount, array $order, string $paymentModel, string $txType, string $gatewayURL, ?CreditCardInterface $creditCard = null): array { - $requestData = $this->requestDataMapper->create3DEnrollmentCheckRequestData($kuveytPosAccount, $order, $paymentModel, $txType, $creditCard); + $requestData = $this->requestDataMapper->create3DEnrollmentCheckRequestData( + $kuveytPosAccount, + $order, + $paymentModel, + $txType, + $creditCard + ); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + $paymentModel + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/PayFlexCPV4Pos.php b/src/Gateways/PayFlexCPV4Pos.php index d7bb5107..9915391a 100644 --- a/src/Gateways/PayFlexCPV4Pos.php +++ b/src/Gateways/PayFlexCPV4Pos.php @@ -88,7 +88,14 @@ public function make3DPayPayment(Request $request, array $order, string $txType) // Burda odemenin basarili olup olmadigini sorguluyoruz. $requestData = $this->requestDataMapper->create3DPaymentStatusRequestData($this->account, $queryParams); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), PosInterface::TX_TYPE_PAY_AUTH); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_PAY + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -238,7 +245,14 @@ private function registerPayment(array $order, string $txType, string $paymentMo $creditCard ); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + $paymentModel + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/PayFlexV4Pos.php b/src/Gateways/PayFlexV4Pos.php index 79285b87..93d38102 100644 --- a/src/Gateways/PayFlexV4Pos.php +++ b/src/Gateways/PayFlexV4Pos.php @@ -78,7 +78,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr // NOT: diger gatewaylerden farkli olarak payflex kredit bilgilerini bu asamada da istiyor. $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $requestData, $creditCard); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -145,7 +152,7 @@ public function get3DFormData(array $order, string $paymentModel, string $txType throw new LogicException('Kredi kartı bilgileri eksik!'); } - $data = $this->sendEnrollmentRequest($order, $creditCard, $txType); + $data = $this->sendEnrollmentRequest($order, $creditCard, $txType, $paymentModel); $status = $data['Message']['VERes']['Status']; /** @@ -186,9 +193,10 @@ protected function send($contents, string $txType, string $paymentModel, string $this->logger->debug('sending request', ['url' => $url]); $isXML = \is_string($contents); - $body = $isXML ? ['form_params' => ['prmstr' => $contents]] : ['form_params' => $contents]; - $response = $this->client->post($url, $body); + $response = $this->client->post($url, [ + 'form_params' => $isXML ? ['prmstr' => $contents] : $contents, + ]); $this->logger->debug('request completed', ['status_code' => $response->getStatusCode()]); return $this->data = $this->serializer->decode($response->getBody()->getContents(), $txType); @@ -199,20 +207,29 @@ protected function send($contents, string $txType, string $paymentModel, string * (Enrollment Status) sorulması, yani kart 3-D Secure programına dâhil mi yoksa değil mi sorgusu * * @phpstan-param PosInterface::TX_TYPE_PAY_AUTH|PosInterface::TX_TYPE_PAY_PRE_AUTH $txType + * @phpstan-param PosInterface::MODEL_3D_* $paymentModel * * @param array $order * @param CreditCardInterface $creditCard * @param string $txType + * @param string $paymentModel * * @return array * * @throws Exception */ - private function sendEnrollmentRequest(array $order, CreditCardInterface $creditCard, string $txType): array + private function sendEnrollmentRequest(array $order, CreditCardInterface $creditCard, string $txType, string $paymentModel): array { $requestData = $this->requestDataMapper->create3DEnrollmentCheckRequestData($this->account, $order, $creditCard); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + $paymentModel + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/PayForPos.php b/src/Gateways/PayForPos.php index e77461e2..bc9d2958 100644 --- a/src/Gateways/PayForPos.php +++ b/src/Gateways/PayForPos.php @@ -77,7 +77,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr // valid ProcReturnCode is V033 in case of success 3D Authentication $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $request->all()); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/PosNet.php b/src/Gateways/PosNet.php index f6e052b5..7a7f2806 100644 --- a/src/Gateways/PosNet.php +++ b/src/Gateways/PosNet.php @@ -69,7 +69,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $request->all() ); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -102,7 +109,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $request->all()); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -153,7 +167,7 @@ public function get3DFormData(array $order, string $paymentModel, string $txType throw new LogicException('Kredi kartı veya sipariş bilgileri eksik!'); } - $data = $this->getOosTransactionData($order, $txType, $creditCard); + $data = $this->getOosTransactionData($order, $txType, $paymentModel, $creditCard); if ($this->responseDataMapper::PROCEDURE_SUCCESS_CODE !== $data['approved']) { $this->logger->error('enrollment fail response', $data); @@ -222,9 +236,11 @@ protected function send($contents, string $txType, string $paymentModel, string * siparis bilgileri ve kart bilgilerinin şifrelendiği adımdır. * * @phpstan-param PosInterface::TX_TYPE_PAY_AUTH|PosInterface::TX_TYPE_PAY_PRE_AUTH $txType + * @phpstan-param PosInterface::MODEL_3D_* $paymentModel * * @param array $order * @param string $txType + * @param string $paymentModel * @param CreditCardInterface $creditCard * * @return array{approved: string, respCode: string, respText: string, oosRequestDataResponse?: array{data1: string, data2: string, sign: string}} @@ -232,11 +248,23 @@ protected function send($contents, string $txType, string $paymentModel, string * @throws UnsupportedTransactionTypeException * @throws ClientExceptionInterface */ - private function getOosTransactionData(array $order, string $txType, CreditCardInterface $creditCard): array + private function getOosTransactionData(array $order, string $txType, string $paymentModel, CreditCardInterface $creditCard): array { - $requestData = $this->requestDataMapper->create3DEnrollmentCheckRequestData($this->account, $order, $txType, $creditCard); + $requestData = $this->requestDataMapper->create3DEnrollmentCheckRequestData( + $this->account, + $order, + $txType, + $creditCard + ); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + $paymentModel + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/PosNetV1Pos.php b/src/Gateways/PosNetV1Pos.php index 44514b15..759f24e1 100644 --- a/src/Gateways/PosNetV1Pos.php +++ b/src/Gateways/PosNetV1Pos.php @@ -89,7 +89,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $request->all()); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/ToslaPos.php b/src/Gateways/ToslaPos.php index 48d67d8f..20770418 100644 --- a/src/Gateways/ToslaPos.php +++ b/src/Gateways/ToslaPos.php @@ -220,7 +220,14 @@ private function registerPayment(array $order, string $paymentModel, string $txT $order ); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + $paymentModel + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/src/Gateways/VakifKatilimPos.php b/src/Gateways/VakifKatilimPos.php index 2bbb57a7..16c67595 100644 --- a/src/Gateways/VakifKatilimPos.php +++ b/src/Gateways/VakifKatilimPos.php @@ -127,7 +127,14 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $requestData = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $gatewayResponse); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + PosInterface::MODEL_3D_SECURE + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ @@ -193,7 +200,14 @@ private function sendEnrollmentRequest(KuveytPosAccount $kuveytPosAccount, array { $requestData = $this->requestDataMapper->create3DEnrollmentCheckRequestData($kuveytPosAccount, $order, $paymentModel, $txType, $creditCard); - $event = new RequestDataPreparedEvent($requestData, $this->account->getBank(), $txType); + $event = new RequestDataPreparedEvent( + $requestData, + $this->account->getBank(), + $txType, + \get_class($this), + $order, + $paymentModel + ); $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ diff --git a/tests/Unit/DataMapper/RequestDataMapper/AkbankPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/AkbankPosRequestDataMapperTest.php index 48526be8..c5d2a8fb 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/AkbankPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/AkbankPosRequestDataMapperTest.php @@ -13,6 +13,7 @@ use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; +use Mews\Pos\Gateways\AkbankPos; use Mews\Pos\PosInterface; use Mews\Pos\Tests\Unit\DataMapper\ResponseDataMapper\AkbankPosResponseDataMapperTest; use PHPUnit\Framework\MockObject\MockObject; @@ -147,7 +148,14 @@ public function testGet3DFormData(array $order, string $txType, string $paymentM $this->dispatcher->expects(self::once()) ->method('dispatch') - ->with($this->logicalAnd($this->isInstanceOf(Before3DFormHashCalculatedEvent::class))); + ->with($this->callback(function ($dispatchedEvent) use ($txType, $paymentModel) { + return $dispatchedEvent instanceof Before3DFormHashCalculatedEvent + && AkbankPos::class === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $paymentModel === $dispatchedEvent->getPaymentModel() + && count($dispatchedEvent->getFormInputs()) > 3 + ; + })); $actual = $this->requestDataMapper->create3DFormData( $this->account, @@ -200,11 +208,25 @@ public function testGet3DFormDataSubMerchant(): void ->method('generateRandomString') ->willReturn('random-123'); + $txType = PosInterface::TX_TYPE_PAY_AUTH; + $paymentModel = PosInterface::MODEL_3D_SECURE; + + $this->dispatcher->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $paymentModel) { + return $dispatchedEvent instanceof Before3DFormHashCalculatedEvent + && AkbankPos::class === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $paymentModel === $dispatchedEvent->getPaymentModel() + && count($dispatchedEvent->getFormInputs()) > 3 + ; + })); + $actual = $this->requestDataMapper->create3DFormData( $this->subMerchantAccount, $this->order, - PosInterface::MODEL_3D_SECURE, - PosInterface::TX_TYPE_PAY_AUTH, + $paymentModel, + $txType, 'https://bank.com/pay', $card ); diff --git a/tests/Unit/DataMapper/RequestDataMapper/EstPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/EstPosRequestDataMapperTest.php index 55460208..2ac444ac 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/EstPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/EstPosRequestDataMapperTest.php @@ -10,10 +10,11 @@ use Mews\Pos\Entity\Account\AbstractPosAccount; use Mews\Pos\Entity\Account\EstPosAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; -use Mews\Pos\Factory\PosFactory; +use Mews\Pos\Gateways\EstPos; use Mews\Pos\PosInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -33,14 +34,15 @@ class EstPosRequestDataMapperTest extends TestCase /** @var CryptInterface & MockObject */ private CryptInterface $crypt; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + private array $order; protected function setUp(): void { parent::setUp(); - $config = require __DIR__.'/../../../../config/pos_test.php'; - $this->account = AccountFactory::createEstPosAccount( 'akbank', '700655000200', @@ -60,12 +62,11 @@ protected function setUp(): void 'fail_url' => 'https://domain.com/fail_url', 'lang' => PosInterface::LANG_TR, ]; - $dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->crypt = $this->createMock(CryptInterface::class); - $pos = PosFactory::createPosGateway($this->account, $config, $dispatcher); - $this->requestDataMapper = new EstPosRequestDataMapper($dispatcher, $this->crypt); - $this->card = CreditCardFactory::createForGateway($pos, '5555444433332222', '22', '01', '123', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); + $this->requestDataMapper = new EstPosRequestDataMapper($this->dispatcher, $this->crypt); + $this->card = CreditCardFactory::create('5555444433332222', '22', '01', '123', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); } /** @@ -220,6 +221,17 @@ public function testGet3DFormData( ->method('generateRandomString') ->willReturn($expected['inputs']['rnd']); + $this->dispatcher->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $paymentModel) { + return $dispatchedEvent instanceof Before3DFormHashCalculatedEvent + && EstPos::class === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $paymentModel === $dispatchedEvent->getPaymentModel() + && count($dispatchedEvent->getFormInputs()) > 3 + ; + })); + $actual = $this->requestDataMapper->create3DFormData( $this->account, $order, diff --git a/tests/Unit/DataMapper/RequestDataMapper/EstV3PosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/EstV3PosRequestDataMapperTest.php index e30fd77e..837fb7c3 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/EstV3PosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/EstV3PosRequestDataMapperTest.php @@ -9,9 +9,10 @@ use Mews\Pos\DataMapper\RequestDataMapper\EstV3PosRequestDataMapper; use Mews\Pos\Entity\Account\EstPosAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; -use Mews\Pos\Factory\PosFactory; +use Mews\Pos\Gateways\EstV3Pos; use Mews\Pos\PosInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -31,12 +32,13 @@ class EstV3PosRequestDataMapperTest extends TestCase /** @var CryptInterface & MockObject */ private CryptInterface $crypt; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + protected function setUp(): void { parent::setUp(); - $config = require __DIR__.'/../../../../config/pos_test.php'; - $this->account = AccountFactory::createEstPosAccount( 'payten_v3_hash', '190100000', @@ -46,12 +48,11 @@ protected function setUp(): void '123456' ); - $dispatcher = $this->createMock(EventDispatcherInterface::class); - $pos = PosFactory::createPosGateway($this->account, $config, $dispatcher); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->crypt = $this->createMock(CryptInterface::class); - $this->requestDataMapper = new EstV3PosRequestDataMapper($dispatcher, $this->crypt); - $this->card = CreditCardFactory::createForGateway($pos, '5555444433332222', '22', '01', '123', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); + $this->requestDataMapper = new EstV3PosRequestDataMapper($this->dispatcher, $this->crypt); + $this->card = CreditCardFactory::create('5555444433332222', '22', '01', '123', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); } /** @@ -74,6 +75,17 @@ public function testGet3DFormData( ->method('generateRandomString') ->willReturn($expected['inputs']['rnd']); + $this->dispatcher->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $paymentModel) { + return $dispatchedEvent instanceof Before3DFormHashCalculatedEvent + && EstV3Pos::class === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $paymentModel === $dispatchedEvent->getPaymentModel() + && count($dispatchedEvent->getFormInputs()) > 3 + ; + })); + $actual = $this->requestDataMapper->create3DFormData( $this->account, $order, diff --git a/tests/Unit/DataMapper/RequestDataMapper/GarantiPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/GarantiPosRequestDataMapperTest.php index 50317c48..15c309f7 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/GarantiPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/GarantiPosRequestDataMapperTest.php @@ -9,13 +9,14 @@ use Mews\Pos\Entity\Account\AbstractPosAccount; use Mews\Pos\Entity\Account\GarantiPosAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Factory\CryptFactory; -use Mews\Pos\Factory\PosFactory; use Mews\Pos\Gateways\GarantiPos; use Mews\Pos\PosInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\NullLogger; @@ -33,14 +34,13 @@ class GarantiPosRequestDataMapperTest extends TestCase private array $order; - private $config; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; protected function setUp(): void { parent::setUp(); - $this->config = require __DIR__.'/../../../../config/pos_test.php'; - $this->account = AccountFactory::createGarantiPosAccount( 'garanti', '7000679', @@ -64,14 +64,13 @@ protected function setUp(): void 'ip' => '156.155.154.153', ]; - $dispatcher = $this->createMock(EventDispatcherInterface::class); - $pos = PosFactory::createPosGateway($this->account, $this->config, $dispatcher); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $crypt = CryptFactory::createGatewayCrypt(GarantiPos::class, new NullLogger()); - $this->requestDataMapper = new GarantiPosRequestDataMapper($dispatcher, $crypt); + $this->requestDataMapper = new GarantiPosRequestDataMapper($this->dispatcher, $crypt); $this->requestDataMapper->setTestMode(true); - $this->card = CreditCardFactory::createForGateway($pos, '5555444433332222', '22', '01', '123', 'ahmet'); + $this->card = CreditCardFactory::create('5555444433332222', '22', '01', '123', 'ahmet'); } /** @@ -207,7 +206,7 @@ public function testCreate3DPaymentRequestData(GarantiPosAccount $garantiPosAcco public function testGet3DFormData(): void { $account = $this->account; - $gatewayURL = $this->config['banks'][$this->account->getBank()]['gateway_endpoints']['gateway_3d']; + $gatewayURL = 'https://sanalposprovtest.garantibbva.com.tr/servlet/gt3dengine'; $inputs = [ 'secure3dsecuritylevel' => '3D', 'mode' => 'TEST', @@ -237,15 +236,29 @@ public function testGet3DFormData(): void 'gateway' => $gatewayURL, ]; - //test without card - $this->assertEquals($form, $this->requestDataMapper->create3DFormData( + $txType = PosInterface::TX_TYPE_PAY_AUTH; + $paymentModel = PosInterface::MODEL_3D_SECURE; + $this->dispatcher->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $paymentModel) { + return $dispatchedEvent instanceof Before3DFormHashCalculatedEvent + && GarantiPos::class === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $paymentModel === $dispatchedEvent->getPaymentModel() + && count($dispatchedEvent->getFormInputs()) > 3; + })); + + $actual = $this->requestDataMapper->create3DFormData( $this->account, $this->order, - PosInterface::MODEL_3D_SECURE, - PosInterface::TX_TYPE_PAY_AUTH, + $paymentModel, + $txType, $gatewayURL, $this->card - )); + ); + + //test without card + $this->assertEquals($form, $actual); } /** diff --git a/tests/Unit/DataMapper/RequestDataMapper/InterPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/InterPosRequestDataMapperTest.php index 75ca552c..b60255fd 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/InterPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/InterPosRequestDataMapperTest.php @@ -9,10 +9,11 @@ use Mews\Pos\DataMapper\RequestDataMapper\InterPosRequestDataMapper; use Mews\Pos\Entity\Account\InterPosAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; -use Mews\Pos\Factory\PosFactory; +use Mews\Pos\Gateways\InterPos; use Mews\Pos\PosInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -32,14 +33,15 @@ class InterPosRequestDataMapperTest extends TestCase /** @var CryptInterface & MockObject */ private CryptInterface $crypt; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + private array $order; protected function setUp(): void { parent::setUp(); - $config = require __DIR__.'/../../../../config/pos_test.php'; - $userCode = 'InterTestApi'; $userPass = '3'; $shopCode = '3123'; @@ -64,13 +66,12 @@ protected function setUp(): void 'lang' => PosInterface::LANG_TR, ]; - $dispatcher = $this->createMock(EventDispatcherInterface::class); - $pos = PosFactory::createPosGateway($this->account, $config, $dispatcher); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->crypt = $this->createMock(CryptInterface::class); - $this->requestDataMapper = new InterPosRequestDataMapper($dispatcher, $this->crypt); + $this->requestDataMapper = new InterPosRequestDataMapper($this->dispatcher, $this->crypt); - $this->card = CreditCardFactory::createForGateway($pos, '5555444433332222', '21', '12', '122', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); + $this->card = CreditCardFactory::create('5555444433332222', '21', '12', '122', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); } /** @@ -216,6 +217,16 @@ public function testGet3DFormData( ->method('generateRandomString') ->willReturn($expected['inputs']['Rnd']); + $this->dispatcher->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $paymentModel) { + return $dispatchedEvent instanceof Before3DFormHashCalculatedEvent + && InterPos::class === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $paymentModel === $dispatchedEvent->getPaymentModel() + && count($dispatchedEvent->getFormInputs()) > 3; + })); + $actual = $this->requestDataMapper->create3DFormData( $this->account, $order, diff --git a/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php index 717995b3..718ee119 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/KuveytPosRequestDataMapperTest.php @@ -33,6 +33,9 @@ class KuveytPosRequestDataMapperTest extends TestCase /** @var CryptInterface|MockObject */ private CryptInterface $crypt; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + protected function setUp(): void { parent::setUp(); @@ -45,7 +48,7 @@ protected function setUp(): void 'Api123' ); - $dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->card = CreditCardFactory::create( '4155650100416111', @@ -57,7 +60,7 @@ protected function setUp(): void ); $this->crypt = $this->createMock(CryptInterface::class); - $this->requestDataMapper = new KuveytPosRequestDataMapper($dispatcher, $this->crypt); + $this->requestDataMapper = new KuveytPosRequestDataMapper($this->dispatcher, $this->crypt); } /** @@ -290,11 +293,16 @@ public function testGet3DFormData(): void ], ]; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + $paymentModel = PosInterface::MODEL_3D_SECURE; + $this->dispatcher->expects(self::never()) + ->method('dispatch'); + $actual = $this->requestDataMapper->create3DFormData( $this->account, ['abc' => '123'], - PosInterface::MODEL_3D_SECURE, - PosInterface::TX_TYPE_PAY_AUTH, + $paymentModel, + $txType, 'https://bank-gateway.com', ); diff --git a/tests/Unit/DataMapper/RequestDataMapper/PayFlexCPV4PosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/PayFlexCPV4PosRequestDataMapperTest.php index a4d46f51..988b09ef 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/PayFlexCPV4PosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/PayFlexCPV4PosRequestDataMapperTest.php @@ -14,13 +14,12 @@ use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Factory\CryptFactory; -use Mews\Pos\Factory\PosFactory; use Mews\Pos\Gateways\PayFlexCPV4Pos; use Mews\Pos\PosInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\NullLogger; -use Symfony\Component\EventDispatcher\EventDispatcher; /** * @covers \Mews\Pos\DataMapper\RequestDataMapper\PayFlexCPV4PosRequestDataMapper @@ -31,6 +30,9 @@ class PayFlexCPV4PosRequestDataMapperTest extends TestCase private PayFlexCPV4PosRequestDataMapper $requestDataMapper; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + protected function setUp(): void { parent::setUp(); @@ -43,8 +45,9 @@ protected function setUp(): void PosInterface::MODEL_3D_SECURE ); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $crypt = CryptFactory::createGatewayCrypt(PayFlexCPV4Pos::class, new NullLogger()); - $this->requestDataMapper = new PayFlexCPV4PosRequestDataMapper($this->createMock(EventDispatcherInterface::class), $crypt); + $this->requestDataMapper = new PayFlexCPV4PosRequestDataMapper($this->dispatcher, $crypt); } /** @@ -140,6 +143,9 @@ public function testCreate3DEnrollmentCheckData(AbstractPosAccount $posAccount, */ public function testCreate3DFormData(array $queryParams, array $expected): void { + $this->dispatcher->expects(self::never()) + ->method('dispatch'); + $actualData = $this->requestDataMapper->create3DFormData( null, null, @@ -175,10 +181,7 @@ public static function registerDataProvider(): iterable 'ip' => '127.0.0.1', ]; - $pos = PosFactory::createPosGateway($account, $config, new EventDispatcher()); - $pos->setTestMode(true); - - $card = CreditCardFactory::createForGateway($pos, '5555444433332222', '2021', '12', '122', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); + $card = CreditCardFactory::create('5555444433332222', '2021', '12', '122', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); yield 'with_card_1' => [ 'account' => $account, diff --git a/tests/Unit/DataMapper/RequestDataMapper/PayFlexV4PosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/PayFlexV4PosRequestDataMapperTest.php index 3735f8bd..d42623f8 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/PayFlexV4PosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/PayFlexV4PosRequestDataMapperTest.php @@ -14,7 +14,6 @@ use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; -use Mews\Pos\Factory\PosFactory; use Mews\Pos\PosInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -34,14 +33,15 @@ class PayFlexV4PosRequestDataMapperTest extends TestCase /** @var CryptInterface & MockObject */ private CryptInterface $crypt; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + private array $order; protected function setUp(): void { parent::setUp(); - $config = require __DIR__.'/../../../../config/pos_test.php'; - $this->account = AccountFactory::createPayFlexAccount( 'vakifbank', '000000000111111', @@ -61,12 +61,11 @@ protected function setUp(): void 'ip' => '127.0.0.1', ]; - $dispatcher = $this->createMock(EventDispatcherInterface::class); - $pos = PosFactory::createPosGateway($this->account, $config, $dispatcher); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->crypt = $this->createMock(CryptInterface::class); - $this->requestDataMapper = new PayFlexV4PosRequestDataMapper($dispatcher, $this->crypt); - $this->card = CreditCardFactory::createForGateway($pos, '5555444433332222', '2021', '12', '122', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); + $this->requestDataMapper = new PayFlexV4PosRequestDataMapper($this->dispatcher, $this->crypt); + $this->card = CreditCardFactory::create('5555444433332222', '2021', '12', '122', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); } /** @@ -232,6 +231,10 @@ public function testCreateRefundRequestData(): void public function testCreate3DFormData(): void { $expectedValue = $this->getSample3DFormDataFromEnrollmentResponse(); + + $this->dispatcher->expects(self::never()) + ->method('dispatch'); + $actualData = $this->requestDataMapper->create3DFormData( null, null, diff --git a/tests/Unit/DataMapper/RequestDataMapper/PayForPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/PayForPosRequestDataMapperTest.php index 55cc5147..5a5bb0cd 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/PayForPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/PayForPosRequestDataMapperTest.php @@ -10,10 +10,11 @@ use Mews\Pos\Entity\Account\AbstractPosAccount; use Mews\Pos\Entity\Account\PayForAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; -use Mews\Pos\Factory\PosFactory; +use Mews\Pos\Gateways\PayForPos; use Mews\Pos\PosInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -35,14 +36,13 @@ class PayForPosRequestDataMapperTest extends TestCase private array $order; - private array $config; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; protected function setUp(): void { parent::setUp(); - $this->config = require __DIR__.'/../../../../config/pos_test.php'; - $this->account = AccountFactory::createPayForAccount( 'qnbfinansbank-payfor', '085300000009704', @@ -62,12 +62,11 @@ protected function setUp(): void 'lang' => PosInterface::LANG_TR, ]; - $this->crypt = $this->createMock(CryptInterface::class); - $dispatcher = $this->createMock(EventDispatcherInterface::class); - $pos = PosFactory::createPosGateway($this->account, $this->config, $dispatcher); + $this->crypt = $this->createMock(CryptInterface::class); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); - $this->requestDataMapper = new PayForPosRequestDataMapper($dispatcher, $this->crypt); - $this->card = CreditCardFactory::createForGateway($pos, '5555444433332222', '22', '01', '123', 'ahmet'); + $this->requestDataMapper = new PayForPosRequestDataMapper($this->dispatcher, $this->crypt); + $this->card = CreditCardFactory::create('5555444433332222', '22', '01', '123', 'ahmet'); } /** @@ -229,6 +228,15 @@ public function testGet3DFormData( ->method('generateRandomString') ->willReturn($expected['inputs']['Rnd']); + $this->dispatcher->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $paymentModel) { + return $dispatchedEvent instanceof Before3DFormHashCalculatedEvent + && PayForPos::class === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $paymentModel === $dispatchedEvent->getPaymentModel() + && count($dispatchedEvent->getFormInputs()) > 3; + })); $actual = $this->requestDataMapper->create3DFormData( $this->account, diff --git a/tests/Unit/DataMapper/RequestDataMapper/PosNetRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/PosNetRequestDataMapperTest.php index d8919b9d..abb2457e 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/PosNetRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/PosNetRequestDataMapperTest.php @@ -13,9 +13,9 @@ use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Factory\CryptFactory; -use Mews\Pos\Factory\PosFactory; use Mews\Pos\Gateways\PosNet; use Mews\Pos\PosInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\NullLogger; @@ -33,12 +33,13 @@ class PosNetRequestDataMapperTest extends TestCase private PosNetAccount $account; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + protected function setUp(): void { parent::setUp(); - $config = require __DIR__.'/../../../../config/pos_test.php'; - $this->account = AccountFactory::createPosNetAccount( 'yapikredi', '6706598320', @@ -58,11 +59,10 @@ protected function setUp(): void 'lang' => PosInterface::LANG_TR, ]; - $dispatcher = $this->createMock(EventDispatcherInterface::class); - $pos = PosFactory::createPosGateway($this->account, $config, $dispatcher); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $crypt = CryptFactory::createGatewayCrypt(PosNet::class, new NullLogger()); - $this->requestDataMapper = new PosNetRequestDataMapper($dispatcher, $crypt); - $this->card = CreditCardFactory::createForGateway($pos, '5555444433332222', '22', '01', '123', 'ahmet'); + $this->requestDataMapper = new PosNetRequestDataMapper($this->dispatcher, $crypt); + $this->card = CreditCardFactory::create('5555444433332222', '22', '01', '123', 'ahmet'); } /** @@ -232,6 +232,9 @@ public function testCreate3DResolveMerchantRequestData(array $order, array $resp */ public function testCreate3DFormData(array $ooTxSuccessData, array $order, string $gatewayURL, array $expected): void { + $this->dispatcher->expects(self::never()) + ->method('dispatch'); + $actual = $this->requestDataMapper->create3DFormData( $this->account, $order, diff --git a/tests/Unit/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapperTest.php index 486f7166..43bcda0b 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/PosNetV1PosRequestDataMapperTest.php @@ -9,16 +9,17 @@ use Mews\Pos\DataMapper\RequestDataMapper\PosNetV1PosRequestDataMapper; use Mews\Pos\Entity\Account\PosNetAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Event\Before3DFormHashCalculatedEvent; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Factory\CryptFactory; -use Mews\Pos\Factory\PosFactory; use Mews\Pos\Gateways\PosNetV1Pos; use Mews\Pos\PosInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\NullLogger; -use Symfony\Component\EventDispatcher\EventDispatcher; /** * @covers \Mews\Pos\DataMapper\RequestDataMapper\PosNetV1PosRequestDataMapper @@ -31,12 +32,13 @@ class PosNetV1PosRequestDataMapperTest extends TestCase private PosNetAccount $account; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + protected function setUp(): void { parent::setUp(); - $config = require __DIR__.'/../../../../config/pos_test.php'; - $this->account = AccountFactory::createPosNetAccount( 'albaraka', '6700950031', @@ -46,13 +48,12 @@ protected function setUp(): void '10,10,10,10,10,10,10,10' ); - $dispatcher = new EventDispatcher(); - $pos = PosFactory::createPosGateway($this->account, $config, $dispatcher); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); - $this->card = CreditCardFactory::createForGateway($pos, '5400619360964581', '20', '01', '056', 'ahmet'); + $this->card = CreditCardFactory::create('5400619360964581', '20', '01', '056', 'ahmet'); $crypt = CryptFactory::createGatewayCrypt(PosNetV1Pos::class, new NullLogger()); - $this->requestDataMapper = new PosNetV1PosRequestDataMapper($dispatcher, $crypt); + $this->requestDataMapper = new PosNetV1PosRequestDataMapper($this->dispatcher, $crypt); } /** @@ -182,10 +183,21 @@ public function testCreate3DPaymentRequestData(array $order, string $txType, arr */ public function testCreate3DFormData(array $order, string $txType, string $gatewayUrl, array $expected): void { + $paymentModel = PosInterface::MODEL_3D_SECURE; + $this->dispatcher->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $paymentModel) { + return $dispatchedEvent instanceof Before3DFormHashCalculatedEvent + && PosNetV1Pos::class === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $paymentModel === $dispatchedEvent->getPaymentModel() + && count($dispatchedEvent->getFormInputs()) > 3; + })); + $actual = $this->requestDataMapper->create3DFormData( $this->account, $order, - PosInterface::MODEL_3D_SECURE, + $paymentModel, $txType, $gatewayUrl, $this->card diff --git a/tests/Unit/DataMapper/RequestDataMapper/ToslaPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/ToslaPosRequestDataMapperTest.php index 3fd40e4b..ae1b2beb 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/ToslaPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/ToslaPosRequestDataMapperTest.php @@ -12,7 +12,6 @@ use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; -use Mews\Pos\Factory\PosFactory; use Mews\Pos\PosInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -30,14 +29,15 @@ class ToslaPosRequestDataMapperTest extends TestCase /** @var CryptInterface & MockObject */ private CryptInterface $crypt; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + private ToslaPosRequestDataMapper $requestDataMapper; protected function setUp(): void { parent::setUp(); - $config = require __DIR__.'/../../../../config/pos_test.php'; - $this->account = AccountFactory::createToslaPosAccount( 'tosla', '1000000494', @@ -45,12 +45,11 @@ protected function setUp(): void 'POS_ENT_Test_001!*!*', ); - $dispatcher = $this->createMock(EventDispatcherInterface::class); - $pos = PosFactory::createPosGateway($this->account, $config, $dispatcher); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->crypt = $this->createMock(CryptInterface::class); - $this->requestDataMapper = new ToslaPosRequestDataMapper($dispatcher, $this->crypt); - $this->card = CreditCardFactory::createForGateway($pos, '5555444433332222', '22', '01', '123', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); + $this->requestDataMapper = new ToslaPosRequestDataMapper($this->dispatcher, $this->crypt); + $this->card = CreditCardFactory::create('5555444433332222', '22', '01', '123', 'ahmet', CreditCardInterface::CARD_TYPE_VISA); } /** @@ -200,6 +199,9 @@ public function testGet3DFormData(array $order, string $txType, string $paymentM $this->crypt->expects(self::never()) ->method('generateRandomString'); + $this->dispatcher->expects(self::never()) + ->method('dispatch'); + $actual = $this->requestDataMapper->create3DFormData( $this->account, $order, diff --git a/tests/Unit/DataMapper/RequestDataMapper/VakifKatilimPosRequestDataMapperTest.php b/tests/Unit/DataMapper/RequestDataMapper/VakifKatilimPosRequestDataMapperTest.php index e42cc322..b1a63bdd 100644 --- a/tests/Unit/DataMapper/RequestDataMapper/VakifKatilimPosRequestDataMapperTest.php +++ b/tests/Unit/DataMapper/RequestDataMapper/VakifKatilimPosRequestDataMapperTest.php @@ -15,6 +15,7 @@ use Mews\Pos\Factory\CryptFactory; use Mews\Pos\Gateways\VakifKatilimPos; use Mews\Pos\PosInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\NullLogger; @@ -30,6 +31,9 @@ class VakifKatilimPosRequestDataMapperTest extends TestCase private VakifKatilimPosRequestDataMapper $requestDataMapper; + /** @var EventDispatcherInterface & MockObject */ + private EventDispatcherInterface $dispatcher; + protected function setUp(): void { parent::setUp(); @@ -42,7 +46,7 @@ protected function setUp(): void 'kdsnsksl', ); - $dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->card = CreditCardFactory::create( '4155650100416111', @@ -53,7 +57,7 @@ protected function setUp(): void ); $crypt = CryptFactory::createGatewayCrypt(VakifKatilimPos::class, new NullLogger()); - $this->requestDataMapper = new VakifKatilimPosRequestDataMapper($dispatcher, $crypt); + $this->requestDataMapper = new VakifKatilimPosRequestDataMapper($this->dispatcher, $crypt); } /** diff --git a/tests/Unit/DataMapper/ResponseDataMapper/PosNetResponseDataMapperTest.php b/tests/Unit/DataMapper/ResponseDataMapper/PosNetResponseDataMapperTest.php index dd60d976..cbc872cf 100644 --- a/tests/Unit/DataMapper/ResponseDataMapper/PosNetResponseDataMapperTest.php +++ b/tests/Unit/DataMapper/ResponseDataMapper/PosNetResponseDataMapperTest.php @@ -98,6 +98,10 @@ public function testMap3DPaymentData(array $order, string $txType, array $threeD } unset($actualData['transaction_time'], $expectedData['transaction_time']); + + $this->assertArrayHasKey('3d_all', $actualData); + $this->assertIsArray($actualData['3d_all']); + $this->assertNotEmpty($actualData['3d_all']); unset($actualData['all'], $actualData['3d_all']); \ksort($expectedData); \ksort($actualData); diff --git a/tests/Unit/Gateways/AkbankPosTest.php b/tests/Unit/Gateways/AkbankPosTest.php index 2fa30594..a1b0b5ac 100644 --- a/tests/Unit/Gateways/AkbankPosTest.php +++ b/tests/Unit/Gateways/AkbankPosTest.php @@ -297,7 +297,9 @@ public function testMake3DPayment( $create3DPaymentRequestData, 'request-body', 'response-body', - $paymentResponse + $paymentResponse, + $order, + PosInterface::MODEL_3D_SECURE ); $this->responseMapperMock->expects(self::once()) @@ -315,6 +317,8 @@ public function testMake3DPayment( ->method('encode'); $this->serializerMock->expects(self::never()) ->method('decode'); + $this->eventDispatcherMock->expects(self::never()) + ->method('dispatch'); } $this->pos->make3DPayment($request, $order, $txType); @@ -387,27 +391,30 @@ public function testGet3DFormData( */ public function testHistoryRequest(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_HISTORY; - + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_HISTORY; $requestData = ['createHistoryRequestData']; + $this->requestMapperMock->expects(self::once()) ->method('createHistoryRequestData') ->with($account, $order) ->willReturn($requestData); + $decodedResponse = ['decodedData']; $this->configureClientResponse( $txType, $apiUrl, $requestData, 'request-body', 'response-body', - ['decodedResponse'] + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); $this->responseMapperMock->expects(self::once()) ->method('mapHistoryResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->history($order); @@ -426,27 +433,28 @@ public function testOrderHistory( bool $isSuccess ): void { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $this->requestMapperMock->expects(self::once()) ->method('createOrderHistoryRequestData') - ->with($this->pos->getAccount(), $order) + ->with($account, $order) ->willReturn($requestData); - $this->eventDispatcherMock->expects(self::once()) - ->method('dispatch') - ->with($this->logicalAnd($this->isInstanceOf(RequestDataPreparedEvent::class))); - $this->responseMapperMock->expects(self::once()) ->method('mapOrderHistoryResponse') ->with($decodedResponse) ->willReturn($mappedResponse); $this->configureClientResponse( - PosInterface::TX_TYPE_ORDER_HISTORY, + $txType, 'https://apipre.akbank.com/api/v1/payment/virtualpos/transaction/process', $requestData, $encodedRequest, $responseContent, - $decodedResponse + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); $this->pos->orderHistory($order); @@ -468,13 +476,16 @@ public function testMakeRegularPayment(array $order, string $txType, string $api ->with($account, $order, $txType, $card) ->willReturn(['createNonSecurePaymentRequestData']); + $requestData = ['createNonSecurePaymentRequestData']; $this->configureClientResponse( $txType, $apiUrl, - ['createNonSecurePaymentRequestData'], + $requestData, 'request-body', $apiUrl, - ['paymentResponse'] + ['paymentResponse'], + $order, + PosInterface::MODEL_NON_SECURE ); $this->responseMapperMock->expects(self::once()) @@ -490,20 +501,23 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPaymentBadRequest(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); + ->willReturn($requestData); $this->configureClientResponse( $txType, $apiUrl, - ['createNonSecurePaymentRequestData'], + $requestData, 'request-body', $apiUrl, ['code' => 123, 'message' => 'error'], + $order, + PosInterface::MODEL_NON_SECURE, 400 ); @@ -516,21 +530,24 @@ public function testMakeRegularPaymentBadRequest(array $order, string $txType, s */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePostAuthPaymentRequestData') ->with($account, $order) - ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + ->willReturn($requestData); $this->configureClientResponse( $txType, $apiUrl, - ['createNonSecurePostAuthPaymentRequestData'], + $requestData, 'request-body', $apiUrl, - ['paymentResponse'] + ['paymentResponse'], + $order, + PosInterface::MODEL_NON_SECURE ); $this->responseMapperMock->expects(self::once()) @@ -552,21 +569,24 @@ public function testStatusRequest(): void */ public function testCancelRequest(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_CANCEL; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createCancelRequestData') ->with($account, $order) - ->willReturn(['createCancelRequestData']); + ->willReturn($requestData); $this->configureClientResponse( $txType, $apiUrl, - ['createCancelRequestData'], + $requestData, 'request-body', $apiUrl, - ['decodedResponse'] + ['decodedResponse'], + $order, + PosInterface::MODEL_NON_SECURE ); $this->responseMapperMock->expects(self::once()) @@ -582,13 +602,14 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createRefundRequestData') ->with($account, $order) - ->willReturn(['createRefundRequestData']); + ->willReturn($requestData); $this->configureClientResponse( $txType, @@ -596,7 +617,9 @@ public function testRefundRequest(array $order, string $apiUrl): void ['createRefundRequestData'], 'request-body', $apiUrl, - ['decodedResponse'] + ['decodedResponse'], + $order, + PosInterface::MODEL_NON_SECURE ); $this->responseMapperMock->expects(self::once()) @@ -607,36 +630,6 @@ public function testRefundRequest(array $order, string $apiUrl): void $this->pos->refund($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->configureClientResponse( - $txType, - $apiUrl, - ['createOrderHistoryRequestData'], - 'request-body', - $apiUrl, - ['decodedResponse'] - ); - - $this->responseMapperMock->expects(self::once()) - ->method('mapOrderHistoryResponse') - ->with(['decodedResponse']) - ->willReturn(['result']); - - $this->pos->orderHistory($order); - } - public static function getApiUrlDataProvider(): array { return [ @@ -718,18 +711,6 @@ public static function refundRequestDataProvider(): array ]; } - public static function orderHistoryRequestDataProvider(): array - { - return [ - [ - 'order' => [ - 'id' => '2020110828BC', - ], - 'api_url' => 'https://apipre.akbank.com/api/v1/payment/virtualpos/transaction/process', - ], - ]; - } - public static function make3DPaymentDataProvider(): array { return [ @@ -867,6 +848,18 @@ public static function make3DHostPaymentDataProvider(): array ]; } + public static function historyRequestDataProvider(): array + { + return [ + [ + 'order' => [ + 'batch_num' => 123, + ], + 'api_url' => 'https://apipre.akbank.com/api/v1/payment/virtualpos/portal/report/transaction', + ], + ]; + } + private function configureClientResponse( string $txType, string $apiUrl, @@ -874,6 +867,8 @@ private function configureClientResponse( string $encodedRequestData, string $responseContent, array $decodedResponse, + array $order, + string $paymentModel, ?int $statusCode = null ): void { @@ -905,17 +900,17 @@ private function configureClientResponse( ], $statusCode ); - } - public static function historyRequestDataProvider(): array - { - return [ - [ - 'order' => [ - 'batch_num' => 123, - ], - 'api_url' => 'https://apipre.akbank.com/api/v1/payment/virtualpos/portal/report/transaction', - ], - ]; + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); } } diff --git a/tests/Unit/Gateways/EstPosTest.php b/tests/Unit/Gateways/EstPosTest.php index 3273fe3e..eb2713b4 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\Event\RequestDataPreparedEvent; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; @@ -27,8 +28,7 @@ /** * @covers \Mews\Pos\Gateways\EstPos - * - * @uses \Mews\Pos\Gateways\AbstractGateway + * @covers \Mews\Pos\Gateways\AbstractGateway */ class EstPosTest extends TestCase { @@ -238,38 +238,33 @@ public function testMake3DPayPaymentSuccess(): void */ public function testStatus(array $bankResponse, array $expectedData, bool $isSuccess): void { - $statusRequestData = [ - 'statusRequestData', - ]; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $requestData = ['createStatusRequestData']; + $order = $this->order; + $this->requestMapperMock->expects(self::once()) ->method('createStatusRequestData') - ->willReturn($statusRequestData); + ->with($account, $order) + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $this->configureClientResponse( + $txType, 'https://entegrasyon.asseco-see.com.tr/fim/api', - [ - 'body' => 'request-body', - ], + $requestData, + 'request-body', + 'response-body', + $bankResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with($statusRequestData, PosInterface::TX_TYPE_STATUS) - ->willReturn('request-body'); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', PosInterface::TX_TYPE_STATUS) - ->willReturn($bankResponse); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') ->with($bankResponse) ->willReturn($expectedData); - $this->pos->status($this->order); + $this->pos->status($order); $result = $this->pos->getResponse(); $this->assertSame($expectedData, $result); @@ -287,39 +282,33 @@ public function testHistoryRequest(): void */ public function testOrderHistory(array $bankResponse, array $expectedData, bool $isSuccess): void { - $historyRequestData = [ - 'historyRequestData', - ]; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $requestData = ['createOrderHistoryRequestData']; + $order = $this->order; + $this->requestMapperMock->expects(self::once()) ->method('createOrderHistoryRequestData') - ->willReturn($historyRequestData); + ->with($account, $order) + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $this->configureClientResponse( + $txType, 'https://entegrasyon.asseco-see.com.tr/fim/api', - [ - 'body' => 'request-body', - ], + $requestData, + 'request-body', + 'response-body', + $bankResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with($historyRequestData, PosInterface::TX_TYPE_ORDER_HISTORY) - ->willReturn('request-body'); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', PosInterface::TX_TYPE_ORDER_HISTORY) - ->willReturn($bankResponse); - $this->responseMapperMock->expects(self::once()) ->method('mapOrderHistoryResponse') ->with($bankResponse) ->willReturn($expectedData); - - $this->pos->orderHistory($this->order); + $this->pos->orderHistory($order); $result = $this->pos->getResponse(); $this->assertSame($expectedData, $result); @@ -331,38 +320,33 @@ public function testOrderHistory(array $bankResponse, array $expectedData, bool */ public function testCancel(array $bankResponse, array $expectedData, bool $isSuccess): void { - $cancelRequestData = [ - 'cancelRequestData', - ]; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; + $order = $this->order; + $this->requestMapperMock->expects(self::once()) ->method('createCancelRequestData') - ->willReturn($cancelRequestData); + ->with($account, $order) + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $this->configureClientResponse( + $txType, 'https://entegrasyon.asseco-see.com.tr/fim/api', - [ - 'body' => 'request-body', - ], + $requestData, + 'request-body', + 'response-body', + $bankResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with($cancelRequestData, PosInterface::TX_TYPE_CANCEL) - ->willReturn('request-body'); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', PosInterface::TX_TYPE_CANCEL) - ->willReturn($bankResponse); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') ->with($bankResponse) ->willReturn($expectedData); - $this->pos->cancel($this->order); + $this->pos->cancel($order); $result = $this->pos->getResponse(); $this->assertSame($expectedData, $result); @@ -374,38 +358,33 @@ public function testCancel(array $bankResponse, array $expectedData, bool $isSuc */ public function testRefund(array $bankResponse, array $expectedData, bool $isSuccess): void { - $refundRequestData = [ - 'refundRequestData', - ]; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; + $order = $this->order; + $this->requestMapperMock->expects(self::once()) ->method('createRefundRequestData') - ->willReturn($refundRequestData); + ->with($account, $order) + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $this->configureClientResponse( + $txType, 'https://entegrasyon.asseco-see.com.tr/fim/api', - [ - 'body' => 'request-body', - ], + $requestData, + 'request-body', + 'response-body', + $bankResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with($refundRequestData, PosInterface::TX_TYPE_REFUND) - ->willReturn('request-body'); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', PosInterface::TX_TYPE_REFUND) - ->willReturn($bankResponse); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') ->with($bankResponse) ->willReturn($expectedData); - $this->pos->refund($this->order); + $this->pos->refund($order); $result = $this->pos->getResponse(); $this->assertSame($expectedData, $result); @@ -450,24 +429,18 @@ public function testMake3DPayment( ->method('create3DPaymentRequestData') ->with($this->account, $order, $txType, $request->request->all()) ->willReturn($create3DPaymentRequestData); - $this->prepareClient( - $this->httpClientMock, + + $this->configureClientResponse( + $txType, + 'https://entegrasyon.asseco-see.com.tr/fim/api', + $create3DPaymentRequestData, + 'request-body', 'response-body', - $this->config['gateway_endpoints']['payment_api'], - [ - 'body' => 'request-body', - ], + $paymentResponse, + $order, + PosInterface::MODEL_3D_SECURE ); - $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) @@ -483,6 +456,8 @@ public function testMake3DPayment( ->method('encode'); $this->serializerMock->expects(self::never()) ->method('decode'); + $this->eventDispatcherMock->expects(self::never()) + ->method('dispatch'); } $this->pos->make3DPayment($request, $order, $txType); @@ -497,34 +472,29 @@ public function testMake3DPayment( */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + ->willReturn($requestData); + + $decodedResponse = ['paymentResponse']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $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) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -535,33 +505,27 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePostAuthPaymentRequestData') ->with($account, $order) - ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['paymentResponse']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') ->with(['paymentResponse'], $txType, $order) @@ -726,39 +690,46 @@ public static function cancelRequestDataProvider(): array ]; } - public static function refundRequestDataProvider(): array + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + string $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel + ): void { - return [ - [ - 'order' => [ - 'id' => '2020110828BC', - ], - 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', - ], - ]; - } + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); - public static function historyRequestDataProvider(): array - { - return [ - [ - 'order' => [ - 'id' => '2020110828BC', - ], - 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', - ], - ]; - } + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); - public static function orderHistoryRequestDataProvider(): array - { - return [ + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, [ - 'order' => [ - 'id' => '2020110828BC', - ], - 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + 'body' => $encodedRequestData, ], - ]; + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); } } diff --git a/tests/Unit/Gateways/GarantiPosTest.php b/tests/Unit/Gateways/GarantiPosTest.php index 51deea5f..e3f95bd5 100644 --- a/tests/Unit/Gateways/GarantiPosTest.php +++ b/tests/Unit/Gateways/GarantiPosTest.php @@ -11,6 +11,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\GarantiPosAccount; 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; @@ -28,8 +29,7 @@ /** * @covers \Mews\Pos\Gateways\GarantiPos - * - * @uses \Mews\Pos\Gateways\AbstractGateway + * @covers \Mews\Pos\Gateways\AbstractGateway */ class GarantiPosTest extends TestCase { @@ -207,24 +207,18 @@ public function testMake3DPayment( ->method('create3DPaymentRequestData') ->with($this->account, $order, $txType, $request->request->all()) ->willReturn($create3DPaymentRequestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['payment_api'], - [ - 'body' => 'request-body', - ], + $create3DPaymentRequestData, + 'request-body', + 'response-body', + $paymentResponse, + $order, + PosInterface::MODEL_3D_SECURE ); - $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) @@ -240,6 +234,8 @@ public function testMake3DPayment( ->method('encode'); $this->serializerMock->expects(self::never()) ->method('decode'); + $this->eventDispatcherMock->expects(self::never()) + ->method('dispatch'); } $this->pos->make3DPayment($request, $order, $txType); @@ -289,34 +285,30 @@ public function testMake3DPayPayment(): void */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; + $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + ->willReturn($requestData); + + $decodedResponse = ['paymentResponse']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $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) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -327,36 +319,29 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; - + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPostPayment($order); @@ -367,37 +352,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $requestData = ['createStatusRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->status($order); @@ -408,36 +386,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->cancel($order); @@ -448,36 +420,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createRefundRequestData') ->with($account, $order) - ->willReturn(['createRefundRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createRefundRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->refund($order); @@ -494,36 +460,30 @@ public function testHistoryRequest(): void */ public function testOrderHistoryRequest(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $requestData = ['createOrderHistoryRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapOrderHistoryResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->orderHistory($order); @@ -621,7 +581,7 @@ public static function refundRequestDataProvider(): array ]; } - public static function historyRequestDataProvider(): array + public static function orderHistoryRequestDataProvider(): array { return [ [ @@ -633,15 +593,46 @@ public static function historyRequestDataProvider(): array ]; } - public static function orderHistoryRequestDataProvider(): array + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + string $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel + ): void { - return [ + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); + + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, [ - 'order' => [ - 'id' => '2020110828BC', - ], - 'api_url' => 'https://sanalposprovtest.garantibbva.com.tr/VPServlet', + 'body' => $encodedRequestData, ], - ]; + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); } } diff --git a/tests/Unit/Gateways/InterPosTest.php b/tests/Unit/Gateways/InterPosTest.php index cf0591de..42d66bce 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\Event\RequestDataPreparedEvent; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; @@ -26,6 +27,7 @@ /** * @covers \Mews\Pos\Gateways\InterPos + * @covers \Mews\Pos\Gateways\AbstractGateway */ class InterPosTest extends TestCase { @@ -200,24 +202,18 @@ public function testMake3DPayment( ->method('create3DPaymentRequestData') ->with($this->account, $order, $txType, $request->request->all()) ->willReturn($create3DPaymentRequestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['payment_api'], - [ - 'body' => 'request-body', - ], + $create3DPaymentRequestData, + ['request-body'], + 'response-body', + $paymentResponse, + $order, + PosInterface::MODEL_3D_SECURE ); - $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) @@ -233,6 +229,8 @@ public function testMake3DPayment( ->method('encode'); $this->serializerMock->expects(self::never()) ->method('decode'); + $this->eventDispatcherMock->expects(self::never()) + ->method('dispatch'); } $this->pos->make3DPayment($request, $order, $txType); @@ -314,34 +312,29 @@ public function testOrderHistoryRequest(): void */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + ->willReturn($requestData); + + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['request-body'], - ] + $requestData, + ['request-body'], + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $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) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -352,36 +345,30 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePostAuthPaymentRequestData') ->with($account, $order) - ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) - ->willReturn(['request-body']); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['request-body'], - ] + $requestData, + ['request-body'], + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPostPayment($order); @@ -393,36 +380,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $requestData = ['createStatusRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createStatusRequestData') ->with($account, $order) - ->willReturn(['createStatusRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createStatusRequestData'], $txType) - ->willReturn(['request-body']); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['request-body'], - ] + $requestData, + ['request-body'], + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->status($order); @@ -433,36 +414,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createCancelRequestData') ->with($account, $order) - ->willReturn(['createCancelRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createCancelRequestData'], $txType) - ->willReturn(['request-body']); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['request-body'], - ] + $requestData, + ['request-body'], + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->cancel($order); @@ -473,36 +448,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createRefundRequestData') ->with($account, $order) - ->willReturn(['createRefundRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createRefundRequestData'], $txType) - ->willReturn(['request-body']); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['request-body'], - ] + $requestData, + ['request-body'], + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->refund($order); @@ -593,27 +562,46 @@ public static function refundRequestDataProvider(): array ]; } - public static function historyRequestDataProvider(): array + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + array $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel + ): void { - return [ - [ - 'order' => [ - 'id' => '2020110828BC', - ], - 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', - ], - ]; - } + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); - public static function orderHistoryRequestDataProvider(): array - { - return [ + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); + + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, [ - 'order' => [ - 'id' => '2020110828BC', - ], - 'api_url' => 'https://test.inter-vpos.com.tr/mpi/Default.aspx', + 'form_params' => $encodedRequestData, ], - ]; + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); } } diff --git a/tests/Unit/Gateways/KuveytPosTest.php b/tests/Unit/Gateways/KuveytPosTest.php index d8a0c837..c900c100 100644 --- a/tests/Unit/Gateways/KuveytPosTest.php +++ b/tests/Unit/Gateways/KuveytPosTest.php @@ -11,6 +11,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\KuveytPosAccount; 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; @@ -29,6 +30,7 @@ /** * @covers \Mews\Pos\Gateways\KuveytPos + * @covers \Mews\Pos\Gateways\AbstractGateway */ class KuveytPosTest extends TestCase { @@ -193,7 +195,8 @@ public function testGetCommon3DFormDataSuccessResponse(): void $txType = PosInterface::TX_TYPE_PAY_AUTH; $paymentModel = PosInterface::MODEL_3D_SECURE; $card = $this->card; - $requestData = ['form-data']; + $requestData = ['form-data']; + $order = $this->order; $this->configureClientResponse( $txType, 'https://boatest.kuveytturk.com.tr/boa.virtualpos.services/Home/ThreeDModelPayGate', @@ -201,11 +204,10 @@ public function testGetCommon3DFormDataSuccessResponse(): void 'encoded-request-data', $response, ['form_inputs' => ['form-inputs'], 'gateway' => 'form-action-url'], + $order, + $paymentModel ); - $this->eventDispatcherMock->expects(self::once()) - ->method('dispatch'); - $this->requestMapperMock->expects(self::once()) ->method('create3DEnrollmentCheckRequestData') ->with( @@ -228,7 +230,7 @@ public function testGetCommon3DFormDataSuccessResponse(): void $card ) ->willReturn(['3d-form-data']); - $result = $this->pos->get3DFormData($this->order, $paymentModel, $txType, $card); + $result = $this->pos->get3DFormData($order, $paymentModel, $txType, $card); $this->assertSame(['3d-form-data'], $result); } @@ -281,6 +283,18 @@ public function testMake3DPayment( ], ] ); + $paymentModel = PosInterface::MODEL_3D_SECURE; + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $create3DPaymentRequestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $create3DPaymentRequestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); $this->serializerMock->expects(self::once()) ->method('encode') @@ -333,22 +347,25 @@ public function testMake3DPayment( */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); + ->willReturn($requestData); $paymentResponse = ['paymentResponse']; $this->configureClientResponse( $txType, $apiUrl, - ['createNonSecurePaymentRequestData'], + $requestData, 'request-body', 'response-body', - $paymentResponse + $paymentResponse, + $order, + PosInterface::MODEL_NON_SECURE ); $this->responseMapperMock->expects(self::once()) @@ -455,40 +472,6 @@ public static function makeRegularPaymentDataProvider(): array ]; } - private function configureClientResponse( - string $txType, - string $apiUrl, - array $requestData, - string $encodedRequestData, - string $responseContent, - array $decodedResponse, - ?int $statusCode = null - ): void - { - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with($requestData, $txType) - ->willReturn($encodedRequestData); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with($responseContent, $txType) - ->willReturn($decodedResponse); - - $this->prepareClient( - $this->httpClientMock, - $responseContent, - $apiUrl, - [ - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - 'body' => $encodedRequestData, - ], - $statusCode - ); - } - public static function getApiUrlDataProvider(): array { return [ @@ -535,4 +518,52 @@ public static function getApiUrlExceptionDataProvider(): array ], ]; } + + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + string $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel, + ?int $statusCode = null + ): void + { + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); + + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, + [ + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + 'body' => $encodedRequestData, + ], + $statusCode + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); + } } diff --git a/tests/Unit/Gateways/PayFlexCPV4PosTest.php b/tests/Unit/Gateways/PayFlexCPV4PosTest.php index 158aad82..d6adf81e 100644 --- a/tests/Unit/Gateways/PayFlexCPV4PosTest.php +++ b/tests/Unit/Gateways/PayFlexCPV4PosTest.php @@ -14,6 +14,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PayFlexAccount; 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; @@ -32,6 +33,7 @@ /** * @covers \Mews\Pos\Gateways\PayFlexCPV4Pos + * @covers \Mews\Pos\Gateways\AbstractGateway */ class PayFlexCPV4PosTest extends TestCase { @@ -153,6 +155,7 @@ public function testGet3DFormDataSuccess(): void $enrollmentResponse = PayFlexCPV4PosRequestDataMapperTest::threeDFormDataProvider()->current()['queryParams']; $txType = PosInterface::TX_TYPE_PAY_AUTH; $paymentModel = PosInterface::MODEL_3D_SECURE; + $requestData = ['request-data']; $card = $this->card; $order = $this->order; @@ -165,23 +168,19 @@ public function testGet3DFormDataSuccess(): void $paymentModel, $card ) - ->willReturn(['request-data']); + ->willReturn($requestData); - $this->serializerMock->expects(self::never()) - ->method('encode'); - - $this->prepareClient( - $this->httpClientMock, - 'response-content', + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['payment_api'], - ['form_params' => ['request-data']], + $requestData, + $requestData, + 'response-body', + $enrollmentResponse, + $order, + $paymentModel ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-content', $txType) - ->willReturn($enrollmentResponse); - $this->requestMapperMock->expects(self::once()) ->method('create3DFormData') ->with( @@ -211,6 +210,7 @@ public function testGet3DFormDataEnrollmentFail(): void $paymentModel = PosInterface::MODEL_3D_SECURE; $card = $this->card; $order = $this->order; + $requestData = ['request-data']; $this->requestMapperMock->expects(self::once()) ->method('create3DEnrollmentCheckRequestData') @@ -221,28 +221,25 @@ public function testGet3DFormDataEnrollmentFail(): void $paymentModel, $card ) - ->willReturn(['request-data']); - - $this->serializerMock->expects(self::never()) - ->method('encode'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-content', + $enrollmentResponse = [ + 'CommonPaymentUrl' => null, + 'PaymentToken' => null, + 'ErrorCode' => '5007', + 'ResponseMessage' => 'Güvenlik Numarası Hatalı', + ]; + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['payment_api'], - ['form_params' => ['request-data']], + $requestData, + $requestData, + 'response-body', + $enrollmentResponse, + $order, + $paymentModel ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-content', $txType) - ->willReturn([ - 'CommonPaymentUrl' => null, - 'PaymentToken' => null, - 'ErrorCode' => '5007', - 'ResponseMessage' => 'Güvenlik Numarası Hatalı', - ]); - $this->requestMapperMock->expects(self::never()) ->method('create3DFormData'); @@ -291,23 +288,18 @@ public function testMake3DPayPayment( ->method('create3DPaymentStatusRequestData') ->with($this->account, $request->query->all()) ->willReturn($create3DPaymentStatusRequestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['query_api'], - [ - 'form_params' => $create3DPaymentStatusRequestData, - ], + $create3DPaymentStatusRequestData, + $create3DPaymentStatusRequestData, + 'response-body', + $paymentResponse, + $order, + PosInterface::MODEL_3D_PAY ); - $this->serializerMock->expects(self::never()) - ->method('encode'); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn($paymentResponse); - $this->responseMapperMock->expects(self::once()) ->method('map3DPayResponseData') ->with($request->query->all(), $txType, $order) @@ -323,6 +315,8 @@ public function testMake3DPayPayment( ->method('encode'); $this->serializerMock->expects(self::never()) ->method('decode'); + $this->eventDispatcherMock->expects(self::never()) + ->method('dispatch'); } $this->pos->make3DPayPayment($request, $order, $txType); @@ -337,34 +331,29 @@ public function testMake3DPayPayment( */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + ->willReturn($requestData); + + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $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) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -375,36 +364,30 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPostPayment($order); @@ -421,36 +404,30 @@ public function testStatusRequest(): void */ public function testCancelRequest(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_CANCEL; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->cancel($order); @@ -461,36 +438,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->refund($order); @@ -599,4 +570,50 @@ public static function refundRequestDataProvider(): array ], ]; } + + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel + ): void + { + if ($requestData === $encodedRequestData) { + $this->serializerMock->expects(self::never()) + ->method('encode'); + } else { + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); + } + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); + + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, + is_string($encodedRequestData) ? ['body' => $encodedRequestData] : ['form_params' => $encodedRequestData], + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); + } } diff --git a/tests/Unit/Gateways/PayFlexV4PosTest.php b/tests/Unit/Gateways/PayFlexV4PosTest.php index 0f0d3dcc..3cb5b433 100644 --- a/tests/Unit/Gateways/PayFlexV4PosTest.php +++ b/tests/Unit/Gateways/PayFlexV4PosTest.php @@ -13,6 +13,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PayFlexAccount; 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; @@ -31,6 +32,7 @@ /** * @covers \Mews\Pos\Gateways\PayFlexV4Pos + * @covers \Mews\Pos\Gateways\AbstractGateway */ class PayFlexV4PosTest extends TestCase { @@ -150,30 +152,29 @@ public function testInit(): void public function testGet3DFormDataEnrollmentFail(): void { $this->expectException(Exception::class); + $txType = PosInterface::TX_TYPE_PAY_AUTH; + $requestData = ['request-data']; + $order = $this->order; $this->requestMapperMock->expects(self::once()) ->method('create3DEnrollmentCheckRequestData') - ->with($this->pos->getAccount(), $this->order, $this->card) - ->willReturn(['request-data']); + ->with($this->pos->getAccount(), $order, $this->card) + ->willReturn($requestData); - $this->serializerMock->expects(self::never()) - ->method('encode'); - - $this->prepareClient( - $this->httpClientMock, - 'response-content', + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['gateway_3d'], - ['form_params' => ['request-data']], + $requestData, + $requestData, + 'response-body', + self::getSampleEnrollmentFailResponseDataProvider(), + $order, + PosInterface::MODEL_3D_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-content', PosInterface::TX_TYPE_PAY_AUTH) - ->willReturn(self::getSampleEnrollmentFailResponseDataProvider()); - $this->requestMapperMock->expects(self::never()) ->method('create3DFormData'); - $this->pos->get3DFormData($this->order, PosInterface::MODEL_3D_SECURE, PosInterface::TX_TYPE_PAY_AUTH, $this->card); + $this->pos->get3DFormData($order, PosInterface::MODEL_3D_SECURE, $txType, $this->card); } /** @@ -184,31 +185,28 @@ public function testGet3DFormDataEnrollmentFail(): void public function testGet3DFormDataSuccess(): void { $enrollmentResponse = PayFlexV4PosRequestDataMapperTest::getSampleEnrollmentSuccessResponseDataProvider(); - $txType = PosInterface::TX_TYPE_PAY_AUTH; - $paymentModel = PosInterface::MODEL_3D_SECURE; - $card = $this->card; - $order = $this->order; + $txType = PosInterface::TX_TYPE_PAY_AUTH; + $paymentModel = PosInterface::MODEL_3D_SECURE; + $card = $this->card; + $order = $this->order; + $requestData = ['request-data']; $this->requestMapperMock->expects(self::once()) ->method('create3DEnrollmentCheckRequestData') ->with($this->pos->getAccount(), $order, $card) - ->willReturn(['request-data']); - - $this->serializerMock->expects(self::never()) - ->method('encode'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-content', + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['gateway_3d'], - ['form_params' => ['request-data']], + $requestData, + $requestData, + 'response-body', + $enrollmentResponse, + $order, + $paymentModel ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-content', $txType) - ->willReturn($enrollmentResponse); - $this->requestMapperMock->expects(self::once()) ->method('create3DFormData') ->with( @@ -263,24 +261,18 @@ public function testMake3DPayment( ->method('create3DPaymentRequestData') ->with($this->account, $order, $txType, $request->request->all()) ->willReturn($create3DPaymentRequestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['payment_api'], - [ - 'form_params' => ['prmstr' => 'request-body'], - ], + $create3DPaymentRequestData, + 'request-body', + 'response-body', + $paymentResponse, + $order, + PosInterface::MODEL_3D_SECURE ); - $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) @@ -296,6 +288,8 @@ public function testMake3DPayment( ->method('encode'); $this->serializerMock->expects(self::never()) ->method('decode'); + $this->eventDispatcherMock->expects(self::never()) + ->method('dispatch'); } $this->pos->make3DPayment($request, $order, $txType); @@ -326,34 +320,29 @@ public function testMake3DPayPayment(): void */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + ->willReturn($requestData); + + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['prmstr' => 'request-body'], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $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) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -364,36 +353,29 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; - + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePostAuthPaymentRequestData') ->with($account, $order) - ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['prmstr' => 'request-body'], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPostPayment($order); @@ -405,36 +387,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $requestData = ['createStatusRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['prmstr' => 'request-body'], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->status($order); @@ -445,36 +421,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createCancelRequestData') ->with($account, $order) - ->willReturn(['createCancelRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createCancelRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['prmstr' => 'request-body'], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->cancel($order); @@ -485,36 +455,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createRefundRequestData') ->with($account, $order) - ->willReturn(['createRefundRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createRefundRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'form_params' => ['prmstr' => 'request-body'], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->refund($order); @@ -546,9 +510,6 @@ public static function getSampleEnrollmentFailResponseDataProvider(): array ]; } - - - public static function makeRegularPaymentDataProvider(): array { return [ @@ -659,4 +620,52 @@ public static function make3DPaymentDataProvider(): array ], ]; } + + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel + ): void + { + if ($requestData === $encodedRequestData) { + $this->serializerMock->expects(self::never()) + ->method('encode'); + } else { + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); + } + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); + + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, + [ + 'form_params' => is_string($encodedRequestData) ? ['prmstr' => $encodedRequestData] : $encodedRequestData + ] + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); + } } diff --git a/tests/Unit/Gateways/PayForTest.php b/tests/Unit/Gateways/PayForTest.php index a7688c53..ebee9a6c 100644 --- a/tests/Unit/Gateways/PayForTest.php +++ b/tests/Unit/Gateways/PayForTest.php @@ -11,6 +11,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PayForAccount; use Mews\Pos\Entity\Card\CreditCardInterface; +use Mews\Pos\Event\RequestDataPreparedEvent; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\Factory\CreditCardFactory; use Mews\Pos\Gateways\PayForPos; @@ -26,6 +27,7 @@ /** * @covers \Mews\Pos\Gateways\PayForPos + * @covers \Mews\Pos\Gateways\AbstractGateway */ class PayForTest extends TestCase { @@ -218,27 +220,18 @@ public function testMake3DPayment( ->method('create3DPaymentRequestData') ->with($this->account, $order, $txType, $request->request->all()) ->willReturn($create3DPaymentRequestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['payment_api'], - [ - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - 'body' => 'request-body', - ], + $create3DPaymentRequestData, + 'request-body', + 'response-body', + $paymentResponse, + $order, + PosInterface::MODEL_3D_SECURE ); - $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) @@ -254,6 +247,8 @@ public function testMake3DPayment( ->method('encode'); $this->serializerMock->expects(self::never()) ->method('decode'); + $this->eventDispatcherMock->expects(self::never()) + ->method('dispatch'); } $this->pos->make3DPayment($request, $order, $txType); @@ -322,37 +317,29 @@ public function testMake3DHostPayment(): void */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + ->willReturn($requestData); + + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $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) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -363,39 +350,30 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPostPayment($order); @@ -407,40 +385,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $requestData = ['createStatusRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createStatusRequestData') ->with($account, $order) - ->willReturn(['createStatusRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createStatusRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->status($order); @@ -451,39 +419,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->cancel($order); @@ -494,39 +453,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->refund($order); @@ -537,39 +487,30 @@ public function testRefundRequest(array $order, string $apiUrl): void */ public function testHistoryRequest(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_HISTORY; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_HISTORY; + $requestData = ['createHistoryRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapHistoryResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->history($order); @@ -580,39 +521,30 @@ public function testHistoryRequest(array $order, string $apiUrl): void */ public function testOrderHistoryRequest(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $requestData = ['createOrderHistoryRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapOrderHistoryResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->orderHistory($order); @@ -742,4 +674,50 @@ public static function orderHistoryRequestDataProvider(): array ], ]; } + + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + string $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel + ): void + { + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); + + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, + [ + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + 'body' => $encodedRequestData, + ], + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); + } } diff --git a/tests/Unit/Gateways/PosNetTest.php b/tests/Unit/Gateways/PosNetTest.php index 2b1f2f82..5a69e42d 100644 --- a/tests/Unit/Gateways/PosNetTest.php +++ b/tests/Unit/Gateways/PosNetTest.php @@ -14,6 +14,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PosNetAccount; 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; @@ -31,6 +32,7 @@ /** * @covers \Mews\Pos\Gateways\PosNet + * @covers \Mews\Pos\Gateways\AbstractGateway */ class PosNetTest extends TestCase { @@ -148,43 +150,36 @@ public function testInit(): void */ public function testGet3DFormDataOosTransactionFail(): void { + $txType = PosInterface::TX_TYPE_PAY_AUTH; + $requestData = ['request-data']; + $order = $this->order; $this->expectException(Exception::class); $this->requestMapperMock->expects(self::once()) ->method('create3DEnrollmentCheckRequestData') - ->with($this->pos->getAccount(), $this->order, PosInterface::TX_TYPE_PAY_AUTH, $this->card) - ->willReturn(['request-data']); - - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['request-data'], PosInterface::TX_TYPE_PAY_AUTH) - ->willReturn('encoded-request-data'); - - $this->prepareClient( - $this->httpClientMock, - 'response-content', - $this->config['gateway_endpoints']['payment_api'], - [ - 'headers' => [ - 'Content-Type' => 'application/x-www-form-urlencoded', - ], - 'body' => \sprintf('xmldata=%s', 'encoded-request-data'), - ], - ); + ->with($this->pos->getAccount(), $order, $txType, $this->card) + ->willReturn($requestData); $responseData = [ 'approved' => '0', 'respCode' => '0003', 'respText' => '148 MID,TID,IP HATALI:89.244.149.137', ]; - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-content', PosInterface::TX_TYPE_PAY_AUTH) - ->willReturn($responseData); + + $this->configureClientResponse( + $txType, + $this->config['gateway_endpoints']['payment_api'], + $requestData, + 'request-body', + 'response-body', + $responseData, + $order, + PosInterface::MODEL_3D_SECURE + ); $this->requestMapperMock->expects(self::never()) ->method('create3DFormData'); - $this->pos->get3DFormData($this->order, PosInterface::MODEL_3D_SECURE, PosInterface::TX_TYPE_PAY_AUTH, $this->card); + $this->pos->get3DFormData($order, PosInterface::MODEL_3D_SECURE, $txType, $this->card); } /** @@ -293,31 +288,45 @@ public function testMake3DPayment( ] ); + $paymentModel = PosInterface::MODEL_3D_SECURE; + $this->eventDispatcherMock->expects(self::exactly(2)) + ->method('dispatch') + // could not find another way expect using deprecated withConsecutive() function + ->withConsecutive( + [$this->callback(function ($dispatchedEvent) use ($txType, $resolveMerchantRequestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $resolveMerchantRequestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })], + [$this->callback(function ($dispatchedEvent) use ($txType, $create3DPaymentRequestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $create3DPaymentRequestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })] + ); + $this->responseMapperMock->expects(self::once()) ->method('map3DPaymentData') ->with($resolveResponse, $paymentResponse, $txType, $order) ->willReturn($expectedResponse); } else { - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with($resolveMerchantRequestData, $txType) - ->willReturn('resolveMerchantRequestData-body'); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('resolveMerchantRequestData-body', $txType) - ->willReturn($resolveResponse); - - $this->prepareClient( - $this->httpClientMock, - 'resolveMerchantRequestData-body', + $this->configureClientResponse( + $txType, $this->config['gateway_endpoints']['payment_api'], - [ - 'headers' => [ - 'Content-Type' => 'application/x-www-form-urlencoded', - ], - 'body' => \sprintf('xmldata=%s', 'resolveMerchantRequestData-body'), - ], + $resolveMerchantRequestData, + 'resolveMerchantRequestData-body', + 'resolveMerchantRequestData-body', + $resolveResponse, + $order, + PosInterface::MODEL_3D_SECURE ); $this->responseMapperMock->expects(self::once()) @@ -357,37 +366,29 @@ public function testMake3DPayPayment(): void */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + ->willReturn($requestData); + + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'xmldata=request-body', - 'headers' => [ - 'Content-Type' => 'application/x-www-form-urlencoded', - ], - ], + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $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) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -398,39 +399,30 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePostAuthPaymentRequestData') ->with($account, $order) - ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'xmldata=request-body', - 'headers' => [ - 'Content-Type' => 'application/x-www-form-urlencoded', - ], - ], + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPostPayment($order); @@ -442,40 +434,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $requestData = ['createStatusRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'xmldata=request-body', - 'headers' => [ - 'Content-Type' => 'application/x-www-form-urlencoded', - ], - ], + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->status($order); @@ -486,39 +468,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'xmldata=request-body', - 'headers' => [ - 'Content-Type' => 'application/x-www-form-urlencoded', - ], - ], + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->cancel($order); @@ -529,39 +502,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createRefundRequestData') ->with($account, $order) - ->willReturn(['createRefundRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createRefundRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'xmldata=request-body', - 'headers' => [ - 'Content-Type' => 'application/x-www-form-urlencoded', - ], - ], + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->refund($order); @@ -689,27 +653,49 @@ public static function refundRequestDataProvider(): array ]; } - public static function historyRequestDataProvider(): array + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + string $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel + ): void { - return [ - [ - 'order' => [ - 'id' => '2020110828BC', - ], - 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', - ], - ]; - } + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); - public static function orderHistoryRequestDataProvider(): array - { - return [ + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); + + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, [ - 'order' => [ - 'id' => '2020110828BC', + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded', ], - 'api_url' => 'https://setmpos.ykb.com/PosnetWebService/XML', + 'body' => \sprintf('xmldata=%s', $encodedRequestData), ], - ]; + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); } } diff --git a/tests/Unit/Gateways/PosNetV1PosTest.php b/tests/Unit/Gateways/PosNetV1PosTest.php index 39de6f7e..6ccc040b 100644 --- a/tests/Unit/Gateways/PosNetV1PosTest.php +++ b/tests/Unit/Gateways/PosNetV1PosTest.php @@ -11,6 +11,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\PosNetAccount; 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; @@ -28,6 +29,7 @@ /** * @covers \Mews\Pos\Gateways\PosNetV1Pos + * @covers \Mews\Pos\Gateways\AbstractGateway */ class PosNetV1PosTest extends TestCase { @@ -211,27 +213,18 @@ public function testMake3DPayment( ->method('create3DPaymentRequestData') ->with($this->account, $order, $txType, $request->request->all()) ->willReturn($create3DPaymentRequestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + + $this->configureClientResponse( + $txType, 'https://epostest.albarakaturk.com.tr/ALBMerchantService/MerchantJSONAPI.svc/Sale', - [ - 'headers' => [ - 'Content-Type' => 'application/json', - ], - 'body' => 'request-body', - ] + $create3DPaymentRequestData, + 'request-body', + 'response-body', + $paymentResponse, + $order, + PosInterface::MODEL_3D_SECURE ); - $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) @@ -247,6 +240,8 @@ public function testMake3DPayment( ->method('encode'); $this->serializerMock->expects(self::never()) ->method('decode'); + $this->eventDispatcherMock->expects(self::never()) + ->method('dispatch'); } $this->pos->make3DPayment($request, $order, $txType); @@ -277,43 +272,35 @@ public function testMake3DPayPayment(): void */ public function testMakeRegularPayment(array $order, string $txType, string $mappedTxType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('mapTxType') ->with($txType) ->willReturn($mappedTxType); - $this->requestMapperMock->expects(self::once()) + $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + ->willReturn($requestData); + + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'application/json', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $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) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -324,8 +311,9 @@ public function testMakeRegularPayment(array $order, string $txType, string $map */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('mapTxType') @@ -335,33 +323,23 @@ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): vo $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'application/json', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPostPayment($order); @@ -373,8 +351,9 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $requestData = ['createStatusRequestData']; $this->requestMapperMock->expects(self::once()) ->method('mapTxType') @@ -384,34 +363,23 @@ public function testStatusRequest(array $order, string $apiUrl): void $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'application/json', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->status($order); @@ -422,8 +390,9 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $this->requestMapperMock->expects(self::once()) ->method('mapTxType') @@ -433,33 +402,23 @@ public function testCancelRequest(array $order, string $apiUrl): void $this->requestMapperMock->expects(self::once()) ->method('createCancelRequestData') ->with($account, $order) - ->willReturn(['createCancelRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createCancelRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'application/json', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->cancel($order); @@ -470,8 +429,9 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $this->requestMapperMock->expects(self::once()) ->method('mapTxType') @@ -481,33 +441,23 @@ public function testRefundRequest(array $order, string $apiUrl): void $this->requestMapperMock->expects(self::once()) ->method('createRefundRequestData') ->with($account, $order) - ->willReturn(['createRefundRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createRefundRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'application/json', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->refund($order); @@ -525,24 +475,6 @@ public function testOrderHistoryRequest(): void $this->pos->orderHistory([]); } - public static function make3dPaymentTestProvider(): iterable - { - $dataSamples = iterator_to_array(PosNetV1PosResponseDataMapperTest::threeDPaymentDataProvider()); - $success1Data = $dataSamples['success1']; - yield 'success1' => [ - 'order' => [ - 'id' => $success1Data['expectedData']['order_id'], - 'amount' => 1.75, - 'installment' => 0, - 'currency' => PosInterface::CURRENCY_TRY, - 'success_url' => 'https://domain.com/success', - ], - 'threeDResponseData' => $success1Data['threeDResponseData'], - 'paymentResponseData' => $success1Data['paymentData'], - 'expectedData' => $success1Data['expectedData'], - ]; - } - public static function getApiURLDataProvider(): iterable { yield [ @@ -670,4 +602,50 @@ public static function refundRequestDataProvider(): array ], ]; } + + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + string $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel + ): void + { + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); + + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); + + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, + [ + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'body' => $encodedRequestData, + ], + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); + } } diff --git a/tests/Unit/Gateways/ToslaPosTest.php b/tests/Unit/Gateways/ToslaPosTest.php index 1a98b632..ea80211b 100644 --- a/tests/Unit/Gateways/ToslaPosTest.php +++ b/tests/Unit/Gateways/ToslaPosTest.php @@ -26,13 +26,12 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\EventDispatcher\EventDispatcherInterface; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\StreamInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; /** * @covers \Mews\Pos\Gateways\ToslaPos + * @covers \Mews\Pos\Gateways\AbstractGateway */ class ToslaPosTest extends TestCase { @@ -272,6 +271,8 @@ public function testGet3DFormData( $encodedRequestData, $responseData, $decodedResponseData, + $order, + $paymentModel ); $this->requestMapperMock->expects(self::once()) @@ -304,27 +305,28 @@ public function testStatus( bool $isSuccess ): void { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $this->requestMapperMock->expects(self::once()) ->method('createStatusRequestData') - ->with($this->pos->getAccount(), $order) + ->with($account, $order) ->willReturn($requestData); - $this->eventDispatcherMock->expects(self::once()) - ->method('dispatch') - ->with($this->logicalAnd($this->isInstanceOf(RequestDataPreparedEvent::class))); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') ->with($decodedResponse) ->willReturn($mappedResponse); $this->configureClientResponse( - PosInterface::TX_TYPE_STATUS, + $txType, 'https://ent.akodepos.com/api/Payment/inquiry', $requestData, $encodedRequest, $responseContent, - $decodedResponse + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); $this->pos->status($order); @@ -353,10 +355,6 @@ public function testCancel( ->with($this->pos->getAccount(), $order) ->willReturn($requestData); - $this->eventDispatcherMock->expects(self::once()) - ->method('dispatch') - ->with($this->logicalAnd($this->isInstanceOf(RequestDataPreparedEvent::class))); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') ->with($decodedResponse) @@ -368,7 +366,9 @@ public function testCancel( $requestData, $encodedRequest, $responseContent, - $decodedResponse + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); $this->pos->cancel($order); @@ -396,10 +396,6 @@ public function testRefund( ->with($this->pos->getAccount(), $order) ->willReturn($requestData); - $this->eventDispatcherMock->expects(self::once()) - ->method('dispatch') - ->with($this->logicalAnd($this->isInstanceOf(RequestDataPreparedEvent::class))); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') ->with($decodedResponse) @@ -411,7 +407,9 @@ public function testRefund( $requestData, $encodedRequest, $responseContent, - $decodedResponse + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); $this->pos->refund($order); @@ -440,27 +438,28 @@ public function testOrderHistory( bool $isSuccess ): void { + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $this->requestMapperMock->expects(self::once()) ->method('createOrderHistoryRequestData') - ->with($this->pos->getAccount(), $order) + ->with($account, $order) ->willReturn($requestData); - $this->eventDispatcherMock->expects(self::once()) - ->method('dispatch') - ->with($this->logicalAnd($this->isInstanceOf(RequestDataPreparedEvent::class))); - $this->responseMapperMock->expects(self::once()) ->method('mapOrderHistoryResponse') ->with($decodedResponse) ->willReturn($mappedResponse); $this->configureClientResponse( - PosInterface::TX_TYPE_ORDER_HISTORY, + $txType, 'https://ent.akodepos.com/api/Payment/history', $requestData, $encodedRequest, $responseContent, - $decodedResponse + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); $this->pos->orderHistory($order); @@ -579,37 +578,30 @@ public static function make3DPayPaymentDataProvider(): array */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $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', - ], - ] - ); + ->willReturn($requestData); - $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']); + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, + $apiUrl, + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE + ); $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -620,39 +612,30 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'application/json', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPostPayment($order); @@ -664,40 +647,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $requestData = ['createStatusRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'application/json', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->status($order); @@ -708,39 +681,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'application/json', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->cancel($order); @@ -751,86 +715,35 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'application/json', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->refund($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' => 'application/json', - ], - ] - ); - - $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 getApiUrlDataProvider(): array { @@ -950,25 +863,15 @@ public static function refundRequestDataProvider(): array ]; } - public static function orderHistoryRequestDataProvider(): array - { - return [ - [ - 'order' => [ - 'id' => '2020110828BC', - ], - 'api_url' => 'https://ent.akodepos.com/api/Payment/history', - ], - ]; - } - private function configureClientResponse( string $txType, string $apiUrl, array $requestData, string $encodedRequestData, string $responseContent, - array $decodedResponse + array $decodedResponse, + array $order, + string $paymentModel ): void { $this->serializerMock->expects(self::once()) @@ -980,23 +883,28 @@ private function configureClientResponse( ->with($responseContent, $txType) ->willReturn($decodedResponse); - $responseMock = $this->createMock(ResponseInterface::class); - $streamMock = $this->createMock(StreamInterface::class); - $streamMock->expects(self::once()) - ->method('getContents') - ->willReturn($responseContent); - $responseMock->expects(self::once()) - ->method('getBody') - ->willReturn($streamMock); - $this->httpClientMock->expects(self::once()) - ->method('post') - ->with($apiUrl, [ + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, + [ 'headers' => [ 'Content-Type' => 'application/json', ], 'body' => $encodedRequestData, - ]) - ->willReturn($responseMock); - } + ], + ); + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); + } } diff --git a/tests/Unit/Gateways/VakifKatilimTest.php b/tests/Unit/Gateways/VakifKatilimTest.php index 26e9a873..bad22549 100644 --- a/tests/Unit/Gateways/VakifKatilimTest.php +++ b/tests/Unit/Gateways/VakifKatilimTest.php @@ -11,6 +11,7 @@ use Mews\Pos\DataMapper\ResponseDataMapper\ResponseDataMapperInterface; use Mews\Pos\Entity\Account\KuveytPosAccount; 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; @@ -28,6 +29,7 @@ /** * @covers \Mews\Pos\Gateways\VakifKatilimPos + * @covers \Mews\Pos\Gateways\AbstractGateway */ class VakifKatilimTest extends TestCase { @@ -174,35 +176,24 @@ public function testGetApiURLException(string $txType, string $paymentModel, str */ public function testGetCommon3DFormDataSuccessResponse(): void { - $response = 'bank-api-html-response'; $txType = PosInterface::TX_TYPE_PAY_AUTH; $paymentModel = PosInterface::MODEL_3D_SECURE; $card = $this->card; + $requestData = ['form-data']; + $order = $this->order; - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['form-data'], $txType) - ->willReturn('encoded-request-data'); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with($response, $txType) - ->willReturn(['form_inputs' => ['form-inputs'], 'gateway' => 'form-action-url']); - $this->prepareClient( - $this->httpClientMock, - $response, + $decodedResponse = ['form_inputs' => ['form-inputs'], 'gateway' => 'form-action-url']; + $this->configureClientResponse( + $txType, 'https://boa.vakifkatilim.com.tr/VirtualPOS.Gateway/Home/ThreeDModelPayGate', - [ - 'body' => 'encoded-request-data', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ], + $requestData, + 'request-body', + 'bank-api-html-response', + $decodedResponse, + $order, + $paymentModel ); - $this->eventDispatcherMock->expects(self::once()) - ->method('dispatch'); - $this->requestMapperMock->expects(self::once()) ->method('create3DEnrollmentCheckRequestData') ->with( @@ -212,7 +203,7 @@ public function testGetCommon3DFormDataSuccessResponse(): void $txType, $card ) - ->willReturn(['form-data']); + ->willReturn($requestData); $this->requestMapperMock->expects(self::once()) ->method('create3DFormData') @@ -225,7 +216,7 @@ public function testGetCommon3DFormDataSuccessResponse(): void $card ) ->willReturn(['3d-form-data']); - $result = $this->pos->get3DFormData($this->order, $paymentModel, $txType, $card); + $result = $this->pos->get3DFormData($order, $paymentModel, $txType, $card); $this->assertSame(['3d-form-data'], $result); } @@ -282,39 +273,23 @@ public function testMake3DPayment( '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->configureClientResponse( + $txType, 'https://boa.vakifkatilim.com.tr/VirtualPOS.Gateway/Home/ThreeDModelProvisionGate', - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $create3DPaymentRequestData, + 'request-body', + 'response-body', + $paymentResponse, + $order, + PosInterface::MODEL_3D_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with($create3DPaymentRequestData, $txType) - ->willReturn('request-body'); - - $this->serializerMock->expects(self::exactly(1)) - ->method('decode') - ->willReturnMap([ - [ - 'response-body', - $txType, - $paymentResponse, - ], - ]); - $this->responseMapperMock->expects(self::once()) ->method('map3DPaymentData') ->with($request->request->all(), $paymentResponse, $txType, $order) @@ -332,6 +307,8 @@ public function testMake3DPayment( ->method('decode') ->with(urldecode($request->request->get('AuthenticationResponse')), $txType) ->willReturn($request->request->all()); + $this->eventDispatcherMock->expects(self::never()) + ->method('dispatch'); } $this->pos->make3DPayment($request, $order, $txType); @@ -378,37 +355,29 @@ public function testMake3DPayPayment(): void */ public function testMakeRegularPayment(array $order, string $txType, string $apiUrl): void { - $account = $this->pos->getAccount(); - $card = $this->card; + $account = $this->pos->getAccount(); + $card = $this->card; + $requestData = ['createNonSecurePaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePaymentRequestData') ->with($account, $order, $txType, $card) - ->willReturn(['createNonSecurePaymentRequestData']); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + ->willReturn($requestData); + + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $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) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPayment($order, $card, $txType); @@ -419,39 +388,30 @@ public function testMakeRegularPayment(array $order, string $txType, string $api */ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_PAY_POST_AUTH; + $requestData = ['createNonSecurePostAuthPaymentRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createNonSecurePostAuthPaymentRequestData') ->with($account, $order) - ->willReturn(['createNonSecurePostAuthPaymentRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createNonSecurePostAuthPaymentRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['paymentResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapPaymentResponse') - ->with(['paymentResponse'], $txType, $order) + ->with($decodedResponse, $txType, $order) ->willReturn(['result']); $this->pos->makeRegularPostPayment($order); @@ -463,40 +423,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_STATUS; + $requestData = ['createStatusRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapStatusResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->status($order); @@ -507,39 +457,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_CANCEL; + $requestData = ['createCancelRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createCancelRequestData') ->with($account, $order) - ->willReturn(['createCancelRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createCancelRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapCancelResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->cancel($order); @@ -550,39 +491,30 @@ 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; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_REFUND; + $requestData = ['createRefundRequestData']; $this->requestMapperMock->expects(self::once()) ->method('createRefundRequestData') ->with($account, $order) - ->willReturn(['createRefundRequestData']); + ->willReturn($requestData); - $this->serializerMock->expects(self::once()) - ->method('encode') - ->with(['createRefundRequestData'], $txType) - ->willReturn('request-body'); - - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapRefundResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->refund($order); @@ -594,39 +526,30 @@ public function testRefundRequest(array $order, string $apiUrl): void */ public function testHistoryRequest(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_HISTORY; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_HISTORY; + $requestData = ['createHistoryRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapHistoryResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->history($order); @@ -637,39 +560,30 @@ public function testHistoryRequest(array $order, string $apiUrl): void */ public function testOrderHistoryRequest(array $order, string $apiUrl): void { - $account = $this->pos->getAccount(); - $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $account = $this->pos->getAccount(); + $txType = PosInterface::TX_TYPE_ORDER_HISTORY; + $requestData = ['createOrderHistoryRequestData']; $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'); + ->willReturn($requestData); - $this->prepareClient( - $this->httpClientMock, - 'response-body', + $decodedResponse = ['decodedData']; + $this->configureClientResponse( + $txType, $apiUrl, - [ - 'body' => 'request-body', - 'headers' => [ - 'Content-Type' => 'text/xml; charset=UTF-8', - ], - ] + $requestData, + 'request-body', + 'response-body', + $decodedResponse, + $order, + PosInterface::MODEL_NON_SECURE ); - $this->serializerMock->expects(self::once()) - ->method('decode') - ->with('response-body', $txType) - ->willReturn(['decodedResponse']); - $this->responseMapperMock->expects(self::once()) ->method('mapOrderHistoryResponse') - ->with(['decodedResponse']) + ->with($decodedResponse) ->willReturn(['result']); $this->pos->orderHistory($order); @@ -878,4 +792,49 @@ public static function orderHistoryRequestDataProvider(): array ], ]; } + + private function configureClientResponse( + string $txType, + string $apiUrl, + array $requestData, + string $encodedRequestData, + string $responseContent, + array $decodedResponse, + array $order, + string $paymentModel + ): void + { + $this->serializerMock->expects(self::once()) + ->method('encode') + ->with($requestData, $txType) + ->willReturn($encodedRequestData); + $this->serializerMock->expects(self::once()) + ->method('decode') + ->with($responseContent, $txType) + ->willReturn($decodedResponse); + + $this->prepareClient( + $this->httpClientMock, + $responseContent, + $apiUrl, + [ + 'body' => $encodedRequestData, + 'headers' => [ + 'Content-Type' => 'text/xml; charset=UTF-8', + ], + ], + ); + + $this->eventDispatcherMock->expects(self::once()) + ->method('dispatch') + ->with($this->callback(function ($dispatchedEvent) use ($txType, $requestData, $order, $paymentModel) { + return $dispatchedEvent instanceof RequestDataPreparedEvent + && get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel() + ; + })); + } }