Skip to content

Commit

Permalink
Constraint payload to require success and cancel urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Nov 19, 2024
1 parent cd963b0 commit 3c6fbac
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
20 changes: 20 additions & 0 deletions config/services/validators.yaml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions config/validation/AddPaymentRequest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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 http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Bundle\ApiBundle\Command\Payment\AddPaymentRequest">
<property name="payload">
<constraint name="FluxSE\SyliusStripePlugin\Validator\Constraints\CheckoutSessionCreatePayloadRequirement">
<option name="groups">
<value>sylius</value>
<value>stripe</value>
<value>stripe_checkout</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusStripePlugin\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

final class CheckoutSessionCreatePayloadRequirement extends Constraint
{
public string $noSuccessUrlFound = 'flux_se_sylius_stripe_plugin.stripe_checkout.success_url.not_found';
public string $noCancelUrlFound = 'flux_se_sylius_stripe_plugin.stripe_checkout.cancel_url.not_found';

public function validatedBy(): string
{
return 'flux_se_sylius_stripe_checkout_session_create_payload_requirement';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusStripePlugin\Validator\Constraints;

use Sylius\Bundle\ApiBundle\Command\Payment\AddPaymentRequest;
use Sylius\Bundle\PaymentBundle\Provider\GatewayFactoryNameProviderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;

final class CheckoutSessionCreatePayloadRequirementValidator extends ConstraintValidator
{
/**
* @param string[] $supportedFactoryNames
* @param string[] $supportedActions
*/
public function __construct(
private PaymentMethodRepositoryInterface $paymentMethodRepository,
private GatewayFactoryNameProviderInterface $gatewayFactoryNameProvider,
private array $supportedFactoryNames,
private array $supportedActions,
) {
}

public function validate(mixed $value, Constraint $constraint): void
{
/** @var CheckoutSessionCreatePayloadRequirement $constraint */
Assert::isInstanceOf($constraint, CheckoutSessionCreatePayloadRequirement::class);

/** @var AddPaymentRequest $addPaymentRequest */
$addPaymentRequest = $this->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);
}
}
}
5 changes: 5 additions & 0 deletions translations/validators.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 5 additions & 0 deletions translations/validators.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 3c6fbac

Please sign in to comment.