Checkpointing stripe work.
This commit is contained in:
parent
211fd6bcd7
commit
7bd18c1bab
11 changed files with 172 additions and 6 deletions
|
@ -1,4 +1,5 @@
|
|||
import logging
|
||||
import stripe
|
||||
|
||||
from flask import request, make_response, jsonify, abort
|
||||
from flask.ext.login import login_required, current_user
|
||||
|
@ -317,3 +318,53 @@ def delete_permissions(namespace, repository, username):
|
|||
return make_response('Deleted', 204)
|
||||
|
||||
abort(403) # Permission denied
|
||||
|
||||
|
||||
def subscription_view(stripe_subscription):
|
||||
return {
|
||||
'current_period_start': stripe_subscription.current_period_start,
|
||||
'current_period_end': stripe_subscription.current_period_end,
|
||||
'plan': stripe_subscription.plan.id,
|
||||
}
|
||||
|
||||
|
||||
@app.route('/api/user/plan', methods=['PUT'])
|
||||
@api_login_required
|
||||
def subscribe():
|
||||
# Amount in cents
|
||||
amount = 500
|
||||
|
||||
request_data = request.get_json()
|
||||
plan = request_data['plan']
|
||||
card = request_data['token']
|
||||
|
||||
user = current_user.db_user
|
||||
|
||||
if not user.stripe_id:
|
||||
# Create the customer and plan simultaneously
|
||||
cus = stripe.Customer.create(email=user.email, plan=plan, card=card)
|
||||
user.stripe_id = cus.id
|
||||
user.save()
|
||||
|
||||
resp = jsonify(subscription_view(cus.subscription))
|
||||
resp.status_code = 201
|
||||
return resp
|
||||
|
||||
else:
|
||||
# Change the plan
|
||||
cus = stripe.Customer.retrieve(user.stripe_id)
|
||||
cus.plan = plan
|
||||
cus.save()
|
||||
return jsonify(subscription_view(cus.subscription))
|
||||
|
||||
|
||||
@app.route('/api/user/plan', methods=['GET'])
|
||||
@api_login_required
|
||||
def get_subscription():
|
||||
user = current_user.db_user
|
||||
|
||||
if user.stripe_id:
|
||||
cus = stripe.Customer.retrieve(user.stripe_id)
|
||||
return jsonify(subscription_view(cus.subscription))
|
||||
|
||||
abort(404)
|
||||
|
|
Reference in a new issue