Stripe: Download Invoice on my Website

stripe invoice api
stripe invoice template
stripe edit invoice
stripe get customer invoices
stripe partial payment
stripe mark invoice as paid
stripe invoice example
stripe customize invoice

I am working with Stripe Payment Gateway and everything is working so far, execept the option, that my users can download their invoice at my website. It is possible to download an invoice inside the stripe dashboard but that is not what I need. After my users made a payment, the should be able to download theiur invoice at my system.

Currently I am working with stripe webhooks. A user makes a payment and the data is saved inside my database. This is how my code looks like:

\Stripe\Stripe::setApiKey("$stripe_secret_key_test");
// Retrieve the request's body and parse it as JSON:
$input = @file_get_contents('php://input');
$event_json = json_decode($input);

// Update database with new billing entry
if ($event_json->type == 'invoice.payment_succeeded') {
    $invoice_id = $event_json->data->object->id;
    $customer = $event_json->data->object->customer;
    $date = $event_json->data->object->date;
    $price = $event_json->data->object->amount_paid;
    $user = $event_json->data->object->lines->data[0]->metadata->userid;
    $stripe_sub_id = $event_json->data->object->lines->data[0]->id;
    $plan_id = $event_json->data->object->lines->data[0]->plan->id;
    $stamp_created = $event_json->data->object->lines->data[0]->plan->created;
    $comments = $event_json->data->object->lines->data[0]->plan->nickname;
    $period_start = $event_json->data->object->lines->data[0]->period->start;
    $period_end = $event_json->data->object->lines->data[0]->period->end;

    $content=array("invoice"=>$invoice_id,
        "user"=>$user,
        "stripe_cust_id"=>$customer,
        "stripe_sub_id"=>$stripe_sub_id,
        "idea_id"=>0,
        "billing_date"=>$date,
        "plan"=>$plan_id,
        "price"=>$price,
        "stamp_created"=>$stamp_created,
        "period_start"=>$period_start,
        "period_end"=>$period_end,
        "status"=>'paid',
        "printed"=>0);
    insertGWInfo('billing',$content);

    $content=array("user"=>$user,
        "trans_time"=>date("Y-m-d H:i:s"),
        "ip"=>$_SERVER['REMOTE_ADDR'],
        "trans_type"=>1,
        "cash_amount"=>$price,
        "comments"=>"Subscription for ".$comments."",
        "idea_id"=>0);
    insertGWInfo('user_accounting',$content);
}

Now I´ve created a table where I fetch those entries from my database and display it to my customer. I would like to have a button now with "Download Invoice" where my customers can download the subscription invoice which appears monthly.

Any idea how I can do this? I have the invoice_id but where do I get the URL to download an invoice? I wasn´t able to find anything inside the stripe docs. Any help would be really appreciated.

EDIT: Here is the JSON outcome for invoice.payment_suceeded:

"{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "invoice.payment_succeeded",
  "object": "event",
  "request": null,
  "pending_webhooks": 1,
  "api_version": "2018-02-28",
  "data": {
    "object": {
      "id": "in_00000000000000",
      "object": "invoice",
      "amount_due": 1000,
      "amount_paid": 1000,
      "amount_remaining": 0,
      "application_fee": null,
      "attempt_count": 1,
      "attempted": true,
      "billing": "charge_automatically",
      "charge": "_00000000000000",
      "closed": true,
      "currency": "eur",
      "customer": "cus_00000000000000",
      "date": 1526455801,
      "description": null,
      "discount": null,
      "due_date": null,
      "ending_balance": 0,
      "forgiven": false,
      "lines": {
        "data": [
          {
            "id": "sub_Cs4DUPJKcAJ7cg",
            "object": "line_item",
            "amount": 1000,
            "currency": "eur",
            "description": "1 standard-inv × Standard Investor (at €10.00 / month)",
            "discountable": true,
            "livemode": false,
            "metadata": {
            },
            "period": {
              "end": 1531726201,
              "start": 1529134201
            },
            "plan": {
              "id": "3",
              "object": "plan",
              "aggregate_usage": null,
              "amount": 1000,
              "billing_scheme": "per_unit",
              "created": 1526375755,
              "currency": "eur",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {
              },
              "nickname": "Standard Plan for Investors",
              "product": "prod_CrihTVfgRHgImD",
              "tiers": null,
              "tiers_mode": null,
              "transform_usage": null,
              "trial_period_days": null,
              "usage_type": "licensed"
            },
            "proration": false,
            "quantity": 1,
            "subscription": null,
            "subscription_item": "si_Cs4DrvYN2FlCbS",
            "type": "subscription"
          }
        ],
        "has_more": false,
        "object": "list",
        "url": "/v1/invoices/in_1CSK5FDsOByhb3e8m8Z1BooY/lines"
      },
      "livemode": false,
      "metadata": {
      },
      "next_payment_attempt": null,
      "number": "774C629-0001",
      "paid": true,
      "period_end": 1526455801,
      "period_start": 1526455801,
      "receipt_number": null,
      "starting_balance": 0,
      "statement_descriptor": null,
      "subscription": "sub_00000000000000",
      "subtotal": 1000,
      "tax": null,
      "tax_percent": null,
      "total": 1000,
      "webhooks_delivered_at": 1526455810
    }
  }
}"

Stripe does not support downloading the invoice as a PDF at the moment via the API and it is only available in the dashboard instead.

For now, the best solution is to build your own invoice receipt/pdf instead. You can retrieve the Invoice object via the API which tells you what the invoice is for: period, amount, when it was paid, what's in that invoice, etc. And you can then generate your own receipt for your customers on your end.

Invoices, An invoice is a list of line items and their Line items contain services or goods sold, and their quantity, price, and tax rates. Invoices can be created from both the  Generate Invoices & Get Paid Easily. Easy To Use. Start a 30-Day Free Trial! Simple & Customizable Invoices From Top Brands! Flexible & Automated Invoicing.

Stripe DOES support this now. The PDF link is available by listing the invoices via the API and accessing the invoice_pdf attribute of an invoice.

Here's how you can get an array of invoices with their PDF links:

const invoicesResponse = await stripe.invoices.list({ customer: customerStripeId });
const invoices = invoicesResponse.map(invoice => ({
    invoiceId: invoice['id'],
    pdf: invoice['invoice_pdf'],
  }));
return invoices;

Here's the API docs: https://stripe.com/docs/api/invoices/object#invoice_object-invoice_pdf Thanks @avelis for the suggestion.

Hosted invoice page, Charge invoices automatically through Stripe, using the customer's default payment method. Send an invoice to a customer via email (this document). These email  Create, Edit, & Sign Invoice Documents Online Today - Fast, Easy, & Free! Build Invoice Forms & Other Transaction Records Free - Easy-to-Use Platform

it is posible :) works like a charm with the Python API.

import stripe
import requests

stripe_keys = {'secret_key': 'XXXxxxx','publishable_key': 'XXXxxxx'}
stripe.api_key = stripe_keys['secret_key']

stripe_subscription_id = 'XXXXXXX'
invoices = stripe.Invoice.list(subscription=stripe_subscription_id)
for inv in invoices:
    pdf          = inv.invoice_pdf
    invoice_file = requests.get(pdf)
    open('C://User//XXXXX//Desktop//invoice.pdf', 'wb').write(invoice_file.content)

Invoice object - Stripe API Reference, Create and download professional PDF invoices in seconds with the invoice template gallery, send invoices via email, get paid by card. Create, Edit, & Sign Invoice Documents Online Today - Fast, Easy, & Free! Build Invoice Forms & Other Transaction Records Free - Easy-to-Use Platform

Send invoices to customers, Your custom fields will render in the top-right of your invoice and receipt PDFs. Common example use cases of custom fields are: Purchase Order (PO) numbers​  1) Create An Invoice Online & Simplify Transactions. 2) Save & Print Instantly- 100% Free! Export To PDF & Word - Over 1M Forms Made - Answer Simple Questions & Create In Minutes!

Stripe Partners: Invoice Home, Instead, you can view or manually send a receipt using the Dashboard. Each payment in the Dashboard also has a Receipt history where you can view and in your email receipt settings, such as your phone number or website address. Invoices can be created from both the Stripe Dashboard and the Invoices API, and come in two forms: An invoice can be a one-off bill that you charge your customer. An invoice can be generated by a subscription on a recurring basis for every billing cycle. Each invoice represents the amount that the customer owes for that period. Payment collection

Customize invoices, You can build your own payment page and include a link to it in the email or you can use a Stripe-hosted page that allow customers to pay, download, and view  Stripe does not support downloading the invoice as a PDF at the moment via the API and it is only available in the dashboard instead. For now, the best solution is to build your own invoice receipt/pdf instead.

Comments
  • If you have invoice id then you can retrieve it by using \Stripe\Invoice::retrieve("in_InvoideId");
  • Thank you that is what I thought too BUT how do I get the correct download link (url)? Do you know what I mean?
  • There must be an url also to download invoice with key url
  • I added the outcome of the json to my first post so that you can see what info I get. I am not sure how to get the correct download url. any idea?
  • If there is no any download option, then you can write content from retrieve invoice in file and download that file.
  • Thank you, this is what I needed to know. Hope Stripe will offer this feature in the near future.
  • A link to the API docs showing the invoice PDF response attribute. stripe.com/docs/api/invoices/object#invoice_object-invoice_pdf