优雅/laravel-stripe

为您的 Laravel 应用程序提供 Stripe 和 Stripe Connect

v2.1.1 2024-09-14 14:48 UTC

This package is auto-updated.

Last update: 2024-09-14 14:52:07 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

在 Laravel 中简单地将 Stripe 客户和账户附加到您的模型。

  • 开箱即用的 Stripe webhooks
  • 轻松访问 Stripe php sdk

安装

您可以通过 composer 安装此包

composer require elegantly/laravel-stripe

您应该使用以下命令发布和运行迁移

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

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

php artisan vendor:publish --tag="stripe-config"

这是已发布配置文件的内容

use Elegantly\Stripe\Commands\CreateStripeWebhooksCommand;
use Elegantly\Stripe\ModelRepository;

return [

    'models' => [
        'accounts' => [
            \App\Models\User::class,
        ],
        'customers' => [
            \App\Models\User::class,
        ],
        'repository' => ModelRepository::class,
    ],

    'cache' => [
        'accounts' => true,
        'customers' => false,
    ],

    'key' => env('STRIPE_KEY'),

    'secret' => env('STRIPE_SECRET'),

    'version' => env('STRIPE_VERSION', '2024-04-10'),

    /**
     * This is only used for the CreateStripeWebhooksCommand
     * You can add more webhooks directly from your Stripe Dashboard
     */
    'webhooks' => [
        [
            'url' => '/webhooks/stripe',
            'connect' => false,
            'enabled_events' => [
                ...CreateStripeWebhooksCommand::DEFAULT_WEBHOOKS_EVENTS,
            ],
        ],
    ],

];

示例

创建和检索 Stripe 账户

$user->createStripeAccount();
$user->getStripeAccount();

创建和检索 Stripe 客户

$user->createStripeCustomer();
$user->getStripeCustomer();

准备您的模型

设置您的数据库

此包简单地依赖于您需要添加到具有 Stripe 客户或账户的任何模型中的列。为此,我们提供了一个迁移,它将自动将所需的列添加到您的模型中。要配置与 Stripe 相关的模型,您必须编辑配置。

添加正确的特性

HasStripeCustomer 特性添加到您的模型

class Organization extends Model
{
    use HasStripeCustomer;
    // ...
}

配置 Webhooks

此包提供了命令 stripe:create-webhooks,它将为您在 Stripe 仪表板上创建和配置 webhooks。您需要做的就是编辑配置文件中的 webhooks 和您想要启用的端点。

编辑您的配置

例如,您可以配置两个具有不同路由和端点的不同 webhooks,如下所示

return [

    // configs ...

    /**
     * This is only used for the CreateStripeWebhooksCommand
     * You can add more webhooks directly from your Stripe Dashboard
     */
    'webhooks' => [
        [
            'url' => '/stripe/webhook/account',
            'connect' => false,
            'enabled_events' => [
                ...CreateStripeWebhooksCommand::DEFAULT_WEBHOOKS_EVENTS,

                'checkout.session.expired',
                'checkout.session.completed',
                'checkout.session.async_payment_succeeded',
                'checkout.session.async_payment_failed',
            ],
        ],
        [
            'url' => '/stripe/webhook/connect',
            'connect' => true,
            'enabled_events' => [
                ...CreateStripeWebhooksCommand::DEFAULT_WEBHOOKS_EVENTS,
            ],
        ],
    ],

];

运行命令

现在您对配置满意了,只需运行

php artisan stripe:create-webhooks

从 Stripe 激活 webhooks

默认情况下,此命令配置的所有 webhooks 都被禁用,以防止意外行为。当您准备好时,只需从您的 Stripe 仪表板激活它们。

在您的应用程序中监听 Stripe 事件

现在 Stripe 实际上向您的应用程序发送 webhooks,您可以从 EventServiceProvider 中监听它们。

此包依赖于优秀的 spatie/laravel-stripe-webhooks 包。

您必须 遵循他们的文档 来设置您的监听器。

测试

composer test

变更日志

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

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请查看 我们的安全策略 了解如何报告安全漏洞。

致谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。