Learn how to send one-time invoices in Stripe with this step-by-step guide, covering setup, dashboard and API methods, customization, and troubleshooting.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
How to Send One-Time Invoices in Stripe: A Comprehensive Tutorial
Step 1: Set Up Your Stripe Account
Before you can send invoices, you need to have a Stripe account set up and properly configured:
Step 2: Install and Set Up the Stripe SDK (For Programmatic Creation)
If you plan to create invoices programmatically, you'll need to install the Stripe SDK for your preferred programming language:
For Node.js:
npm install stripe
For Python:
pip install stripe
For PHP:
composer require stripe/stripe-php
Once installed, initialize the Stripe client with your API key:
Node.js:
const stripe = require('stripe')('sk_test_your_test_key');
// Use 'sk_live_your_live_key' for production
Python:
import stripe
stripe.api_key = "sk_test_your_test\_key"
# Use 'sk_live_your_live_key' for production
PHP:
\Stripe\Stripe::setApiKey('sk_test_your_test_key');
// Use 'sk_live_your_live_key' for production
Step 3: Create a Customer in Stripe
Before creating an invoice, it's best to create or identify a customer record in Stripe:
Through the Stripe Dashboard:
Programmatically with Node.js:
const customer = await stripe.customers.create({
email: '[email protected]',
name: 'Jenny Rosen',
phone: '+15551234567',
address: {
line1: '123 Main St',
city: 'San Francisco',
state: 'CA',
postal\_code: '94111',
country: 'US',
},
description: 'Customer for invoice',
});
Step 4: Create an Invoice Using the Stripe Dashboard
For a simple, manual approach:
Step 5: Create an Invoice Programmatically
For programmatic invoice creation, follow these steps:
Node.js example:
// First, create an invoice item
const invoiceItem = await stripe.invoiceItems.create({
customer: 'cus_customer_id',
amount: 2000, // Amount in cents (e.g., $20.00)
currency: 'usd',
description: 'One-time consultancy fee',
});
// Then create the invoice with the invoice item
const invoice = await stripe.invoices.create({
customer: 'cus_customer_id',
auto\_advance: false, // draft invoice
collection_method: 'send_invoice',
description: 'Consultancy Services',
footer: 'Thank you for your business!',
due\_date: Math.floor(Date.now() / 1000) + 7 _ 24 _ 60 \* 60, // 7 days from now
});
Python example:
import time
# First, create an invoice item
invoice\_item = stripe.InvoiceItem.create(
customer='cus_customer_id',
amount=2000, # $20.00
currency='usd',
description='One-time consultancy fee',
)
# Then create the invoice with the invoice item
invoice = stripe.Invoice.create(
customer='cus_customer_id',
auto\_advance=False, # draft invoice
collection_method='send_invoice',
description='Consultancy Services',
footer='Thank you for your business!',
due\_date=int(time.time()) + 7 _ 24 _ 60 \* 60, # 7 days from now
)
Step 6: Finalize and Send the Invoice
Through the Stripe Dashboard:
Programmatically with Node.js:
// Finalize the invoice
const finalizedInvoice = await stripe.invoices.finalizeInvoice(invoice.id);
// Send the invoice
const sentInvoice = await stripe.invoices.sendInvoice(invoice.id);
Python:
# Finalize the invoice
finalized_invoice = stripe.Invoice.finalize_invoice(invoice.id)
# Send the invoice
sent_invoice = stripe.Invoice.send_invoice(invoice.id)
Step 7: Customize Your Invoice Template (Optional)
To brand your invoices:
Step 8: Monitor Invoice Status
Track the status of your sent invoices:
Through the Stripe Dashboard:
Programmatically with Node.js:
// Retrieve an invoice to check its status
const invoice = await stripe.invoices.retrieve('in_invoice_id');
console.log(`Invoice status: ${invoice.status}`);
// List all invoices
const invoices = await stripe.invoices.list({
limit: 10,
});
invoices.data.forEach(invoice => {
console.log(`Invoice ${invoice.id}: ${invoice.status}`);
});
Step 9: Set Up Webhook Events for Invoice Notifications (Advanced)
To get real-time updates when an invoice is paid or becomes past due:
// Express server example for handling webhook events
const express = require('express');
const app = express();
// Use JSON parser for webhook requests
app.use('/webhook', express.raw({type: 'application/json'}));
app.post('/webhook', async (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(
req.body,
sig,
'whsec_your_webhook_signing_secret'
);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle invoice events
switch (event.type) {
case 'invoice.paid':
const invoicePaid = event.data.object;
console.log(`Invoice ${invoicePaid.id} was paid!`);
// Update your database, send a thank-you email, etc.
break;
case 'invoice.payment\_failed':
const invoiceFailed = event.data.object;
console.log(`Invoice ${invoiceFailed.id} payment failed!`);
// Follow up with the customer
break;
}
res.json({received: true});
});
app.listen(3000, () => console.log('Running on port 3000'));
Step 10: Handle Invoice Payments and Receipts
When a customer pays an invoice:
To customize receipt emails:
Step 11: Troubleshooting Common Issues
If you encounter problems with invoices:
Example error handling in Node.js:
try {
const invoice = await stripe.invoices.create({
customer: 'cus_customer_id',
collection_method: 'send_invoice',
days_until_due: 30,
});
} catch (error) {
console.error('Error creating invoice:', error.message);
// Handle specific error types
if (error.type === 'StripeInvalidRequestError') {
console.error('Invalid parameters were supplied to Stripe API');
}
}
Final Tips for Effective Invoicing in Stripe
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.