Stripe API display invoices and line items

David Carr

Stripe API Nova Framework Tutorials

Table of Contents

Invoices are created automatically, using Stripe’s API it’s possible to pull the invoices into your application.

I’m using Nova Framework not setup with Nova? please read Getting Stripe API setup with Nova Framework

To get all invoices from Stripe call Invoice::all()->data this will return an array of the invoices.

$invoices = Invoice::all()->data;

Loop over the array and display the invoices in a table, I’ve chosen to display only the date, payment amount, status and a link to download a custom invoice.

<table class='table table-striped table-hover table-bordered'>
<tr>
    <th>{{ __d('users', 'Date') }}</th>
    <th>{{ __d('users', 'Payment') }}</th>
    <th>{{ __d('users', 'Status') }}</th>
    <th>{{ __d('users', 'Invoice') }}</th>
</tr>
@foreach($invoices as $invoice)
    <tr>
        <td>{{ date('jS M Y H:iA', $invoice->date) }}</td>
        <td>&pound;{{ $invoice->total / 100 }}</td>
        <td>{{ ($invoice->paid) ? 'Yes' : 'No' }}</td>
        <td><a href='{{ site_url("account/invoice/$invoice->id") }}' class='btn btn-info btn-xs'>{{ __d('users', 'Download') }}</a></td>
    </tr>
@endforeach
</table>

The array contains line items for each invoice, it’s not practical to display the line items here so instead display them in a dedicated page. 

Getting line items

First get a single invoice:

$invoice = Invoice::retrieve($id);

Next setup a table loop over the items by calling $invoice->lines->data and loop over that.

<table class='table table-striped table-hover'>
<tr>
    <th>Description</th>
    <th>Amount</th>
</tr>
@foreach($invoice->lines->data as $line)
    <tr>
        <td>{{ ($line->description =='') ? $line->plan->name : $line->description }}</td>
        <td>&pound;{{ $line->amount / 100 }}</td>
    </tr>
@endforeach
    <tr>
        <td style="text-align: right"><b>Total</b></td>
        <td><b>&pound;{{ $invoice->total / 100 }}</b></td>
    </tr>
</table>

 

Fathom Analytics $10 discount on your first invoice using this link

David Carr - Laravel Developer

Hi, I’m David Carr

A Senior Developer at Vivedia
I love to use the TALL stack (Tailwind CSS, Alpine.js, Laravel, and Laravel Livewire)

I enjoy writing tutorials and working on Open Source packages.

I also write books. I'm writing a new book Laravel Testing Cookbook, This book focuses on testing coving both PestPHP and PHPUnit.

Sponsor me on GitHub

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Laravel Testing Cookbook

Help support the blog so that I can continue creating new content!

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2023 DC Blog. All code MIT license. All rights reserved.