PaymentMethodBlockedError example

return $paymentMethodResponse;
    }

    /** * @param array<string> $blockedPaymentMethodNames */
    private function getErrorCollection(array $blockedPaymentMethodNames = []): ErrorCollection
    {
        $errorCollection = new ErrorCollection();

        foreach ($blockedPaymentMethodNames as $name) {
            $errorCollection->add(new PaymentMethodBlockedError($name, 'Payment method blocked'));
        }

        return $errorCollection;
    }

    private function getSalesChannelContext(bool $dontReturnDefaultPaymentMethod = false): SalesChannelContext
    {
        $salesChannel = new SalesChannelEntity();
        $salesChannel->setId(TestDefaults::SALES_CHANNEL);
        $salesChannel->setLanguageId(Defaults::LANGUAGE_SYSTEM);
        if ($dontReturnDefaultPaymentMethod) {
            

        foreach ($cartErrors as $error) {
            if (!$error instanceof ShippingMethodChangedError && !$error instanceof PaymentMethodChangedError) {
                continue;
            }

            if ($error instanceof ShippingMethodChangedError) {
                $cartErrors->add(new ShippingMethodBlockedError($error->getOldShippingMethodName()));
            }

            if ($error instanceof PaymentMethodChangedError) {
                $cartErrors->add(new PaymentMethodBlockedError($error->getOldPaymentMethodName()));
            }

            $cartErrors->remove($error->getId());
        }
    }
}
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

#[Package('checkout')] class PaymentMethodValidator implements CartValidatorInterface
{
    public function validate(Cart $cart, ErrorCollection $errors, SalesChannelContext $context): void
    {
        $paymentMethod = $context->getPaymentMethod();
        if (!$paymentMethod->getActive()) {
            $errors->add(
                new PaymentMethodBlockedError((string) $paymentMethod->getTranslation('name'), 'inactive')
            );
        }

        $ruleId = $paymentMethod->getAvailabilityRuleId();

        if ($ruleId && !\in_array($ruleId$context->getRuleIds(), true)) {
            $errors->add(
                new PaymentMethodBlockedError((string) $paymentMethod->getTranslation('name'), 'rule not matching')
            );
        }

        
$cartErrors = new ErrorCollection();
        if ($blockShippingMethod) {
            $cartErrors->add(
                new ShippingMethodBlockedError(
                    'original-shipping-method-name'
                )
            );
        }

        if ($blockPaymentMethod) {
            $cartErrors->add(
                new PaymentMethodBlockedError(
                    'original-payment-method-name',
                    ''
                )
            );
        }

        return $cartErrors;
    }

    /** * @param callable(ErrorCollection, SalesChannelContext): ShippingMethodEntity|null $shippingSwitcherCallbackMethod * @param callable(ErrorCollection, SalesChannelContext): PaymentMethodEntity|null $paymentSwitcherCallbackMethod */
Home | Imprint | This part of the site doesn't use cookies.