Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating and Invoicing Stripe Subscription Quantity with Invoice description laravel cashier

Good Day,

I'm working on a project involving Laravel Cashier. I want to give user's the ability to update their subscription quantity and get charged immediately (which I have been able to achieve, using the code below)

$user = Auth::user()
$user->subscription('main')->incrementAndInvoice(10000);

As much as the above works as expected, the invoice returned doesn't include a description indicating the changes instead the invoice description is blank. But when I checked the Event Data on stripe the two descriptions are there [see below image]

First Description which is the user's current/unused quantity enter image description here

The above images shows a user who was currently on a subscription plan with 5000 quantities but increased to 15000 quantities. Is there a way to include these descriptions in the invoice generated.

After i checked the incrementAndInvoice() method , it only accepts two parameter (1. count, 2. Plan) as seen below;

enter image description here

no option to include description like we have for the charge() method. Is there any workaround to this? Any ideas or pointers in the right direction would be really appreciated.

Thanks for your help in advance.

like image 467
Kolawole Emmanuel Izzy Avatar asked Sep 12 '25 14:09

Kolawole Emmanuel Izzy


1 Answers

At the time being there is no implementation to include the description in incrementAndInvoice().

So we have to implement it and before we do that please checkout Update an invoice.

First change this line: $user->subscription('main')->incrementAndInvoice(10000); to $subscription = $user->subscription('main')->incrementAndInvoice(10000); (we are assigning it to $subscription variable)

then get the invoice as below:

$new_invoice = $user->invoices()->filter(function($invoice) use ($subscription) {
   return $invoice->subscription === $subscription->stripe_id;
});

After updating the subscription quantity we will add the following:

$client  = new \GuzzleHttp\Client();
$request = $client->post('https://api.stripe.com/v1/invoices/' . $invoice_id, [
    'auth' => [$secret_key, null]
    'json' => ['description' => 'your description']
]);

$response = json_decode($request->getBody());

Or the following:

$stripe = new \Stripe\StripeClient(
  $secret_key
);
$stripe->invoices->update(
  $invoice_id,
  ['description' => 'your description']
);

Please note that:

  • the $invoice_id is the id of the invoice.
  • the $secret_key is the API key.
like image 66
Mahmood Ahmad Avatar answered Sep 15 '25 04:09

Mahmood Ahmad