gpt4 book ai didi

php - Laravel 收银员 Webhook : Get current user

转载 作者:可可西里 更新时间:2023-11-01 12:32:37 25 4
gpt4 key购买 nike

我正在使用以下内容来覆盖默认的 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);
}

}

为了让它真正覆盖默认值,我应该更新我的路由到我的扩展 Controller ,还是仍然引用默认值?我认为将其保留为默认值甚至不会让 Laravel 知道我的 Controller 存在,但我也想确认一下。

当前:

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

另一部分在 handleCustomerSubscriptionDeleted 中,我还希望能够获取正在引用的当前用户并执行其他操作;在这种情况下,自动将记录设置为未发布。检索用户的最佳方式是什么?看起来 $payload['data']['object']['customer'] 可能持有并关联到用户表中的 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');

但它仍然没有开火。但是,我还想知道 handleCustomerSubscriptionDeleted 是否被称为“当他们取消时”,或者在他们的宽限期结束并且实际订阅被取消之后。有没有比在本地玩等待游戏更可靠的测试方法?

更新 #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 以搜索用户,因为这就是响应返回的内容——而不是我曾经认为的用户 ID。正如@Shaz 提到的那样,我最终确实尝试了 localtunnel.me,它确实允许我向它发送请求,但无法传递我想要影响的客户 ID,我不确定如何验证一切是否正确实际上工作。我可能会尝试深入了解是否可以通过 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,因为我在 Stripe 响应日志中看到 500 个错误,即使我的“Test 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...

最佳答案

阅读 this question 的答案- 事件在订阅实际结束的日期/时间触发,而不是在客户发起取消时触发。

基于 Laravels Cashier Docs - 你的路线实际上应该是:

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

确保您在 stripe 中的 webhook 设置实际指向 yourapp.com/stripe/webhook

基于Laravel\Cashier中的handleWebhook方法代码 - ,任何适当命名的方法(以 handle 开头,驼峰命名为 strip 事件)将覆盖现有方法。


如果你想在路由中使用你的扩展 Controller ;记得在类的开头添加构造函数:

public function __construct(){
parent::__construct();
}

所以 handleWebhook 方法在您这样做时可用

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

而不是在路由中调用 Laravel\Cashier\WebhookController

关于php - Laravel 收银员 Webhook : Get current user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com