Fix #248 -- Failed Payment error handling (#333)

* [WIP] Failed Payment error handling

When finished, this should fix #248

* rename PaymentFailedException to PaymentException\nimported Exception where neccessary

* comments fixed

* minor style fixes

* Fixed a name error
This commit is contained in:
Jakob Schnell
2017-02-24 14:11:41 +01:00
committed by Raphael Michel
parent 8e4b71eb19
commit c251a48e31
5 changed files with 43 additions and 26 deletions

View File

@@ -9,7 +9,7 @@ from django.template.loader import get_template
from django.utils.translation import ugettext_lazy as _
from pretix.base.models import Quota, RequiredAction
from pretix.base.payment import BasePaymentProvider
from pretix.base.payment import BasePaymentProvider, PaymentException
from pretix.base.services.mail import SendMailException
from pretix.base.services.orders import mark_order_paid, mark_order_refunded
from pretix.multidomain.urlreverse import build_absolute_uri
@@ -110,13 +110,14 @@ class Stripe(BasePaymentProvider):
else:
err = {'message': str(e)}
logger.exception('Stripe error: %s' % str(e))
messages.error(request, _('Stripe reported an error with your card: %s' % err['message']))
logger.info('Stripe card error: %s' % str(err))
order.payment_info = json.dumps({
'error': True,
'message': err['message'],
})
order.save()
raise PaymentException(_('Stripe reported an error with your card:%s') % err['message'])
except stripe.error.StripeError as e:
if e.json_body:
err = e.json_body['error']
@@ -124,33 +125,33 @@ class Stripe(BasePaymentProvider):
else:
err = {'message': str(e)}
logger.exception('Stripe error: %s' % str(e))
messages.error(request, _('We had trouble communicating with Stripe. Please try again and get in touch '
'with us if this problem persists.'))
order.payment_info = json.dumps({
'error': True,
'message': err['message'],
})
order.save()
raise PaymentException(_('We had trouble communicating with Stripe. Please try again and get in touch '
'with us if this problem persists.'))
else:
if charge.status == 'succeeded' and charge.paid:
try:
mark_order_paid(order, 'stripe', str(charge))
except Quota.QuotaExceededException as e:
messages.error(request, str(e))
RequiredAction.objects.create(
event=request.event, action_type='pretix.plugins.stripe.overpaid', data=json.dumps({
'order': order.code,
'charge': charge.id
})
)
except SendMailException:
messages.warning(request, _('There was an error sending the confirmation mail.'))
raise PaymentException(str(e))
except SendMailException:
raise PaymentException(_('There was an error sending the confirmation mail.'))
else:
messages.warning(request, _('Stripe reported an error: %s' % charge.failure_message))
logger.info('Charge failed: %s' % str(charge))
order.payment_info = str(charge)
order.save()
raise PaymentException(_('Stripe reported an error: %s') % charge.failure_message)
del request.session['payment_stripe_token']
def order_pending_render(self, request, order) -> str: