Receive Webhook Requests

How to receive the webhook requests

Use the default route

You must enable inbox.uses_default_inbox_route (set it to true)

Laravel Inbox Process ships the /inbox/{topic} route for you, so basically you can use this route to register with your 3rd-party service.

To add a custom Request for your specific topic (eg: stripe), first, you need to create a FormRequest and extend the AbstractInboxProcess.

Additionally, if you want to transform a bit of the payload, you can override the AbstractInboxProcess@getInboxPayload method.

class StripeInvoiceCreatedWebhookRequest extends AbstractInboxRequest
{
    public function getInboxExternalId(): string | null
    {
        return 'stripe_invoice_created-' . $this->input('id');
    }
}

From v1.1.1+: you can add authorize and rules method to validate the request.

Then register it to Inbox Process:

// AppServiceProvider.php
use ShipSaasInboxProcess\InboxProcessSetup;

public function boot(): void
{
    //...
    InboxProcessSetup::addRequest('stripe', new StripeInvoiceCreatedWebhookRequest());
}

Lastly, if you wish to have a specific response for your services, you can use addResponse

// AppServiceProvider.php
use ShipSaasInboxProcess\InboxProcessSetup;

// AppServiceProvider@boot
InboxProcessSetup::addResponse('stripe', function (StripeInvoiceCreatedWebhookRequest $request) {
    // return a response object here
    
    return new JsonResponse();
});

Use your own route

Laravel Inbox Process ships the appendInboxMsg($topic, $uniqueId, $payload) function for manual usage.

Simply invoke the function. The msg will be appended to the table and ready to be resolved under background work.

public function webhook(WebhookRequest $request): JsonResponse
{
    appendInboxMsg(
        'currency-cloud',
        $request->getId(),
        $request->all()
    );
    
    return new JsonResponse();
}

Note: if there is any duplicated message, the appendInboxMsg will throw an Error.

If you need to know if duplicated record happens, check out the logic inside the InboxController.php

Last updated