Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for WebhookHandled event #803

Closed
mitulgolakiya opened this issue Oct 20, 2019 · 3 comments
Closed

Support for WebhookHandled event #803

mitulgolakiya opened this issue Oct 20, 2019 · 3 comments

Comments

@mitulgolakiya
Copy link
Contributor

WebhookHandled Event Support

Cashier provides a great in-built way to handle webhook from Stripe. When some modification happens on a remote stripe website with the subscription it handles it and makes changes to subscriptions in the database.

Problem/Use Case

The additional use case that we came to last week was, When any customer is canceling a Subscription, we want to send him an email to know what was the reason he canceled his subscription and get the feedback. It needs to be done from both website as well as if a user is canceling subscription form the Stripe website directly.

There can be lots of other use cases, where if something happens on Stripe, we need to notify the user after his subscription changes have been recorded to databases via Webhook. E.g.

  • Notify user that he is now on a free plan and will not able to access few features
  • Due to lower plan migration, he can download his data from the given link in next 7 days (in case of storage-specific plans)
    etc.

Proposed Solution

As a solution, Cashier can fire an event after successful webhook handling. A user can optionally listen for that event in code and then can read the payload and take whatever action he wanted to take by reading $payload['type'].

How to listen to WebhookHandled Event

As per Laravel documentation here, create a listener to listen to the event.

<?php

namespace App\Listeners;

use Laravel\Cashier\Events\WebhookHandled;

class WebhookHandledListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Laravel\Cashier\Events\WebhookHandled  $event
     * @return void
     */
    public function handle(WebhookHandled $event)
    {
        // Access the payload using $event->payload...
    }
}

I think by this way, we can give a very clean way if someone is optionally want to take some extra actions on webhooks.

@cwilby
Copy link

cwilby commented Oct 20, 2019

A solution you could use in the meantime is to override the handleWebhook method of WebhookController like so:

<?php

namespace App\Http\Controllers;

use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use Illuminate\Http\Request;
use App\Events\WebhookHandled;

class MyWebhookController extends CashierController
{
    public function handleWebhook(Request $request)
    {
        parent::handleWebhook($request);
        event(new WebhookHandled($request));
    }
}

If you want the event to fire before the webhook is handled, move the dispatch above parent::handleWebhook

@mitulgolakiya
Copy link
Contributor Author

@cwilby Thanks. I already used the same solution as of now. Though I haven't fired the event, just put code there send an email via a job.

@driesvints driesvints changed the title [Proposal] Support for WebhookHandled event Support for WebhookHandled event Oct 31, 2019
@driesvints
Copy link
Member

With #810 two new events WebhookReceived and WebhookHandled will be available. Thanks @mitulgolakiya!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants