'Getting Stripe Upcoming Invoice Line Items with Laravel Cashier

I'm attempting to use Laravel Cashier to retrieve line items of the client's upcoming invoice. I'm having difficulty obtaining the invoice in the first place. Though I've found the undocumented public function "upcomingInvoice," I can't get any of its protected properties out.

Even with it, I'm having trouble understanding how to use the poorly documented "asStripe..." functions, presumably asStripeInovice(), to return the line items.

I've tried a whole host of things and it would muddle things up to write them all out, so I figure it might be better just to ask how to go about it.

Thanks!



Solution 1:[1]

Try this one liner code in laravel 7 or 8 with cashier.

$user->upcomingInvoice()

And you're good getting all upcoming invoices details.

Solution 2:[2]

As up update, I was able to get this to work just using the Stripe client, like so:

$stripe = new StripeClient(env('STRIPE_SECRET'));
$upcomingLineItems = $stripe->invoices
                            ->upcomingLines(['customer' => $customer['stripe_id']]);

I'll still leave this open in case there's a way to do it with Cashier's methods, but it now doesn't seem necessary.

Solution 3:[3]

Similarly, I wanted to be able to display the "amount due today" to a user prior to switching subscription plans (which takes into account proration as well as any credit applied to the customer's account). I was able to do this in Laravel using the following:

    $stripe_client = new StripeClient(config('stripe.secret'));

    $items = [
      [
        'id' => $user->subscription()->items()->first()->stripe_id,
        'price' => $selected_plan, # Switch to new price
      ],
    ];

    $invoice = $stripe_client->invoices->upcoming([
      'customer' => $user->stripe_id,
      'subscription' => $user->subscription()->stripe_id,
      'subscription_items' => $items,
      'subscription_proration_date' => Carbon::now()->timestamp
    ]);

    $amount_due = $invoice->amount_due; // and there it is

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 totski
Solution 2 Jeremy L.
Solution 3 Inigo