From 6467e24ba2b9cf4eaf086f7b4f234d5279e61136 Mon Sep 17 00:00:00 2001 From: yackob03 Date: Mon, 11 Nov 2013 19:02:42 -0500 Subject: [PATCH] All for free user to free business migration. --- endpoints/api.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/endpoints/api.py b/endpoints/api.py index 88305f213..bd558de72 100644 --- a/endpoints/api.py +++ b/endpoints/api.py @@ -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//plan', methods=['PUT'])