Skip to content

Events

Laravel Payments dispatches 7 events throughout the payment lifecycle. Listen to these events to trigger business logic without coupling to the payment flow.

Event Reference

EventDispatched WhenKey Properties
PaymentCreatedPayment successfully createdpaymentId, gateway, amount, currency
PaymentVerifiedPayment verified from callbackpaymentId, gateway
PaymentFailedPayment creation failedpaymentId, gateway, reason
PaymentCancelledUser cancelled paymentpaymentId, gateway
PaymentRefundRequestedRefund initiatedpaymentId, gateway, amount, currency
PaymentRefundedRefund completedpaymentId, gateway, refundReference
WebhookReceivedWebhook received from gatewaygateway, eventType, gatewayReference, payload

Registering Listeners

Using EventServiceProvider

php
use Frolax\Payment\Events\PaymentCreated;
use Frolax\Payment\Events\PaymentVerified;
use Frolax\Payment\Events\PaymentFailed;
use Frolax\Payment\Events\WebhookReceived;

protected $listen = [
    PaymentCreated::class => [
        LogPaymentCreation::class,
    ],
    PaymentVerified::class => [
        ActivateSubscription::class,
        SendPaymentReceipt::class,
    ],
    PaymentFailed::class => [
        NotifyPaymentFailure::class,
        RetryPaymentIfAppropriate::class,
    ],
    WebhookReceived::class => [
        ProcessWebhookLogic::class,
    ],
];

Using Closures

php
use Frolax\Payment\Events\PaymentVerified;

Event::listen(PaymentVerified::class, function ($event) {
    // Activate user's subscription
    $user = User::findByPayment($event->paymentId);
    $user->activateSubscription();
});

Event Properties

PaymentCreated

php
class PaymentCreated
{
    public string $paymentId;
    public string $gateway;
    public float $amount;
    public string $currency;
    public ?string $gatewayReference;
    public array $metadata;
}

PaymentVerified

php
class PaymentVerified
{
    public string $paymentId;
    public string $gateway;
}

PaymentFailed

php
class PaymentFailed
{
    public string $paymentId;
    public string $gateway;
    public ?string $reason;
}

PaymentCancelled

php
class PaymentCancelled
{
    public string $paymentId;
    public string $gateway;
}

PaymentRefundRequested

php
class PaymentRefundRequested
{
    public string $paymentId;
    public string $gateway;
    public float $amount;
    public string $currency;
}

PaymentRefunded

php
class PaymentRefunded
{
    public string $paymentId;
    public string $gateway;
    public ?string $refundReference;
    public float $amount;
    public string $currency;
}

WebhookReceived

php
class WebhookReceived
{
    public string $gateway;
    public ?string $eventType;
    public ?string $gatewayReference;
    public bool $signatureValid;
    public array $payload;
    public array $headers;
}

Queued Listeners

For long-running operations, make your listeners queueable:

php
class ActivateSubscription implements ShouldQueue
{
    public function handle(PaymentVerified $event): void
    {
        // This runs in a queue worker
    }
}

Released under the MIT License.