From 2480b753a3caf89d874e8b814b9f3a2dcbc068f5 Mon Sep 17 00:00:00 2001 From: Jan Goralski Date: Tue, 5 Mar 2024 10:54:30 +0100 Subject: [PATCH 1/2] [Refactor] Simplify configuring allowed payment methods --- config/admin_routing.yml | 14 ++- config/services/.gitkeep | 0 config/services/actions.php | 13 --- config/services/command_handler.php | 11 -- src/Command/ConfigurePaymentMethods.php | 22 ---- .../ConfigurePaymentMethodsHandler.php | 31 ------ .../Action/ConfigurePaymentMethodsAction.php | 100 ------------------ ...aferpayPaymentMethodsConfigurationType.php | 1 + .../Form/_content.html.twig | 1 + 9 files changed, 13 insertions(+), 180 deletions(-) delete mode 100644 config/services/.gitkeep delete mode 100644 src/Command/ConfigurePaymentMethods.php delete mode 100644 src/CommandHandler/ConfigurePaymentMethodsHandler.php delete mode 100644 src/Controller/Action/ConfigurePaymentMethodsAction.php diff --git a/config/admin_routing.yml b/config/admin_routing.yml index b97d9f41..b7b141ba 100644 --- a/config/admin_routing.yml +++ b/config/admin_routing.yml @@ -11,7 +11,7 @@ commerce_weavers_saferpay_transaction_log: icon: 'th list' type: sylius.resource prefix: saferpay - + commerce_weavers_saferpay_admin_transaction_log_show: path: /saferpay/transaction-logs/{id} methods: [ GET ] @@ -22,6 +22,14 @@ commerce_weavers_saferpay_admin_transaction_log_show: commerce_weavers_sylius_saferpay_admin_configure_payment_methods: path: /payment-methods/{id}/configure-saferpay-payment-methods - methods: [GET, POST] + methods: [GET, PATCH] defaults: - _controller: CommerceWeavers\SyliusSaferpayPlugin\Controller\Action\ConfigurePaymentMethodsAction + _controller: sylius.controller.payment_method::updateAction + _sylius: + template: '@CommerceWeaversSyliusSaferpayPlugin/Admin/PaymentMethod/configurePaymentMethods.html.twig' + redirect: sylius_admin_payment_method_index + form: + type: CommerceWeavers\SyliusSaferpayPlugin\Form\Type\SaferpayPaymentMethodsConfigurationType + options: + paymentMethod: expr:service('sylius.repository.payment_method').find($id) + validation_groups: ['sylius'] diff --git a/config/services/.gitkeep b/config/services/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/config/services/actions.php b/config/services/actions.php index 107019e0..666ec014 100644 --- a/config/services/actions.php +++ b/config/services/actions.php @@ -4,7 +4,6 @@ use CommerceWeavers\SyliusSaferpayPlugin\Controller\Action\AfterUnsuccessfulPaymentAction; use CommerceWeavers\SyliusSaferpayPlugin\Controller\Action\AssertAction; -use CommerceWeavers\SyliusSaferpayPlugin\Controller\Action\ConfigurePaymentMethodsAction; use CommerceWeavers\SyliusSaferpayPlugin\Controller\Action\PrepareAssertAction; use CommerceWeavers\SyliusSaferpayPlugin\Controller\Action\PrepareCaptureAction; use CommerceWeavers\SyliusSaferpayPlugin\Controller\Action\WebhookAction; @@ -64,18 +63,6 @@ ->tag('controller.service_arguments') ; - $services - ->set(ConfigurePaymentMethodsAction::class) - ->args([ - service('sylius.command_bus'), - service('form.factory'), - service('twig'), - service('router'), - service('sylius.repository.payment_method'), - ]) - ->tag('controller.service_arguments') - ; - $services ->set(WebhookAction::class) ->args([ diff --git a/config/services/command_handler.php b/config/services/command_handler.php index dd9d8781..08bbce3a 100644 --- a/config/services/command_handler.php +++ b/config/services/command_handler.php @@ -2,28 +2,17 @@ declare(strict_types=1); -use CommerceWeavers\SyliusSaferpayPlugin\CommandHandler\ConfigurePaymentMethodsHandler; use CommerceWeavers\SyliusSaferpayPlugin\Payment\Command\Handler\AssertPaymentHandler; use CommerceWeavers\SyliusSaferpayPlugin\Payment\Command\Handler\CapturePaymentHandler; use CommerceWeavers\SyliusSaferpayPlugin\Payum\Factory\AssertFactoryInterface; use CommerceWeavers\SyliusSaferpayPlugin\Payum\Factory\CaptureFactoryInterface; use CommerceWeavers\SyliusSaferpayPlugin\Payum\Factory\ResolveNextCommandFactoryInterface; -use CommerceWeavers\SyliusSaferpayPlugin\Processor\SaferpayPaymentProcessor; -use CommerceWeavers\SyliusSaferpayPlugin\Provider\PaymentProviderInterface; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; return static function (ContainerConfigurator $containerConfigurator) { $services = $containerConfigurator->services(); - $services - ->set(ConfigurePaymentMethodsHandler::class) - ->args([ - service('sylius.repository.payment_method'), - ]) - ->tag('messenger.message_handler', ['bus' => 'sylius.command_bus']) - ; - $services ->set(AssertPaymentHandler::class) ->args([ diff --git a/src/Command/ConfigurePaymentMethods.php b/src/Command/ConfigurePaymentMethods.php deleted file mode 100644 index 51a45597..00000000 --- a/src/Command/ConfigurePaymentMethods.php +++ /dev/null @@ -1,22 +0,0 @@ -paymentMethodId; - } - - public function getPaymentMethods(): array - { - return $this->paymentMethods; - } -} diff --git a/src/CommandHandler/ConfigurePaymentMethodsHandler.php b/src/CommandHandler/ConfigurePaymentMethodsHandler.php deleted file mode 100644 index 5c5974ec..00000000 --- a/src/CommandHandler/ConfigurePaymentMethodsHandler.php +++ /dev/null @@ -1,31 +0,0 @@ -paymentMethodRepository->find($command->getPaymentMethodId()); - Assert::notNull($paymentMethod); - - $gatewayConfig = $paymentMethod->getGatewayConfig(); - Assert::notNull($gatewayConfig); - - $configuration = $gatewayConfig->getConfig(); - $configuration['allowed_payment_methods'] = $command->getPaymentMethods(); - $gatewayConfig->setConfig($configuration); - } -} diff --git a/src/Controller/Action/ConfigurePaymentMethodsAction.php b/src/Controller/Action/ConfigurePaymentMethodsAction.php deleted file mode 100644 index d5c0eac7..00000000 --- a/src/Controller/Action/ConfigurePaymentMethodsAction.php +++ /dev/null @@ -1,100 +0,0 @@ -attributes->get('id'); - - /** @var PaymentMethodInterface|null $paymentMethod */ - $paymentMethod = $this->paymentMethodRepository->find($paymentMethodId); - if (null === $paymentMethod) { - throw new NotFoundHttpException('The payment method has not been found'); - } - - $gatewayConfig = $paymentMethod->getGatewayConfig(); - if (null === $gatewayConfig) { - throw new NotFoundHttpException('The gateway config for payment method has not been found'); - } - - if ($gatewayConfig->getFactoryName() !== self::SAFERPAY_FACTORY_NAME) { - return $this->provideRedirectResponse($request); - } - - $form = $this->formFactory->create( - SaferpayPaymentMethodsConfigurationType::class, - $gatewayConfig->getConfig(), - ['paymentMethod' => $paymentMethod], - ); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - /** @var array $data */ - $data = $form->getData(); - - /** @var array $allowedPaymentMethods */ - $allowedPaymentMethods = $data['allowed_payment_methods']; - - $this->commandBus->dispatch(new ConfigurePaymentMethods($paymentMethodId, $allowedPaymentMethods)); - - $this->addFlashMessage($request, 'success', 'sylius_saferpay.payment_method.configure_payment_methods'); - - return $this->provideRedirectResponse($request); - } - - return new Response($this->twig->render( - '@CommerceWeaversSyliusSaferpayPlugin/Admin/PaymentMethod/configurePaymentMethods.html.twig', - [ - 'form' => $form->createView(), - 'payment_method' => $paymentMethod, - ], - )); - } - - private function addFlashMessage(Request $request, string $type, string $message): void - { - /** @var Session $session */ - $session = $request->getSession(); - $session->getFlashBag()->add($type, $message); - } - - private function provideRedirectResponse(Request $request): RedirectResponse - { - $referer = $request->headers->get('referer'); - if (null === $referer) { - return new RedirectResponse($this->router->generate('sylius_admin_payment_method_index')); - } - - return new RedirectResponse($referer); - } -} diff --git a/src/Form/Type/SaferpayPaymentMethodsConfigurationType.php b/src/Form/Type/SaferpayPaymentMethodsConfigurationType.php index 9b64414e..1b854234 100644 --- a/src/Form/Type/SaferpayPaymentMethodsConfigurationType.php +++ b/src/Form/Type/SaferpayPaymentMethodsConfigurationType.php @@ -33,6 +33,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'choice_label' => function (string $paymentMethodData): string { return $paymentMethodData; }, + 'property_path' => 'gatewayConfig.config[allowed_payment_methods]', 'expanded' => true, 'label' => 'commerce_weavers_saferpay.ui.allowed_payment_methods', 'multiple' => true, diff --git a/templates/Admin/PaymentMethod/ConfigurePaymentMethods/Form/_content.html.twig b/templates/Admin/PaymentMethod/ConfigurePaymentMethods/Form/_content.html.twig index 57a9d26e..8f5e148c 100644 --- a/templates/Admin/PaymentMethod/ConfigurePaymentMethods/Form/_content.html.twig +++ b/templates/Admin/PaymentMethod/ConfigurePaymentMethods/Form/_content.html.twig @@ -1,6 +1,7 @@ {% form_theme form '@SyliusAdmin/Form/theme.html.twig' %} {{ form_start(form, {'attr': {'class': 'ui loadable form', 'novalidate': 'novalidate'}}) }} + {% include '@SyliusAdmin/Crud/form_validation_errors_checker.html.twig' %} {{ form_errors(form) }} From c18301e7d76ea1b20cd24e2f5603bf44128a3b27 Mon Sep 17 00:00:00 2001 From: Jan Goralski Date: Tue, 5 Mar 2024 12:55:03 +0100 Subject: [PATCH 2/2] [Maintenance] Remove leftover specs --- spec/Command/ConfigurePaymentMethodsSpec.php | 18 ------ .../ConfigurePaymentMethodsHandlerSpec.php | 57 ------------------- 2 files changed, 75 deletions(-) delete mode 100644 spec/Command/ConfigurePaymentMethodsSpec.php delete mode 100644 spec/CommandHandler/ConfigurePaymentMethodsHandlerSpec.php diff --git a/spec/Command/ConfigurePaymentMethodsSpec.php b/spec/Command/ConfigurePaymentMethodsSpec.php deleted file mode 100644 index 55529cfe..00000000 --- a/spec/Command/ConfigurePaymentMethodsSpec.php +++ /dev/null @@ -1,18 +0,0 @@ -beConstructedWith('payment_method_id', [1, 2]); - - $this->getPaymentMethodId()->shouldReturn('payment_method_id'); - $this->getPaymentMethods()->shouldReturn([1, 2]); - } -} diff --git a/spec/CommandHandler/ConfigurePaymentMethodsHandlerSpec.php b/spec/CommandHandler/ConfigurePaymentMethodsHandlerSpec.php deleted file mode 100644 index 968d5088..00000000 --- a/spec/CommandHandler/ConfigurePaymentMethodsHandlerSpec.php +++ /dev/null @@ -1,57 +0,0 @@ -beConstructedWith($paymentMethodRepository); - } - - public function it_updates_the_allowed_payment_methods_config_of_a_given_payment_method( - PaymentMethodRepositoryInterface $paymentMethodRepository, - PaymentMethodInterface $paymentMethod, - GatewayConfigInterface $gatewayConfig, - ): void { - $paymentMethodRepository->find('payment_method_id')->willReturn($paymentMethod); - $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); - $gatewayConfig->getConfig()->willReturn([]); - - $gatewayConfig->setConfig(['allowed_payment_methods' => ['1', '1']])->shouldBeCalled(); - - $this(new ConfigurePaymentMethods('payment_method_id', ['1', '1'])); - } - - function it_throws_an_exception_when_the_payment_method_cannot_be_found( - PaymentMethodRepositoryInterface $paymentMethodRepository, - ): void { - $paymentMethodRepository->find('payment_method_id')->willReturn(null); - - $this - ->shouldThrow(\InvalidArgumentException::class) - ->during('__invoke', [new ConfigurePaymentMethods('payment_method_id', ['1', '1'])]) - ; - } - - function it_throws_an_exception_when_the_payment_method_has_no_gateway_config( - PaymentMethodRepositoryInterface $paymentMethodRepository, - PaymentMethodInterface $paymentMethod, - ): void { - $paymentMethodRepository->find('payment_method_id')->willReturn($paymentMethod); - $paymentMethod->getGatewayConfig()->willReturn(null); - - $this - ->shouldThrow(\InvalidArgumentException::class) - ->during('__invoke', [new ConfigurePaymentMethods('payment_method_id', ['1', '1'])]) - ; - } -}