Copied!

BeforeUpdatePaymentMethodEvent

BeforeUpdatePaymentMethodEvent.php : 72
Extends BeforeEvent

Event dispatched before a payment method is updated.

Typical use case are to modify the payment method update struct before executing operation or completely override payment method update logic. See the two examples below.

The following first example capitalizes the first character of each word in each names of the payment method to update:

final class PaymentMethodEventSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
       return [
           BeforeUpdatePaymentMethodEvent::class => 'onBeforeUpdatePaymentMethod',
       ];
    }

   public function onBeforeUpdatePaymentMethod(BeforeUpdatePaymentMethodEvent $event): void
   {
        $updateStruct = $event->getUpdateStruct();

        foreach ($updateStruct->getNames() ?? [] as $languageCode => $name) {
            if ($name !== null) {
                // Capitalize payment method name
                $updateStruct->setName($languageCode, ucwords($name));
            }
        }
   }
}

The following second example sets its own payment method, and cancel the default payment method update process:

final class PaymentMethodEventSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
             BeforeUpdatePaymentMethodEvent::class => 'onBeforeUpdatePaymentMethod',
        ];
    }

  public function onBeforeUpdatePaymentMethod(BeforeUpdatePaymentMethodEvent $event): void
  {
     // ...
     $event->setPaymentMethodResult($paymentMethod);
     $event->stopPropagation();
  }
}
Tags
See
PaymentMethodServiceInterface::updatePaymentMethod()

Methods

public__construct()

BeforeUpdatePaymentMethodEvent.php : 80
public __construct(PaymentMethodInterface $paymentMethod, PaymentMethodUpdateStruct $updateStruct[, PaymentMethodInterface|null $paymentMethodResult = null ])

Parameters

Name Type Default value Description
$paymentMethod PaymentMethodInterface - -
$updateStruct PaymentMethodUpdateStruct - -
$paymentMethodResult PaymentMethodInterface|null null -

publicgetPaymentMethod()

BeforeUpdatePaymentMethodEvent.php : 93

Returns the payment method being updated.

public getPaymentMethod() : PaymentMethodInterface

Return values

PaymentMethodInterface

publicgetPaymentMethodResult()

BeforeUpdatePaymentMethodEvent.php : 113

Returns overridden payment method.

public getPaymentMethodResult() : PaymentMethodInterface

Returns overridden PaymentMethodServiceInterface::updatePaymentMethod() result.

Return values

PaymentMethodInterface

Tags
Throws
UnexpectedValueException

If the return value is not set

publicgetUpdateStruct()

BeforeUpdatePaymentMethodEvent.php : 101

Returns the data used to update payment method.

public getUpdateStruct() : PaymentMethodUpdateStruct

Return values

PaymentMethodUpdateStruct

publichasPaymentMethodResult()

BeforeUpdatePaymentMethodEvent.php : 140

Checks if payment method is overridden.

public hasPaymentMethodResult() : bool

Returns whether the PaymentMethodServiceInterface::updatePaymentMethod() result is overridden.

Return values

bool

publicsetPaymentMethodResult()

BeforeUpdatePaymentMethodEvent.php : 129

Overrides payment method.

public setPaymentMethodResult(PaymentMethodInterface|null $paymentMethodResult) : void

Overrides PaymentMethodServiceInterface::updatePaymentMethod() execution result.

Parameters

Name Type Default value Description
$paymentMethodResult PaymentMethodInterface|null - -