Improve handling of Stripe exceptions (closes #50)

This commit is contained in:
Raphael Michel
2015-04-15 23:30:35 +02:00
parent 90d6078f85
commit 3c827be4b1

View File

@@ -60,13 +60,30 @@ class Stripe(BasePaymentProvider):
def checkout_perform(self, request, order) -> str: def checkout_perform(self, request, order) -> str:
self._init_api() self._init_api()
try:
charge = stripe.Charge.create( charge = stripe.Charge.create(
amount=int(order.total * 100), amount=int(order.total * 100),
currency=request.event.currency.lower(), currency=request.event.currency.lower(),
source=request.session['payment_stripe_token'], source=request.session['payment_stripe_token'],
idempotency_key=self.event.identity + order.code # TODO: Use something better idempotency_key=self.event.identity + order.code # TODO: Use something better
) )
logging.info(charge) except stripe.error.CardError as e:
err = e.json_body['error']
messages.error(request, _('Stripe reported an error with your card: %s' % err['message']))
logger.info('Stripe card error: %s' % str(err))
except stripe.error.InvalidRequestError or stripe.error.AuthenticationError or stripe.error.APIConnectionError \
as e:
err = e.json_body['error']
messages.error(request, _('We had trouble communicating with Stripe. Please try again and get in touch '
'with us if this problem persists.'))
logger.error('Stripe error: %s' % str(err))
except stripe.error.StripeError as e:
err = e.json_body['error']
messages.error(request, _('We had trouble processing the payment. Please try again and get '
'in touch with us if this problem persists.'))
logger.error('Stripe error: %s' % str(err))
else:
logger.info(charge)
if charge.status == 'succeeded' and charge.paid: if charge.status == 'succeeded' and charge.paid:
try: try:
order.mark_paid('paypal', str(charge)) order.mark_paid('paypal', str(charge))
@@ -118,6 +135,12 @@ class Stripe(BasePaymentProvider):
ch = stripe.Charge.retrieve(payment_info['id']) ch = stripe.Charge.retrieve(payment_info['id'])
ch.refunds.create() ch.refunds.create()
ch.refresh() ch.refresh()
except stripe.error.InvalidRequestError or stripe.error.AuthenticationError or stripe.error.APIConnectionError \
as e:
err = e.json_body['error']
messages.error(request, _('We had trouble communicating with Stripe. Please try again and contact '
'support if the problem persists.'))
logger.error('Stripe error: %s' % str(err))
except stripe.error.StripeError: except stripe.error.StripeError:
order.mark_refunded() order.mark_refunded()
messages.warning(request, _('We were unable to transfer the money back automatically. ' messages.warning(request, _('We were unable to transfer the money back automatically. '