mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-05 20:57:12 +00:00
[API] Enforce channel eligibility check when changing payment method via account endpoint
This commit is contained in:
parent
d2b16b81fd
commit
97ea859c21
4 changed files with 14 additions and 1 deletions
|
|
@ -19,6 +19,7 @@ use Sylius\Component\Core\Model\PaymentMethodInterface;
|
|||
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
|
||||
use Sylius\Component\Core\Repository\PaymentRepositoryInterface;
|
||||
use Sylius\Component\Payment\Model\PaymentInterface;
|
||||
use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
final readonly class PaymentMethodChanger implements PaymentMethodChangerInterface
|
||||
|
|
@ -26,6 +27,7 @@ final readonly class PaymentMethodChanger implements PaymentMethodChangerInterfa
|
|||
public function __construct(
|
||||
private PaymentRepositoryInterface $paymentRepository,
|
||||
private PaymentMethodRepositoryInterface $paymentMethodRepository,
|
||||
private PaymentMethodsResolverInterface $paymentMethodsResolver,
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -43,6 +45,10 @@ final readonly class PaymentMethodChanger implements PaymentMethodChangerInterfa
|
|||
$payment = $this->paymentRepository->findOneByOrderId($paymentId, $order->getId());
|
||||
Assert::notNull($payment, 'Can not find payment with given identifier.');
|
||||
|
||||
if (!in_array($paymentMethod, $this->paymentMethodsResolver->getSupportedMethods($payment), true)) {
|
||||
throw new PaymentMethodCannotBeChangedException();
|
||||
}
|
||||
|
||||
if ($order->getState() === OrderInterface::STATE_NEW) {
|
||||
Assert::same(
|
||||
$payment->getState(),
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@
|
|||
<service id="sylius_api.changer.payment_method" class="Sylius\Bundle\ApiBundle\Changer\PaymentMethodChanger">
|
||||
<argument type="service" id="sylius.repository.payment" />
|
||||
<argument type="service" id="sylius.repository.payment_method" />
|
||||
<argument type="service" id="sylius.resolver.payment_methods" />
|
||||
</service>
|
||||
<service id="Sylius\Bundle\ApiBundle\Changer\PaymentMethodChangerInterface" alias="sylius_api.changer.payment_method" />
|
||||
|
||||
|
|
|
|||
|
|
@ -21,5 +21,10 @@
|
|||
<value>sylius</value>
|
||||
</option>
|
||||
</constraint>
|
||||
<constraint name="Sylius\Bundle\ApiBundle\Validator\Constraints\ChosenPaymentMethodEligibility">
|
||||
<option name="groups">
|
||||
<value>sylius</value>
|
||||
</option>
|
||||
</constraint>
|
||||
</class>
|
||||
</constraint-mapping>
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Sylius\Bundle\ApiBundle\Validator\Constraints;
|
||||
|
||||
use Sylius\Bundle\ApiBundle\Command\Account\ChangePaymentMethod;
|
||||
use Sylius\Bundle\ApiBundle\Command\Checkout\ChoosePaymentMethod;
|
||||
use Sylius\Component\Core\Model\PaymentInterface;
|
||||
use Sylius\Component\Core\Model\PaymentMethodInterface;
|
||||
|
|
@ -38,7 +39,7 @@ final class ChosenPaymentMethodEligibilityValidator extends ConstraintValidator
|
|||
|
||||
public function validate(mixed $value, Constraint $constraint): void
|
||||
{
|
||||
Assert::isInstanceOf($value, ChoosePaymentMethod::class);
|
||||
Assert::isInstanceOfAny($value, [ChoosePaymentMethod::class, ChangePaymentMethod::class]);
|
||||
|
||||
/** @var ChosenPaymentMethodEligibility $constraint */
|
||||
Assert::isInstanceOf($constraint, ChosenPaymentMethodEligibility::class);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue