conedevelopment/bazar-stripe

Bazar的Stripe支付集成。

v1.1.2 2024-09-26 12:46 UTC

README

安装

composer require conedevelopment/bazar-stripe

配置

.env

STRIPE_TEST_MODE=
STRIPE_API_KEY=
STRIPE_SECRET=

Bazar配置

    // config/bazar.php

    'gateway' => [
        'drivers' => [
            // ...
            'stripe' => [
                'test_mode' => env('STRIPE_TEST_MODE', false),
                'api_key' => env('STRIPE_API_KEY'),
                'secret' => env('STRIPE_SECRET'),
            ],
        ],
    ],

    // ...

Webhook事件

php artisan make:listener StripeWebhookHandler
namespace App\Listeners;

use Cone\Bazar\Stripe\WebhookInvoked;
use Stripe\Event;

class StripeWebhookHandler
{
    public function handle(WebhookInvoked $event): void
    {
        // https://stripe.com/docs/api/events/types
        $callback = match ($event->event->type) {
            'payment_intent.payment_failed' => function (Event $event): void {
                // mark transaction as failed
            },
            'payment_intent.succeeded' => function (Event $event): void {
                // mark transaction as completed and order as paid
            },
            default => function (): void {
                //
            },
        };

        call_user_func_array($callback, [$event->event]);
    }
}

提示

如果事件发现已禁用,请确保监听器已绑定到您的EventServiceProvider中的WebhookInvoked事件。