Events in Spike

There are multiple events fired when something happens in Spike. You can listen to these events to perform any custom logic you might need.

Listening to events in Laravel

The App\Providers\EventServiceProvider included with your Laravel application provides a convenient place to register all of your application's event listeners. The listen property contains an array of all events (keys) and their listeners (values). You may add as many events to this array as your application requires. For example, let's add an SubscriptionActivated event:

use Opcodes\Spike\Events\SubscriptionActivated;
use App\Listeners\SendSubscriptionActiveNotification;

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    SubscriptionActivated::class => [
        SendSubscriptionActiveNotification::class,
    ],
];

You may also register class or closure based event listeners manually in the boot method of your EventServiceProvider:

use Opcodes\Spike\Events\SubscriptionActivated;
use App\Listeners\SendSubscriptionActiveNotification;
use Illuminate\Support\Facades\Event;

/**
 * Register any other events for your application.
 *
 * @return void
 */
public function boot()
{
    Event::listen(
        SubscriptionActivated::class,
        [SendSubscriptionActiveNotification::class, 'handle']
    );
 
    Event::listen(function (SubscriptionActivated $event) {
        //
    });
}

Event reference

Below is a list of events and their short descriptions.

SubscriptionActivated

The event is fired whenever a specific subscription plan is activated. This includes new subscriptions, as well as switches to different plans.

Opcodes\Spike\Events\SubscriptionActivated
public SpikeBillable $billable;
public SubscriptionPlan $plan;

SubscriptionDeactivated

The event is fired whenever a specific subscription plan is no longer active. This includes switches to different plans, as well as plan cancellations (after the "grace period" is over).

Opcodes\Spike\Events\SubscriptionDeactivated
public SpikeBillable $billable;
public SubscriptionPlan $plan;

SubscriptionCancelled

The event is fired whenever a subscription is cancelled, but has a "grace period" remaining. The user still has access to the benefits of the subscription, but this event lets you know the user's intention to cancel the subscription.

Opcodes\Spike\Events\SubscriptionCancelled
public SpikeBillable $billable;
public SubscriptionPlan $plan;

SubscriptionResumed

The event is fired whenever a subscription is resumed after previously being cancelled. This will only happen if the user was still on the "grace period" after cancellation and decided to keep their subscription running.

If the user's subscription had previously been deactivated, Spike will not use this event and instead fire the SubscriptionActivated event.

Opcodes\Spike\Events\SubscriptionResumed
public SpikeBillable $billable;
public SubscriptionPlan $plan;

ProductPurchased

The event is fired for each of the unique products purchased. It includes the product definition, as well as the quantity of that product.

If the user, for example, purchases four of ProductA and one of ProductB, then there will be 2 events fired - one for ProductA, and another for ProductB.

Opcodes\Spike\Events\ProductPurchased
public SpikeBillable $billable;
public Product $product;
public int $quantity;

CreditBalanceUpdated

The event is fired whenever credits are added, removed, or spent. It includes the new balance that you can use to be informed when, for example, the credit balance is running low, or to activate/disable certain features in your app.

If you need to get the billable related to this event, you can get it with $event->relatedCreditTransaction->billable.

Opcodes\Spike\Events\CreditBalanceUpdated
public SpikeBillable $billable;
public int $balance;
public CreditTransaction $relatedCreditTransaction;

Support

If you have any questions, feedback, or need any help setting up Spike within your project, feel free to reach out to me.