我通过在 app/Http/Controllers/WebHookController.php
中放置以下内容来使用以下内容来覆盖默认的 handleCustomerSubscriptionDeleted
方法:
<?php namespace App\Http\Controllers;
use Laravel\Cashier\WebhookController;
use Symfony\Component\HttpFoundation\Response;
class StripeWebhooks extends WebhookController {
/**
* Handle stripe subscription webhook
*
* @param array $payload
* @return Response
*/
public function handleCustomerSubscriptionDeleted($payload)
{
$billable = $this->getBillable($payload['data']['object']['customer']);
if ($billable && $billable->subscribed()) {
$billable->subscription()->cancel();
}
return new Response('Webhook Handled', 200);
}
}
为了实际覆盖默认值,我应该更新到扩展控制器的路由,还是仍然引用默认值?我不认为将它保留为默认值甚至会让Laravel知道我的控制器存在,但也想确认一下 .
当前:
Route::post('stripe/webhook', '\Laravel\Cashier\WebhookController@handleWebhook');
另一部分是 handleCustomerSubscriptionDeleted
我也希望能够 grab 当前正在引用的用户并执行其他操作;在这种情况下,自动将记录设置为未发布 . 检索用户的最佳方法是什么?看起来 $payload['data']['object']['customer']
可能会保留并与users表中的 stripe_id
列相关,但是想要确认 . 谢谢你的帮助!
更新
我相信(基于文档),它应该更像这样:
<?php namespace App\Http\Controllers;
use App\Models\Dashboard\Listing;
use App\User;
use Symfony\Component\HttpFoundation\Response;
use Laravel\Cashier\WebhookController as BaseController;
class WebhookController extends BaseController
{
/**
* Handle stripe subscription webhook
*
* @param array $payload
* @return Response
*/
public function handleCustomerSubscriptionDeleted($payload)
{
$billable = $this->getBillable($payload['data']['object']['customer']);
if ($billable && $billable->subscribed()) {
$billable->subscription()->cancel();
// Get current user
$user = User::find($billable);
// Set each listing to draft
foreach ($user->listings as $listing) {
$current_listing = Listing::find($listing->id);
if ($current_listing->status == 'published') {
$current_listing->status = 'draft';
$current_listing->save();
}
}
}
return new Response('Webhook Handled', 200);
}
}
然后我将路线更新为以下内容:
Route::post('stripe/webhook', 'WebhookController@handleWebhook');
但是's still not firing. BUT, what I' m也想知道 handleCustomerSubscriptionDeleted
是否被称为"when they cancel",或者在宽限期结束后实际订阅被取消 . 对我来说,测试这个比在本地玩等待游戏有更可靠的方法吗?
更新#2
我已将覆盖类更新为以下内容:
<?php namespace App\Http\Controllers;
use App\Models\Dashboard\Listing;
use App\User;
use Symfony\Component\HttpFoundation\Response;
use Laravel\Cashier\WebhookController as BaseController;
class WebhookController extends BaseController
{
/**
* Handle stripe subscription webhook
*
* @param array $payload
* @return Response
*/
public function handleCustomerSubscriptionDeleted(array $payload)
{
$billable = $this->getBillable($payload['data']['object']['customer']);
if ($billable && $billable->subscribed()) {
$billable->subscription()->cancel();
// Get current user
$user = User::where('stripe_id', $billable)->firstOrFail();
// Set each listing to draft
foreach ($user->listings as $listing) {
$current_listing = Listing::find($listing->id);
if ($current_listing->status == 'published') {
$current_listing->status = 'draft';
$current_listing->save();
}
}
}
return new Response('Webhook Handled', 200);
}
}
我改变的部分是改变 $billable
来搜索用户,因为这样的影响,我不知道如何验证一切是否真的有效 . 我可能会试着深入了解是否可以通过Cashier手动运行事件,但仍然有点奇怪 .
更新#3
尝试做一些更简单的事情听到 customer.subscription.created
(因为它会立即触发):
<?php namespace App\Http\Controllers;
use App\Models\Dashboard\Listing;
use App\User;
use Symfony\Component\HttpFoundation\Response;
use Laravel\Cashier\WebhookController as BaseController;
class WebhookController extends BaseController
{
/**
* @param array $payload
*/
public function handleCustomerSubscriptionCreated(array $payload) {
$billable = $this->getBillable($payload['data']['object']['customer']);
if ($billable) {
// Get current user
$user = User::where('stripe_id', $billable)->firstOrFail();
$user->first_name = 'Helloooooo';
$user->save();
}
}
/**
* Handle stripe subscription webhook
*
* @param array $payload
* @return Response
*/
public function handleCustomerSubscriptionDeleted(array $payload)
{
$billable = $this->getBillable($payload['data']['object']['customer']);
if ($billable && $billable->subscribed()) {
$billable->subscription()->cancel();
// Get current user
$user = User::where('stripe_id', $billable)->firstOrFail();
// Set each listing to draft
foreach ($user->listings as $listing) {
$current_listing = Listing::find($listing->id);
if ($current_listing->status == 'published') {
$current_listing->status = 'draft';
$current_listing->save();
}
}
}
return new Response('Webhook Handled', 200);
}
}
我使用localtunnel.me来设置webhook,但它似乎没有正确响应webhook,因为我看到条带响应日志中有500个错误,即使我的“测试Webhooks”事件是从Stripe仪表板触发的(带有显然没有设置客户ID)很好 . 遗憾的是,我对500错误的反应在Laravel正在吐出的混乱的源代码中丢失/切断:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow" />
<style>
/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}
html { backgr...
3 回答
阅读this question的答案 - 事件在订阅实际结束的日期/时间被触发,而不是在客户启动取消时触发 .
基于Laravels Cashier Docs - 您的路线应该是:
确保条带中的webhook设置实际指向
yourapp.com/stripe/webhook
基于Laravel\Cashier代码 yes 中的
handleWebhook
方法,任何适当命名的方法(以handle开头,camelcased到strip事件)将覆盖现有方法 .如果要在路径中使用扩展控制器;记得在类的开头添加一个构造函数:
所以
handleWebhook
方法可用而不是在路线中调用
Laravel\Cashier\WebhookController
.我有这个工作,我指向我的控制器的路线 .
由于我们正在扩展基本控制器,因此基本控制器中的任何方法都已可用 . 该控制器的要点是可以访问所有基本控制器方法 . 这样,您可以扩展任何新方法或覆盖现有方法 .
我在获取代码以捕获webhook事件时遇到了问题 . 需要注意的是,如果您处于条带测试环境并触发webhook事件,请确保添加env变量 .
在基本webhook控制器中,它首先检查以确保事件存在,并且如果在测试环境中生成webhook,则事件不存在 .