Fix so that credit card issues are displayed to the user
This commit is contained in:
parent
c7355f5509
commit
b0ac7883e3
6 changed files with 99 additions and 21 deletions
|
@ -1283,7 +1283,7 @@ def subscription_view(stripe_subscription, used_repos):
|
|||
@api_login_required
|
||||
def get_user_card_api():
|
||||
user = current_user.db_user()
|
||||
return jsonify(get_card(user))
|
||||
return get_card(user)
|
||||
|
||||
|
||||
@app.route('/api/organization/<orgname>/card', methods=['GET'])
|
||||
|
@ -1292,7 +1292,7 @@ def get_org_card_api(orgname):
|
|||
permission = AdministerOrganizationPermission(orgname)
|
||||
if permission.can():
|
||||
organization = model.get_organization(orgname)
|
||||
return jsonify(get_card(organization))
|
||||
return get_card(organization)
|
||||
|
||||
abort(403)
|
||||
|
||||
|
@ -1302,7 +1302,7 @@ def get_org_card_api(orgname):
|
|||
def set_user_card_api():
|
||||
user = current_user.db_user()
|
||||
token = request.get_json()['token']
|
||||
return jsonify(set_card(user, token))
|
||||
return set_card(user, token)
|
||||
|
||||
|
||||
@app.route('/api/organization/<orgname>/card', methods=['POST'])
|
||||
|
@ -1318,13 +1318,14 @@ def set_org_card_api(orgname):
|
|||
|
||||
|
||||
def set_card(user, token):
|
||||
print token
|
||||
|
||||
if user.stripe_id:
|
||||
cus = stripe.Customer.retrieve(user.stripe_id)
|
||||
if cus:
|
||||
cus.card = token
|
||||
cus.save()
|
||||
try:
|
||||
cus.card = token
|
||||
cus.save()
|
||||
except stripe.CardError as e:
|
||||
return carderror_response(e)
|
||||
|
||||
return get_card(user)
|
||||
|
||||
|
@ -1351,7 +1352,7 @@ def get_card(user):
|
|||
'last4': card.last4
|
||||
}
|
||||
|
||||
return {'card': card_info}
|
||||
return jsonify({'card': card_info})
|
||||
|
||||
@app.route('/api/user/plan', methods=['PUT'])
|
||||
@api_login_required
|
||||
|
@ -1363,6 +1364,14 @@ def subscribe_api():
|
|||
return subscribe(user, plan, token, USER_PLANS)
|
||||
|
||||
|
||||
def carderror_response(e):
|
||||
resp = jsonify({
|
||||
'carderror': e.message,
|
||||
})
|
||||
resp.status_code = 402
|
||||
return resp
|
||||
|
||||
|
||||
def subscribe(user, plan, token, accepted_plans):
|
||||
plan_found = None
|
||||
for plan_obj in accepted_plans:
|
||||
|
@ -1387,9 +1396,13 @@ def subscribe(user, plan, token, accepted_plans):
|
|||
# They want a real paying plan, 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()
|
||||
|
||||
try:
|
||||
cus = stripe.Customer.create(email=user.email, plan=plan, card=card)
|
||||
user.stripe_id = cus.id
|
||||
user.save()
|
||||
except stripe.CardError as e:
|
||||
return carderror_response(e)
|
||||
|
||||
response_json = subscription_view(cus.subscription, private_repos)
|
||||
status_code = 201
|
||||
|
@ -1405,12 +1418,17 @@ def subscribe(user, plan, token, accepted_plans):
|
|||
cus.save()
|
||||
|
||||
else:
|
||||
cus.plan = plan
|
||||
# User may have been a previous customer who is resubscribing
|
||||
if token:
|
||||
cus.card = token
|
||||
|
||||
cus.save()
|
||||
cus.plan = plan
|
||||
|
||||
try:
|
||||
cus.save()
|
||||
except stripe.CardError as e:
|
||||
return carderror_response(e)
|
||||
|
||||
response_json = subscription_view(cus.subscription, private_repos)
|
||||
|
||||
resp = jsonify(response_json)
|
||||
|
|
Reference in a new issue