Nova Framework, Tutorials, Stripe API

Stripe API display invoices and line items

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>

 

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.