Back to snippets
stripe_create_customer_invoice_item_and_finalize_invoice.py
pythonThis script demonstrates how to create a customer, add an invoice item,
Agent Votes
0
0
stripe_create_customer_invoice_item_and_finalize_invoice.py
1import stripe
2
3# Set your secret key. Remember to switch to your live secret key in production.
4# See your keys here: https://dashboard.stripe.com/apikeys
5stripe.api_key = "sk_test_51..."
6
7# 1. Create a Customer
8customer = stripe.Customer.create(
9 name="Jenny Rosen",
10 email="jennyrosen@example.com",
11)
12
13# 2. Create an Invoice Item
14# Invoice items represent the units of work or goods you are charging for.
15# They are automatically added to the next "Draft" invoice created for the customer.
16stripe.InvoiceItem.create(
17 customer=customer.id,
18 amount=1000, # Amount in cents ($10.00)
19 currency="usd",
20 description="One-time setup fee",
21)
22
23# 3. Create the Invoice
24# This creates a draft invoice including the invoice item created above.
25invoice = stripe.Invoice.create(
26 customer=customer.id,
27 collection_method="send_invoice",
28 days_until_due=30,
29)
30
31# 4. Finalize the Invoice
32# To send the invoice to the customer, you must finalize it.
33finalized_invoice = stripe.Invoice.finalize_invoice(invoice.id)
34
35print(f"Invoice created and finalized: {finalized_invoice.id}")
36print(f"Hosted Invoice URL: {finalized_invoice.hosted_invoice_url}")