php - 当我用 spatie 包处理 stripewebook 时什么也没有发生
问题描述
我使用 spatie/laravel-stripe-webhook 来处理从 stripe 发送的 webhook。我使用条带 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');
应用程序/提供者/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。
App/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 联系 localhost,以便您可以在本地测试您的代码,但因为它不是公共 URL,Stripe 看不到它,也无法向您发送请求。使用“localhost”只能在您的机器上使用。
要在本地使用 Stripe webhooks 测试您的代码,您可以使用 Expose.sh 使用 Stripe 可以使用的公共 HTTPS URL 公开您的本地服务器。
安装expose.sh
对于 Mac 或 Linux,转到Expose.sh并将显示的安装代码复制/粘贴到终端中。
对于 Windows,请转到Expose.sh,下载二进制文件并将其放在 PATH 中的某个位置。
将您的本地 API 服务器公开给网络
启动您的 API 服务器。然后运行expose <port>
其中的端口是您的 API 服务器正在运行的端口,例如 80 或 8080。
Expose.sh 将生成一个随机的公共 Expose.sh URL。你会看到像这样的输出
https://s3rh.expose.sh is forwarding to localhost:80
http://s3rh.expose.sh is forwarding to localhost:80
然后,您可以让 Stripe 使用公共 HTTPS URL,该 URL 将转发到您本地运行的服务器。
获得 URL 后,您可以stripe listen --events checkout.session.completed --forward-to <https expose.sh url>
使用输出中的 URL 进行替换,这类似于https://s3rh.expose.sh
.
免责声明:我构建了expose.sh