|
|
@ -12,6 +12,7 @@ from __future__ import division
|
|
|
|
from flask import Flask, request, session, render_template,Response
|
|
|
|
from flask import Flask, request, session, render_template,Response
|
|
|
|
from flask_cors import CORS
|
|
|
|
from flask_cors import CORS
|
|
|
|
import Domain
|
|
|
|
import Domain
|
|
|
|
|
|
|
|
from User import User
|
|
|
|
from couchdbkit import Server, Document
|
|
|
|
from couchdbkit import Server, Document
|
|
|
|
import stripe
|
|
|
|
import stripe
|
|
|
|
import json
|
|
|
|
import json
|
|
|
@ -37,6 +38,7 @@ stripe.api_key = stripe_keys['secret_key'].strip()
|
|
|
|
app = Flask(__name__)
|
|
|
|
app = Flask(__name__)
|
|
|
|
CORS(app)
|
|
|
|
CORS(app)
|
|
|
|
COUCHDB = Server(uri=CONFIG['couchdb']['uri']) ;
|
|
|
|
COUCHDB = Server(uri=CONFIG['couchdb']['uri']) ;
|
|
|
|
|
|
|
|
SYS_STORE = CONFIG['couchdb']
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
This function will set the user information to the session and update the information
|
|
|
|
This function will set the user information to the session and update the information
|
|
|
|
@header uid user email address
|
|
|
|
@header uid user email address
|
|
|
@ -49,18 +51,30 @@ def init(product):
|
|
|
|
@param uid user's email (primary)
|
|
|
|
@param uid user's email (primary)
|
|
|
|
@param pid plan identifier
|
|
|
|
@param pid plan identifier
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
email = request.headers['uid']
|
|
|
|
uid = request.headers['uid']
|
|
|
|
plan_id = request.headers['pid']
|
|
|
|
plan_id = request.headers['pid'] if 'pid' in request.headers else None
|
|
|
|
user = User(stripe=stripe,store=CONFIG['couchdb'],product=product)
|
|
|
|
user = User(stripe=stripe,store=CONFIG['couchdb'],product=product)
|
|
|
|
user.susbscribe(uid,plan_id)
|
|
|
|
user.subscribe(uid,plan_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub = None
|
|
|
|
if 'auid' in request.headers :
|
|
|
|
if 'auid' in request.headers :
|
|
|
|
auid = request.header['auid']
|
|
|
|
auid = request.header['auid']
|
|
|
|
user.update(emails=auid)
|
|
|
|
user.update(emails=auid)
|
|
|
|
user.post()
|
|
|
|
user.post()
|
|
|
|
features = json.loads(users.me['subscriptions'][pid][0]['metadata'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
features = user.get(uid,key)
|
|
|
|
store = dict(CONFIG['couchdb'],**{})
|
|
|
|
user.refresh()
|
|
|
|
store['dbname'] = product
|
|
|
|
|
|
|
|
store['uid'] = 'logs'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub = user.get(uid,'subscriptions')
|
|
|
|
|
|
|
|
features = {}
|
|
|
|
|
|
|
|
for id in sub :
|
|
|
|
|
|
|
|
if sub[id]['active'] is True :
|
|
|
|
|
|
|
|
features[id] = sub[id]
|
|
|
|
|
|
|
|
user.refresh(uid)
|
|
|
|
|
|
|
|
session['key'] = user.user_key
|
|
|
|
|
|
|
|
session['uid'] = uid
|
|
|
|
return json.dumps(features),200
|
|
|
|
return json.dumps(features),200
|
|
|
|
|
|
|
|
|
|
|
|
# @app.route("/init/<app_name>",methods=['POST'])
|
|
|
|
# @app.route("/init/<app_name>",methods=['POST'])
|
|
|
@ -109,16 +123,16 @@ def init(product):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
This function will update the user's email
|
|
|
|
This function will update the user's email
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
@app.route('/init/<app_name>',methods=['PUT'])
|
|
|
|
# @app.route('/init/<app_name>',methods=['PUT'])
|
|
|
|
def init_update(app_name):
|
|
|
|
# def init_update(app_name):
|
|
|
|
uid = request.headers['uid']
|
|
|
|
# uid = request.headers['uid']
|
|
|
|
id = session['customer.id']
|
|
|
|
# id = session['customer.id']
|
|
|
|
couchdb = Couchdb(uri=CONFIG['couchdb']['uri'],dbname=app_name,uid=None)
|
|
|
|
# couchdb = Couchdb(uri=CONFIG['couchdb']['uri'],dbname=app_name,uid=None)
|
|
|
|
DB = couchdb.dbase
|
|
|
|
# DB = couchdb.dbase
|
|
|
|
handler = Domain.User(DB,stripe)
|
|
|
|
# handler = Domain.User(DB,stripe)
|
|
|
|
handler.update_user(uid=uid,id=id)
|
|
|
|
# handler.update_user(uid=uid,id=id)
|
|
|
|
|
|
|
|
|
|
|
|
return ('',204)
|
|
|
|
# return ('',204)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -127,12 +141,13 @@ def init_update(app_name):
|
|
|
|
@resource name name of the application {cloud-music}
|
|
|
|
@resource name name of the application {cloud-music}
|
|
|
|
@header key service/plan
|
|
|
|
@header key service/plan
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
@app.route('/subscribe/<app_name>',methods=['POST'])
|
|
|
|
@app.route('/subscribe/<product>',methods=['POST'])
|
|
|
|
def subscribe(app_name):
|
|
|
|
def subscribe(product):
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# The name is the full name of the service
|
|
|
|
# The name is the full name of the service
|
|
|
|
#
|
|
|
|
#
|
|
|
|
resp = "0"
|
|
|
|
resp = "0"
|
|
|
|
|
|
|
|
user = User(stripe=stripe,store=CONFIG['couchdb'],product=product)
|
|
|
|
if 'stripeToken' in request.form :
|
|
|
|
if 'stripeToken' in request.form :
|
|
|
|
stripeToken = request.form['stripeToken']
|
|
|
|
stripeToken = request.form['stripeToken']
|
|
|
|
uid = request.form['stripeEmail']
|
|
|
|
uid = request.form['stripeEmail']
|
|
|
@ -141,43 +156,73 @@ def subscribe(app_name):
|
|
|
|
pid = request.form['plan']
|
|
|
|
pid = request.form['plan']
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
pid = request.headers['pid']
|
|
|
|
pid = request.headers['pid'] if 'pid' in request.headers else None
|
|
|
|
uid = request.headers['uid']
|
|
|
|
uid = request.headers['uid']
|
|
|
|
stripeToken = None
|
|
|
|
stripeToken = None
|
|
|
|
|
|
|
|
user.subscribe(uid,pid,stripeToken)
|
|
|
|
|
|
|
|
if 'auid' in request.headers :
|
|
|
|
|
|
|
|
|
|
|
|
plans = stripe.Plan.list().data
|
|
|
|
if request.headers['auid'].startswith('[') or request.headers['auid'].startswith("{") :
|
|
|
|
plan = [item for item in plans if item.id == pid]
|
|
|
|
auid = json.loads(request.headers['auid'])
|
|
|
|
resp = "0"
|
|
|
|
else:
|
|
|
|
|
|
|
|
auid = [request.headers['auid']]
|
|
|
|
couch_handler = Couchdb(uri=CONFIG['couchdb']['uri'],dbname=app_name,uid=uid)
|
|
|
|
user.update(emails=auid)
|
|
|
|
DB = couch_handler.dbase
|
|
|
|
user.refresh(uid)
|
|
|
|
handler = Domain.User(DB,stripe,stripeToken) ;
|
|
|
|
|
|
|
|
|
|
|
|
reader = CouchdbReader(uri=SYS_STORE['uri'],dbname=product,uid=uid,create=False)
|
|
|
|
if plan :
|
|
|
|
plans = reader.view('users/active_plan',key=user.user_key) #me['_id'])
|
|
|
|
handler.init(uid,plan)
|
|
|
|
#session['plans'] = plans
|
|
|
|
resp = plan[0].id
|
|
|
|
session['key'] = user.user_key
|
|
|
|
|
|
|
|
session['uid'] = uid
|
|
|
|
return ('',204)
|
|
|
|
|
|
|
|
|
|
|
|
return (json.dumps(plans),200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_plans(product) :
|
|
|
|
|
|
|
|
lproducts = stripe.Product.list()
|
|
|
|
|
|
|
|
lproducts = [item for item in lproducts.auto_paging_iter() if item.name == product ]
|
|
|
|
|
|
|
|
if lproducts :
|
|
|
|
|
|
|
|
product_id = lproducts[0].id
|
|
|
|
|
|
|
|
alias = lproducts[0].statement_descriptor
|
|
|
|
|
|
|
|
plans = stripe.Plan.list(product=product_id)
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
for plan in plans :
|
|
|
|
|
|
|
|
plan['product_alias'] = alias if alias is not None else ''
|
|
|
|
|
|
|
|
if 'features' in plan['metadata']:
|
|
|
|
|
|
|
|
plan['metadata']['features'] = json.loads(plan['metadata']['features'])
|
|
|
|
|
|
|
|
plans[i] = plan
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
if plans :
|
|
|
|
|
|
|
|
plans = plans.data
|
|
|
|
|
|
|
|
plans.sort(key = lambda x:x.amount)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return plans
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
@app.route('/features/<product>')
|
|
|
|
This function returns the features of a user for a given application
|
|
|
|
def features(product):
|
|
|
|
The features are returned provided the plan is still valid i.e (end_date,status={active|trailing})
|
|
|
|
"""
|
|
|
|
|
|
|
|
This function returns the plan/features of a user for a given application if provided a uid (email)
|
|
|
|
|
|
|
|
if no uid are provided then the function will return all the plans/features associated with the given product
|
|
|
|
@header uid user's email
|
|
|
|
@header uid user's email
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
@app.route('/features/<app_name>')
|
|
|
|
|
|
|
|
def features(app_name):
|
|
|
|
|
|
|
|
plans = []
|
|
|
|
plans = []
|
|
|
|
if 'uid' in request.headers :
|
|
|
|
if 'uid' in request.headers :
|
|
|
|
uid = request.headers['uid']
|
|
|
|
uid = request.headers['uid']
|
|
|
|
|
|
|
|
couchdb = CouchdbReader(uri=CONFIG['couchdb']['uri'],dbname=product,uid=uid,create=False)
|
|
|
|
couchdb = CouchdbReader(uri=CONFIG['couchdb']['uri'],dbname=app_name,uid=uid,create=False)
|
|
|
|
key = couchdb.view("users/uid_map",key=uid)
|
|
|
|
handler = Domain.User(couchdb.dbase,stripe)
|
|
|
|
if key :
|
|
|
|
handler.initialize(uid)
|
|
|
|
key = key[0]['value']
|
|
|
|
lsub = handler.subscriptions()
|
|
|
|
plans = couchdb.view('users/active_plan',key=key)
|
|
|
|
|
|
|
|
plans = [plan['value'] for plan in plans if 'value' in plan]
|
|
|
|
plans = [ sub['plan'] for sub in lsub if sub['ended_at'] is None ]
|
|
|
|
else:
|
|
|
|
plans = [item['metadata'] for item in plans if item['id']]
|
|
|
|
plans = []
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
plans = get_plans(product)
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
# formatting plans for the output
|
|
|
|
|
|
|
|
#
|
|
|
|
return json.dumps(plans)
|
|
|
|
return json.dumps(plans)
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
This function returns a user's plans/status for an application
|
|
|
|
This function returns a user's plans/status for an application
|
|
|
@ -199,48 +244,31 @@ def status(app_name):
|
|
|
|
It will allow user's to be able to signup for various plans
|
|
|
|
It will allow user's to be able to signup for various plans
|
|
|
|
@pre 'uid' in request.headers
|
|
|
|
@pre 'uid' in request.headers
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
@app.route('/signup/<app_name>')
|
|
|
|
@app.route('/signup/<product>')
|
|
|
|
def get_plans(app_name) :
|
|
|
|
def signup(product) :
|
|
|
|
apikey = CONFIG['stripe']['pub'].strip()
|
|
|
|
apikey = CONFIG['stripe']['pub'].strip()
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# This function returns the plans for a given application
|
|
|
|
# This function returns the plans for a given application
|
|
|
|
# We assume the application name is the prefix of the plan identifier in stripe
|
|
|
|
# We assume the application name is the prefix of the plan identifier in stripe
|
|
|
|
#
|
|
|
|
#
|
|
|
|
uid = session['uid'] if 'uid' in session else None
|
|
|
|
if 'user-info' in session :
|
|
|
|
|
|
|
|
_info = session['user-info']
|
|
|
|
|
|
|
|
session['uid'] = _info['uid']
|
|
|
|
|
|
|
|
uid = session['uid'] if 'uid' in session else ''
|
|
|
|
can_purchase = True
|
|
|
|
can_purchase = True
|
|
|
|
if uid is None :
|
|
|
|
if uid is None :
|
|
|
|
uid = request.args.get('uid') if 'uid' in request.args else None
|
|
|
|
uid = request.args.get('uid') if 'uid' in request.args else None
|
|
|
|
plans = stripe.Plan.list(limit=10)
|
|
|
|
plans = get_plans(product)
|
|
|
|
|
|
|
|
|
|
|
|
# plans = handler.cast(plans.data)
|
|
|
|
|
|
|
|
plans = json.loads(json.dumps(plans.data, sort_keys=True, indent=2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
plans = [item for item in plans if re.match(app_name,item['name'])]
|
|
|
|
|
|
|
|
if uid is not None:
|
|
|
|
|
|
|
|
couchdb = Couchdb(uri=CONFIG['couchdb']['uri'],dbname=app_name,uid=uid,create=False)
|
|
|
|
|
|
|
|
handler = Domain.User(couchdb.dbase,stripe)
|
|
|
|
|
|
|
|
if handler.getId(uid) is not None:
|
|
|
|
|
|
|
|
handler.init(uid,[])
|
|
|
|
|
|
|
|
subs = handler.subscriptions().data
|
|
|
|
|
|
|
|
ids = [item.id for item in subs]
|
|
|
|
|
|
|
|
for item in plans :
|
|
|
|
|
|
|
|
if item['id'] in ids :
|
|
|
|
|
|
|
|
item['active'] = True
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
# let's determine if this user can make a purchase
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# #
|
|
|
|
|
|
|
|
# This should address any case of the database of customers being out of sync
|
|
|
|
|
|
|
|
# @TODO: Have a log suggesting a utility imports the customer
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
return json.dumps(plans)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# @TODO: Mark the plans the current user is signed up for
|
|
|
|
# @TODO: Mark the plans the current user is signed up for
|
|
|
|
#
|
|
|
|
#
|
|
|
|
return render_template('subscribe.html',context=CONTEXT,uid=uid,app_name=app_name,plans=plans,apikey=apikey)
|
|
|
|
platform='web' if 'platform' not in request.args else request.args['platform']
|
|
|
|
|
|
|
|
alias = plans[0]['product_alias']
|
|
|
|
|
|
|
|
active_plan = session['plans'] if 'plans' in session else []
|
|
|
|
|
|
|
|
print [' ** ',uid,active_plan]
|
|
|
|
|
|
|
|
return render_template('subscribe.html',context=CONTEXT,uid=uid,alias=alias,platform=platform,app_name=product,plans=plans,apikey=apikey)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/subscribe/<name>',methods=['DELETE'])
|
|
|
|
@app.route('/subscribe/<name>',methods=['DELETE'])
|
|
|
|
def cancel_subscribe(name) :
|
|
|
|
def cancel_subscribe(name) :
|
|
|
@ -279,4 +307,5 @@ def is_customer (app_name):
|
|
|
|
if __name__ == '__main__' :
|
|
|
|
if __name__ == '__main__' :
|
|
|
|
app.debug = True ;
|
|
|
|
app.debug = True ;
|
|
|
|
app.secret_key = '360-8y-[0v@t10n]+kr81v17y'
|
|
|
|
app.secret_key = '360-8y-[0v@t10n]+kr81v17y'
|
|
|
|
app.run(port=PORT,threaded=True)
|
|
|
|
app.config['MAX_CONTENT_LENGTH'] = 1600 * 1024 * 1024
|
|
|
|
|
|
|
|
app.run(port=PORT,threaded=True,host='0.0.0.0')
|
|
|
|