From 8800876d0a13beb710842d7522081433c5760d68 Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Wed, 7 Feb 2024 12:14:40 +0100 Subject: [PATCH] Add Pagolight payment method unique constraint --- config/services/validator.php | 18 ++++++ config/validation/PaymentMethod.xml | 11 ++++ docs/docs.md | 2 +- src/Controller/PaymentController.php | 2 +- .../PagolightPaymentMethodsResolver.php | 4 +- .../PagolightPaymentMethodUnique.php | 20 ++++++ .../PagolightPaymentMethodUniqueValidator.php | 61 +++++++++++++++++++ translations/.gitignore | 0 translations/validators.en.yaml | 3 + translations/validators.it.yaml | 3 + 10 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 config/services/validator.php create mode 100644 config/validation/PaymentMethod.xml create mode 100644 src/Validator/PagolightPaymentMethodUnique.php create mode 100644 src/Validator/PagolightPaymentMethodUniqueValidator.php delete mode 100644 translations/.gitignore create mode 100644 translations/validators.en.yaml create mode 100644 translations/validators.it.yaml diff --git a/config/services/validator.php b/config/services/validator.php new file mode 100644 index 0000000..298895e --- /dev/null +++ b/config/services/validator.php @@ -0,0 +1,18 @@ +services(); + + $services->set('webgriffe_sylius_pagolight.validator.pagolight_payment_method_unique', PagolightPaymentMethodUniqueValidator::class) + ->args([ + service('sylius.repository.payment_method'), + ]) + ->tag('validator.constraint_validator') + ; +}; diff --git a/config/validation/PaymentMethod.xml b/config/validation/PaymentMethod.xml new file mode 100644 index 0000000..e183872 --- /dev/null +++ b/config/validation/PaymentMethod.xml @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/docs/docs.md b/docs/docs.md index f0c72b9..9a71bb0 100644 --- a/docs/docs.md +++ b/docs/docs.md @@ -15,7 +15,7 @@ potrebbe essere una GET senza parametri alla rotta specificata in fase di creazi TODO - [x] Salvare in cache il bearer token - [x] Creare delle Payum API che siano univoche per ogni gateway normale/PRO -- [ ] Aggiungere regola di validazione univocità gateway normale/PRO +- [x] Aggiungere regola di validazione univocità gateway normale/PRO - [x] Completare il contract - [x] Aggiungere il webhook - [x] Valutare pagina di stato con JS che pinga il server diff --git a/src/Controller/PaymentController.php b/src/Controller/PaymentController.php index dc98282..1138724 100644 --- a/src/Controller/PaymentController.php +++ b/src/Controller/PaymentController.php @@ -42,7 +42,7 @@ public function statusAction(mixed $paymentId): Response if (!$paymentGatewayConfig instanceof GatewayConfigInterface) { throw $this->createAccessDeniedException(); } - if (!in_array($paymentGatewayConfig->getGatewayName(), [PagolightApi::PAGOLIGHT_GATEWAY_CODE, PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE], true)) { + if (!in_array($paymentGatewayConfig->getFactoryName(), [PagolightApi::PAGOLIGHT_GATEWAY_CODE, PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE], true)) { throw $this->createAccessDeniedException(); } diff --git a/src/Resolver/PagolightPaymentMethodsResolver.php b/src/Resolver/PagolightPaymentMethodsResolver.php index 83a8720..1486a6c 100644 --- a/src/Resolver/PagolightPaymentMethodsResolver.php +++ b/src/Resolver/PagolightPaymentMethodsResolver.php @@ -57,7 +57,7 @@ static function (PaymentMethodInterface $paymentMethod) use ($billingAddress, $s if ($gatewayConfig === null) { return false; } - if (!in_array($gatewayConfig->getGatewayName(), [PagolightApi::PAGOLIGHT_GATEWAY_CODE, PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE], true)) { + if (!in_array($gatewayConfig->getFactoryName(), [PagolightApi::PAGOLIGHT_GATEWAY_CODE, PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE], true)) { return true; } if (!in_array($billingAddress->getCountryCode(), Config::ALLOWED_COUNTRY_CODES, true)) { @@ -70,7 +70,7 @@ static function (PaymentMethodInterface $paymentMethod) use ($billingAddress, $s return false; } if ($orderAmount <= (Config::PAGOLIGHT_PRO_MINIMUM_AMOUNT * 100) && - $gatewayConfig->getGatewayName() === PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE + $gatewayConfig->getFactoryName() === PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE ) { return false; } diff --git a/src/Validator/PagolightPaymentMethodUnique.php b/src/Validator/PagolightPaymentMethodUnique.php new file mode 100644 index 0000000..849fcd4 --- /dev/null +++ b/src/Validator/PagolightPaymentMethodUnique.php @@ -0,0 +1,20 @@ +getGatewayConfig(); + if ($gatewayConfig === null || + !in_array($gatewayConfig->getFactoryName(), [ + PagolightApi::PAGOLIGHT_GATEWAY_CODE, + PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, + ], true) + ) { + return; + } + + $paymentMethods = $this->paymentMethodRepository->findAll(); + $paymentMethodsWithSameGatewayConfig = array_filter( + $paymentMethods, + static fn (PaymentMethodInterface $paymentMethod) => $paymentMethod->getGatewayConfig()?->getFactoryName() === $gatewayConfig->getFactoryName() + ); + if (count($paymentMethodsWithSameGatewayConfig) > 1 || + (count($paymentMethodsWithSameGatewayConfig) === 1 && reset($paymentMethodsWithSameGatewayConfig) !== $value) + ) { + $this->context + ->buildViolation($constraint->message) + ->atPath('gatewayConfig') + ->addViolation(); + } + } +} diff --git a/translations/.gitignore b/translations/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/translations/validators.en.yaml b/translations/validators.en.yaml new file mode 100644 index 0000000..bebc61e --- /dev/null +++ b/translations/validators.en.yaml @@ -0,0 +1,3 @@ +webgriffe_sylius_pagolight: + payment_method: + unique: 'There should be only one payment method for this gateway.' diff --git a/translations/validators.it.yaml b/translations/validators.it.yaml new file mode 100644 index 0000000..d9597fd --- /dev/null +++ b/translations/validators.it.yaml @@ -0,0 +1,3 @@ +webgriffe_sylius_pagolight: + payment_method: + unique: 'Ci deve essere un solo metodo di pagamento per questo gateway.'