nestednet/gocardless-laravel

GoCardless Pro PHP 客户端包集成于 Laravel。

v0.2.0 2019-03-04 16:20 UTC

This package is auto-updated.

Last update: 2024-09-12 03:41:07 UTC


README

Build Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version License

GoCardless Pro PHP 客户端包集成于 Laravel。

此包旨在提供一种简单、可扩展且易于维护的方式,将 Gocardless 集成到您的 Laravel 项目中。

它提供了一个包装 Gocardless PHP 客户端的 Facade,并提供了处理 Gocardless 发送的 webhooks 的简便方法。这是按照 Spatie 使用的步骤来实现的,用于他们的 Stripe Laravel Webhooks 包

安装

使用 composer 获取包

composer require nestednet/gocardless-laravel

1. 如果您使用 Laravel >5.5,则该包将自动发现,对于旧版本,请在 config/app.php 文件中添加服务提供者。

2. 发布配置文件

$ php artisan vendor:publish --provider="Nestednet\Gocardless\GocardlessServiceProvider"

这将发布配置文件和迁移文件。

3. 审查配置文件

config/gocardless.php

并将您的 Gocardless API 令牌和环境添加到 .env 文件中。

4. 发布迁移后,您可以运行迁移并创建 gocardless_webhooks_table

5. 该包提供了一个宏路由(gocardlessWebhooks)。您可以在应用的 routes 文件中创建一个路由。此路由将是 Gocardless 发送 webhooks 的端点,您应该在 Gocardless 控制台中注册此 webhook 端点。

Route::gocardlessWebhooks('gocardless-webhook-endpoint');

这将注册一个到该包提供的控制器的 POST 路由。您应该将此路由添加到 VerifyCsrfToken 中间件的 except 数组中。

protected $except = [
    'gocardless-webhook-endpoint',
];
用法

安装包后,您可以使用 Gocardless Facade 访问 Gocardless PHP 客户端的方法。这些方法的文档可以在这里找到: Gocardless PHP 客户端文档

如果您在项目中使用 Gocardless,您很可能会使用 webhooks 来处理异步支付状态。此包提供了一种处理 webhooks 的简便方法。

Gocardless 会发送包含事件的 webhooks。这些事件将包含您的 Gocardless 资源更新。

此包将验证请求的签名,如果签名有效。除非出现严重错误,即使 webhook 中的某个事件失败,控制器也会向 Gocardless 返回 200 响应。这防止了 Gocardless 对端点进行垃圾邮件式的重试。

如果事件处理失败,异常将被保存到数据库中的 gocardless_webhook_calls 表中,您可以在那里找到失败的事件。

此包提供了两种处理 webhook 请求的方法

  • 使用作业
  • 使用事件
使用作业

您可以在 config\gocardless.php 中找到作业数组。

您可以将任何作业注册到 Gocardless 事件中。来自 Gocardless 的事件引用一个资源 resource_type 和一个 action。为了将作业注册到某个操作,您应该使用键 {resource_type}_{action} 添加它。

'jobs' => [
 // '{resource_type}_{action} => path/to/job::class,
    'payments_created' => App\Jobs\PaymentConfirmed::class,
]

为了避免超时,强烈建议使用队列作业。

使用事件

每当事件被包处理时,它将触发一个具有此结构的结构化事件

gocardless-webhooks::{resource_type}_{action}

事件的负载将是使用请求创建的 GocardlessWebhookCall(或扩展模型)实例。

您可以在 EventServiceProvider 中注册对此事件的监听器。

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