parent
cd6ccf1b34
commit
968bda4c0a
@ -0,0 +1,127 @@
|
||||
"""
|
||||
This file is a wrapper for stripe and store handling operations :
|
||||
- subscribe
|
||||
- user
|
||||
- upgrade
|
||||
- downgrade
|
||||
- plans (for a given product)
|
||||
"""
|
||||
import stripe
|
||||
from utils.params import PARAMS as SYS_ARGS
|
||||
from utils import void
|
||||
|
||||
class Store :
|
||||
# stripe_keys = {
|
||||
# "secret_key":SYS_ARGS['stripe']['secret'].strip(),
|
||||
# "publishable_key":SYS_ARGS['stripe']['pub'].strip()
|
||||
# }
|
||||
# stripe.api_key = self.stripe_keys['secret_key']
|
||||
|
||||
def __init__(self,**args):
|
||||
self.get = void()
|
||||
self.get.user = self._get_user
|
||||
self.get.plans = self._get_plans
|
||||
product_name = args['product']
|
||||
stripe.api_key = args['secret'] if 'secret' in args else stripe.api_key
|
||||
if stripe.api_key :
|
||||
|
||||
self.__init_product(product_name)
|
||||
else:
|
||||
print (['secret' in args, stripe.api_key is None])
|
||||
pass
|
||||
def __init_product(self,product_name):
|
||||
PRODUCT_INDEX = 2
|
||||
DATA_TYPE_INDEX = 1
|
||||
STATUS_INDEX = 0
|
||||
resp = stripe.product.Product.list().to_dict_recursive()['data']
|
||||
self.product = []
|
||||
self.plan = []
|
||||
|
||||
if resp :
|
||||
|
||||
if resp :
|
||||
self.product = [p for p in resp if p['name'] == product_name]
|
||||
self.product = self.product[0] if self.product else []
|
||||
else:
|
||||
self.product = resp[0]
|
||||
|
||||
if 'id' in self.product :
|
||||
self.__init_plan(self.product['id'])
|
||||
|
||||
def __init_plan(self,id):
|
||||
"""
|
||||
This function will retrieve plan information associated with the given product id
|
||||
"""
|
||||
self.plans = stripe.plan.Plan.list(product=id).to_dict_recursive()['data']
|
||||
|
||||
|
||||
def _get_user(self,**args):
|
||||
"""
|
||||
This function will return a customer information and associated plan (hopefully)
|
||||
:email
|
||||
"""
|
||||
email = args['email']
|
||||
customers = stripe.customer.Customer.list(email=email).to_dict_recursive()['data']
|
||||
if not customers:
|
||||
return None
|
||||
else:
|
||||
customers = customers[0]
|
||||
return {"email":email,"id":customers['id']}
|
||||
|
||||
pass
|
||||
def __cancel_plan(self,**args) :
|
||||
pass
|
||||
def __set_charge(self,**args):
|
||||
pass
|
||||
def __set_plan(self,**args):
|
||||
"""
|
||||
This function is effectively a subscription for the current user
|
||||
"""
|
||||
plan = args['plan']
|
||||
email = args['email']
|
||||
user_plan = args['user_plan'] if 'user_plan' in args else self.get.plans(email=email)
|
||||
if plan == user_plan :
|
||||
#
|
||||
# There is nothing to do here
|
||||
pass
|
||||
else:
|
||||
#
|
||||
# we need to cancel, the user_plan unless it is free
|
||||
|
||||
pass
|
||||
pass
|
||||
def _get_plans(self,**args) :
|
||||
"""
|
||||
The product name is provided in the constructor and at this poit, if a user email is not provided,
|
||||
This function operates as an accessor to the plans attribute. If an email is provided
|
||||
The function will return the plans associated for a user given the current product.
|
||||
|
||||
:email user email
|
||||
"""
|
||||
if 'email' in args :
|
||||
#
|
||||
email = args['email']
|
||||
info = stripe.customer.Customer.list(email=email).to_dict_recursive()['data']
|
||||
|
||||
if info :
|
||||
#
|
||||
#
|
||||
_found = None
|
||||
ids = [plan['id'] for plan in self.plans ]
|
||||
subscriptions = info[0]['subscriptions']['data']
|
||||
for sub in subscriptions :
|
||||
aplan = sub['plan']
|
||||
if set([aplan['id']]) & set(ids):
|
||||
_found = sub
|
||||
break
|
||||
|
||||
info = _found if _found else None
|
||||
|
||||
else:
|
||||
info = None
|
||||
return info
|
||||
else :
|
||||
|
||||
return self.plans
|
||||
|
||||
|
Loading…
Reference in new issue