From 94c60093c220995383d5a871d39f27447f784f61 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Mon, 14 Oct 2024 12:14:45 +0200 Subject: [PATCH 01/10] Visa Mobile redirected UI version --- config/config/sylius_fixtures.php | 17 ++++ config/config/sylius_template_events.php | 4 + config/services/payum/action.php | 11 ++- config/services/tpay.php | 18 +++- ...nnecessaryPaymentDetailsFieldsListener.php | 4 + src/Form/Type/TpayPaymentDetailsType.php | 8 ++ .../Api/CreateVisaMobileTransactionAction.php | 69 +++++++++++++++ .../CreateVisaMobilePaymentPayloadFactory.php | 34 ++++++++ ...saMobilePaymentPayloadFactoryInterface.php | 10 +++ .../shop/cart/complete/_visaMobile.html.twig | 6 ++ ..._paying_with_visa_mobile_payment_type.json | 12 +++ .../Shop/PayingForOrdersByVisaMobileTest.php | 84 +++++++++++++++++++ .../CreateVisaMobileTransactionActionTest.php | 77 +++++++++++++++++ ...ateVisaMobilePaymentPayloadFactoryTest.php | 62 ++++++++++++++ 14 files changed, 413 insertions(+), 3 deletions(-) create mode 100644 src/Payum/Action/Api/CreateVisaMobileTransactionAction.php create mode 100644 src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php create mode 100644 src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryInterface.php create mode 100644 templates/shop/cart/complete/_visaMobile.html.twig create mode 100644 tests/Api/Responses/shop/paying_for_orders_by_visa_mobile/test_paying_with_visa_mobile_payment_type.json create mode 100644 tests/Api/Shop/PayingForOrdersByVisaMobileTest.php create mode 100644 tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php create mode 100644 tests/Unit/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryTest.php diff --git a/config/config/sylius_fixtures.php b/config/config/sylius_fixtures.php index 5952b9cb..a9f9fb5c 100644 --- a/config/config/sylius_fixtures.php +++ b/config/config/sylius_fixtures.php @@ -136,6 +136,23 @@ ], 'enabled' => true, ], + 'visa_mobile' => [ + 'code' => 'visa_mobile', + 'name' => 'Visa mobile (Tpay)', + 'gatewayFactory' => 'tpay', + 'gatewayName' => 'tpay', + 'gatewayConfig' => [ + 'client_id' => '%env(string:TPAY_CLIENT_ID)%', + 'client_secret' => '%env(string:TPAY_CLIENT_SECRET)%', + 'type' => 'visa-mobile', + 'notification_security_code' => '%env(string:TPAY_NOTIFICATION_SECURITY_CODE)%', + 'production_mode' => false, + ], + 'channels' => [ + 'FASHION_WEB', + ], + 'enabled' => true, + ], ], ], ]); diff --git a/config/config/sylius_template_events.php b/config/config/sylius_template_events.php index 61268535..03a9f736 100644 --- a/config/config/sylius_template_events.php +++ b/config/config/sylius_template_events.php @@ -32,6 +32,10 @@ 'pay-by-link' => [ 'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_payByLink.html.twig', 'priority' => 5, + ], + 'visa-mobile' => [ + 'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_visaMobile.html.twig', + 'priority' => 5, ] ], ], diff --git a/config/services/payum/action.php b/config/services/payum/action.php index 2b49617d..c944d36c 100644 --- a/config/services/payum/action.php +++ b/config/services/payum/action.php @@ -10,6 +10,7 @@ use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateGooglePayTransactionAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreatePayByLinkTransactionAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction; +use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateVisaMobileTransactionAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\GetTpayTransactionsChannelsAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\NotifyAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\PayWithCardAction; @@ -19,7 +20,7 @@ use CommerceWeavers\SyliusTpayPlugin\Payum\Action\ResolveNextRouteAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory; -return function(ContainerConfigurator $container): void { +return static function(ContainerConfigurator $container): void { $services = $container->services(); $services->defaults() ->public() @@ -72,6 +73,14 @@ ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_redirect_based_transaction']) ; + $services->set(CreateVisaMobileTransactionAction::class) + ->args([ + service('commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload'), + service('commerce_weavers_sylius_tpay.payum.factory.token.notify'), + ]) + ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_visa_mobile_transaction']) + ; + $services->set(NotifyAction::class) ->args([ service('commerce_weavers_sylius_tpay.tpay.security.notification.factory.basic_payment'), diff --git a/config/services/tpay.php b/config/services/tpay.php index 538b3e11..7d6d8109 100644 --- a/config/services/tpay.php +++ b/config/services/tpay.php @@ -17,8 +17,8 @@ use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateRedirectBasedPaymentPayloadFactoryInterface; use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiBankListProvider; use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiBankListProviderInterface; -use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiChannelListProvider; -use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiChannelListProviderInterface; +use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateVisaMobilePaymentPayloadFactory; +use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateVisaMobilePaymentPayloadFactoryInterface; use CommerceWeavers\SyliusTpayPlugin\Tpay\Resolver\CachedTpayTransactionChannelResolver; use CommerceWeavers\SyliusTpayPlugin\Tpay\Resolver\TpayTransactionChannelResolver; use CommerceWeavers\SyliusTpayPlugin\Tpay\Security\Notification\Factory\BasicPaymentFactory; @@ -66,6 +66,13 @@ ->alias(CreateGooglePayPaymentPayloadFactory::class, 'commerce_weavers_sylius_tpay.tpay.factory.create_google_pay_payment_payload') ; + $services->set('commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload', CreateVisaMobilePaymentPayloadFactory::class) + ->args([ + service('commerce_weavers_sylius_tpay.tpay.factory.create_redirect_based_payment_payload'), + ]) + ->alias(CreateVisaMobilePaymentPayloadFactoryInterface::class, 'commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload') + ; + $services->set('commerce_weavers_sylius_tpay.tpay.factory.create_redirect_based_payment_payload', CreateRedirectBasedPaymentPayloadFactory::class) ->args([ service('commerce_weavers_sylius_tpay.tpay.routing.generator.callback_url'), @@ -81,6 +88,13 @@ ->alias(CreatePayByLinkPayloadFactoryInterface::class, 'commerce_weavers_sylius_tpay.tpay.factory.create_pay_by_link_payment_payload') ; + $services->set('commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload', CreateVisaMobilePaymentPayloadFactory::class) + ->args([ + service('commerce_weavers_sylius_tpay.tpay.factory.create_redirect_based_payment_payload'), + ]) + ->alias(CreateVisaMobilePaymentPayloadFactoryInterface::class, 'commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload') + ; + $services->set('commerce_weavers_sylius_tpay.tpay.security.notification.factory.basic_payment', BasicPaymentFactory::class) ->alias(BasicPaymentFactory::class, 'commerce_weavers_sylius_tpay.tpay.security.notification.factory.basic_payment') ; diff --git a/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php b/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php index 64532ed9..61fd5786 100644 --- a/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php +++ b/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php @@ -29,5 +29,9 @@ public function __invoke(FormEvent $event): void if (!isset($data['tpay_channel_id'])) { $form->remove('tpay_channel_id'); } + + if (!isset($data['visa_mobile'])) { + $form->remove('visa_mobile'); + } } } diff --git a/src/Form/Type/TpayPaymentDetailsType.php b/src/Form/Type/TpayPaymentDetailsType.php index b8c67759..6436fe51 100644 --- a/src/Form/Type/TpayPaymentDetailsType.php +++ b/src/Form/Type/TpayPaymentDetailsType.php @@ -64,6 +64,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'property_path' => '[tpay_channel_id]', ], ) + ->add( + 'visa_mobile', + HiddenType::class, + [ + 'property_path' => '[visa_mobile]', + 'data' => true, + ], + ) ; $builder->addEventListener( diff --git a/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php b/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php new file mode 100644 index 00000000..331cbb6c --- /dev/null +++ b/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php @@ -0,0 +1,69 @@ +getModel(); + $gatewayName = $request->getToken()?->getGatewayName() ?? $this->getGatewayNameFrom($model); + + $localeCode = $this->getLocaleCodeFrom($model); + $notifyToken = $this->notifyTokenFactory->create($model, $gatewayName, $localeCode); + + $paymentDetails = PaymentDetails::fromArray($model->getDetails()); + + $response = $this->api->transactions()->createTransaction( + $this->createVisaMobilePaymentPayloadFactory->createFrom($model, $notifyToken->getTargetUrl(), $localeCode), + ); + + $paymentDetails->setTransactionId($response['transactionId']); + $paymentDetails->setStatus($response['status']); + $paymentDetails->setPaymentUrl($response['transactionPaymentUrl']); + + $model->setDetails($paymentDetails->toArray()); + + if ($paymentDetails->getPaymentUrl() !== null) { + throw new HttpRedirect($paymentDetails->getPaymentUrl()); + } + } + + public function supports($request): bool + { + if (!$request instanceof CreateTransaction) { + return false; + } + + $model = $request->getModel(); + + if (!$model instanceof PaymentInterface) { + return false; + } + + $details = $model->getDetails(); + + // TODO: Refactor after task #72 + return isset($details['tpay']['visa_mobile']); + } +} diff --git a/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php new file mode 100644 index 00000000..262b0993 --- /dev/null +++ b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php @@ -0,0 +1,34 @@ +} $payload */ + $payload = $this->createRedirectBasedPaymentPayloadFactory->createFrom($payment, $notifyUrl, $localeCode); + + /** @var array{tpay?: array{visa_mobile?: string}} $paymentDetails */ + $paymentDetails = $payment->getDetails(); + + Assert::keyExists($paymentDetails['tpay'], 'visa_mobile', + 'The given payment is not visa mobile payment type.' + ); + + $payload['pay']['groupId'] = PayGroup::VISA_MOBILE; + + return $payload; + } +} diff --git a/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryInterface.php b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryInterface.php new file mode 100644 index 00000000..4123ce1e --- /dev/null +++ b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryInterface.php @@ -0,0 +1,10 @@ +setUpOrderPlacer(); + } + + public function test_paying_with_redirect_based_payment_type(): void + { + $this->loadFixturesFromDirectory('shop/paying_for_orders_by_card'); + + $order = $this->doPlaceOrder('t0k3n'); + + $this->client->request( + Request::METHOD_POST, + sprintf('/api/v2/shop/orders/%s/pay', $order->getTokenValue()), + server: self::CONTENT_TYPE_HEADER, + content: json_encode([ + 'successUrl' => 'https://example.com/success', + 'failureUrl' => 'https://example.com/failure', + 'pay' => [ + 'groupId' => PayGroup::VISA_MOBILE, + ], + ]), + ); + + $response = $this->client->getResponse(); + + $this->assertResponseCode($response, Response::HTTP_OK); + $this->assertResponse($response, 'shop/paying_for_orders_by_redirect/test_paying_with_visa_mobile_payment_type'); + } + + private function doPlaceOrder( + string $tokenValue, + string $email = 'sylius@example.com', + string $productVariantCode = 'MUG_BLUE', + string $shippingMethodCode = 'UPS', + string $paymentMethodCode = 'tpay', + int $quantity = 1, + ?\DateTimeImmutable $checkoutCompletedAt = null, + + ): OrderInterface { + $this->checkSetUpOrderPlacerCalled(); + + $this->pickUpCart($tokenValue); + $this->addItemToCart($productVariantCode, $quantity, $tokenValue); + $cart = $this->updateCartWithAddressAndCouponCode($tokenValue, $email); + $this->dispatchShippingMethodChooseCommand( + $tokenValue, + $shippingMethodCode, + (string)$cart->getShipments()->first()->getId(), + ); + $this->dispatchPaymentMethodChooseCommand( + $tokenValue, + $paymentMethodCode, + (string)$cart->getLastPayment()->getId(), + ); + + $order = $this->dispatchCompleteOrderCommand($tokenValue); + + $this->setCheckoutCompletedAt($order, $checkoutCompletedAt); + + return $order; + } +} diff --git a/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php new file mode 100644 index 00000000..05f78e51 --- /dev/null +++ b/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php @@ -0,0 +1,77 @@ +api = $this->prophesize(TpayApi::class); + $this->createRedirectBasedPaymentPayloadFactory = $this->prophesize(CreateVisaMobilePaymentPayloadFactoryInterface::class); + $this->tokenFactory = $this->prophesize(GenericTokenFactoryInterface::class); + $this->notifyTokenFactory = $this->prophesize(NotifyTokenFactoryInterface::class); + } + + public function test_it_supports_create_transaction_requests_with_a_valid_payment_model(): void + { + $payment = $this->prophesize(PaymentInterface::class); + $payment->getDetails()->willReturn([]); + + $request = $this->prophesize(CreateTransaction::class); + $request->getModel()->willReturn($payment); + + $isSupported = $this->createTestSubject()->supports($request->reveal()); + + $this->assertTrue($isSupported); + } + + public function test_it_does_not_support_non_create_transaction_requests(): void + { + $payment = $this->prophesize(PaymentInterface::class); + $payment->getDetails()->willReturn([]); + + $request = $this->prophesize(Capture::class); + $request->getModel()->willReturn($payment); + + $isSupported = $this->createTestSubject()->supports($request->reveal()); + + $this->assertFalse($isSupported); + } + + private function createTestSubject(): CreateVisaMobileTransactionAction + { + $action = new CreateVisaMobileTransactionAction( + $this->createVisaMobilePaymentPayloadFactory->reveal(), + $this->notifyTokenFactoryInterface->reveal(), + ); + + $action->setApi($this->api->reveal()); + $action->setGenericTokenFactory($this->tokenFactory->reveal()); + + return $action; + } +} diff --git a/tests/Unit/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryTest.php b/tests/Unit/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryTest.php new file mode 100644 index 00000000..a8e90d2d --- /dev/null +++ b/tests/Unit/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryTest.php @@ -0,0 +1,62 @@ +createRedirectBasedPaymentPayloadFactory = $this->prophesize(CreateRedirectBasedPaymentPayloadFactoryInterface::class); + } + + public function test_it_throws_exception_if_payment_visa_mobile_key_is_missing(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The given payment is not visa mobile payment type.'); + + $payment = $this->prophesize(PaymentInterface::class); + + $payment->getDetails()->willReturn(['tpay' => ['some_other_key' => true]]); + + $this->createRedirectBasedPaymentPayloadFactory->createFrom($payment, 'https://cw.org/notify', 'pl_PL')->willReturn(['some' => 'data']); + + $this->createTestSubject()->createFrom($payment->reveal(), 'https://cw.org/notify', 'pl_PL'); + } + + public function test_it_adds_card_related_data_to_a_basic_create_payment_payload_output(): void + { + $payment = $this->prophesize(PaymentInterface::class); + + $payment->getDetails()->willReturn(['tpay' => ['visa_mobile' => true]]); + + $this->createRedirectBasedPaymentPayloadFactory->createFrom($payment, 'https://cw.org/notify', 'pl_PL')->willReturn(['some' => 'data']); + + $payload = $this->createTestSubject()->createFrom($payment->reveal(), 'https://cw.org/notify', 'pl_PL'); + + $this->assertSame([ + 'some' => 'data', + 'pay' => [ + 'groupId' => PayGroup::VISA_MOBILE, + ], + ], $payload); + } + + private function createTestSubject(): CreateVisaMobilePaymentPayloadFactory + { + return new CreateVisaMobilePaymentPayloadFactory($this->createRedirectBasedPaymentPayloadFactory->reveal()); + } +} From d2094ed1650573124ad993258c226b4398c87f08 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Mon, 14 Oct 2024 20:59:56 +0200 Subject: [PATCH 02/10] Visa Mobile payment - API --- config/config/sylius_fixtures.php | 4 +- config/serialization/Pay.xml | 3 ++ config/services/api/command.php | 8 +++- config/services/api/factory.php | 7 ++- src/Api/Command/Pay.php | 1 + src/Api/Command/PayByVisaMobile.php | 13 ++++++ src/Api/Command/PayByVisaMobileHandler.php | 45 +++++++++++++++++++ .../NextCommand/PayByVisaMobileFactory.php | 31 +++++++++++++ src/Model/PaymentDetails.php | 13 ++++++ src/Tpay/PaymentType.php | 2 + .../Shop/PayingForOrdersByVisaMobileTest.php | 2 +- 11 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 src/Api/Command/PayByVisaMobile.php create mode 100644 src/Api/Command/PayByVisaMobileHandler.php create mode 100644 src/Api/Factory/NextCommand/PayByVisaMobileFactory.php diff --git a/config/config/sylius_fixtures.php b/config/config/sylius_fixtures.php index a9f9fb5c..78a1a867 100644 --- a/config/config/sylius_fixtures.php +++ b/config/config/sylius_fixtures.php @@ -7,7 +7,7 @@ use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType; use Symfony\Config\SyliusFixturesConfig; -return function(SyliusFixturesConfig $fixtures): void { +return static function(SyliusFixturesConfig $fixtures): void { $defaultSuite = $fixtures->suites('default'); $defaultSuite->fixtures('shipping_method', [ 'options' => [ @@ -144,7 +144,7 @@ 'gatewayConfig' => [ 'client_id' => '%env(string:TPAY_CLIENT_ID)%', 'client_secret' => '%env(string:TPAY_CLIENT_SECRET)%', - 'type' => 'visa-mobile', + 'type' => PaymentType::VISA_MOBILE, 'notification_security_code' => '%env(string:TPAY_NOTIFICATION_SECURITY_CODE)%', 'production_mode' => false, ], diff --git a/config/serialization/Pay.xml b/config/serialization/Pay.xml index dbeea44e..bba6ce7b 100644 --- a/config/serialization/Pay.xml +++ b/config/serialization/Pay.xml @@ -37,5 +37,8 @@ commerce_weavers_sylius_tpay:shop:order:pay + + commerce_weavers_sylius_tpay:shop:order:pay + diff --git a/config/services/api/command.php b/config/services/api/command.php index d0df0d4d..8977aaae 100644 --- a/config/services/api/command.php +++ b/config/services/api/command.php @@ -11,10 +11,11 @@ use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByLinkHandler; use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByGooglePayHandler; use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByRedirectHandler; +use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByVisaMobileHandler; use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayHandler; use CommerceWeavers\SyliusTpayPlugin\Command\CancelLastPaymentHandler; -return function(ContainerConfigurator $container): void { +return static function(ContainerConfigurator $container): void { $services = $container->services(); $services->set('commerce_weavers_sylius_tpay.api.command.cancel_last_payment_handler', CancelLastPaymentHandler::class) @@ -73,4 +74,9 @@ ->parent('commerce_weavers_sylius_tpay.api.command.abstract_pay_by_handler') ->tag('messenger.message_handler') ; + + $services->set('commerce_weavers_sylius_tpay.api.command.pay_by_visa_mobile_handler', PayByVisaMobileHandler::class) + ->parent('commerce_weavers_sylius_tpay.api.command.abstract_pay_by_handler') + ->tag('messenger.message_handler') + ; }; diff --git a/config/services/api/factory.php b/config/services/api/factory.php index 607c29ec..dab15e3d 100644 --- a/config/services/api/factory.php +++ b/config/services/api/factory.php @@ -10,10 +10,11 @@ use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand\PayByGooglePayFactory; use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand\PayByLinkFactory; use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand\PayByRedirectFactory; +use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand\PayByVisaMobileFactory; use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommandFactory; use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommandFactoryInterface; -return function(ContainerConfigurator $container): void { +return static function(ContainerConfigurator $container): void { $services = $container->services(); $services->set('commerce_weavers_sylius_tpay.api.factory.next_command', NextCommandFactory::class) @@ -49,4 +50,8 @@ $services->set('commerce_weavers_sylius_tpay.api.factory.next_command.pay_by_link', PayByLinkFactory::class) ->tag('commerce_weavers_sylius_tpay.api.factory.next_command') ; + + $services->set('commerce_weavers_sylius_tpay.api.factory.next_command.pay_by_visa_mobile', PayByVisaMobileFactory::class) + ->tag('commerce_weavers_sylius_tpay.api.factory.next_command') + ; }; diff --git a/src/Api/Command/Pay.php b/src/Api/Command/Pay.php index f35b4355..852b8c34 100644 --- a/src/Api/Command/Pay.php +++ b/src/Api/Command/Pay.php @@ -17,6 +17,7 @@ public function __construct( public readonly ?string $googlePayToken = null, public readonly ?string $encodedCardData = null, public readonly ?string $tpayChannelId = null, + public readonly bool $isVisaMobilePayment = false, ) { } diff --git a/src/Api/Command/PayByVisaMobile.php b/src/Api/Command/PayByVisaMobile.php new file mode 100644 index 00000000..8a7f225f --- /dev/null +++ b/src/Api/Command/PayByVisaMobile.php @@ -0,0 +1,13 @@ +findOr404($command->paymentId); + + $this->setTransactionData($payment); + $this->createTransaction($payment); + + return $this->createResultFrom($payment); + } + + private function setTransactionData(PaymentInterface $payment): void + { + $paymentDetails = PaymentDetails::fromArray($payment->getDetails()); + $paymentDetails->setVisaMobile(true); + + $payment->setDetails($paymentDetails->toArray()); + } + + private function createResultFrom(PaymentInterface $payment): PayResult + { + $paymentDetails = PaymentDetails::fromArray($payment->getDetails()); + + Assert::notNull($paymentDetails->getStatus(), 'Payment status is required to create a result.'); + Assert::notNull($paymentDetails->getPaymentUrl(), 'Payment URL is required to create a result.'); + + return new PayResult( + $paymentDetails->getStatus(), + $paymentDetails->getPaymentUrl(), + ); + } +} diff --git a/src/Api/Factory/NextCommand/PayByVisaMobileFactory.php b/src/Api/Factory/NextCommand/PayByVisaMobileFactory.php new file mode 100644 index 00000000..038a9f63 --- /dev/null +++ b/src/Api/Factory/NextCommand/PayByVisaMobileFactory.php @@ -0,0 +1,31 @@ +supports($command, $payment)) { + throw new UnsupportedNextCommandFactory('This factory does not support the given command.'); + } + + /** @var int $paymentId */ + $paymentId = $payment->getId(); + + return new PayByVisaMobile($paymentId); + } + + public function supports(Pay $command, PaymentInterface $payment): bool + { + return $command->isVisaMobilePayment === true && $payment->getId() !== null; + } +} diff --git a/src/Model/PaymentDetails.php b/src/Model/PaymentDetails.php index b96f5568..0b507ed3 100644 --- a/src/Model/PaymentDetails.php +++ b/src/Model/PaymentDetails.php @@ -24,6 +24,7 @@ public function __construct( private ?string $successUrl = null, private ?string $failureUrl = null, private ?string $tpayChannelId = null, + private ?bool $visaMobile = false, ) { } @@ -149,6 +150,16 @@ public function getType(): string }; } + public function getVisaMobile(): ?bool + { + return $this->visaMobile; + } + + public function setVisaMobile(bool $visaMobile): void + { + $this->visaMobile = $visaMobile; + } + public function clearSensitiveData(): void { $this->applePayToken = null; @@ -171,6 +182,7 @@ public static function fromArray(array $details): self $details['tpay']['success_url'] ?? null, $details['tpay']['failure_url'] ?? null, $details['tpay']['tpay_channel_id'] ?? null, + $details['tpay']['visa_mobile'] ?? false, ); } @@ -189,6 +201,7 @@ public function toArray(): array 'success_url' => $this->successUrl, 'failure_url' => $this->failureUrl, 'tpay_channel_id' => $this->tpayChannelId, + 'visa_mobile' => $this->visaMobile, ], ]; } diff --git a/src/Tpay/PaymentType.php b/src/Tpay/PaymentType.php index 1d3b3bdf..86ded903 100644 --- a/src/Tpay/PaymentType.php +++ b/src/Tpay/PaymentType.php @@ -17,4 +17,6 @@ class PaymentType public const GOOGLE_PAY = 'google_pay'; public const APPLE_PAY = 'apple_pay'; + + public const VISA_MOBILE = 'visa_mobile'; } diff --git a/tests/Api/Shop/PayingForOrdersByVisaMobileTest.php b/tests/Api/Shop/PayingForOrdersByVisaMobileTest.php index c4273ae3..8e0e01ee 100644 --- a/tests/Api/Shop/PayingForOrdersByVisaMobileTest.php +++ b/tests/Api/Shop/PayingForOrdersByVisaMobileTest.php @@ -46,7 +46,7 @@ public function test_paying_with_redirect_based_payment_type(): void $response = $this->client->getResponse(); $this->assertResponseCode($response, Response::HTTP_OK); - $this->assertResponse($response, 'shop/paying_for_orders_by_redirect/test_paying_with_visa_mobile_payment_type'); + $this->assertResponse($response, 'shop/paying_for_orders_by_visa_mobile/test_paying_with_visa_mobile_payment_type'); } private function doPlaceOrder( From 3c9af0e95a8b1bbf537a845752fb5224c2d17fa3 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Tue, 15 Oct 2024 15:08:39 +0200 Subject: [PATCH 03/10] Add tests and additional changes to make Visa Mobile API and UI --- .../Api/CreateVisaMobileTransactionAction.php | 4 +- .../CreateVisaMobilePaymentPayloadFactory.php | 8 +- ...saMobilePaymentPayloadFactoryInterface.php | 2 + .../Unit/Api/Command/PayByBlikHandlerTest.php | 3 +- .../Unit/Api/Command/PayByCardHandlerTest.php | 3 +- .../Api/Command/PayByGooglePayHandlerTest.php | 2 + .../Command/PayByVisaMobileHandlerTest.php | 159 ++++++++++++++++++ tests/Unit/Api/Command/PayHandlerTest.php | 1 + .../PayByVisaMobileFactoryTest.php | 79 +++++++++ ...essaryPaymentDetailsFieldsListenerTest.php | 17 ++ ...eateBlikLevelZeroTransactionActionTest.php | 2 + .../Api/CreateCardTransactionActionTest.php | 6 +- .../CreateGooglePayTransactionActionTest.php | 3 + .../CreatePayByLinkTransactionActionTest.php | 3 +- ...eateRedirectBasedTransactionActionTest.php | 6 +- .../CreateVisaMobileTransactionActionTest.php | 12 +- .../Payum/Action/Api/NotifyActionTest.php | 3 +- .../Action/Api/PayWithCardActionTest.php | 6 +- 18 files changed, 299 insertions(+), 20 deletions(-) create mode 100644 tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php create mode 100644 tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.php diff --git a/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php b/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php index 331cbb6c..4c6eafb8 100644 --- a/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php +++ b/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php @@ -23,6 +23,9 @@ public function __construct( parent::__construct(); } + /** + * @param CreateTransaction $request + */ public function execute($request): void { /** @var PaymentInterface $model */ @@ -63,7 +66,6 @@ public function supports($request): bool $details = $model->getDetails(); - // TODO: Refactor after task #72 return isset($details['tpay']['visa_mobile']); } } diff --git a/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php index 262b0993..72278e77 100644 --- a/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php +++ b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php @@ -20,11 +20,13 @@ public function createFrom(PaymentInterface $payment, string $notifyUrl, string /** @var array{pay: array} $payload */ $payload = $this->createRedirectBasedPaymentPayloadFactory->createFrom($payment, $notifyUrl, $localeCode); - /** @var array{tpay?: array{visa_mobile?: string}} $paymentDetails */ + /** @phpstan-param array{tpay?: array{visa_mobile?: bool}} $paymentDetails */ $paymentDetails = $payment->getDetails(); - Assert::keyExists($paymentDetails['tpay'], 'visa_mobile', - 'The given payment is not visa mobile payment type.' + Assert::keyExists( + $paymentDetails['tpay'], + 'visa_mobile', + 'The given payment is not visa mobile payment type.', ); $payload['pay']['groupId'] = PayGroup::VISA_MOBILE; diff --git a/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryInterface.php b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryInterface.php index 4123ce1e..d079ebf1 100644 --- a/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryInterface.php +++ b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryInterface.php @@ -1,5 +1,7 @@ null, 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Api/Command/PayByCardHandlerTest.php b/tests/Unit/Api/Command/PayByCardHandlerTest.php index ed91a198..9c7f9d1c 100644 --- a/tests/Unit/Api/Command/PayByCardHandlerTest.php +++ b/tests/Unit/Api/Command/PayByCardHandlerTest.php @@ -86,7 +86,8 @@ public function test_it_creates_a_card_based_transaction(): void 'payment_url' => null, 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Api/Command/PayByGooglePayHandlerTest.php b/tests/Unit/Api/Command/PayByGooglePayHandlerTest.php index 1a967f8e..a50637a6 100644 --- a/tests/Unit/Api/Command/PayByGooglePayHandlerTest.php +++ b/tests/Unit/Api/Command/PayByGooglePayHandlerTest.php @@ -92,6 +92,7 @@ public function test_it_throws_an_exception_if_a_payment_status_is_null(): void 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); $gateway->execute($createTransaction, catchReply: true)->shouldBeCalled(); @@ -130,6 +131,7 @@ public function test_it_creates_a_google_pay_based_transaction(): void 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); $gateway->execute($createTransaction, catchReply: true)->shouldBeCalled(); diff --git a/tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php b/tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php new file mode 100644 index 00000000..6c36b1c8 --- /dev/null +++ b/tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php @@ -0,0 +1,159 @@ +paymentRepository = $this->prophesize(PaymentRepositoryInterface::class); + $this->payum = $this->prophesize(Payum::class); + $this->createTransactionFactory = $this->prophesize(CreateTransactionFactoryInterface::class); + } + + public function test_it_throw_an_exception_if_a_payment_cannot_be_found(): void + { + $this->expectException(NotFoundHttpException::class); + $this->expectExceptionMessage('Payment with id "1" cannot be found.'); + + $this->paymentRepository->find(1)->willReturn(null); + + $this->createTestSubject()->__invoke(new PayByVisaMobile(1)); + } + + public function test_it_throws_an_exception_if_a_gateway_name_cannot_be_determined(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Gateway name cannot be determined.'); + + $payment = $this->prophesize(PaymentInterface::class); + $payment->getDetails()->willReturn([]); + $payment->setDetails(Argument::any()); + $payment->getMethod()->willReturn(null); + + $this->paymentRepository->find(1)->willReturn($payment); + + $this->createTestSubject()->__invoke(new PayByVisaMobile(1)); + } + + public function test_it_throws_an_exception_if_payment_details_does_not_have_a_set_status(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Payment status is required to create a result.'); + + $gatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $gatewayConfig->getGatewayName()->willReturn('tpay'); + + $paymentMethod = $this->prophesize(PaymentMethodInterface::class); + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); + + $payment = $this->prophesize(PaymentInterface::class); + $payment->getMethod()->willReturn($paymentMethod); + $payment->getDetails()->willReturn(['tpay' => ['status' => null]]); + $payment->setDetails([ + 'tpay' => [ + 'transaction_id' => null, + 'result' => null, + 'status' => null, + 'blik_token' => null, + 'google_pay_token' => null, + 'card' => null, + 'payment_url' => null, + 'success_url' => null, + 'failure_url' => null, + 'visa_mobile' => true, + ], + ])->shouldBeCalled(); + + $this->paymentRepository->find(1)->willReturn($payment); + + $createTransaction = $this->prophesize(CreateTransaction::class); + + $this->createTransactionFactory->createNewWithModel($payment)->willReturn($createTransaction); + + $gateway = $this->prophesize(GatewayInterface::class); + + $this->payum->getGateway('tpay')->willReturn($gateway); + + $this->createTestSubject()->__invoke(new PayByVisaMobile(1)); + } + + public function test_it_creates_a_visa_mobile_based_transaction(): void + { + $gatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $gatewayConfig->getGatewayName()->willReturn('tpay'); + + $paymentMethod = $this->prophesize(PaymentMethodInterface::class); + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); + + $payment = $this->prophesize(PaymentInterface::class); + $payment->getMethod()->willReturn($paymentMethod); + $payment->getDetails()->willReturn(['tpay' => ['status' => 'pending', 'payment_url' => 'https://cw.org/pay']]); + $payment->setDetails([ + 'tpay' => [ + 'transaction_id' => null, + 'result' => null, + 'status' => 'pending', + 'blik_token' => null, + 'google_pay_token' => null, + 'card' => null, + 'payment_url' => 'https://cw.org/pay', + 'success_url' => null, + 'failure_url' => null, + 'visa_mobile' => true, + ], + ])->shouldBeCalled(); + + $this->paymentRepository->find(1)->willReturn($payment); + + $createTransaction = $this->prophesize(CreateTransaction::class); + + $this->createTransactionFactory->createNewWithModel($payment)->willReturn($createTransaction); + + $gateway = $this->prophesize(GatewayInterface::class); + $gateway->execute($createTransaction, catchReply: true)->shouldBeCalled(); + + $this->payum->getGateway('tpay')->willReturn($gateway); + + $result = $this->createTestSubject()->__invoke(new PayByVisaMobile(1)); + + self::assertSame('pending', $result->status); + self::assertSame('https://cw.org/pay', $result->transactionPaymentUrl); + } + + private function createTestSubject(): PayByVisaMobileHandler + { + return new PayByVisaMobileHandler( + $this->paymentRepository->reveal(), + $this->payum->reveal(), + $this->createTransactionFactory->reveal(), + ); + } +} diff --git a/tests/Unit/Api/Command/PayHandlerTest.php b/tests/Unit/Api/Command/PayHandlerTest.php index dae09005..ee2aafce 100644 --- a/tests/Unit/Api/Command/PayHandlerTest.php +++ b/tests/Unit/Api/Command/PayHandlerTest.php @@ -82,6 +82,7 @@ public function test_it_executes_pay_by_blik_command_if_a_blik_token_is_passed() 'payment_url' => null, 'success_url' => 'https://cw.nonexisting/success', 'failure_url' => 'https://cw.nonexisting/failure', + 'visa_mobile' => false, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.php b/tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.php new file mode 100644 index 00000000..607086de --- /dev/null +++ b/tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.php @@ -0,0 +1,79 @@ +createTestSubject(); + + $this->assertFalse($factory->supports($this->createCommand(isVisaMobilePayment: false), $this->createPayment())); + } + + public function test_it_does_not_support_a_command_without_a_payment_with_id(): void + { + $factory = $this->createTestSubject(); + + $this->assertFalse($factory->supports($this->createCommand(), new Payment())); + } + + public function test_it_supports_a_command_with_a_visa_mobile_payment_set_as_true(): void + { + $factory = $this->createTestSubject(); + + $this->assertTrue($factory->supports($this->createCommand(), $this->createPayment())); + } + + public function test_it_creates_a_pay_by_visa_mobile_command(): void + { + $command = $this->createTestSubject()->create($this->createCommand(), $this->createPayment()); + + $this->assertInstanceOf(PayByVisaMobile::class, $command); + } + + public function test_it_throws_an_exception_when_trying_to_create_a_command_with_unsupported_factory(): void + { + $this->expectException(UnsupportedNextCommandFactory::class); + + $this->createTestSubject()->create($this->createCommand(), new Payment()); + } + + private function createCommand(?string $token = null, bool $isVisaMobilePayment = true): Pay + { + return new Pay( + $token ?? 'token', + 'https://cw.nonexisting/success', + 'https://cw.nonexisting/failure', + isVisaMobilePayment: $isVisaMobilePayment, + ); + } + + private function createPayment(int $id = 1): PaymentInterface + { + $payment = $this->prophesize(PaymentInterface::class); + $payment->getId()->willReturn($id); + + return $payment->reveal(); + } + + private function createTestSubject(): NextCommandFactoryInterface + { + return new PayByVisaMobileFactory(); + } + +} diff --git a/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php b/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php index 57247e53..83eac444 100644 --- a/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php +++ b/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php @@ -21,6 +21,7 @@ public function test_it_leaves_blik_field_once_blik_token_is_set(): void $form->remove('blik_token')->shouldNotBeCalled(); $form->remove('google_pay_token')->shouldBeCalled()->willReturn($form); $form->remove('tpay_channel_id')->shouldBeCalled()->willReturn($form); + $form->remove('visa_mobile')->shouldBeCalled()->willReturn($form); $event = new FormEvent($form->reveal(), ['blik_token' => '123456']); @@ -47,6 +48,7 @@ public function test_it_leaves_card_field_once_card_is_set(): void $form->remove('blik_token')->shouldBeCalled()->willReturn($form); $form->remove('google_pay_token')->shouldBeCalled()->willReturn($form); $form->remove('tpay_channel_id')->shouldBeCalled()->willReturn($form); + $form->remove('visa_mobile')->shouldBeCalled()->willReturn($form); $event = new FormEvent($form->reveal(), ['card' => 'h45h']); @@ -60,12 +62,26 @@ public function test_it_leaves_pbl_channel_id_field_once_pbl_channel_id_is_set() $form->remove('blik_token')->shouldBeCalled()->willReturn($form); $form->remove('google_pay_token')->shouldBeCalled()->willReturn($form); $form->remove('tpay_channel_id')->shouldNotBeCalled(); + $form->remove('visa_mobile')->shouldBeCalled()->willReturn($form); $event = new FormEvent($form->reveal(), ['tpay_channel_id' => 1]); $this->createTestSubject()->__invoke($event); } + public function test_it_leaves_visa_mobile_field_once_visa_mobile_is_set(): void + { + $form = $this->prophesize(FormInterface::class); + $form->remove('card')->shouldBeCalled()->willReturn($form); + $form->remove('blik_token')->shouldBeCalled()->willReturn($form); + $form->remove('tpay_channel_id')->shouldBeCalled()->willReturn($form); + $form->remove('visa_mobile')->shouldNotBeCalled(); + + $event = new FormEvent($form->reveal(), ['visa_mobile' => true]); + + $this->createTestSubject()->__invoke($event); + } + public function test_it_removes_all_additional_fields_if_none_of_them_are_passed(): void { $form = $this->prophesize(FormInterface::class); @@ -73,6 +89,7 @@ public function test_it_removes_all_additional_fields_if_none_of_them_are_passed $form->remove('blik_token')->shouldBeCalled()->willReturn($form); $form->remove('google_pay_token')->shouldBeCalled()->willReturn($form); $form->remove('tpay_channel_id')->shouldBeCalled()->willReturn($form); + $form->remove('visa_mobile')->shouldBeCalled()->willReturn($form); $event = new FormEvent($form->reveal(), []); diff --git a/tests/Unit/Payum/Action/Api/CreateBlikLevelZeroTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateBlikLevelZeroTransactionActionTest.php index 4d6d1ff5..786ffe90 100644 --- a/tests/Unit/Payum/Action/Api/CreateBlikLevelZeroTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateBlikLevelZeroTransactionActionTest.php @@ -132,6 +132,7 @@ public function test_it_creates_a_payment_and_requests_paying_it_with_a_provided 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); @@ -189,6 +190,7 @@ public function test_it_tries_to_determine_a_gateway_name_by_model_once_token_is 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/CreateCardTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateCardTransactionActionTest.php index 14f851aa..1311f43e 100644 --- a/tests/Unit/Payum/Action/Api/CreateCardTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateCardTransactionActionTest.php @@ -137,7 +137,8 @@ public function test_it_creates_a_payment_and_requests_paying_it_with_a_provided 'payment_url' => 'https://tpay.org/pay', 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); @@ -198,7 +199,8 @@ public function test_it_tries_to_determine_a_gateway_name_by_model_once_token_is 'payment_url' => 'https://tpay.org/pay', 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/CreateGooglePayTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateGooglePayTransactionActionTest.php index f80bb2ba..0e279325 100644 --- a/tests/Unit/Payum/Action/Api/CreateGooglePayTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateGooglePayTransactionActionTest.php @@ -122,6 +122,7 @@ public function test_it_creates_a_payment_and_requests_paying_it_with_a_provided 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); } @@ -167,6 +168,7 @@ public function test_it_redirects_payment_if_3d_secure_authentication_required() 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); @@ -249,6 +251,7 @@ public function test_it_tries_to_determine_a_gateway_name_by_model_once_token_is 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); } diff --git a/tests/Unit/Payum/Action/Api/CreatePayByLinkTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreatePayByLinkTransactionActionTest.php index 283e6ace..87c20e57 100644 --- a/tests/Unit/Payum/Action/Api/CreatePayByLinkTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreatePayByLinkTransactionActionTest.php @@ -133,7 +133,8 @@ public function test_it_creates_a_payment_and_redirects_to_a_payment_page(): voi 'payment_url' => 'https://tpay.org/pay', 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php index cd590048..3526498b 100644 --- a/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php @@ -144,7 +144,8 @@ public function test_it_creates_a_payment_and_redirects_to_a_payment_page(): voi 'payment_url' => 'https://tpay.org/pay', 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); @@ -212,7 +213,8 @@ public function test_it_tries_to_determine_a_gateway_name_by_model_once_token_is 'payment_url' => 'https://tpay.org/pay', 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php index 05f78e51..801dce01 100644 --- a/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php @@ -20,18 +20,18 @@ class CreateVisaMobileTransactionActionTest extends TestCase { use ProphecyTrait; - private TpayApi|ObjectProphecy $tpayApi; + private TpayApi|ObjectProphecy $api; private CreateVisaMobilePaymentPayloadFactoryInterface|ObjectProphecy $createVisaMobilePaymentPayloadFactory; - private GenericTokenFactoryInterface|ObjectProphecy $tokenFactoryInterface; + private GenericTokenFactoryInterface|ObjectProphecy $tokenFactory; - private NotifyTokenFactoryInterface|ObjectProphecy $notifyTokenFactoryInterface; + private NotifyTokenFactoryInterface|ObjectProphecy $notifyTokenFactory; protected function setUp(): void { $this->api = $this->prophesize(TpayApi::class); - $this->createRedirectBasedPaymentPayloadFactory = $this->prophesize(CreateVisaMobilePaymentPayloadFactoryInterface::class); + $this->createVisaMobilePaymentPayloadFactory = $this->prophesize(CreateVisaMobilePaymentPayloadFactoryInterface::class); $this->tokenFactory = $this->prophesize(GenericTokenFactoryInterface::class); $this->notifyTokenFactory = $this->prophesize(NotifyTokenFactoryInterface::class); } @@ -39,7 +39,7 @@ protected function setUp(): void public function test_it_supports_create_transaction_requests_with_a_valid_payment_model(): void { $payment = $this->prophesize(PaymentInterface::class); - $payment->getDetails()->willReturn([]); + $payment->getDetails()->willReturn(['tpay' => ['visa_mobile' => true]]); $request = $this->prophesize(CreateTransaction::class); $request->getModel()->willReturn($payment); @@ -66,7 +66,7 @@ private function createTestSubject(): CreateVisaMobileTransactionAction { $action = new CreateVisaMobileTransactionAction( $this->createVisaMobilePaymentPayloadFactory->reveal(), - $this->notifyTokenFactoryInterface->reveal(), + $this->notifyTokenFactory->reveal(), ); $action->setApi($this->api->reveal()); diff --git a/tests/Unit/Payum/Action/Api/NotifyActionTest.php b/tests/Unit/Payum/Action/Api/NotifyActionTest.php index 64c67749..0ffb6e71 100644 --- a/tests/Unit/Payum/Action/Api/NotifyActionTest.php +++ b/tests/Unit/Payum/Action/Api/NotifyActionTest.php @@ -99,7 +99,8 @@ public function test_it_converts_tpay_notification_status(string $status, string 'payment_url' => null, 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/PayWithCardActionTest.php b/tests/Unit/Payum/Action/Api/PayWithCardActionTest.php index 215c046c..2c4d1f78 100644 --- a/tests/Unit/Payum/Action/Api/PayWithCardActionTest.php +++ b/tests/Unit/Payum/Action/Api/PayWithCardActionTest.php @@ -98,7 +98,8 @@ public function test_it_redirects_a_customer_to_3ds_verification_once_a_transact 'payment_url' => 'http://example.com', 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); @@ -151,7 +152,8 @@ public function test_it_marks_a_payment_status_as_failed_once_a_transaction_stat 'payment_url' => 'http://example.com', 'success_url' => null, 'failure_url' => null, - 'tpay_channel_id' => null + 'tpay_channel_id' => null, + 'visa_mobile' => false, ], ])->shouldBeCalled(); From f74b99870dd8f164839d68c44bb2024d6f157ae8 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Tue, 15 Oct 2024 18:19:19 +0200 Subject: [PATCH 04/10] Remake the task as Visa Mobile On-site --- config/serialization/Pay.xml | 2 +- src/Api/Command/Pay.php | 2 +- src/Api/Command/PayByVisaMobile.php | 1 + src/Api/Command/PayByVisaMobileHandler.php | 2 +- .../Factory/NextCommand/PayByVisaMobileFactory.php | 6 ++++-- ...moveUnnecessaryPaymentDetailsFieldsListener.php | 4 ++-- src/Form/Type/TpayPaymentDetailsType.php | 8 ++++---- src/Model/PaymentDetails.php | 14 +++++++------- .../Api/CreateVisaMobileTransactionAction.php | 8 +------- .../CreateVisaMobilePaymentPayloadFactory.php | 12 ++++++------ templates/shop/cart/complete/_visaMobile.html.twig | 12 ++++++++++-- tests/Api/Shop/PayingForOrdersByVisaMobileTest.php | 5 ++++- tests/Unit/Api/Command/PayByBlikHandlerTest.php | 2 +- tests/Unit/Api/Command/PayByCardHandlerTest.php | 2 +- .../Unit/Api/Command/PayByGooglePayHandlerTest.php | 4 ++-- .../Api/Command/PayByVisaMobileHandlerTest.php | 14 +++++++------- tests/Unit/Api/Command/PayHandlerTest.php | 2 +- .../NextCommand/PayByVisaMobileFactoryTest.php | 14 +++++++------- ...UnnecessaryPaymentDetailsFieldsListenerTest.php | 12 ++++++------ .../CreateBlikLevelZeroTransactionActionTest.php | 4 ++-- .../Action/Api/CreateCardTransactionActionTest.php | 4 ++-- .../Api/CreateGooglePayTransactionActionTest.php | 6 +++--- .../Api/CreatePayByLinkTransactionActionTest.php | 2 +- .../CreateRedirectBasedTransactionActionTest.php | 4 ++-- .../Api/CreateVisaMobileTransactionActionTest.php | 2 +- tests/Unit/Payum/Action/Api/NotifyActionTest.php | 2 +- .../Payum/Action/Api/PayWithCardActionTest.php | 4 ++-- .../CreateVisaMobilePaymentPayloadFactoryTest.php | 7 +++++-- 28 files changed, 86 insertions(+), 75 deletions(-) diff --git a/config/serialization/Pay.xml b/config/serialization/Pay.xml index bba6ce7b..e181a40f 100644 --- a/config/serialization/Pay.xml +++ b/config/serialization/Pay.xml @@ -37,7 +37,7 @@ commerce_weavers_sylius_tpay:shop:order:pay - + commerce_weavers_sylius_tpay:shop:order:pay diff --git a/src/Api/Command/Pay.php b/src/Api/Command/Pay.php index 852b8c34..c39ca829 100644 --- a/src/Api/Command/Pay.php +++ b/src/Api/Command/Pay.php @@ -17,7 +17,7 @@ public function __construct( public readonly ?string $googlePayToken = null, public readonly ?string $encodedCardData = null, public readonly ?string $tpayChannelId = null, - public readonly bool $isVisaMobilePayment = false, + public readonly ?string $visaMobilePhoneNumber = null, ) { } diff --git a/src/Api/Command/PayByVisaMobile.php b/src/Api/Command/PayByVisaMobile.php index 8a7f225f..d9180156 100644 --- a/src/Api/Command/PayByVisaMobile.php +++ b/src/Api/Command/PayByVisaMobile.php @@ -8,6 +8,7 @@ final class PayByVisaMobile { public function __construct( public readonly int $paymentId, + public readonly string $visaMobilePhoneNumber, ) { } } diff --git a/src/Api/Command/PayByVisaMobileHandler.php b/src/Api/Command/PayByVisaMobileHandler.php index 5e0d4678..3b779f7b 100644 --- a/src/Api/Command/PayByVisaMobileHandler.php +++ b/src/Api/Command/PayByVisaMobileHandler.php @@ -25,7 +25,7 @@ public function __invoke(PayByVisaMobile $command): PayResult private function setTransactionData(PaymentInterface $payment): void { $paymentDetails = PaymentDetails::fromArray($payment->getDetails()); - $paymentDetails->setVisaMobile(true); + $paymentDetails->setVisaMobilePhoneNumber($paymentDetails->getVisaMobilePhoneNumber()); $payment->setDetails($paymentDetails->toArray()); } diff --git a/src/Api/Factory/NextCommand/PayByVisaMobileFactory.php b/src/Api/Factory/NextCommand/PayByVisaMobileFactory.php index 038a9f63..3a8ee86e 100644 --- a/src/Api/Factory/NextCommand/PayByVisaMobileFactory.php +++ b/src/Api/Factory/NextCommand/PayByVisaMobileFactory.php @@ -20,12 +20,14 @@ public function create(Pay $command, PaymentInterface $payment): PayByVisaMobile /** @var int $paymentId */ $paymentId = $payment->getId(); + /** @var string $visaMobilePhoneNumber */ + $visaMobilePhoneNumber = $command->visaMobilePhoneNumber; - return new PayByVisaMobile($paymentId); + return new PayByVisaMobile($paymentId, $visaMobilePhoneNumber); } public function supports(Pay $command, PaymentInterface $payment): bool { - return $command->isVisaMobilePayment === true && $payment->getId() !== null; + return $command->visaMobilePhoneNumber !== null && $payment->getId() !== null; } } diff --git a/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php b/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php index 61fd5786..b6dae9e1 100644 --- a/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php +++ b/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php @@ -30,8 +30,8 @@ public function __invoke(FormEvent $event): void $form->remove('tpay_channel_id'); } - if (!isset($data['visa_mobile'])) { - $form->remove('visa_mobile'); + if (!isset($data['visa_mobile_phone_number'])) { + $form->remove('visa_mobile_phone_number'); } } } diff --git a/src/Form/Type/TpayPaymentDetailsType.php b/src/Form/Type/TpayPaymentDetailsType.php index 6436fe51..4fe9eeec 100644 --- a/src/Form/Type/TpayPaymentDetailsType.php +++ b/src/Form/Type/TpayPaymentDetailsType.php @@ -7,6 +7,7 @@ use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\EncodedGooglePayToken; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; +use Symfony\Component\Form\Extension\Core\Type\TelType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvents; @@ -65,11 +66,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ], ) ->add( - 'visa_mobile', - HiddenType::class, + 'visa_mobile_phone_number', + TelType::class, [ - 'property_path' => '[visa_mobile]', - 'data' => true, + 'property_path' => '[visa_mobile_phone_number]', ], ) ; diff --git a/src/Model/PaymentDetails.php b/src/Model/PaymentDetails.php index 0b507ed3..9cd9b52b 100644 --- a/src/Model/PaymentDetails.php +++ b/src/Model/PaymentDetails.php @@ -24,7 +24,7 @@ public function __construct( private ?string $successUrl = null, private ?string $failureUrl = null, private ?string $tpayChannelId = null, - private ?bool $visaMobile = false, + private ?string $visaMobilePhoneNumber = null, ) { } @@ -150,14 +150,14 @@ public function getType(): string }; } - public function getVisaMobile(): ?bool + public function getVisaMobilePhoneNumber(): ?string { - return $this->visaMobile; + return $this->visaMobilePhoneNumber; } - public function setVisaMobile(bool $visaMobile): void + public function setVisaMobilePhoneNumber(?string $visaMobilePhoneNumber): void { - $this->visaMobile = $visaMobile; + $this->visaMobilePhoneNumber = $visaMobilePhoneNumber; } public function clearSensitiveData(): void @@ -182,7 +182,7 @@ public static function fromArray(array $details): self $details['tpay']['success_url'] ?? null, $details['tpay']['failure_url'] ?? null, $details['tpay']['tpay_channel_id'] ?? null, - $details['tpay']['visa_mobile'] ?? false, + $details['tpay']['visa_mobile_phone_number'] ?? null, ); } @@ -201,7 +201,7 @@ public function toArray(): array 'success_url' => $this->successUrl, 'failure_url' => $this->failureUrl, 'tpay_channel_id' => $this->tpayChannelId, - 'visa_mobile' => $this->visaMobile, + 'visa_mobile_phone_number' => $this->visaMobilePhoneNumber, ], ]; } diff --git a/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php b/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php index 4c6eafb8..38a73fef 100644 --- a/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php +++ b/src/Payum/Action/Api/CreateVisaMobileTransactionAction.php @@ -8,7 +8,6 @@ use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\Token\NotifyTokenFactoryInterface; use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateVisaMobilePaymentPayloadFactoryInterface; -use Payum\Core\Reply\HttpRedirect; use Payum\Core\Security\GenericTokenFactoryAwareTrait; use Sylius\Component\Core\Model\PaymentInterface; @@ -43,13 +42,8 @@ public function execute($request): void $paymentDetails->setTransactionId($response['transactionId']); $paymentDetails->setStatus($response['status']); - $paymentDetails->setPaymentUrl($response['transactionPaymentUrl']); $model->setDetails($paymentDetails->toArray()); - - if ($paymentDetails->getPaymentUrl() !== null) { - throw new HttpRedirect($paymentDetails->getPaymentUrl()); - } } public function supports($request): bool @@ -66,6 +60,6 @@ public function supports($request): bool $details = $model->getDetails(); - return isset($details['tpay']['visa_mobile']); + return isset($details['tpay']['visa_mobile_phone_number']); } } diff --git a/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php index 72278e77..744185aa 100644 --- a/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php +++ b/src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php @@ -4,6 +4,7 @@ namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Factory; +use CommerceWeavers\SyliusTpayPlugin\Model\PaymentDetails; use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup; use Sylius\Component\Core\Model\PaymentInterface; use Webmozart\Assert\Assert; @@ -20,15 +21,14 @@ public function createFrom(PaymentInterface $payment, string $notifyUrl, string /** @var array{pay: array} $payload */ $payload = $this->createRedirectBasedPaymentPayloadFactory->createFrom($payment, $notifyUrl, $localeCode); - /** @phpstan-param array{tpay?: array{visa_mobile?: bool}} $paymentDetails */ - $paymentDetails = $payment->getDetails(); + $paymentDetails = PaymentDetails::fromArray($payment->getDetails()); - Assert::keyExists( - $paymentDetails['tpay'], - 'visa_mobile', - 'The given payment is not visa mobile payment type.', + Assert::notNull( + $visaMobilePhoneNumber = $paymentDetails->getVisaMobilePhoneNumber(), + 'The given payment has no visa mobile phone number.', ); + $payload['payer']['phone'] = $visaMobilePhoneNumber; $payload['pay']['groupId'] = PayGroup::VISA_MOBILE; return $payload; diff --git a/templates/shop/cart/complete/_visaMobile.html.twig b/templates/shop/cart/complete/_visaMobile.html.twig index 36392e26..ef5df5f4 100644 --- a/templates/shop/cart/complete/_visaMobile.html.twig +++ b/templates/shop/cart/complete/_visaMobile.html.twig @@ -1,6 +1,14 @@ {% set payment = order.lastCartPayment() %} {% if cw_tpay_get_gateway_config_value(payment.method.gatewayConfig, 'type') == 'visa-mobile' %} - {% dump(form.tpay) %} - {{ form_row(form.tpay.visa_mobile) }} +
+
+
+ +
+ {{ form_row(form.tpay.visa_mobile_phone_number) }} +
+
+
+
{% endif %} diff --git a/tests/Api/Shop/PayingForOrdersByVisaMobileTest.php b/tests/Api/Shop/PayingForOrdersByVisaMobileTest.php index 8e0e01ee..6b4afb13 100644 --- a/tests/Api/Shop/PayingForOrdersByVisaMobileTest.php +++ b/tests/Api/Shop/PayingForOrdersByVisaMobileTest.php @@ -24,7 +24,7 @@ protected function setUp(): void $this->setUpOrderPlacer(); } - public function test_paying_with_redirect_based_payment_type(): void + public function test_paying_with_visa_mobile_based_payment_type(): void { $this->loadFixturesFromDirectory('shop/paying_for_orders_by_card'); @@ -37,6 +37,9 @@ public function test_paying_with_redirect_based_payment_type(): void content: json_encode([ 'successUrl' => 'https://example.com/success', 'failureUrl' => 'https://example.com/failure', + 'payer' => [ + 'phone' => '44123456789', + ], 'pay' => [ 'groupId' => PayGroup::VISA_MOBILE, ], diff --git a/tests/Unit/Api/Command/PayByBlikHandlerTest.php b/tests/Unit/Api/Command/PayByBlikHandlerTest.php index 42751b6f..a7519193 100644 --- a/tests/Unit/Api/Command/PayByBlikHandlerTest.php +++ b/tests/Unit/Api/Command/PayByBlikHandlerTest.php @@ -86,7 +86,7 @@ public function test_it_creates_a_blik_based_transaction(): void 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Api/Command/PayByCardHandlerTest.php b/tests/Unit/Api/Command/PayByCardHandlerTest.php index 9c7f9d1c..a43d9d73 100644 --- a/tests/Unit/Api/Command/PayByCardHandlerTest.php +++ b/tests/Unit/Api/Command/PayByCardHandlerTest.php @@ -87,7 +87,7 @@ public function test_it_creates_a_card_based_transaction(): void 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Api/Command/PayByGooglePayHandlerTest.php b/tests/Unit/Api/Command/PayByGooglePayHandlerTest.php index a50637a6..5d7e89be 100644 --- a/tests/Unit/Api/Command/PayByGooglePayHandlerTest.php +++ b/tests/Unit/Api/Command/PayByGooglePayHandlerTest.php @@ -92,7 +92,7 @@ public function test_it_throws_an_exception_if_a_payment_status_is_null(): void 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); $gateway->execute($createTransaction, catchReply: true)->shouldBeCalled(); @@ -131,7 +131,7 @@ public function test_it_creates_a_google_pay_based_transaction(): void 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); $gateway->execute($createTransaction, catchReply: true)->shouldBeCalled(); diff --git a/tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php b/tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php index 6c36b1c8..f21b138b 100644 --- a/tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php +++ b/tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php @@ -45,7 +45,7 @@ public function test_it_throw_an_exception_if_a_payment_cannot_be_found(): void $this->paymentRepository->find(1)->willReturn(null); - $this->createTestSubject()->__invoke(new PayByVisaMobile(1)); + $this->createTestSubject()->__invoke(new PayByVisaMobile(1, visaMobilePhoneNumber: '44123456789')); } public function test_it_throws_an_exception_if_a_gateway_name_cannot_be_determined(): void @@ -60,7 +60,7 @@ public function test_it_throws_an_exception_if_a_gateway_name_cannot_be_determin $this->paymentRepository->find(1)->willReturn($payment); - $this->createTestSubject()->__invoke(new PayByVisaMobile(1)); + $this->createTestSubject()->__invoke(new PayByVisaMobile(1, visaMobilePhoneNumber: '44123456789')); } public function test_it_throws_an_exception_if_payment_details_does_not_have_a_set_status(): void @@ -88,7 +88,7 @@ public function test_it_throws_an_exception_if_payment_details_does_not_have_a_s 'payment_url' => null, 'success_url' => null, 'failure_url' => null, - 'visa_mobile' => true, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); @@ -102,7 +102,7 @@ public function test_it_throws_an_exception_if_payment_details_does_not_have_a_s $this->payum->getGateway('tpay')->willReturn($gateway); - $this->createTestSubject()->__invoke(new PayByVisaMobile(1)); + $this->createTestSubject()->__invoke(new PayByVisaMobile(1, visaMobilePhoneNumber: '44123456789')); } public function test_it_creates_a_visa_mobile_based_transaction(): void @@ -115,7 +115,7 @@ public function test_it_creates_a_visa_mobile_based_transaction(): void $payment = $this->prophesize(PaymentInterface::class); $payment->getMethod()->willReturn($paymentMethod); - $payment->getDetails()->willReturn(['tpay' => ['status' => 'pending', 'payment_url' => 'https://cw.org/pay']]); + $payment->getDetails()->willReturn(['tpay' => ['status' => 'pending', 'payment_url' => 'https://cw.org/pay', 'visa_mobile_phone_number' => '44123456789']]); $payment->setDetails([ 'tpay' => [ 'transaction_id' => null, @@ -127,7 +127,7 @@ public function test_it_creates_a_visa_mobile_based_transaction(): void 'payment_url' => 'https://cw.org/pay', 'success_url' => null, 'failure_url' => null, - 'visa_mobile' => true, + 'visa_mobile_phone_number' => '44123456789', ], ])->shouldBeCalled(); @@ -142,7 +142,7 @@ public function test_it_creates_a_visa_mobile_based_transaction(): void $this->payum->getGateway('tpay')->willReturn($gateway); - $result = $this->createTestSubject()->__invoke(new PayByVisaMobile(1)); + $result = $this->createTestSubject()->__invoke(new PayByVisaMobile(1, visaMobilePhoneNumber: '44123456789')); self::assertSame('pending', $result->status); self::assertSame('https://cw.org/pay', $result->transactionPaymentUrl); diff --git a/tests/Unit/Api/Command/PayHandlerTest.php b/tests/Unit/Api/Command/PayHandlerTest.php index ee2aafce..5e895e5e 100644 --- a/tests/Unit/Api/Command/PayHandlerTest.php +++ b/tests/Unit/Api/Command/PayHandlerTest.php @@ -82,7 +82,7 @@ public function test_it_executes_pay_by_blik_command_if_a_blik_token_is_passed() 'payment_url' => null, 'success_url' => 'https://cw.nonexisting/success', 'failure_url' => 'https://cw.nonexisting/failure', - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.php b/tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.php index 607086de..303ec9b6 100644 --- a/tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.php +++ b/tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.php @@ -18,11 +18,11 @@ class PayByVisaMobileFactoryTest extends TestCase { use ProphecyTrait; - public function test_it_does_not_support_a_command_without_an_visa_mobile_data(): void + public function test_it_does_not_support_a_command_without_an_visa_mobile_phone_number(): void { $factory = $this->createTestSubject(); - $this->assertFalse($factory->supports($this->createCommand(isVisaMobilePayment: false), $this->createPayment())); + $this->assertFalse($factory->supports($this->createCommand(), $this->createPayment())); } public function test_it_does_not_support_a_command_without_a_payment_with_id(): void @@ -32,16 +32,16 @@ public function test_it_does_not_support_a_command_without_a_payment_with_id(): $this->assertFalse($factory->supports($this->createCommand(), new Payment())); } - public function test_it_supports_a_command_with_a_visa_mobile_payment_set_as_true(): void + public function test_it_supports_a_command_with_a_visa_mobile_phone_number(): void { $factory = $this->createTestSubject(); - $this->assertTrue($factory->supports($this->createCommand(), $this->createPayment())); + $this->assertTrue($factory->supports($this->createCommand(visaMobilePhoneNumber: '44123456789'), $this->createPayment())); } public function test_it_creates_a_pay_by_visa_mobile_command(): void { - $command = $this->createTestSubject()->create($this->createCommand(), $this->createPayment()); + $command = $this->createTestSubject()->create($this->createCommand(visaMobilePhoneNumber: '44123456789'), $this->createPayment()); $this->assertInstanceOf(PayByVisaMobile::class, $command); } @@ -53,13 +53,13 @@ public function test_it_throws_an_exception_when_trying_to_create_a_command_with $this->createTestSubject()->create($this->createCommand(), new Payment()); } - private function createCommand(?string $token = null, bool $isVisaMobilePayment = true): Pay + private function createCommand(?string $token = null, ?string $visaMobilePhoneNumber = null): Pay { return new Pay( $token ?? 'token', 'https://cw.nonexisting/success', 'https://cw.nonexisting/failure', - isVisaMobilePayment: $isVisaMobilePayment, + visaMobilePhoneNumber: $visaMobilePhoneNumber, ); } diff --git a/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php b/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php index 83eac444..7e0385d7 100644 --- a/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php +++ b/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php @@ -21,7 +21,7 @@ public function test_it_leaves_blik_field_once_blik_token_is_set(): void $form->remove('blik_token')->shouldNotBeCalled(); $form->remove('google_pay_token')->shouldBeCalled()->willReturn($form); $form->remove('tpay_channel_id')->shouldBeCalled()->willReturn($form); - $form->remove('visa_mobile')->shouldBeCalled()->willReturn($form); + $form->remove('visa_mobile_phone_number')->shouldBeCalled()->willReturn($form); $event = new FormEvent($form->reveal(), ['blik_token' => '123456']); @@ -48,7 +48,7 @@ public function test_it_leaves_card_field_once_card_is_set(): void $form->remove('blik_token')->shouldBeCalled()->willReturn($form); $form->remove('google_pay_token')->shouldBeCalled()->willReturn($form); $form->remove('tpay_channel_id')->shouldBeCalled()->willReturn($form); - $form->remove('visa_mobile')->shouldBeCalled()->willReturn($form); + $form->remove('visa_mobile_phone_number')->shouldBeCalled()->willReturn($form); $event = new FormEvent($form->reveal(), ['card' => 'h45h']); @@ -62,7 +62,7 @@ public function test_it_leaves_pbl_channel_id_field_once_pbl_channel_id_is_set() $form->remove('blik_token')->shouldBeCalled()->willReturn($form); $form->remove('google_pay_token')->shouldBeCalled()->willReturn($form); $form->remove('tpay_channel_id')->shouldNotBeCalled(); - $form->remove('visa_mobile')->shouldBeCalled()->willReturn($form); + $form->remove('visa_mobile_phone_number')->shouldBeCalled()->willReturn($form); $event = new FormEvent($form->reveal(), ['tpay_channel_id' => 1]); @@ -75,9 +75,9 @@ public function test_it_leaves_visa_mobile_field_once_visa_mobile_is_set(): void $form->remove('card')->shouldBeCalled()->willReturn($form); $form->remove('blik_token')->shouldBeCalled()->willReturn($form); $form->remove('tpay_channel_id')->shouldBeCalled()->willReturn($form); - $form->remove('visa_mobile')->shouldNotBeCalled(); + $form->remove('visa_mobile_phone_number')->shouldNotBeCalled(); - $event = new FormEvent($form->reveal(), ['visa_mobile' => true]); + $event = new FormEvent($form->reveal(), ['visa_mobile_phone_number' => true]); $this->createTestSubject()->__invoke($event); } @@ -89,7 +89,7 @@ public function test_it_removes_all_additional_fields_if_none_of_them_are_passed $form->remove('blik_token')->shouldBeCalled()->willReturn($form); $form->remove('google_pay_token')->shouldBeCalled()->willReturn($form); $form->remove('tpay_channel_id')->shouldBeCalled()->willReturn($form); - $form->remove('visa_mobile')->shouldBeCalled()->willReturn($form); + $form->remove('visa_mobile_phone_number')->shouldBeCalled()->willReturn($form); $event = new FormEvent($form->reveal(), []); diff --git a/tests/Unit/Payum/Action/Api/CreateBlikLevelZeroTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateBlikLevelZeroTransactionActionTest.php index 786ffe90..0ce07da3 100644 --- a/tests/Unit/Payum/Action/Api/CreateBlikLevelZeroTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateBlikLevelZeroTransactionActionTest.php @@ -132,7 +132,7 @@ public function test_it_creates_a_payment_and_requests_paying_it_with_a_provided 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); @@ -190,7 +190,7 @@ public function test_it_tries_to_determine_a_gateway_name_by_model_once_token_is 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/CreateCardTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateCardTransactionActionTest.php index 1311f43e..a7435651 100644 --- a/tests/Unit/Payum/Action/Api/CreateCardTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateCardTransactionActionTest.php @@ -138,7 +138,7 @@ public function test_it_creates_a_payment_and_requests_paying_it_with_a_provided 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); @@ -200,7 +200,7 @@ public function test_it_tries_to_determine_a_gateway_name_by_model_once_token_is 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/CreateGooglePayTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateGooglePayTransactionActionTest.php index 0e279325..9af913ea 100644 --- a/tests/Unit/Payum/Action/Api/CreateGooglePayTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateGooglePayTransactionActionTest.php @@ -122,7 +122,7 @@ public function test_it_creates_a_payment_and_requests_paying_it_with_a_provided 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); } @@ -168,7 +168,7 @@ public function test_it_redirects_payment_if_3d_secure_authentication_required() 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); @@ -251,7 +251,7 @@ public function test_it_tries_to_determine_a_gateway_name_by_model_once_token_is 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); } diff --git a/tests/Unit/Payum/Action/Api/CreatePayByLinkTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreatePayByLinkTransactionActionTest.php index 87c20e57..27b1856f 100644 --- a/tests/Unit/Payum/Action/Api/CreatePayByLinkTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreatePayByLinkTransactionActionTest.php @@ -134,7 +134,7 @@ public function test_it_creates_a_payment_and_redirects_to_a_payment_page(): voi 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php index 3526498b..763a7974 100644 --- a/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php @@ -145,7 +145,7 @@ public function test_it_creates_a_payment_and_redirects_to_a_payment_page(): voi 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); @@ -214,7 +214,7 @@ public function test_it_tries_to_determine_a_gateway_name_by_model_once_token_is 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php index 801dce01..28ac3c04 100644 --- a/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateVisaMobileTransactionActionTest.php @@ -39,7 +39,7 @@ protected function setUp(): void public function test_it_supports_create_transaction_requests_with_a_valid_payment_model(): void { $payment = $this->prophesize(PaymentInterface::class); - $payment->getDetails()->willReturn(['tpay' => ['visa_mobile' => true]]); + $payment->getDetails()->willReturn(['tpay' => ['visa_mobile_phone_number' => '44123456789']]); $request = $this->prophesize(CreateTransaction::class); $request->getModel()->willReturn($payment); diff --git a/tests/Unit/Payum/Action/Api/NotifyActionTest.php b/tests/Unit/Payum/Action/Api/NotifyActionTest.php index 0ffb6e71..64fd8ac9 100644 --- a/tests/Unit/Payum/Action/Api/NotifyActionTest.php +++ b/tests/Unit/Payum/Action/Api/NotifyActionTest.php @@ -100,7 +100,7 @@ public function test_it_converts_tpay_notification_status(string $status, string 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Payum/Action/Api/PayWithCardActionTest.php b/tests/Unit/Payum/Action/Api/PayWithCardActionTest.php index 2c4d1f78..690c8283 100644 --- a/tests/Unit/Payum/Action/Api/PayWithCardActionTest.php +++ b/tests/Unit/Payum/Action/Api/PayWithCardActionTest.php @@ -99,7 +99,7 @@ public function test_it_redirects_a_customer_to_3ds_verification_once_a_transact 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); @@ -153,7 +153,7 @@ public function test_it_marks_a_payment_status_as_failed_once_a_transaction_stat 'success_url' => null, 'failure_url' => null, 'tpay_channel_id' => null, - 'visa_mobile' => false, + 'visa_mobile_phone_number' => null, ], ])->shouldBeCalled(); diff --git a/tests/Unit/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryTest.php b/tests/Unit/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryTest.php index a8e90d2d..d74f549b 100644 --- a/tests/Unit/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryTest.php +++ b/tests/Unit/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryTest.php @@ -26,7 +26,7 @@ protected function setUp(): void public function test_it_throws_exception_if_payment_visa_mobile_key_is_missing(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('The given payment is not visa mobile payment type.'); + $this->expectExceptionMessage('The given payment has no visa mobile phone number.'); $payment = $this->prophesize(PaymentInterface::class); @@ -41,7 +41,7 @@ public function test_it_adds_card_related_data_to_a_basic_create_payment_payload { $payment = $this->prophesize(PaymentInterface::class); - $payment->getDetails()->willReturn(['tpay' => ['visa_mobile' => true]]); + $payment->getDetails()->willReturn(['tpay' => ['visa_mobile_phone_number' => '44123456789']]); $this->createRedirectBasedPaymentPayloadFactory->createFrom($payment, 'https://cw.org/notify', 'pl_PL')->willReturn(['some' => 'data']); @@ -49,6 +49,9 @@ public function test_it_adds_card_related_data_to_a_basic_create_payment_payload $this->assertSame([ 'some' => 'data', + 'payer' => [ + 'phone' => '44123456789', + ], 'pay' => [ 'groupId' => PayGroup::VISA_MOBILE, ], From a5ce2f851987059ac8c46ea8166844eb316184a5 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Wed, 16 Oct 2024 18:43:29 +0200 Subject: [PATCH 05/10] Validate UI and API value of mobile phone --- assets/shop/checkout_complete_entrypoint.js | 1 + assets/shop/js/_visa_mobile.js | 73 +++++++++++++++++++ config/validation/Pay.xml | 16 ++++ package.json | 3 + .../shop/cart/complete/_visaMobile.html.twig | 14 +++- translations/validators.en.yaml | 4 + translations/validators.pl.yaml | 4 + 7 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 assets/shop/js/_visa_mobile.js diff --git a/assets/shop/checkout_complete_entrypoint.js b/assets/shop/checkout_complete_entrypoint.js index 5762ce79..cef35072 100644 --- a/assets/shop/checkout_complete_entrypoint.js +++ b/assets/shop/checkout_complete_entrypoint.js @@ -1,6 +1,7 @@ import './img/hourglass.gif'; import './scss/style.scss'; import './js/_pay_by_link'; +import './js/_visa_mobile'; import {CardForm} from "./js/card_form"; document.addEventListener('DOMContentLoaded', () => { diff --git a/assets/shop/js/_visa_mobile.js b/assets/shop/js/_visa_mobile.js new file mode 100644 index 00000000..e4b30db9 --- /dev/null +++ b/assets/shop/js/_visa_mobile.js @@ -0,0 +1,73 @@ +import 'intl-tel-input/build/css/intlTelInput.css'; + +import intlTelInput from 'intl-tel-input'; + +const inputElement = '#sylius_checkout_complete_tpay_visa_mobile_phone_number'; + +const input = document.querySelector(inputElement); +const locale = input.dataset.locale; + +const iti = intlTelInput(input, { + initialCountry: locale, + strictMode: true, + nationalMode: true, + utilsScript: "/intl-tel-input/js/utils.js?1727952657388", + loadUtilsOnInit: () => import("intl-tel-input/utils"), +}); + +document.addEventListener('DOMContentLoaded', () => { + let form = document.querySelector('[name="sylius_checkout_complete"]'); + let phoneNumber = form.querySelector('[data-visa-mobile-phone-number]'); + + form.addEventListener('submit', (event) => { + validateVisaMobilePhoneNumber(); + + const isValid = form.querySelectorAll('.sylius-validation-error').length === 0; + + if (!isValid) { + event.preventDefault(); + event.stopPropagation(); + + form.classList.remove('loading'); + + return; + } + + phoneNumber.value = iti.getNumber().replace(/\D/g, ''); + + form.submit(); + }); + + function validateVisaMobilePhoneNumber() { + if (iti.isValidNumber()) { + clearErrors(phoneNumber); + return; + } + + addError(phoneNumber); + } + + function clearErrors(field) { + const tpayField = field.closest('[data-tpay-field]'); + const errorContainer = tpayField.querySelector('[data-tpay-error-container]'); + + errorContainer.innerHTML = ''; + } + + function addError(field) { + const tpayField = field.closest('[data-tpay-field]'); + const errorContainer = tpayField.querySelector('[data-tpay-error-container]'); + + errorContainer.innerHTML = createErrorElement(field); + } + + function createErrorElement(field) { + const errorMessage = field.dataset.validationError; + + return ` +
+ ${errorMessage} +
+ `; + } +}); diff --git a/config/validation/Pay.xml b/config/validation/Pay.xml index f194917c..2d7409d9 100644 --- a/config/validation/Pay.xml +++ b/config/validation/Pay.xml @@ -63,5 +63,21 @@ + + + + + + + + + + + + diff --git a/package.json b/package.json index d83a414d..7362adf6 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,9 @@ "webpack-cli": "^4.10.0", "webpack-notifier": "^1.15.0" }, + "dependencies": { + "intl-tel-input": "^24.6.0" + }, "license": "UNLICENSED", "private": true, "scripts": { diff --git a/templates/shop/cart/complete/_visaMobile.html.twig b/templates/shop/cart/complete/_visaMobile.html.twig index ef5df5f4..f2f00370 100644 --- a/templates/shop/cart/complete/_visaMobile.html.twig +++ b/templates/shop/cart/complete/_visaMobile.html.twig @@ -6,7 +6,19 @@
- {{ form_row(form.tpay.visa_mobile_phone_number) }} +
+ {{ form_label(form.tpay.visa_mobile_phone_number) }} +
+ {{ form_widget(form.tpay.visa_mobile_phone_number, { + attr: { + 'data-locale': app.request.locale|slice(3, 2), + 'data-validation-error': 'commerce_weavers_sylius_tpay.shop.pay.visa_mobile.number'|trans({}, 'validators'), + 'data-visa-mobile-phone-number': '' + } + }) }} +
+
+
diff --git a/translations/validators.en.yaml b/translations/validators.en.yaml index a4919add..a8e6246e 100644 --- a/translations/validators.en.yaml +++ b/translations/validators.en.yaml @@ -22,3 +22,7 @@ commerce_weavers_sylius_tpay: expiration_year: 'The expiration year must be the current year or later.' number: 'The card number must be composed of 16 digits.' name: 'The cardholder name is required.' + visa_mobile: + required: 'The mobile phone number is required.' + number: 'The mobile phone number is not valid.' + length: 'The mobile phone must be composed of 11 digits (national code and phone number).' diff --git a/translations/validators.pl.yaml b/translations/validators.pl.yaml index 84c321d9..0edcbba1 100644 --- a/translations/validators.pl.yaml +++ b/translations/validators.pl.yaml @@ -23,3 +23,7 @@ commerce_weavers_sylius_tpay: expiration_year: 'Rok ważności musi być bieżący lub późniejszy.' number: 'Numer karty musi składać się z 16 cyfr.' name: 'Imię i nazwisko właściciela karty są wymagane.' + visa_mobile: + number: 'Numer telefonu nie jest prawidłowy.' + required: 'Numer telefonu jest wymagany.' + length: 'Numer telefonu musi składać się z 11 cyfr (numer kierunkowy oraz numer telefonu).' From 97b877f4711f32625d13c914fdbc80bf89ced04b Mon Sep 17 00:00:00 2001 From: arti0090 Date: Wed, 16 Oct 2024 19:09:39 +0200 Subject: [PATCH 06/10] Fixes after rebase --- assets/shop/js/_visa_mobile.js | 41 ++++++++----------- config/config/sylius_template_events.php | 4 +- config/validation/Pay.xml | 7 ++++ package.json | 3 -- src/Api/Command/PayByVisaMobileHandler.php | 16 +------- .../BankListContextProvider.php | 3 +- src/Form/Type/TpayPaymentDetailsType.php | 4 ++ src/Model/PaymentDetails.php | 1 + .../Api/CreateVisaMobileTransactionAction.php | 5 ++- src/Tpay/PaymentType.php | 2 +- .../shop/cart/complete/_payByLink.html.twig | 2 +- .../shop/cart/complete/_visaMobile.html.twig | 6 +-- .../payment_method.yml | 2 +- .../payment_method.yml | 2 +- .../fixtures/common/payment_method.yaml | 2 +- .../Unit/Api/Command/PayByLinkHandlerTest.php | 6 ++- .../Command/PayByVisaMobileHandlerTest.php | 2 + .../TpayChannelIdEligibilityValidatorTest.php | 8 ++-- translations/messages.en.yaml | 2 + translations/messages.pl.yaml | 2 + translations/validators.en.yaml | 1 - translations/validators.pl.yaml | 1 - 22 files changed, 58 insertions(+), 64 deletions(-) diff --git a/assets/shop/js/_visa_mobile.js b/assets/shop/js/_visa_mobile.js index e4b30db9..ad6d88e9 100644 --- a/assets/shop/js/_visa_mobile.js +++ b/assets/shop/js/_visa_mobile.js @@ -1,26 +1,11 @@ -import 'intl-tel-input/build/css/intlTelInput.css'; - -import intlTelInput from 'intl-tel-input'; - -const inputElement = '#sylius_checkout_complete_tpay_visa_mobile_phone_number'; - -const input = document.querySelector(inputElement); -const locale = input.dataset.locale; - -const iti = intlTelInput(input, { - initialCountry: locale, - strictMode: true, - nationalMode: true, - utilsScript: "/intl-tel-input/js/utils.js?1727952657388", - loadUtilsOnInit: () => import("intl-tel-input/utils"), -}); - document.addEventListener('DOMContentLoaded', () => { + const COUNTRY_CODED_MOBILE_PHONE_LENGTH = 11; + let form = document.querySelector('[name="sylius_checkout_complete"]'); let phoneNumber = form.querySelector('[data-visa-mobile-phone-number]'); form.addEventListener('submit', (event) => { - validateVisaMobilePhoneNumber(); + validateVisaMobilePhoneNumber(phoneNumber); const isValid = form.querySelectorAll('.sylius-validation-error').length === 0; @@ -33,17 +18,23 @@ document.addEventListener('DOMContentLoaded', () => { return; } - phoneNumber.value = iti.getNumber().replace(/\D/g, ''); form.submit(); }); - function validateVisaMobilePhoneNumber() { - if (iti.isValidNumber()) { + function validateVisaMobilePhoneNumber(field) { + let fieldLength = field.value.length; + + if (fieldLength === COUNTRY_CODED_MOBILE_PHONE_LENGTH) { clearErrors(phoneNumber); return; } + if (fieldLength === 0) { + addError(phoneNumber, 'validationErrorRequired'); + return; + } + addError(phoneNumber); } @@ -54,15 +45,15 @@ document.addEventListener('DOMContentLoaded', () => { errorContainer.innerHTML = ''; } - function addError(field) { + function addError(field, validationErrorName = 'validationErrorLength') { const tpayField = field.closest('[data-tpay-field]'); const errorContainer = tpayField.querySelector('[data-tpay-error-container]'); - errorContainer.innerHTML = createErrorElement(field); + errorContainer.innerHTML = createErrorElement(field, validationErrorName); } - function createErrorElement(field) { - const errorMessage = field.dataset.validationError; + function createErrorElement(field, validationErrorName = 'validationErrorLength') { + const errorMessage = field.dataset[validationErrorName]; return `
diff --git a/config/config/sylius_template_events.php b/config/config/sylius_template_events.php index 03a9f736..92b78810 100644 --- a/config/config/sylius_template_events.php +++ b/config/config/sylius_template_events.php @@ -29,11 +29,11 @@ 'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_card.html.twig', 'priority' => 5, ], - 'pay-by-link' => [ + 'pay_by_link' => [ 'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_payByLink.html.twig', 'priority' => 5, ], - 'visa-mobile' => [ + 'visa_mobile' => [ 'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_visaMobile.html.twig', 'priority' => 5, ] diff --git a/config/validation/Pay.xml b/config/validation/Pay.xml index 2d7409d9..2df94a27 100644 --- a/config/validation/Pay.xml +++ b/config/validation/Pay.xml @@ -50,6 +50,13 @@ + + + + +