Search code examples
laravelredislaravel-cashierstripe-payments

Integrating stripe webhook in laravel with redis queue driver


I've been implemented stripe webhook in laravel 7 using library https://github.com/spatie/laravel-stripe-webhooks

The goal is to subscribe my users to plan created in stripe and generate an invoice on charge succeeded webhook request. Now to achieve this I created a cron script that dispatches the job to subscribe users. Also, set up a webhook endpoint in stripe. All setup is done and configuration in environment variables. It works perfectly when set the queue connection to "sync". However, when setting the queue connection to Redis it doesn't work.

Here's the code inside my config/stripe-webhooks.php

<?php

return [

/*
 * Stripe will sign each webhook using a secret. You can find the used secret at the
 * webhook configuration settings: https://dashboard.stripe.com/account/webhooks.
 */
'signing_secret' => env('STRIPE_WEBHOOK_SECRET'),

/*
 * You can define the job that should be run when a certain webhook hits your application
 * here. The key is the name of the Stripe event type with the `.` replaced by a `_`.
 *
 * You can find a list of Stripe webhook types here:
 * https://stripe.com/docs/api#event_types.
 */
'jobs' => [
    'charge_succeeded' => \App\Jobs\StripeWebhooks\ChargeSucceededJob::class,
    // 'source_chargeable' => \App\Jobs\StripeWebhooks\HandleChargeableSource::class,
    // 'charge_failed' => \App\Jobs\StripeWebhooks\HandleFailedCharge::class,
],

/*
 * The classname of the model to be used. The class should equal or extend
 * Spatie\StripeWebhooks\ProcessStripeWebhookJob.
 */
'model' => \Spatie\StripeWebhooks\ProcessStripeWebhookJob::class,
];

And inside the command that dispatches SubscribeCustomerJob:

$subscribed = 0;
    $users = User::role('admin')->where(function ($q) {
            $q->where('stripe_id', '!=', null)->where('verified_employees', '>=', 5);
        })->get();

    foreach ($users as $user) {

        if ( $user->subscribed('default') && now()->format('Y-m-d') >= $user->trial_ends_at->format('Y-m-d')) {
            SubscribeCustomerJob::dispatch($user)->onQueue('api');
            $subscribed++;
        }
    }

Handling webhook requests using jobs

<?php

namespace App\Jobs\StripeWebhooks;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;


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

/** @var \Spatie\WebhookClient\Models\WebhookCall */
public $webhookCall;

public function __construct(WebhookCall $webhookCall)
{
    $this->webhookCall = $webhookCall;
}

public function handle()
{
    // At this point, I store data to Payments table, 
    // generate invoice and send email notification to subscribed user.
 }
}

The output inside the logs of job:

[2021-04-14 07:53:46][Nr84GbvR3kxqnBGRrrWHtkTj34XRYsGv] Processing: App\Jobs\SubscribeCustomerJob
[2021-04-14 07:53:53][Nr84GbvR3kxqnBGRrrWHtkTj34XRYsGv] Processed:  App\Jobs\SubscribeCustomerJob

Inside table in webhook_calls: webhook_calls table click to see it

All thigs works well with queue connection set to sync. However, the problem is when I set queue connection to "redis".

I know webhook call works because there's data inside webhook_calls table but I guess fails to reach the job.

Any idea with stripe webhook and laravel using redis as queue driver, please add your comment below and thanks in advance.


Solution

  • I've fixed this issue it's my bad that I accidentally put an invalid queue name in my queue worker setup in supervisor.

    [program:queue-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=php /var/www/html/artisan queue:work --queue=api,notification
    autostart=true
    autorestart=true
    user=root
    numprocs=8
    redirect_stderr=true
    stdout_logfile=/var/www/html/worker.log