Updating and Invoicing Stripe Subscription Quantity with Invoice description laravel cashier
Asked Answered
E

3

23

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.

Evelyne answered 14/7, 2020 at 0:56 Comment(1)
Try with $invoice = $subscription->asStripeSubscription(['latest_invoice']); $invoice->descriptionPolaroid
S
0

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.
Shenika answered 22/7, 2020 at 10:20 Comment(8)
Thanks for your response. The thing is, there is no access to invoice ID because the invoice is sent back from stripe when a subscription quantity is updated and the user is charged immediately (which is called IncrementAndInvoice() )Evelyne
what do you get when you do like this dd($user->subscription('main')->incrementAndInvoice(10000));Shenika
i get subscription table data for the user and the change in quantity .... nothing regarding invoice. ibb.co/wg0LLHr and ibb.co/z7Tm3SSEvelyne
checkout the updated answer ... please checkout this link laracasts.com/discuss/channels/eloquent/… for more infoShenika
Invoice came in still with no description ibb.co/5Mq14srEvelyne
can you please share the new code that you use now ?Shenika
This is what i added to the line ... ibb.co/FnqLmbv ... The thing is the invoice is being prepared and returned by stripe and its added to the payment history table. If i subscribe to a plan, the invoice works fine with all the description but when i update quantity and invoice, the description is not includedEvelyne
Ok before return response add the following '''$client = new \GuzzleHttp\Client(); $request = $client->post('api.stripe.com/v1/invoices' . $new_invoice->id, [ 'auth' => [$secret_key, null] 'json' => ['description' => 'your description'] ]); $response = json_decode($request->getBody());'''Shenika
S
0

Try this custom approach :

In this we are getting the current user main subscription and then manually updating the quantity and description for invoice item instead of using the default method

    $user = User::find($userId);
    $currentSubscription = $user->subscription('main');

   
    // Calculate the new quantity
    $newQuantity = $currentSubscription->quantity + $incrementBy;

    // Update the subscription quantity
    $currentSubscription->updateQuantity($newQuantity);

    // Create an invoice item with a custom description
    InvoiceItem::create([
        'customer' => $user->stripe_id,
        'amount' => 10000, // the amount to be charged in cents
        'currency' => 'usd',
        'description' => "Subscription quantity increased to {$newQuantity}",
    ]);

    // Create an invoice
    $invoice = Invoice::create([
        'customer' => $user->stripe_id,
        'auto_advance' => true, // Auto-finalize this draft after ~1 hour
    ]);

    // Finalize the invoice
    $invoice->finalizeInvoice();

    // Optionally, you can immediately pay the invoice
    $invoice->pay();
Somnus answered 28/5 at 8:0 Comment(1)
@DarkBee the main question was how he can add the description to a invoice item. User::find is just a for getting the current user or this can achieved by also $user= auth()->user(); The main problem was solved by getting the main subscription and then manually updating the quantity and description which in the question was done using a default method ->incrementAndInvoice(10000);Somnus
C
0

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:

Complect answered 22/6 at 20:35 Comment(1)
use Stripe\Subscription; $user = Auth::user(); $subscription = Subscription::retrieve($user->subscription('main')->stripe_id); $subscription->metadata = [ 'description' => 'Updated subscription quantity' ]; $subscription->save(); $user->subscription('main')->incrementAndInvoice(10000);Complect

© 2022 - 2024 — McMap. All rights reserved.