Allow user to unsubscribe and change their plan.

This commit is contained in:
yackob03 2013-10-02 02:05:53 -04:00
parent da8eccef11
commit 35c1e6e53b
3 changed files with 65 additions and 8 deletions

View file

@ -365,13 +365,13 @@ def subscribe():
request_data = request.get_json()
plan = request_data['plan']
card = request_data['token']
user = current_user.db_user
private_repos = model.get_private_repo_count(user.username)
if not user.stripe_id:
# Create the customer and plan simultaneously
card = request_data['token']
cus = stripe.Customer.create(email=user.email, plan=plan, card=card)
user.stripe_id = cus.id
user.save()
@ -384,6 +384,11 @@ def subscribe():
# Change the plan
cus = stripe.Customer.retrieve(user.stripe_id)
cus.plan = plan
# User may have been a previous customer who is resubscribing
if 'token' in request_data:
cus.card = request_data['token']
cus.save()
return jsonify(subscription_view(cus.subscription, private_repos))
@ -399,3 +404,18 @@ def get_subscription():
return jsonify(subscription_view(cus.subscription, private_repos))
abort(404)
@app.route('/api/user/plan', methods=['DELETE'])
@api_login_required
def cancel_subscription():
user = current_user.db_user
if user.stripe_id:
private_repos = model.get_private_repo_count(user.username)
cus = stripe.Customer.retrieve(user.stripe_id)
cus.cancel_subscription()
return make_response('Deleted', 204)
abort(404)