From 3c6fbac2c0909f5b1199cec41dd91a55cb223706 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Tue, 19 Nov 2024 20:01:32 +0100 Subject: [PATCH] Constraint payload to require success and cancel urls --- config/services/validators.yaml | 20 ++++++ config/validation/AddPaymentRequest.xml | 15 ++++ ...heckoutSessionCreatePayloadRequirement.php | 18 +++++ ...ssionCreatePayloadRequirementValidator.php | 71 +++++++++++++++++++ translations/validators.en.yaml | 5 ++ translations/validators.fr.yaml | 5 ++ 6 files changed, 134 insertions(+) create mode 100644 config/services/validators.yaml create mode 100644 config/validation/AddPaymentRequest.xml create mode 100644 src/Validator/Constraints/CheckoutSessionCreatePayloadRequirement.php create mode 100644 src/Validator/Constraints/CheckoutSessionCreatePayloadRequirementValidator.php diff --git a/config/services/validators.yaml b/config/services/validators.yaml new file mode 100644 index 0000000..e20fb06 --- /dev/null +++ b/config/services/validators.yaml @@ -0,0 +1,20 @@ +parameters: + flux_se.sylius_stripe.validator.payload_requirements.supported_factory_names: + - stripe_checkout + flux_se.sylius_stripe.validator.payload_requirements.supported_actions: + - ~ + - !php/const Sylius\Component\Payment\Model\PaymentRequestInterface::ACTION_AUTHORIZE + - !php/const Sylius\Component\Payment\Model\PaymentRequestInterface::ACTION_CAPTURE + +services: + + flux_se.sylius_stripe.validator.checkout_session_create_payload_requirement: + class: FluxSE\SyliusStripePlugin\Validator\Constraints\CheckoutSessionCreatePayloadRequirementValidator + arguments: + - '@sylius.repository.payment_method' + - '@sylius.provider.payment_request.gateway_factory_name' + - '%flux_se.sylius_stripe.validator.payload_requirements.supported_factory_names%' + - '%flux_se.sylius_stripe.validator.payload_requirements.supported_actions%' + tags: + - name: validator.constraint_validator + alias: flux_se_sylius_stripe_checkout_session_create_payload_requirement diff --git a/config/validation/AddPaymentRequest.xml b/config/validation/AddPaymentRequest.xml new file mode 100644 index 0000000..717e5f7 --- /dev/null +++ b/config/validation/AddPaymentRequest.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/src/Validator/Constraints/CheckoutSessionCreatePayloadRequirement.php b/src/Validator/Constraints/CheckoutSessionCreatePayloadRequirement.php new file mode 100644 index 0000000..87927c8 --- /dev/null +++ b/src/Validator/Constraints/CheckoutSessionCreatePayloadRequirement.php @@ -0,0 +1,18 @@ +context->getObject(); + Assert::isInstanceOf($addPaymentRequest, AddPaymentRequest::class); + + /** @var PaymentMethodInterface|null $paymentMethod */ + $paymentMethod = $this->paymentMethodRepository->findOneBy([ + 'code' => $addPaymentRequest->paymentMethodCode + ]); + + if (null === $paymentMethod) { + return; + } + + if (false === in_array( + $this->gatewayFactoryNameProvider->provide($paymentMethod), + $this->supportedFactoryNames, + true + )) { + return; + } + + if (false === in_array( + $addPaymentRequest->action, + $this->supportedActions, + true + )) { + return; + } + + if (false === isset($value['success_url'])) { + $this->context->addViolation($constraint->noSuccessUrlFound); + } + + if (false === isset($value['cancel_url'])) { + $this->context->addViolation($constraint->noCancelUrlFound); + } + } +} diff --git a/translations/validators.en.yaml b/translations/validators.en.yaml index 8fbed0e..cc3eea9 100644 --- a/translations/validators.en.yaml +++ b/translations/validators.en.yaml @@ -6,3 +6,8 @@ flux_se_sylius_stripe_plugin: not_blank: Please enter stripe secret key. publishable_key: not_blank: Please enter stripe publishable key. + stripe_checkout: + success_url: + not_found: The payload must contain a "success_url" array property to be able to redirect the visitor after the Stripe Checkout Session portal completion. + cancel_url: + not_found: The payload must contain a "cancel_url" array property to be able to redirect the visitor after the Stripe Checkout Session portal completion. diff --git a/translations/validators.fr.yaml b/translations/validators.fr.yaml index 346e1d3..9b4fed2 100644 --- a/translations/validators.fr.yaml +++ b/translations/validators.fr.yaml @@ -6,3 +6,8 @@ flux_se_sylius_stripe_plugin: not_blank: Veuillez entrer votre clé secrète Stripe. publishable_key: not_blank: Veuillez entrer votre clé publique Stripe. + stripe_checkout: + success_url: + not_found: Le "payload" doit contenir un champ de tableau nommé "success_url" pour rediriger le visiteur après avoir terminé une Stripe Checkout Session. + cancel_url: + not_found: Le "payload" doit contenir un champ de tableau nommé "cancel_url" pour rediriger le visiteur après avoir terminé une Stripe Checkout Session.