pascalebeier/laravel-stripe-webhooks

在 Laravel 应用中处理 Stripe Webhooks

v3.4.0 2022-08-28 11:36 UTC

README

Latest Version on Packagist GitHub Workflow Status Total Downloads

Fork自 spatie/laravel-stripe-webhooks

Stripe 可以通过 Webhooks 通知您的应用事件。此包可以帮助您处理这些 Webhooks。默认情况下,它将验证所有传入请求的 Stripe 签名。所有有效的调用都将记录到数据库中。您可以轻松定义在特定事件发生时应该分发的作业或事件。

此包不会处理在验证 Webhook 请求并调用正确的作业或事件之后应该执行的操作。您仍然需要自己编写任何工作(例如关于支付的工作)的代码。

在使用此包之前,我们强烈建议您阅读Stripe 上的 Webhooks 完整文档

此包是 spatie/laravel-stripe-webhooks 的分支。有关所有更改,请参阅 变更日志

安装

您可以通过 composer 安装此包

composer require pascalebeier/laravel-stripe-webhooks

服务提供程序将自动注册自己。

您必须使用以下命令发布配置文件

php artisan vendor:publish --provider="PascaleBeier\StripeWebhooks\StripeWebhooksServiceProvider"

这是将在 config/stripe-webhooks.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 a default job that should be run for all other Stripe event type
     * without a job defined in next configuration.
     * You may leave it empty to store the job in database but without processing it.
     */
    'default_job' => '',

    /*
     * 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' => [
        // '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\WebhookClient\Models\WebhookCall.
     */
    'model' => \Spatie\WebhookClient\Models\WebhookCall::class,

    /**
     * This class determines if the webhook call should be stored and processed.
     */
    'profile' => \PascaleBeier\StripeWebhooks\StripeWebhookProfile::class,

    /*
     * When disabled, the package will not verify if the signature is valid.
     * This can be handy in local environments.
     */
    'verify_signature' => env('STRIPE_SIGNATURE_VERIFY', true),
];

在配置文件的 signing_secret 键中,您应该添加一个有效的 Webhook 密钥。您可以在 Stripe 控制台中的 Webhook 配置设置中找到该密钥。

接下来,您必须使用以下命令发布迁移

php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="webhook-client-migrations"

迁移发布后,您可以通过运行迁移来创建 webhook_calls

php artisan migrate

最后,处理路由:在 Stripe 控制台 中,您必须配置 Stripe Webhooks 应击中您的应用的 URL。在您的应用的 routes 文件中,您必须将该路由传递给 Route::stripeWebhooks

Route::stripeWebhooks('webhook-route-configured-at-the-stripe-dashboard');

在幕后,这将注册一个由本包提供的控制器提供的 POST 路由。因为 Stripe 没有获取 csrf-token 的方法,所以您必须将此路由添加到 VerifyCsrfToken 中间件的 except 数组中

protected $except = [
    'webhook-route-configured-at-the-stripe-dashboard',
];

使用方法

Stripe 会发送多种事件类型的 Webhooks。您可以在 Stripe 文档中找到事件类型完整列表

Stripe 将对击中您的应用 Webhook URL 的所有请求进行签名。此包将自动验证签名是否有效。如果签名无效,则请求可能不是由 Stripe 发送的。

除非发生严重错误,否则此包将始终以 200 的状态响应 Webhook 请求。发送 200 将防止 Stripe 反复重发相同的事件。Stripe 可能会偶尔发送重复的 Webhook 请求多次。此包确保每个请求只处理一次。所有带有有效签名的 Webhook 请求都将记录在 webhook_calls 表中。该表有一个 payload 列,其中保存了整个传入 Webhook 的有效负载。

如果签名无效,请求将不会记录在webhook_calls表中,而是会抛出PascaleBeier\StripeWebhooks\WebhookFailed异常。如果在webhook请求过程中出现问题,抛出的异常将被保存在exception列中。在这种情况下,控制器将发送500而不是200

此包提供了两种处理webhook请求的方式:您可以选择排队一个任务或监听该包将触发的事件。

使用任务处理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:
use App\Actions\UpdatePaymentIntentAction;

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

    public function __construct(private readonly WebhookCall $webhookCall, private readonly UpdatePaymentIntentAction $updatePaymentIntentAction)

    public function handle()
    {
        // do your work here
        
        $this->updatePaymentIntentAction->update($this->webhookCall->payload);

        // you can access the payload of the webhook call with `$this->webhookCall->payload`
    }
}

我们强烈建议您使此任务可排队,因为这将最小化webhook请求的响应时间。这允许您处理更多的Stripe webhook请求并避免超时。

创建您的任务后,必须在stripe-webhooks.php配置文件中的jobs数组中注册它。键应该是Stripe事件类型的名称,但用.替换为_。值应该是完全限定的类名。

// config/stripe-webhooks.php

'jobs' => [
    'source_chargeable' => \App\Jobs\StripeWebhooks\HandleChargeableSource::class,
],

如果您想将一个任务配置为默认处理所有未定义的事件,您可以在stripe-webhooks.php配置文件中将任务设置为default_job。值应该是完全限定的类名。

默认情况下,配置是一个空字符串'',这将只在数据库中存储事件,但不进行处理。

// config/stripe-webhooks.php
'default_job' => \App\Jobs\StripeWebhooks\HandleOtherEvent::class,

使用事件处理webhook请求

您可以选择监听此包将触发的事件,而不是排队任务以在webhook请求到来时执行某些操作。每当有效的请求击中您的应用程序时,该包将触发一个stripe-webhooks::<事件名称>事件。

事件的负载将是为传入请求创建的WebhookCall实例。

让我们看看您如何监听此类事件。在EventServiceProvider中,您可以注册监听器。

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'stripe-webhooks::source.chargeable' => [
        App\Listeners\ChargeSource::class,
    ],
];

以下是一个此类监听器的示例

<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class ChargeSource implements ShouldQueue
{
    public function handle(WebhookCall $webhookCall)
    {
        // do your work here

        // you can access the payload of the webhook call with `$webhookCall->payload`
    }
}

我们强烈建议您使事件监听器可排队,因为这将最小化webhook请求的响应时间。这允许您处理更多的Stripe webhook请求并避免超时。

上面的示例只是Laravel中处理事件的一种方式。要了解其他选项,请参阅Laravel处理事件的文档

高级用法

重试处理webhook

所有传入的webhook请求都被写入数据库。当处理webhook调用出现问题时,这非常有价值。您可以在调查并修复失败原因后,轻松地重试处理webhook调用,如下所示

use Spatie\WebhookClient\Models\WebhookCall;
use PascaleBeier\StripeWebhooks\ProcessStripeWebhookJob;

dispatch(new ProcessStripeWebhookJob(WebhookCall::find($id)));

执行自定义逻辑

您可以通过指定自己的模型来添加一些在排队的任务调度之前和/或之后应该执行的自定义逻辑。您可以通过在stripe-webhooks配置文件的model键中指定自己的模型来实现这一点。该类应扩展PascaleBeier\StripeWebhooks\ProcessStripeWebhookJob

以下是一个示例

use PascaleBeier\StripeWebhooks\ProcessStripeWebhookJob;

class MyCustomStripeWebhookJob extends ProcessStripeWebhookJob
{
    public function handle()
    {
        // do some custom stuff beforehand

        parent::handle();

        // do some custom stuff afterwards
    }
}

确定是否应处理请求

您可以使用自己的逻辑来确定是否应处理请求。您可以通过在stripe-webhooks配置文件的profile键中指定自己的配置来实现这一点。该类应实现Spatie\WebhookClient\WebhookProfile\WebhookProfile

在此示例中,我们将确保仅在请求之前未处理的情况下才处理请求。

use Illuminate\Http\Request;
use Spatie\WebhookClient\Models\WebhookCall;
use Spatie\WebhookClient\WebhookProfile\WebhookProfile;

class StripeWebhookProfile implements WebhookProfile
{
    public function shouldProcess(Request $request): bool
    {
        return ! WebhookCall::where('payload->id', $request->get('id'))->exists();
    }
}

处理多个签名密钥

当使用 Stripe Connect 时,您可能需要配置处理多个端点和密钥的软件包。以下是配置该行为的步骤。

如果您正在使用 Route::stripeWebhooks 宏,您可以如下添加 configKey

Route::stripeWebhooks('webhook-url/{configKey}');

或者,如果您正在手动定义路由,您可以如此添加 configKey

Route::post('webhook-url/{configKey}', '\PascaleBeier\StripeWebhooks\StripeWebhooksController');

如果存在此路由参数,验证中间件将使用不同的配置键来查找密钥,通过将给定的参数值附加到默认配置键。例如,如果 Stripe 将消息发送到 webhook-url/my-named-secret,则添加一个新的配置名为 signing_secret_my-named-secret

Connect 的示例配置可能如下所示:

// secret for when Stripe posts to webhook-url/account
'signing_secret_account' => 'whsec_abc',
// secret for when Stripe posts to webhook-url/connect
'signing_secret_connect' => 'whsec_123',

将 Webhook 有效负载转换为 Stripe 对象

您可以将 Webhook 有效负载转换为 Stripe 对象,以帮助访问其各种方法和属性。

为此,请使用 Stripe\Event::constructFrom($payload) 方法,并提供 WebhookCall 的有效负载。

use Stripe\Event;

// ...

public function handle(WebhookCall $webhookCall)
{
    /** @var \Stripe\StripeObject|null */
    $stripeObject = Event::constructFrom($webhookCall->payload)->data?->object;
}

例如,如果您为 invoice.created 事件设置了 Stripe Webhook,则可以将有效负载转换为 StripeInvoice 对象。

/** @var \Stripe\StripeInvoice|null */
$stripeInvoice = Event::constructFrom($webhookCall->payload)->data?->object;

// $stripeInvoice->status
// $stripeInvoice->amount_due
// $stripeInvoice->amount_paid
// $stripeInvoice->amount_remaining

foreach ($stripeInvoice->lines as $invoiceLine) {
    // ...
}

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

测试

composer test

安全性

如果您发现了关于安全性的错误,请发送邮件到 mail@pascalebeier.de 而不是使用问题跟踪器。

鸣谢

特别感谢 Sebastiaan Luca,他慷慨地分享了其 Stripe Webhook 解决方案,该方案启发了此软件包。

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件