diff --git a/src/Domain.py b/src/Domain.py index 8028aca..0eabf9e 100644 --- a/src/Domain.py +++ b/src/Domain.py @@ -12,6 +12,7 @@ class User: self.stripe = stripe; self.stripeToken= stripeToken self.user = None + """ This function will cast an object to JSON, It is designed to address an inherent limitation of stripe object hierarchy @@ -79,8 +80,7 @@ class User: # call stripe to retrieve subscriptions for this user ... # if self.user : - r = stripe.Customer.retrieve(self.user['id']) - + r = stripe.Customer.retrieve(self.user['id']) return r.subscriptions else: return [] @@ -97,6 +97,10 @@ class User: customer=id, plan=plan['id'] ) ; + # + # We need to create an invoiced item out of this plan + # We will attach it later to an invoice ... + # r.append(x) return r """ @@ -118,6 +122,11 @@ class User: self.db.save_doc(user) ; self.user = user + def invoice(self,uid,plans,iid): + self.user = self.db.get(uid) + id = self.user['id'] + r = [ stripe.InvoiceItem.create(customer=id,amount=item['amount'],currency=item['currency'],description=item['statement_descriptor'],subscription=item['subscription'],invoice=iid) for item in plans] + return r """ This function creates an invoice @pre : an item with amount > 0 and status past_due @@ -125,26 +134,28 @@ class User: def get_invoices(self,uid): info = self.db.get(uid) id = info['id'] - return stripe.Invoice.list(customer=id) + #return stripe.Invoice.list(customer=id) """ This function will clear a particular invoice (hopefully). No partial payments should be accepted @pre : card,invoice """ - def charge(self,uid,amount): + def charge(self,uid,token,plans): + info = self.db.get(uid) id = info['id'] - uid = info['_id'] - invoices= self.get_invoices(uid) - index = -1 - for invoice in invoices : - if invoice.paid == False and invoice.amount_due == amount and amount > 0: - index = info['invoices'].index(ii) - invoice.pay() - del info['invoices'][index] - self.db.save_doc(info) - break - pass; + sid = plans[0]['subscription'] + user = stripe.Customer.retrieve(id) + user.source = token + user.save() + for plan in plans: + sid = plan['subscription'] + if plan['amount'] > 0 : + print [' *** ',plan['amount']] + _invoice= stripe.Invoice.create(customer=id,subscription=sid) + r = _invoice.pay() + print r + """ This function is designed to determine if the user exists or not We will check the couchdb and the stripe backend diff --git a/src/api/index.py b/src/api/index.py index 936fc0c..2b6006e 100644 --- a/src/api/index.py +++ b/src/api/index.py @@ -66,15 +66,28 @@ def subscribe(app_name): """ @app.route('/get/info/',methods=['GET']) def get_plan_info(app_name) : - uid = request.headers['uid'] - pid = request.headers['pid'] if 'pid' in request.headers else None - couchdb = CouchdbReader(uri=CONFIG['couchdb']['uri'],dbname=app_name,uid=uid,create=False) - info = couchdb.read() - lsub = info['subscriptions'] - - plans = [ sub['plan'] for sub in lsub if sub['ended_at'] is None ] - if pid is not None : - plans = [item['metadata'] for item in plans if item['id'] == pid] + plans = [] + print ' *** ','uid' in request.headers + if 'uid' in request.headers : + uid = request.headers['uid'] + pid = request.headers['pid'] if 'pid' in request.headers else None + couchdb = CouchdbReader(uri=CONFIG['couchdb']['uri'],dbname=app_name,uid=uid,create=False) + info = couchdb.read() + lsub = info['subscriptions'] + + plans = [ sub['plan'] for sub in lsub if sub['ended_at'] is None ] + if pid is not None : + plans = [item['metadata'] for item in plans if item['id'] == pid] + else: + # + # This function returns the plans for a given application + # We assume the application name is the prefix of the plan identifier in stripe + # + plans = stripe.Plan.list(limit=10) + handler = Domain.User(None) + plans = handler.cast(plans.data) + + plans = [item for item in plans if re.match(app_name,item['name'])] return json.dumps(plans) @app.route('/get/sub/') def get_sub_info(app_name): @@ -94,7 +107,21 @@ def get_sub_info(app_name): else: subs = lsub return json.dumps(subs) +@app.route('/get/plans/') +def get_plans(app_name) : + apikey = CONFIG['stripe']['pub'].strip() + # + # This function returns the plans for a given application + # We assume the application name is the prefix of the plan identifier in stripe + # + uid = session['uid'] if 'uid' in session else 'nyemba@gmail.com' + plans = stripe.Plan.list(limit=10) + handler = Domain.User(None) + plans = handler.cast(plans.data) + plans = [item for item in plans if re.match(app_name,item['name'])] + return render_template('subscribe.html',uid=uid,app_name=app_name,plans=plans,apikey=apikey) + @app.route('/subscribe/',methods=['DELETE']) def cancel_subscribe(name) : pass @@ -130,7 +157,7 @@ def is_customer (app_name): # bill['count'] = len(amount) # bill['amount']= sum(amount) html = """ -
+ +
+ + {% else %} +
Free
+ {% endif %} + + + {% endfor %} + + +
+ + + \ No newline at end of file