forked from CGM_Public/pretix_original
Improve handling of Stripe exceptions (closes #50)
This commit is contained in:
@@ -60,25 +60,42 @@ class Stripe(BasePaymentProvider):
|
|||||||
|
|
||||||
def checkout_perform(self, request, order) -> str:
|
def checkout_perform(self, request, order) -> str:
|
||||||
self._init_api()
|
self._init_api()
|
||||||
charge = stripe.Charge.create(
|
try:
|
||||||
amount=int(order.total * 100),
|
charge = stripe.Charge.create(
|
||||||
currency=request.event.currency.lower(),
|
amount=int(order.total * 100),
|
||||||
source=request.session['payment_stripe_token'],
|
currency=request.event.currency.lower(),
|
||||||
idempotency_key=self.event.identity + order.code # TODO: Use something better
|
source=request.session['payment_stripe_token'],
|
||||||
)
|
idempotency_key=self.event.identity + order.code # TODO: Use something better
|
||||||
logging.info(charge)
|
)
|
||||||
if charge.status == 'succeeded' and charge.paid:
|
except stripe.error.CardError as e:
|
||||||
try:
|
err = e.json_body['error']
|
||||||
order.mark_paid('paypal', str(charge))
|
messages.error(request, _('Stripe reported an error with your card: %s' % err['message']))
|
||||||
messages.success(request, _('We successfully received your payment. Thank you!'))
|
logger.info('Stripe card error: %s' % str(err))
|
||||||
except Quota.QuotaExceededException as e:
|
except stripe.error.InvalidRequestError or stripe.error.AuthenticationError or stripe.error.APIConnectionError \
|
||||||
messages.error(request, str(e))
|
as e:
|
||||||
messages.success(request, _('We successfully received your payment. Thank you!'))
|
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:
|
else:
|
||||||
messages.warning(request, _('Stripe reported an error: %s' % charge.failure_message))
|
logger.info(charge)
|
||||||
order = order.clone()
|
if charge.status == 'succeeded' and charge.paid:
|
||||||
order.payment_info = str(charge)
|
try:
|
||||||
order.save()
|
order.mark_paid('paypal', str(charge))
|
||||||
|
messages.success(request, _('We successfully received your payment. Thank you!'))
|
||||||
|
except Quota.QuotaExceededException as e:
|
||||||
|
messages.error(request, str(e))
|
||||||
|
messages.success(request, _('We successfully received your payment. Thank you!'))
|
||||||
|
else:
|
||||||
|
messages.warning(request, _('Stripe reported an error: %s' % charge.failure_message))
|
||||||
|
order = order.clone()
|
||||||
|
order.payment_info = str(charge)
|
||||||
|
order.save()
|
||||||
|
|
||||||
def order_pending_render(self, request, order) -> str:
|
def order_pending_render(self, request, order) -> str:
|
||||||
template = get_template('pretixplugins/stripe/pending.html')
|
template = get_template('pretixplugins/stripe/pending.html')
|
||||||
@@ -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. '
|
||||||
|
|||||||
Reference in New Issue
Block a user