[API] Enforce channel eligibility check when changing payment method via account endpoint

This commit is contained in:
Grzegorz Sadowski 2026-05-25 12:00:48 +02:00
parent d2b16b81fd
commit 97ea859c21
No known key found for this signature in database
GPG key ID: EF87FF4E6E3BD364
4 changed files with 14 additions and 1 deletions

View file

@ -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(),

View file

@ -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" />

View file

@ -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>

View file

@ -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);