diff --git a/src/Domain.py b/src/Domain.py index 93c5307..f94f9d0 100644 --- a/src/Domain.py +++ b/src/Domain.py @@ -64,11 +64,11 @@ class User: pass # self.hasPlan(uid,plan) has_plan = True - if self.user is None and plans and stripe : + if id is None : # # First time customer, register them and sign them up for the first plan # - customer['sources'] = str(self.stripeToken) + #customer['sources'] = str(self.stripeToken) customer = self.stripe.Customer.create( source=self.stripeToken, email=uid @@ -81,6 +81,12 @@ class User: has_plan = False id = customer['id'] #self.user = {"_id":uid,"id":id,"source":self.stripeToken} + # + # At this point we have a new user and a plan identifier + # We formt the plan as it is given to us as a string ... later will be subscribed. + # + plans = [{"id":plans}] + self.user = {"_id":id,"emails":[uid],"source":self.stripeToken} else: # @@ -96,19 +102,22 @@ class User: # We perform a set operation to determine if she is alread susbscribed # lsub = self.subscriptions() - lplans = [str(item['plan']['id']) for item in lsub.data if item.ended_at in [None,""]] - x_plans = [item['id'] for item in plans] - - if lplans and not set(x_plans) - set(lplans) : - has_plans = False - x = list(set(x_plans) - set(lplans)) - plans = [ item for item in plans if item.id in x] - else: - # - # In case the user doesn't have any plans with us - # - has_plan = False + if len(lsub.data) > 0 : + lplans = [str(item['plan']['id']) for item in lsub.data if item.ended_at in [None,""]] + x_plans = [item['id'] for item in plans] + + if lplans and not set(x_plans) - set(lplans) : + has_plans = False + x = list(set(x_plans) - set(lplans)) + plans = [ item for item in plans if item.id in x] + else: + # + # In case the user doesn't have any plans with us + # + has_plan = False + + if has_plan == False : r = self.subscribe(id,plans) lsub.data.append(r[0]) @@ -142,6 +151,7 @@ class User: def subscribe(self,id,plans): r = [] for plan in plans: + print ' --- >> ' plan if self.stripeToken is None: sub = self.stripe.Subscription.create( customer=id, diff --git a/src/api/index.py b/src/api/index.py index 39622c7..06f8352 100644 --- a/src/api/index.py +++ b/src/api/index.py @@ -40,7 +40,7 @@ def init(app_name): if 'uid' in request.headers: uid = request.headers['uid'] plan= request.headers['pid'] - couchdb = Couchdb(uri=CONFIG['couchdb']['uri'],dbname=app_name,uid=None) + couchdb = Couchdb(uri=CONFIG['couchdb']['uri'],dbname=app_name,uid=uid) DB = couchdb.dbase handler = Domain.User(DB,stripe) ; if 'customer.id' not in session :