【问题标题】:Integrating stripe webhook in laravel with redis queue driver将 laravel 中的条带 webhook 与 redis 队列驱动程序集成
【发布时间】:2021-07-10 01:13:20
【问题描述】:

我已经在 laravel 7 中使用库 https://github.com/spatie/laravel-stripe-webhooks 实现了条带 webhook

目标是让我的用户订阅在条带中创建的计划,并在收费成功的 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 中的内表: webhook_calls table click to see it

在队列连接设置为同步的情况下,所有这些都可以正常工作。但是,问题是当我将队列连接设置为“redis”时。

我知道 webhook 调用有效,因为 webhook_calls 表中有数据,但我猜无法完成工作。

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

【问题讨论】:

    标签: laravel redis laravel-cashier stripe-payments


    【解决方案1】:

    我已经解决了这个问题,我不小心在主管的队列工作程序设置中放入了无效的队列名称。

    [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
    

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多