22

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.

  • Try with `$invoice = $subscription->asStripeSubscription(['latest_invoice']);` `$invoice->description` – MrEduar Dec 15 '22 at 17:04

1 Answers1

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.
Mahmood Ahmad
  • 452
  • 4
  • 11
  • 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()` ) – Kolawole Emmanuel Izzy Jul 26 '20 at 12:23
  • what do you get when you do like this `dd($user->subscription('main')->incrementAndInvoice(10000));` – Mahmood Ahmad Jul 26 '20 at 12:34
  • i get subscription table data for the user and the change in quantity .... nothing regarding invoice. https://ibb.co/wg0LLHr and https://ibb.co/z7Tm3SS – Kolawole Emmanuel Izzy Jul 26 '20 at 13:30
  • checkout the updated answer ... please checkout this link https://laracasts.com/discuss/channels/eloquent/getting-invoicereceipt-of-selected-subscription-plan?page=0 for more info – Mahmood Ahmad Jul 26 '20 at 14:04
  • Invoice came in still with no description https://ibb.co/5Mq14sr – Kolawole Emmanuel Izzy Jul 27 '20 at 18:50
  • can you please share the new code that you use now ? – Mahmood Ahmad Jul 27 '20 at 19:15
  • This is what i added to the line ... https://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 included – Kolawole Emmanuel Izzy Jul 28 '20 at 09:29
  • Ok before return response add the following '''$client = new \GuzzleHttp\Client(); $request = $client->post('https://api.stripe.com/v1/invoices/' . $new_invoice->id, [ 'auth' => [$secret_key, null] 'json' => ['description' => 'your description'] ]); $response = json_decode($request->getBody());''' – Mahmood Ahmad Jul 28 '20 at 11:00