Laravel Cashier – 在订阅续订时恢复使用限制。

huangapple go评论64阅读模式
英文:

Laravel Cashier - Restore Usage limits when subscription is renewed

问题

我创建了一个带使用限制的SaaS应用程序。

例如,如果用户订阅了“高级会员”,他们每月可以执行10个任务。

我无法找到在订阅周期更新时重置他们的使用限制的解决方案。

在我的用户表中:

id tokens_left
1 5
2 9

我还没有找到任何解决方案。我尝试从Stripe获取订阅信息,但没有任何字段有用。

英文:

I created a Saas application with usage limits.

For example if user subscribe "Premium Membership" they can do 10 task in a month.

I couldn't manage to find a solution to reset their usage limits when subscription period is renewed.

In my users table;

id tokens_left
1 5
2 9

I haven't figure any solutions yet. I tried to retrieve subscription information from the stripe but any fields aren't helpful.

答案1

得分: 1

Stripe可以通过Webhooks通知您的应用程序各种事件。默认情况下,Cashier服务提供程序会自动注册一个指向Cashier的Webhook控制器的路由。此控制器将处理所有传入的Webhook请求。

默认情况下,Cashier Webhook控制器将自动处理取消订阅(根据您的Stripe设置定义的失败收费次数)、客户更新、客户删除、订阅更新和付款方式更改;然而,正如我们将很快发现的那样,您可以扩展此控制器以处理任何您喜欢的Stripe Webhook事件。

如果您有其他要处理的Webhook事件,可以通过监听Cashier分发的以下事件来执行:

  • Laravel\Cashier\Events\WebhookReceived
  • Laravel\Cashier\Events\WebhookHandled

这两个事件都包含完整的Stripe Webhook负载。例如,如果您希望处理customer.subscription.updated Webhook,可以注册一个处理此事件的监听器:

<?php
 
namespace App\Listeners;
 
use Laravel\Cashier\Events\WebhookReceived;
 
class StripeEventListener
{
    /**
     * 处理接收到的Stripe Webhooks。
     */
    public function handle(WebhookReceived $event): void
    {
        if ($event->payload['type'] === 'customer.subscription.updated') {
            // 处理传入的事件...
        }
    }
}

在此监听器中,您可以重置数据库中的任何内容。
这是我建议您遵循的方式,但是还可以通过衡量自订阅创建以来创建的任务来采用其他方式。

您可以在官方的Laravel Cashier文档中阅读更多信息

英文:

Stripe can notify your application of a variety of events via webhooks. By default, a route that points to Cashier's webhook controller is automatically registered by the Cashier service provider. This controller will handle all incoming webhook requests.

By default, the Cashier webhook controller will automatically handle cancelling subscriptions that have too many failed charges (as defined by your Stripe settings), customer updates, customer deletions, subscription updates, and payment method changes; however, as we'll soon discover, you can extend this controller to handle any Stripe webhook event you like.

If you have additional webhook events you would like to handle, you may do so by listening to the following events that are dispatched by Cashier:

Laravel\Cashier\Events\WebhookReceived
Laravel\Cashier\Events\WebhookHandled

Both events contain the full payload of the Stripe webhook. For example, if you wish to handle the customer.subscription.updated webhook, you may register a listener that will handle the event:

&lt;?php
 
namespace App\Listeners;
 
use Laravel\Cashier\Events\WebhookReceived;
 
class StripeEventListener
{
    /**
     * Handle received Stripe webhooks.
     */
    public function handle(WebhookReceived $event): void
    {
        if ($event-&gt;payload[&#39;type&#39;] === &#39;customer.subscription.updated&#39;) {
            // Handle the incoming event...
        }
    }
}

In this listener, you can reset everything you want in the database.
This is the way I recommend you to follow, however there could be other ways by measuring the tasks that were created since the subscription was created.

You can read more information in the official Laravel Cashier documentation.

huangapple
  • 本文由 发表于 2023年5月25日 21:42:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定