From 023eefe9e12858bbe7ffd9554bbdaf6fd5791434 Mon Sep 17 00:00:00 2001 From: Steve Nyemba Date: Fri, 19 Jan 2024 13:05:49 -0600 Subject: [PATCH] store library, powered by stripe --- store/__init__.py | 8 ++++++++ store/get/__init__.py | 33 +++++++++++++++++++++++++++++++++ store/set/__init__.py | 23 +++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 store/__init__.py create mode 100644 store/get/__init__.py create mode 100644 store/set/__init__.py diff --git a/store/__init__.py b/store/__init__.py new file mode 100644 index 0000000..8eb9b3c --- /dev/null +++ b/store/__init__.py @@ -0,0 +1,8 @@ +import stripe +import get +import set +def init(key:str): + """ + Initializing stripe store with a key + """ + stripe.api_key = key diff --git a/store/get/__init__.py b/store/get/__init__.py new file mode 100644 index 0000000..246ccaa --- /dev/null +++ b/store/get/__init__.py @@ -0,0 +1,33 @@ +def customer(email:str) : + """ + This function returns a customer given an email + """ + return stripe.Customer.search(query=f"email:'{email}'").data +def customers(): + """ + This function returns a list of customers (all customers) + """ + _data = [] + customers = stripe.Customer.list(limit=100) + for _customer in customers.auto_paging_iter() : + _data.append(_customer) + return _data + +def product (_name): + """ + returns a product given it's name + """ + return stripe.Product.search(query=f"name:'{_name}'").data +def products(): + """ + return a list of products + """ + _products = stripe.Product.list(limit=100) + return _products.auto_paging_iter() +def plans(_name:str): + """retrieves plans for a given producti + :_name name of the product + """ + _prod = product(_name) + _id = _prod[0]['id'] + return stripe.Price.search(query=f"product:'{_id}'").data diff --git a/store/set/__init__.py b/store/set/__init__.py new file mode 100644 index 0000000..7d6c246 --- /dev/null +++ b/store/set/__init__.py @@ -0,0 +1,23 @@ +def customer(email:str,name:str): + """ + :email customer's email + :name customer's name + """ + return stripe.Customer.create(name=name,email=email) + +def subscribe(_id,_price): + """ + :_id customer identifier + :_price price object associated with product/plan + """ + return stripe.Subscription.create(customer=_id, + items=[{"price":_price}] + ) +def unsubscribe(_id:str): + """ + Cancelling a given subscription for a given user + _id: subscription identifier + """ + return stripe.Subscription.cancel(_id) + +