Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Move error handling to view

Marcin Gębala632d1bbbf9dc
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index b3741c27c7..11de3205d1 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -1,6 +1,5 @@
from django import forms
from django.utils.translation import ugettext_lazy as _
-from payments import PaymentError
from satchless.item import InsufficientStock
from ...cart.forms import QuantityField
@@ -35,35 +34,13 @@ class ManagePaymentForm(forms.Form):
self.payment = kwargs.pop('payment')
super(ManagePaymentForm, self).__init__(*args, **kwargs)
- def save(self, action, user):
- success_msg = None
- error_msg = _('Payment gateway error: ')
+ def handle_action(self, action, user):
if action == 'capture' and self.payment.status == 'preauth':
- try:
- self.payment.capture(
- amount=self.cleaned_data['amount'], user=user)
- except PaymentError as e:
- error_msg += e.message
- else:
- success_msg = _('Funds captured')
+ self.payment.capture(amount=self.cleaned_data['amount'], user=user)
elif action == 'refund' and self.payment.status == 'confirmed':
- try:
- self.payment.refund(
- amount=self.cleaned_data['amount'], user=user)
- except PaymentError as e:
- error_msg += e.message
- except ValueError as e:
- error_msg = e.message
- else:
- success_msg = _('Refund successful')
+ self.payment.refund(amount=self.cleaned_data['amount'], user=user)
elif action == 'release' and self.payment.status == 'preauth':
- try:
- self.payment.release(user=user)
- except PaymentError as e:
- error_msg += e.message
- else:
- success_msg = _('Release successful')
- return success_msg, error_msg
+ self.payment.release(user=user)
class MoveItemsForm(forms.Form):
diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.py
index f355e9d96e..d83522a654 100644
--- a/saleor/dashboard/order/urls.py
+++ b/saleor/dashboard/order/urls.py
@@ -12,7 +12,7 @@ urlpatterns = patterns(
views.address_view, name='address-billing-edit'),
url(r'^(?P<order_pk>[0-9]+)/address-shipping/(?P<group_pk>[0-9]+)/$',
views.address_view, name='address-shipping-edit'),
- url(r'^(?P<pk>[0-9]+)/payment/$',
+ url(r'^(?P<pk>[0-9]+)/payment$',
views.manage_payment, name='manage-payment'),
url(r'^line/(?P<pk>[0-9]+)/$', views.edit_order_line,
name='order-line-edit'),
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 219525c7a7..66dac6ed66 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -5,6 +5,7 @@ from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.translation import ugettext_lazy as _
from django.views.generic import ListView
+from payments import PaymentError
from prices import Price
from ...order.models import Order, OrderedItem, DeliveryGroup, Payment
@@ -71,11 +72,14 @@ def manage_payment(request, pk):
form = ManagePaymentForm(request.POST or None, payment=payment)
if form.is_valid():
action = form.cleaned_data['action']
- success, error = form.save(action, request.user)
- if success:
- messages.success(request, success)
- elif error:
- messages.error(request, error)
+ try:
+ form.handle_action(action, request.user)
+ except PaymentError as e:
+ messages.error(request, _('Payment gateway error: ') + e.message)
+ except ValueError as e:
+ messages.error(request, _('Payment gateway error: ') + e.message)
+ else:
+ messages.success(request, _('Success'))
return redirect('dashboard:order-details', pk=payment.order.pk)
if request.is_ajax():
action = request.GET['action']