【问题标题】:Laravel cashier : how to get plan name or id?Laravel 收银员:如何获取计划名称或 ID?
【发布时间】:2017-10-18 17:46:11
【问题描述】:

我有 8 个条纹计划。 4 个月 + 4 年。喜欢

plan_one.monthly
plan_one.yearly
plan_two.monthly
plan_two.yearly
plan_three.monthly
plan_three.yearly
plan_four.monthly
plan_four.yearly

这里是用户订阅行

id -> 1
user_id -> 1
name -> Plan-One Monthly
stripe_id -> sub_xxxxxxxxxx
stripe_plan -> plan_one.monthly
quantity -> 1
trial_ends_at - null
ends_at - null
created_at - datetime()
updated_at - datetime()

在这种情况下,我如何获取用户活动/当前订阅计划名称?

【问题讨论】:

  • 您可以使用stripe.com/docs/api#retrieve_customer(如果您只有一个订阅)或stripe.com/docs/api#list_subscriptions(如果您有多个订阅)来执行此操作。
  • 奇怪的是,如果不遍历所有计划并检查 $user->subscription($plan_name) 是否为真,就无法在收银员中执行此操作。我知道它很愚蠢,但它就是这样建造的。昨天我自己也遇到了。

标签: php laravel-5 stripe-payments billing


【解决方案1】:

Billable 特征设置上的 subscription 方法返回最后一行。因此,如果一个计费实体在您的应用程序上只有一个有效订阅,这很好:

$user->subscription('default')->stripe_plan;

在 laravel 5.6/cashier 7.1 上测试

【讨论】:

    【解决方案2】:

    我迟到了,但我一直在寻找类似的解决方案,在我的情况下,我只允许用户一次订阅一个,但我想以编程方式获取用户的活动订阅,而无需遍历可用订阅,更彻底地阅读 laravel 文档,我发现我需要使用订阅范围。

    $user->subscriptions()->active()->first();

    https://laravel.com/docs/8.x/billing#subscription-scopes

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 1970-01-01
      • 2019-01-08
      • 2018-11-07
      • 2017-11-24
      • 2021-02-21
      • 2017-08-14
      • 2014-10-28
      • 2018-03-28
      相关资源
      最近更新 更多