Laravel create subscription and add quantity for metered pricing

49 views Asked by At

I'm developing an open-source screen recorder that records technical details such as network requests, database requests on the backend and logs both on frontend and backend.

I'm implementing Stripe for the first time in a Laravel application using Cashier. I'm having problems understanding how I can start a subscription with a metered pricing and charge the user as soon as the subscription starts.

My idea on how this should work is this:

  1. User selects paid plan (gold plan)
  2. On the backend we create a subscription
  3. Attach the metered plan to the user
  4. Report the usage
  5. User gets instantly charged for the first month based on how many members it has in the company

Here's the code for creating a subscription in Gold plan:

private function createSubscriptionForGoldPlan(Company $company): void
{
  // Pricing returns the ID of the metered pricing named "Gold"
  $pricing = $this->subscriptionService->getGoldMonthlyPricingId();
  $numberMembers = $company->members()->count();

  $subscription = $company
    ->newSubscription('default')
    ->meteredPrice($pricing)
    ->create();

  $subscription->reportUsageFor($pricing, $numberMembers);
}

My idea would be to simply pass quantity when creating a subscription to be attached to the metered price with:

$subscription = $company
    ->newSubscription('default')
    ->meteredPrice($pricing)
    ->quantity($numberMembers)
    ->create();

In doing so, it returns an error reported here (and here). Now, when I go the customer's page in Stripe's dashboard, I get an invoice of 0 charged to the user:

enter image description here

I can also see that the customer will be billed normally in the next month with the correct usage reported (70 euros on 17th March):

enter image description here

How can I make sure that as soon as the user chooses a plan, we can charge him according to the amount of company members he has?

1

There are 1 answers

1
Bruno Francisco On

This is a pretty stupid question overall. How can we start charging for a metered price as soon as the plan starts? How will stripe know how much it should charge since there is no usage yet to be used?

I could delete the question but I will leave it here to testament of my own stupidity.