diff --git a/requirements.txt b/requirements.txt index 2191046..c71b360 100755 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,4 @@ six==1.10.0 socketpool==0.5.3 stripe==1.82.1 Werkzeug==0.12.1 +git+https://dev.the-phi.com/git/steve/data-transport.git \ No newline at end of file diff --git a/src/api/static/css/default.css b/src/api/static/css/default.css index 67eea59..cb3bb7b 100644 --- a/src/api/static/css/default.css +++ b/src/api/static/css/default.css @@ -51,11 +51,13 @@ body{ text-align:center; } +.button .text {color:#ffffff} .button:hover { background-color:#ffffff; color:#4682b4; } +.button:hover .text { color :#4682B4;} .active { font-weight:bold; @@ -75,7 +77,7 @@ body{ width: 0px; } -.bold { font-weight:bold} +.bold { font-weight:bold; } .caption { font-size:28px; font-weight:bold ; font-family:sans-serif; diff --git a/src/api/static/js/jx b/src/api/static/js/jx index df17531..3599fff 160000 --- a/src/api/static/js/jx +++ b/src/api/static/js/jx @@ -1 +1 @@ -Subproject commit df17531499104b155e5ac97ee2755a8bf10bf8ff +Subproject commit 3599fff083f1661a1762992d3891210d6c3aec05 diff --git a/src/api/store.py b/src/api/store.py index 5446119..fc48e77 100644 --- a/src/api/store.py +++ b/src/api/store.py @@ -73,39 +73,56 @@ class User : # email = args['email'] # self.user_plan = [] # self.customer = {} - customers = stripe.customer.Customer.list(email=email).to_dict_recursive()['data'] - if customers : - customers = customers[0] + customer = stripe.customer.Customer.list(email=email).to_dict_recursive()['data'] + free_plan = [item for item in self.plans if item['amount'] == 0] + if customer : + customer = customer[0] else: # # The user doesn't exist, we must create the user (without payment initially) # We can probably assign the user to a free plan if available # - customers = stripe.customer.Customer.Create(email=email) - free_plan = [item for item in self.plans if item['amount'] == 0] - if free_plan : - free_plan = free_plan[0] - self.plan.subscribe(free_plan['id'],email) + customer = stripe.customer.Customer.create(email=email) + + # if free_plan : + # free_plan = free_plan[0] + # self.plan.subscribe(free_plan['id'],email) # self.customer = {"email":email,"id":customers['id']} - self.me["customer"] = {"email":email,"id":customers['id']} + self.me["customer"] = {"email":email,"id":customer['id']} # # extract payments available, - if 'sources' in customers and 'data' in customers['sources'] : - if customers['sources']['data'] : + if 'sources' in customer and 'data' in customer['sources'] : + if customer['sources']['data'] : # self.payment = [ card.to_dict_recursive() for card in customers['sources']['data'] ] - self.me['payments'] = [ card.to_dict_recursive() for card in customers['sources']['data'] ] - info = customers #self.me['customer'] - subscriptions = info['subscriptions']['data'] + self.me['payments'] = [ card.to_dict_recursive() for card in customer['sources']['data'] ] + # if not self._has_plan(customer) and free_plan: + # free_plan = free_plan[0] + # self.subscribe(free_plan['id'],email) + # info = customers #self.me['customer'] + # subscriptions = info['subscriptions']['data'] + # ids = [plan['id'] for plan in self.plans ] + # _found = None + # for sub in subscriptions : + # aplan = sub['plan'] + + # if set([aplan['id']]) & set(ids): + # _found = sub.to_dict_recursive() + # break + self.me["plan"] = self._has_plan(customer) #_found if _found else [] + # self.user_plan = _found if _found else None + def _has_plan(self,customer): + subscriptions = customer['subscriptions']['data'] ids = [plan['id'] for plan in self.plans ] _found = None for sub in subscriptions : aplan = sub['plan'] if set([aplan['id']]) & set(ids): - _found = sub.to_dict_recursive() + _found = sub.to_dict_recursive()['plan'] + if 'features' in _found['metadata'] : + _found ['metadata']['features'] = json.loads(_found['metadata']['features']) break - self.me["plan"] = _found if _found else [] - # self.user_plan = _found if _found else None + return _found if _found else [] class Store : def __init__(self,**args): """ @@ -136,7 +153,68 @@ class Store : :email """ pass +class Product(Store): + """ + This class implements a wrapper around products, services or just simple donations + we are inheriting product, plans + """ + def __init__(self,**args): + Store.__init__(self,**args) + + def set(self,key,value): + if key == 'plan' : + # + # do we have this plan + found = [item for item in self.plans if item['id'] == value['id']] + if not found : + plan = value + else: + plan = dict(found[0],**value) + if 'metadata' in value : + plan['metadata'] = self.meta(plan,value) + self.save('plan',plan) + + pass + else : + if key in ['type','name','id','statement_descriptor'] : + if value : + self.product[key] = value + elif key in self.product : + del self.product[key] + elif key == 'metadata' : + self.product['metadata'] = self.meta(self.product,value) + self.save('product',self.product) + + def meta(self,object,value) : + + for k in value : + if value[k] : + object['metadata'][k] = json.dumps(value[k]) if type(value[k]) == dict else value[k] + else: + del object['metadata'][k] + return object + + def save(self,id,object): + pointer = None + if id == 'product' : + p = 'about' in object['metadata'] + q = 'uri' in object['metadata'] + if p and q : + pointer = stripe.product.Product + return 1 + else : + return 0 + elif self.product['id']: + pointer = stripe.plan.Plan + object = dict(object,**{"product":{"name":self.product['id']}} ) + if pointer : + pointer.create(**object) + return 1 + return 0 + + def get(self): + pass class Plans(Store) : def __init__(self,**args): """ @@ -193,14 +271,24 @@ class Plans(Store) : return 1 pass - def subscribe(self,id,email): + # def subscribe(self,id,email): + def subscribe(self,**args): """ Subscribe a user to a given plan assuming the user has already been initialized The responsibility falls upon the calling code to perform the cancellatioin of the existing plan and perform an upgrade + :email user's email + :id plan id (optional) will default to free plan """ + free_plan = [item['id'] for item in self.plans if item['amount'] == 0] + if free_plan : + free_plan = free_plan[0] + # id = args['id'] if 'id' in args else free_plan + email = args['email'] if not self.user : self.user = User(email,self.plans) - plan = {"plan": id} + + + plan = {"plan": args['id'] if 'id' in args else free_plan} amount = sum([item['amount'] for item in self.plans if item['id'] == 0]) if 'email' in self.user.info() and email == self.user.info()['email'] : @@ -210,17 +298,24 @@ class Plans(Store) : # # We must insure the user is one of our customers i.e perhaps if amount == 0 or self.payment : - stripe.subscription.Subscription.create( - customer=self.user.info()['id'], #if not email else email, - items=[plan] + r = stripe.subscription.Subscription.create( + customer = self.user.info()['id'], + items = [plan] ) + # stripe.subscription.Subscription.create( + # customer=self.user.info()['id'], #if not email else email, + # items=[plan] + # ) + self.user.init(email) return 1 else: return 0 class factory: @staticmethod def instance (**args) : - return Plans(**args) + p = Plans(**args) + # p.user.subscribe = p.subscribe + return p # if 'email' in args : # store = Plans(**args) diff --git a/src/api/templates/card.html b/src/api/templates/card.html new file mode 100644 index 0000000..f977d49 --- /dev/null +++ b/src/api/templates/card.html @@ -0,0 +1,10 @@ + + + +
+
+ {% for i in range(12) %} + {% endfor %} +
+
+ \ No newline at end of file diff --git a/src/api/templates/index.html b/src/api/templates/index.html new file mode 100644 index 0000000..6f88c98 --- /dev/null +++ b/src/api/templates/index.html @@ -0,0 +1,50 @@ + + + + + + + + + + + +Store + + +
+ {% for item in products %} +
+ +
+

+ {{item.name.replace('-',' ')}} +

+
+
+

+ {{item.metadata.about}} +

+ +
+
+ {% endfor %} +
+ diff --git a/src/api/templates/plans.html b/src/api/templates/plans.html index 908503a..11fa07e 100755 --- a/src/api/templates/plans.html +++ b/src/api/templates/plans.html @@ -14,18 +14,98 @@ + {{product.replace('-',' ')}} @@ -55,7 +135,7 @@ {% for item in plans%}
-
{{ item.nickname }}
+
{{ item.nickname }}
{% if item.id in active_plans %}
{% else %} @@ -76,7 +156,7 @@
- {% for key,value in item.metadata['features'].iteritems() %} + {% for key,value in item.metadata['features'].items() %}
{{ key }}
{% if value == True %} @@ -93,9 +173,22 @@

-

- Signup Now -
+
+
Signup Now
+ + {% if item.amount == 0%} +
+ Free +
+ + {%else%} +
+ $ {{ item.amount/100 }} + / {{item.interval[:]}} + +
+ {% endif %} +

@@ -108,11 +201,13 @@
+