Laraship

⌘K
  1. Home
  2. Docs
  3. Laraship
  4. Customize Laraship
  5. Webhooks Handler

Webhooks Handler

If your application requires webhooks to accept requests from external applications, then we have our Webhook Handler ready for you, which will make webhooks receiving more stable, manageable, and easier to monitor!

Also, the processing can be asynchronous using the Laravel queue, and even if you run into an issue with the webhook processing you can reprocess it.

 

for example, if you need to create a webhook to accept order status changes from a shipping carrier here is what we need to do:

 

  1. let’s assume the event name is Order Update the request should be sent to the following endpoint as POST:

{domain}/utilities/webhooks/{event_name} and in our example  https://websiite.com/utilities/webhooks/update-order

And YourModule namespace is mymodule

 

2. Under YourModule config.php add the webhooks configuration:

'webhook' => [
	'events' => [
		'order_update' => \Corals\Modules\MyModule\Jobs\HandleOrderUpdated::class,
	]
]

 

 

3. Under Your Module Service Provider add the following code “$this->registerWebhooks();” to the boot function, and register your webhooks

public function boot()
{
	....
	$this->registerWebhooks();
	...
}

protected function registerWebhooks()
{
	foreach (config('mymodule.webhook.events', []) as $eventName => $jobClass) {
		Webhooks::registerEvent($eventName, $jobClass);
	}
}

 

4. Create Your Job class that will handle the webhook data and process it under Corals\Modules\MyModule\Jobs\HandleOrderUpdated.php

<?php

namespace Corals\Modules\MyModule\Jobs;

use Corals\Modules\Utility\Models\Webhook\Webhook;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class HandleOrderUpdated implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * @var Webhook
     */
    public $webhook;

    /**
     * HandleLLOrderUpdate constructor.
     * @param Webhook $webhook
     */
    public function __construct(Webhook $webhook)
    {
        $this->webhook = $webhook;
    }

    public function handle()
    {
        logger("======================================================");
        logger("HandleOrderUpdate Job");
        logger("{$this->webhook->event_name}::{$this->webhook->id} Job Started");
        logger("------------------------------------------------------");

        try {
            $webhook = $this->webhook;

            if ($webhook->status == 'processed') {
                $this->webhook->saveException(new \Exception(trans('Utility::exceptions.webhook.already_processed',
                    ['name' => $webhook->event_name, 'id' => $webhook->id])), false);
                return;
            }

            $payload = $webhook->payload;
             ...
             Your Code Logic Goes here
             ...
 
            $webhook->markAs('processed');
        } catch (\Exception $exception) {
            $this->webhook->saveException($exception);
            logger("{$this->webhook->event_name}::{$this->webhook->id} Exception");
            logger('Exception: ' . $exception->getMessage());
        } finally {
            logger("------------------------------------------------------");
            logger("{$this->webhook->event_name}::{$this->webhook->id} Job Ended");
            logger("HandleOrderUpdate Job Ended");
            logger("======================================================");
        }
    }

   
}