Copied!

BeforeCreatePaymentMethodEvent

BeforeCreatePaymentMethodEvent.php : 67
Extends BeforeEvent

Event dispatched before payment method is created.

Typical use cases are to modify the payment method create struct before executing operation, or o completely override payment method creation logic. See the two examples below.

The following first example forces the struct to create an available payment method, then let the default payment method creation treats this struct:

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

   public function onBeforeCreatePaymentMethod(BeforeCreatePaymentMethodEvent $event): void
   {
        $createStruct = $event->getCreateStruct();
        // Enable payment method by default
        $createStruct->setEnabled(true);
   }
}

The following second example sets its own payment method, and cancels the default payment method creation process:

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

  public function onBeforeCreatePaymentMethod(BeforeCreatePaymentMethodEvent $event): void
  {
     // ...
     $event->setPaymentMethodResult($paymentMethod);
     $event->stopPropagation();
  }
}
Tags
See
PaymentMethodServiceInterface::createPaymentMethod()

Methods

public__construct()

BeforeCreatePaymentMethodEvent.php : 73
public __construct(PaymentMethodCreateStruct $createStruct[, PaymentMethodInterface|null $paymentMethodResult = null ])

Parameters

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

publicgetCreateStruct()

BeforeCreatePaymentMethodEvent.php : 87

Returns the original payment method create struct.

public getCreateStruct() : PaymentMethodCreateStruct

Returns the original payment method creation structure passed to PaymentMethodServiceInterface::createPaymentMethod() method.

Return values

PaymentMethodCreateStruct

publicgetPaymentMethodResult()

BeforeCreatePaymentMethodEvent.php : 99

Returns overridden payment method.

public getPaymentMethodResult() : PaymentMethodInterface

Returns overridden PaymentMethodServiceInterface::createPaymentMethod() result.

Return values

PaymentMethodInterface

Tags
Throws
UnexpectedValueException

If the return value is not set

publichasPaymentMethodResult()

BeforeCreatePaymentMethodEvent.php : 126

Checks if payment method is overridden.

public hasPaymentMethodResult() : bool

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

Return values

bool

publicisPropagationStopped()

Event.php : 38
public isPropagationStopped() : bool

Return values

bool

publicsetPaymentMethodResult()

BeforeCreatePaymentMethodEvent.php : 115

Overrides payment method.

public setPaymentMethodResult(PaymentMethodInterface|null $paymentMethodResult) : void

Overrides PaymentMethodServiceInterface::createPaymentMethod() execution result.

Parameters

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

publicstopPropagation()

Event.php : 50

Stops the propagation of the event to further event listeners.

public stopPropagation() : void

If multiple event listeners are connected to the same event, no further event listener will be triggered once any trigger calls stopPropagation().