All for free user to free business migration.

This commit is contained in:
yackob03 2013-11-11 19:02:42 -05:00
parent 6be20ff626
commit 6467e24ba2

View file

@ -1190,7 +1190,8 @@ def subscribe_api():
token = request_data['token'] if 'token' in request_data else None
user = current_user.db_user()
return subscribe(user, plan, token, USER_PLANS)
def subscribe(user, plan, token, accepted_plans):
plan_found = None
for plan_obj in accepted_plans:
@ -1202,16 +1203,25 @@ def subscribe(user, plan, token, accepted_plans):
private_repos = model.get_private_repo_count(user.username)
if not user.stripe_id:
# Create the customer and plan simultaneously
card = token
cus = stripe.Customer.create(email=user.email, plan=plan, card=card)
user.stripe_id = cus.id
user.save()
# This is the default response
response_json = {
'plan': plan,
'usedPrivateRepos': private_repos,
}
status_code = 200
resp = jsonify(subscription_view(cus.subscription, private_repos))
resp.status_code = 201
return resp
if not user.stripe_id:
# Check if a non-paying user is trying to subscribe to a free plan
if not plan_found['price'] == 0:
# They want a real paying plan, create the customerand plan
# simultaneously
card = token
cus = stripe.Customer.create(email=user.email, plan=plan, card=card)
user.stripe_id = cus.id
user.save()
response_json = subscription_view(cus.subscription, private_repos)
status_code = 201
else:
# Change the plan
@ -1223,11 +1233,6 @@ def subscribe(user, plan, token, accepted_plans):
cus.cancel_subscription()
cus.save()
response_json = {
'plan': plan,
'usedPrivateRepos': private_repos,
}
else:
cus.plan = plan
# User may have been a previous customer who is resubscribing
@ -1237,7 +1242,9 @@ def subscribe(user, plan, token, accepted_plans):
cus.save()
response_json = subscription_view(cus.subscription, private_repos)
return jsonify(response_json)
resp = jsonify(response_json)
resp.status_code = status_code
return resp
@app.route('/api/organization/<orgname>/plan', methods=['PUT'])