Stripe API display invoices and line items

David Carr

Stripe API Tutorials Nova Framework

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>

 

Read articles directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Copyright © 2006 - 2024 DC Blog - All rights reserved.