php - 当我用 spatie 包处理 stripewebbook 时没有任何反应

标签 php laravel routes listener

我使用 spatie/laravel-stripe-webhook 来处理从 stripe 发送的 webhook。我使用 stripe CLI 通过以下命令进行测试:

stripe listen --events checkout.session.completed --forward-to localhost:8000/api/webhook/checkout

一切顺利,我的控制台中有这样的响应:

> Ready! Your webhook signing secret is whsec_mSz72pLPb4B******rfvvYajj641iqZ7 (^C to quit)
2020-06-10 14:12:07   --> \checkout.session.completed\ [\evt_1GsSmjLG****Fak7OpFNC8c2\]
2020-06-10 14:12:07  <--  [200] POST http://localhost:8000/api/webhook/checkout [\evt_1GsSmj*****OFak7OpFNC8c2\]

但是...什么都没发生...我的 laravel.log 中没有任何错误...

我按照 github Spatie 中的所有自述文件进行了操作:

路由/api.php :

Route::stripeWebhooks('webhook/checkout');

App/Providers/EventServiceProvider.php :

protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'stripe-webhooks::checkout.session.completed' => [
            App\Listeners\HandleSubscriptions::class
        ]
    ];

我必须禁用:

Parent::boot();

因为如果启用这个,我会抛出异常:

[object] (ReflectionException(code: -1): Class App\\Providers\\App\\Listeners\\HandleSubscriptions does not exist at /var/www/private/bnb-v2/bnb-back/vendor/laravel/framework/src/Illuminate/Container/Container.php:803)

而且我无法处理 stripeWebhook。

应用程序/Listeners/HandleSubscriptions.php :

class HandleSubscriptions implements ShouldQueue
{
    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(WebhookCall $webhookCall)
    {
        // you can access the payload of the webhook call with `$webhookCall->payload`
        $payload = $webhookCall->payload;

    //     // Handle the event
        switch ($payload->type) {
            case 'checkout.session.completed':
                $sessions = $payload->data->object; 
                SubscriptionController::handle_checkout_session($sessions);
                break;
            case 'checkout.session.async_payment_succeeded':
                $paymentSuccess = $payload->data->object; // contains a \Stripe\PaymentMethod
                // Then define and call a method to handle the successful attachment of a PaymentMethod.
                // handlePaymentMethodAttached($paymentMethod);
                break;
            case 'customer.created':
                $customer = $payload->data->object;
            break;
                // ... handle other event types
            default:
                // Unexpected event type
                http_response_code(400);
                exit();
        }

        http_response_code(200);
    }
}

如果有人有想法...

最佳答案

您要求 Stripe 联系本地主机,以便您可以在本地测试您的代码,但因为它不是公共(public) URL,Stripe 无法看到它,也无法向您发送请求。使用“localhost”仅适用于您的机器。

要在本地使用 Stripe webhook 测试您的代码,您可以使用 expose.sh 通过 Stripe 可以使用的公共(public) HTTPS URL 公开您的本地服务器。

安装暴露.sh

对于 Mac 或 Linux,请转至 Expose.sh并将显示的安装代码复制/粘贴到终端中。

对于 Windows,转到 Expose.sh ,下载二进制文件并将其放在您的 PATH 中的某个位置。

将您的本地 API 服务器暴露给网络

启动您的 API 服务器。然后运行 ​​expose <port>其中 port 是您的 API 服务器运行的端口,例如 80 或 8080。

Expose.sh 将生成一个随机的公共(public) expose.sh URL。你会看到类似的输出

https://s3rh.expose.sh is forwarding to localhost:80
http://s3rh.expose.sh is forwarding to localhost:80

然后您可以让 Stripe 使用公共(public) HTTPS URL,该 URL 将转发到您本地运行的服务器。

一旦你有了 URL,你就可以运行 stripe listen --events checkout.session.completed --forward-to <https expose.sh url>替换为输出中的 URL,类似于 https://s3rh.expose.sh .

我写了一个完整的指南here

免责声明:我构建了 expose.sh

关于php - 当我用 spatie 包处理 stripewebbook 时没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62304151/

相关文章:

php - MySQL - 按时间间隔返回登录尝试

php - adldap 身份验证的证书信任问题

laravel - 缺少 [Route : addapplication. edit] 所需的参数

class - 如何为 Stateful Widget 创建 Flutter 路由?

ruby-on-rails - Rails - 使用/自定义 URL : '/dashboard' 指定根路径

azure - 如何查看Azure系统默认路由

php - 如何在 PHP 中将 "decimal string"转换为没有句点的整数

php - 根据查询类型和表将流量路由到特定的 MySQL 连接

php - 3次登录失败关闭浏览器

php - 对包含 JSON 数组的 JSON 字段进行 Laravel 5.4 查询