Skip to content

Commit

Permalink
Add Pagolight payment method unique constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Feb 7, 2024
1 parent af1dd4a commit 8800876
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 4 deletions.
18 changes: 18 additions & 0 deletions config/services/validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Webgriffe\SyliusPagolightPlugin\Validator\PagolightPaymentMethodUniqueValidator;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();

$services->set('webgriffe_sylius_pagolight.validator.pagolight_payment_method_unique', PagolightPaymentMethodUniqueValidator::class)
->args([
service('sylius.repository.payment_method'),
])
->tag('validator.constraint_validator')
;
};
11 changes: 11 additions & 0 deletions config/validation/PaymentMethod.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Core\Model\PaymentMethod">
<constraint name="Webgriffe\SyliusPagolightPlugin\Validator\PagolightPaymentMethodUnique">
<option name="groups">sylius</option>
</constraint>
</class>
</constraint-mapping>
2 changes: 1 addition & 1 deletion docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Resolver/PagolightPaymentMethodsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Validator/PagolightPaymentMethodUnique.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusPagolightPlugin\Validator;

use Symfony\Component\Validator\Constraint;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class PagolightPaymentMethodUnique extends Constraint
{
public string $message = 'webgriffe_sylius_pagolight.payment_method.unique';

public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
61 changes: 61 additions & 0 deletions src/Validator/PagolightPaymentMethodUniqueValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusPagolightPlugin\Validator;

use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightApi;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class PagolightPaymentMethodUniqueValidator extends ConstraintValidator
{
public function __construct(
private readonly PaymentMethodRepositoryInterface $paymentMethodRepository,
) {
}

/**
* @param mixed|PaymentMethodInterface $value
*/
public function validate(mixed $value, Constraint $constraint): void
{
if (!$value instanceof PaymentMethodInterface) {
throw new UnexpectedValueException($value, PaymentMethodInterface::class);
}

if (!$constraint instanceof PagolightPaymentMethodUnique) {
throw new UnexpectedValueException($constraint, PagolightPaymentMethodUnique::class);
}

$gatewayConfig = $value->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();
}
}
}
Empty file removed translations/.gitignore
Empty file.
3 changes: 3 additions & 0 deletions translations/validators.en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
webgriffe_sylius_pagolight:
payment_method:
unique: 'There should be only one payment method for this gateway.'
3 changes: 3 additions & 0 deletions translations/validators.it.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
webgriffe_sylius_pagolight:
payment_method:
unique: 'Ci deve essere un solo metodo di pagamento per questo gateway.'

0 comments on commit 8800876

Please sign in to comment.