Nissen
Nissen

Reputation: 287

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

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?

Upvotes: 2

Views: 2526

Answers (2)

Mohammad Golmoradi
Mohammad Golmoradi

Reputation: 41

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

Upvotes: 1

Nissen
Nissen

Reputation: 287

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 :(

Upvotes: 3

Related Questions