Stripe Cashier Webhooks

时间:2015-02-17 21:24:36

标签: php stripe-payments laravel-5 webhooks ngrok

我正在寻找澄清Laravel文档中描述的Stripe Cashier中Webhooks控制器的使用,因为我无法确认我的应用程序是否正在接收webhook事件:

http://laravel.com/docs/5.0/billing#handling-failed-payments

文档建议将路由指向webhook控制器,如下所示:

Route::post('stripe/webhook', 'Laravel\Cashier\WebhookController@handleWebhook');

必须将路由中的URI修改为Stripe设置中的URI。在测试环境中,我使用ngrok来公开我的本地服务器。

我正在寻求澄清的是URI应该用于测试和生产。为了测试,我应该只使用ngrok转发URL(例如http://3a4bfceb.ngrok.com),还是需要在公共目录中使用脚本来处理来自Stripe的webhook事件。

我不确定控制器是否能够处理使用handlePayload函数接收数据,或者我是否需要添加额外的PHP脚本(例如。webhook.php)在Stripe文档中描述如:

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account
Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxx");

// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);

// Do something with $event_json

http_response_code(200); // PHP 5.4 or greater

如果有人可以提供测试和生产URI的帮助,以及是否需要额外的处理脚本超出Cashier的WebhookController.php提供的内容,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

ngrok当然会工作,但这种手动测试并不是你应该如何测试的;)

您可以在此处阅读有关在本地测试条带webhook的更多信息:enter link description here

它使用专门设计的软件包来启用自动webhook测试,而无需通过ngrok或其他任何东西暴露您的本地环境。

(完全披露:我的合作伙伴和我写了博客文章和提到的包裹)

答案 1 :(得分:0)

关于URI,本地/测试和生产URI类似(假设使用了ngrok):

本地/测试:http://3a4bfceb.ngrok.com/laravel/public/stripewebhooks

制作:http://website.com/stripewebhooks

两种情况下的路线都是:

Route::post('stripewebhooks','Laravel\Cashier\WebhookController@handleWebhook');

WebhookController.php(这是Cashier包的一部分)处理所有传入事件,因此无需创建包含stripewebhooks.php 200响应代码的file_get_contents文件在条带文档中描述了没有收银员的实现。