Skip to content

Instantly share code, notes, and snippets.

@joram
Created May 5, 2024 22:29
Show Gist options
  • Save joram/ff6068f2d282cae576c93d3b8fa13f83 to your computer and use it in GitHub Desktop.
Save joram/ff6068f2d282cae576c93d3b8fa13f83 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import stripe
stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
stripe.api_version = '2020-08-27' # this is important. pin your version to avoid breaking changes.
def create_a_customer(name, age):
customer = stripe.Customer.create(
name=name,
age=age
)
return customer
def create_a_card(customer_id, card_number, exp_month, exp_year, cvc):
card = stripe.Card.create(
customer=customer_id,
source=card_number,
exp_month=exp_month,
exp_year=exp_year,
cvc=cvc
)
return card
def create_invoice_items(customer_id, amount, currency, description, quantity):
invoice = stripe.InvoiceItem.create(
customer=customer_id,
amount=amount,
currency=currency,
description=description,
quantity=quantity
)
return invoice
def create_an_invoice(customer_id, amount):
# this should return an invoice that has a status of "draft", and has collected all the invoice items that currently exist for the customer
invoice = stripe.Invoice.create(
customer=customer_id,
amount=amount,
lines=[
{
'price': 'price_1Hd2Za2eZvKYlo2C3v1Ll5H5',
'quantity': 1,
},
],
currency='usd',
collection_method='charge_automatically',
)
return invoice
def finalize_invoice(invoice_id):
invoice = stripe.Invoice.finalize_invoice(invoice_id)
return invoice
################
# General Workflow
################
## Create a customer
# 1. Create a customer
# 2. Create a card
## Create an invoice
# 1. Create invoice items
# 2. Create an invoice
# 3. Finalize the invoice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment