-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Constraint payload to require success and cancel urls
- Loading branch information
Showing
6 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
18 changes: 18 additions & 0 deletions
18
src/Validator/Constraints/CheckoutSessionCreatePayloadRequirement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Validator/Constraints/CheckoutSessionCreatePayloadRequirementValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters