Kolawole Emmanuel Izzy
Kolawole Emmanuel Izzy

Reputation: 1052

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.

Upvotes: 24

Views: 2699

Answers (2)

Zuhaib Arshad
Zuhaib Arshad

Reputation: 1

To address the issue of the invoice description being blank, you can use the updateStripeSubscription method to update the subscription metadata, which in turn reflects on the invoice. The metadata can include any custom descriptions or notes that you want to appear on the invoice.

Here’s how you can modify your code to include a description in the invoice:

Retrieve the subscription: First, get the subscription object from Stripe. Update the subscription: Add or update the metadata with your custom description. Increment and invoice: Finally, use the incrementAndInvoice method to increment the subscription quantity and generate an invoice. Here's the updated code with these steps:

Upvotes: 0

Mahmood Ahmad
Mahmood Ahmad

Reputation: 494

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.

Upvotes: 1

Related Questions