Copied!

BeforeCreatePaymentEvent

BeforeCreatePaymentEvent.php : 58
Extends BeforeEvent

Event that is dispatched before a payment is created.

Typical use case is to attach additional context data to payment.

The following example adds channel context to the payment create struct:

namespace App\EventSubscriber;

use Ibexa\Contracts\Core\Collection\ArrayMap;
use Ibexa\Contracts\Payment\Payment\Event\BeforeCreatePaymentEvent;
use Ibexa\Core\MVC\Symfony\SiteAccess\SiteAccessServiceInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class PaymentEventSubscriber implements EventSubscriberInterface
{
    public function __construct(private SiteAccessServiceInterface $siteAccessService)
    {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            BeforeCreatePaymentEvent::class => 'onBeforeCreatePayment',
        ];
    }

    public function onBeforeCreatePayment(BeforeCreatePaymentEvent $event): void
    {
        $context = [
            'channel' => $this->siteAccessService->getCurrent()?->name,
        ];

        $createStruct = $event->getCreateStruct();
        $createStruct->setContext(new ArrayMap($context));
    }
}
Tags
See
PaymentServiceInterface::createPayment()

Methods

public__construct()

BeforeCreatePaymentEvent.php : 64
public __construct(PaymentCreateStruct $createStruct[, PaymentInterface|null $paymentResult = null ])

Parameters

Name Type Default value Description
$createStruct PaymentCreateStruct - -
$paymentResult PaymentInterface|null null -

publicgetCreateStruct()

BeforeCreatePaymentEvent.php : 74

Returns the original payment create struct passed to {@see \Ibexa\Contracts\Payment\PaymentServiceInterface::createPayment()} method.

public getCreateStruct() : PaymentCreateStruct

Return values

PaymentCreateStruct

publicgetPaymentResult()

BeforeCreatePaymentEvent.php : 84

Returns overridden {@see \Ibexa\Contracts\Payment\PaymentServiceInterface::createPayment()} result.

public getPaymentResult() : PaymentInterface

Return values

PaymentInterface

Tags
Throws
UnexpectedValueException

if the payment result is not set

publichasPaymentResult()

BeforeCreatePaymentEvent.php : 108

Returns whether the {@see \Ibexa\Contracts\Payment\PaymentServiceInterface::createPayment()} result has been overridden.

public hasPaymentResult() : bool

Return values

bool

publicsetPaymentResult()

BeforeCreatePaymentEvent.php : 99

Sets the payment result that will be returned by {@see \Ibexa\Contracts\Payment\PaymentServiceInterface::createPayment()}.

public setPaymentResult(PaymentInterface|null $paymentResult) : void

Parameters

Name Type Default value Description
$paymentResult PaymentInterface|null - -