-
-
Notifications
You must be signed in to change notification settings - Fork 113
Event Listener System
In the v20.0.0
release, an Event Listener system has been introduced. This system leverages Laravel's powerful event listener framework, enabling you to seamlessly monitor internal events within your application. This functionality empowers you to execute supplementary tasks in response to these events, enhancing the flexibility and extensibility of your application.
-
AppInstalledEvent
-
ShopAuthenticatedEvent
-
ShopDeletedEvent
-
AppUninstalledEvent
-
PlanActivatedEvent
All events can now be configured through the config/shopify-app.php
file. Navigate to the specified section to conveniently access and manage the relevant configurations.
/*
|--------------------------------------------------------------------------
| Register listeners to the events
|--------------------------------------------------------------------------
|
*/
'listen' => [
\Osiset\ShopifyApp\Messaging\Events\AppInstalledEvent::class => [
// \App\Listeners\MyListener::class,
],
\Osiset\ShopifyApp\Messaging\Events\ShopAuthenticatedEvent::class => [
// \App\Listeners\MyListener::class,
],
\Osiset\ShopifyApp\Messaging\Events\ShopDeletedEvent::class => [
// \App\Listeners\MyListener::class,
],
\Osiset\ShopifyApp\Messaging\Events\AppUninstalledEvent::class => [
// \App\Listeners\MyListener::class,
],
\Osiset\ShopifyApp\Messaging\Events\PlanActivatedEvent::class => [
// \App\Listeners\MyListener::class,
],
],
Explore a comprehensive list of available events in this section. Initially, no listeners are registered for these events in their default state. However, you now have the capability to craft your own listeners and seamlessly register them with the events of your choice.
In Laravel version 11 and later, event listeners in the App\Listeners
directory are automatically registered by default. Therefore, manual registration in this configuration file is unnecessary. If you register the listeners manually again here, the listener will be called twice.
In the past, handling the AppUninstalled event involved using a job when Shopify sent a webhook request. Now, you have the option to manage it through an Event Listener. Rest assured, both systems are compatible, allowing you the flexibility to choose between them. If you prefer to disable the legacy AppUninstalled system, simply set the provided value to false
.
'app_legacy_supports' => [
'after_authenticate_job' => false,
],
Imagine you wish to dispatch an email to the store owner upon their subscription or purchase of a new plan. Let's achieve this using our Event Listener system:
Firstly, let's create a listener specifically designed for sending an email to the store owner.
<?php
namespace App\Listeners;
use Illuminate\Bus\Queueable;
use App\Mail\PlanActivatedMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Osiset\ShopifyApp\Messaging\Events\PlanActivatedEvent;
class SendPlanActivatedEmailListener implements ShouldQueue
{
use Queueable;
/**
* Create the event listener.
*
* @return void
*/
protected Shop $shop;
public function __construct()
{
$this->onQueue('default');
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(PlanActivatedEvent $event)
{
Mail::to($event->shop->email)
->send(new PlanActivatedMail($event->plan, $event->chargeId));
}
}
Now we have to register this listener with PlanActivatedEvent::class
'listen' => [
\Osiset\ShopifyApp\Messaging\Events\AppInstalledEvent::class => [
// \App\Listeners\MyListener::class,
],
\Osiset\ShopifyApp\Messaging\Events\ShopAuthenticatedEvent::class => [
// \App\Listeners\MyListener::class,
],
\Osiset\ShopifyApp\Messaging\Events\ShopDeletedEvent::class => [
// \App\Listeners\MyListener::class,
],
\Osiset\ShopifyApp\Messaging\Events\AppUninstalledEvent::class => [
// \App\Listeners\MyListener::class,
],
\Osiset\ShopifyApp\Messaging\Events\PlanActivatedEvent::class => [
\App\Listeners\PlanActivatedEvent::class,
],
],
That's all there is to it—how simple was that?
Now, when a store activates a new plan, the store owner will receive an email.
Properties:
public ShopId $shopId
Properties:
public ShopId $shopId
Properties:
public ShopModel $shop
Properties:
public ShopModel $shop
Properties:
public ShopModel $shop
public Plan $plan
public ChargeId $chargeId