Laravel Cashier(Stripe)- 如何为按使用量计费创建订阅?

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

Laravel cashier (Stripe) - How to create subscription for metered usage?

问题

我在尝试将Cashier(Stripe)集成到我的Laravel应用程序中时遇到了一些问题。

我尝试做的是在已存在的客户(公司)上创建一个新的订阅。但是当我这样做时,我收到以下错误消息:

"您不能为按用量计费的计划设置数量。"

这是我尝试的代码:

$company = Company::find(1);    
$company->newSubscription('prod_XXX', 'plan_XXX')->create();

在Laravel文档中,我找到了以下内容:

$user->newSubscription('main', 'premium')->create($paymentMethod);

但据我了解,如果客户已经存在,我不需要在create()方法中传递paymentMethod,因为它与数据库中的stripe_id匹配。

如果需要paymentMethod,我也尝试了以下方式:

$company = Company::find(1); 
$paymentMethod = $company->defaultPaymentMethod()->id;
$company->newSubscription('prod_XXX', 'plan_XXX')->create($paymentMethod);

但我收到相同的错误消息。

在Stripe仪表板中,我在错误日志中找到了以下请求体:

{
  "expand": {
    "0": "latest_invoice.payment_intent"
  },
  "plan": "plan_XXX",
  "quantity": "1",
  "off_session": "true"
}

因此,newSubscription()方法似乎默认传递数量1。

那么回到我的问题:如何在现有客户上创建一个新的按用量计费的订阅,而无需传递数量?

英文:

I'm having some trouble trying to implement Cashier (Stripe) into my Laravel application.

What I'm trying to do is create a new subscription on an already existing customer (company). But when I do so I get the following error:

You cannot set the quantity for metered plans.

This is what I tried to do:

$company = Company::find(1);    
$company->newSubscription('prod_XXX', 'plan_XXX')->create();

In the laravel documentation I found this

$user->newSubscription('main', 'premium')->create($paymentMethod);

but as far as I understand, if the customer already exists I don't need to pass a paymentMethod in the create() method, since it pairs with the stripe_id in the database.

In case the paymentmethod was required I also tried like this:

$company = Company::find(1); 
$paymentMethod = $company->defaultPaymentMethod()->id;
$company->newSubscription('prod_XXX', 'plan_XXX')->create($paymentMethod);

But I get the same error message.

In the Stripe dashboard I found this request body in the error log:

{
  "expand": {
    "0": "latest_invoice.payment_intent"
  },
  "plan": "plan_XXX",
  "quantity": "1",
  "off_session": "true"
}

So the newSubscription() apparently passes a quantity of 1 by default.

So back to my question: How do I create a new metered subscription on an existing customer without passing a quantity?

答案1

得分: 3

如Martin Bean所评论,Cashier实际上不支持分阶计费。在他链接的GitHub问题中,我找到了这个解决方法。

$user->newSubscription('default', 'plan_XXX')
     ->quantity(0)
     ->create($request->get('payment_method'));

编辑

好的,不用担心。当我尝试稍后更新使用情况以使用正确的计划时,我得到了与之前相同的错误 Laravel Cashier(Stripe)- 如何为按使用量计费创建订阅?

英文:

As Martin Bean commented, Cashier doesn't really support metered billing. In the github issue he linked I found this workaround.

$user->newSubscription('default', 'plan_XXX')
     ->quantity(0)
     ->create($request->get('payment_method'));

> EDIT
>
> Okay, never mind. When I try to update the usage later on to use the
> correct plan, I get the same error as before Laravel Cashier(Stripe)- 如何为按使用量计费创建订阅?

答案2

得分: 1

要创建使用Cashier的按计量产品订阅,您应该执行2个步骤:

  1. 在您的控制器中,当订阅用户时:调用newSubscription并将数量设置为NULL

    $this->newSubscription('default')
         ->quantity(null)
         ->create();
    
  2. 向Stripe发送请求以添加使用量,并在Cashier表中增加相关项目的数量。
    创建使用记录API

英文:

for create subscription on metered product using cashier you should implement 2 step:

  1. in your controller when subscribe a user: call newsubscription with quantity NULL

    $this->newSubscription('default')
    ->quantity(null)
    ->create();

  2. send request stripe for adding usage and increment related item quantity in cashier table.
    create usage api

huangapple
  • 本文由 发表于 2020年1月6日 18:19:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610308.html
匿名

发表评论

匿名网友

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

确定