forked from uncloud/uncloud
Revamp stripe error handling
This commit is contained in:
parent
4e658d2d77
commit
80fe28588e
4 changed files with 104 additions and 63 deletions
|
|
@ -17,6 +17,7 @@ CURRENCY = 'chf'
|
|||
stripe.api_key = uncloud.secrets.STRIPE_KEY
|
||||
|
||||
# Helper (decorator) used to catch errors raised by stripe logic.
|
||||
# Catch errors that should not be displayed to the end user, raise again.
|
||||
def handle_stripe_error(f):
|
||||
def handle_problems(*args, **kwargs):
|
||||
response = {
|
||||
|
|
@ -25,49 +26,38 @@ def handle_stripe_error(f):
|
|||
'error': None
|
||||
}
|
||||
|
||||
common_message = "Currently it is not possible to make payments."
|
||||
common_message = "Currently it is not possible to make payments. Please try agin later."
|
||||
try:
|
||||
response_object = f(*args, **kwargs)
|
||||
response = {
|
||||
'response_object': response_object,
|
||||
'error': None
|
||||
}
|
||||
return response
|
||||
return response_object
|
||||
except stripe.error.CardError as e:
|
||||
# Since it's a decline, stripe.error.CardError will be caught
|
||||
body = e.json_body
|
||||
err = body['error']
|
||||
response.update({'error': err['message']})
|
||||
logging.error(str(e))
|
||||
return response
|
||||
|
||||
raise e # For error handling.
|
||||
except stripe.error.RateLimitError:
|
||||
response.update(
|
||||
{'error': "Too many requests made to the API too quickly"})
|
||||
return response
|
||||
logging.error("Too many requests made to the API too quickly.")
|
||||
raise Exception(common_message)
|
||||
except stripe.error.InvalidRequestError as e:
|
||||
logging.error(str(e))
|
||||
response.update({'error': "Invalid parameters"})
|
||||
return response
|
||||
raise Exception('Invalid parameters.')
|
||||
except stripe.error.AuthenticationError as e:
|
||||
# Authentication with Stripe's API failed
|
||||
# (maybe you changed API keys recently)
|
||||
logging.error(str(e))
|
||||
response.update({'error': common_message})
|
||||
return response
|
||||
raise Exception(common_message)
|
||||
except stripe.error.APIConnectionError as e:
|
||||
logging.error(str(e))
|
||||
response.update({'error': common_message})
|
||||
return response
|
||||
raise Exception(common_message)
|
||||
except stripe.error.StripeError as e:
|
||||
# maybe send email
|
||||
# XXX: maybe send email
|
||||
logging.error(str(e))
|
||||
response.update({'error': common_message})
|
||||
return response
|
||||
raise Exception(common_message)
|
||||
except Exception as e:
|
||||
# maybe send email
|
||||
logging.error(str(e))
|
||||
response.update({'error': common_message})
|
||||
return response
|
||||
raise Exception(common_message)
|
||||
|
||||
return handle_problems
|
||||
|
||||
|
|
@ -82,14 +72,14 @@ def get_customer_id_for(user):
|
|||
return uncloud_pay.models.StripeCustomer.objects.get(owner=user).stripe_id
|
||||
except ObjectDoesNotExist:
|
||||
# No entry yet - making a new one.
|
||||
customer_request = create_customer(user.username, user.email)
|
||||
if customer_request['error'] == None:
|
||||
mapping = uncloud_pay.models.StripeCustomer.objects.create(
|
||||
try:
|
||||
customer = create_customer(user.username, user.email)
|
||||
uncloud_stripe_mapping = uncloud_pay.models.StripeCustomer.objects.create(
|
||||
owner=user,
|
||||
stripe_id=customer_request['response_object']['id']
|
||||
)
|
||||
return mapping.stripe_id
|
||||
else:
|
||||
return uncloud_stripe_mapping.stripe_id
|
||||
except Exception as e:
|
||||
return None
|
||||
|
||||
@handle_stripe_error
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue