将 laravel 中的 stripe webhook 与 redis 队列驱动程序集成

Integrating stripe webhook in laravel with redis queue driver

我已经在 laravel 7 中使用库 https://github.com/spatie/laravel-stripe-webhooks

实现了 stripe webhook

目标是让我的用户订阅在 stripe 中创建的计划,并在 webhook 请求成功后生成收费发票。现在为了实现这一点,我创建了一个 cron 脚本,用于将作业分派给订阅用户。此外,在条带中设置一个 webhook 端点。所有设置都已完成,并在环境变量中进行了配置。将队列连接设置为“同步”时效果很好。但是,当设置队列连接到 Redis 时,它不起作用。

这是我 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,
];

在调度 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++;
        }
    }

使用作业处理 webhook 请求

<?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.
 }
}

作业日志中的输出:

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

在 webhook_calls 中的 table 中: webhook_calls table click to see it

在队列连接设置为同步的情况下,所有这些都运行良好。但是,问题是当我将队列连接设置为“redis”时。

我知道 webhook 调用有效,因为 webhook_calls table 中有数据,但我想无法完成任务。

任何关于 stripe webhook 和 laravel 使用 redis 作为队列驱动程序的想法,请在下面添加您的评论并提前致谢。

我已经解决了这个问题,我不小心在 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