Zac*_*ach 8 php controllers laravel laravel-5 laravel-cashier
我正在使用以下内容handleCustomerSubscriptionDeleted通过将以下内容置于以下方法来覆盖默认方法app/Http/Controllers/WebHookController.php:
<?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);
}
}
Run Code Online (Sandbox Code Playgroud)
为了实际覆盖默认值,我应该更新到扩展控制器的路由,还是仍然引用默认值?我不认为将它保留为默认值甚至会让Laravel知道我的控制器存在,但也想确认一下.
当前:
Route::post('stripe/webhook', '\Laravel\Cashier\WebhookController@handleWebhook');
Run Code Online (Sandbox Code Playgroud)
另一部分是handleCustomerSubscriptionDeleted我也希望能够抓住当前正在引用的用户并执行其他操作; 在这种情况下,自动将记录设置为未发布.检索用户的最佳方法是什么?它似乎$payload['data']['object']['customer']可能保留并与stripe_idusers表中的列相关,但是想要确认.谢谢你的帮助!
我相信(基于文档),它应该更像这样:
<?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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将路线更新为以下内容:
Route::post('stripe/webhook', 'WebhookController@handleWebhook');
Run Code Online (Sandbox Code Playgroud)
但它仍未解雇.但是,我也想知道的是,handleCustomerSubscriptionDeleted当他们取消时,或者在他们的宽限期结束并且实际订阅被取消之后被称为.对我来说,测试这个比在本地玩等待游戏有更可靠的方法吗?
我已将覆盖类更新为以下内容:
<?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);
}
}
Run Code Online (Sandbox Code Playgroud)
我更改的部分是更改$billable为搜索用户,因为这是响应返回的内容 - 而不是我曾经想过的用户ID.我最终尝试使用localtunnel.me作为@Shaz提到它允许我向它发送请求,但是我没有能够传递客户ID我想要影响,我不知道我怎么能验证一切都是实际工作.我可能会试着深入了解是否可以通过Cashier手动运行事件,但仍然有点奇怪.
尝试做一些听起来更简单的事情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);
}
}
Run Code Online (Sandbox Code Playgroud)
我使用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...
Run Code Online (Sandbox Code Playgroud)
阅读此问题的答案- 事件在订阅实际结束的日期/时间被触发,而不是在客户启动取消时触发.
基于Laravels Cashier Docs - 您的路线应该是:
Route::post('stripe/webhook', 'Laravel\Cashier\WebhookController@handleWebhook');
Run Code Online (Sandbox Code Playgroud)
确保条带中的webhook设置实际指向 yourapp.com/stripe/webhook
基于代码中的handleWebhook方法Laravel\Cashier- 是的,任何适当命名的方法(以handle开头,camelcased到strip事件)将覆盖现有的方法.
如果要在路径中使用扩展控制器; 记得在类的开头添加一个构造函数:
public function __construct(){
parent::__construct();
}
Run Code Online (Sandbox Code Playgroud)
这样handleWebhook你就可以使用这种方法
Route::post('stripe/webhook', 'WebhookController@handleWebhook');
Run Code Online (Sandbox Code Playgroud)
而不是Laravel\Cashier\WebhookController在路线中呼叫.
| 归档时间: |
|
| 查看次数: |
1822 次 |
| 最近记录: |