Skip to content

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

Use payment built-in methods for validation

Unknowne21d91f7f526
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 51eff9419c..f1bd639c1f 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -259,7 +259,6 @@ class BasePaymentForm(forms.Form):
max_digits=settings.DEFAULT_MAX_DIGITS,
decimal_places=settings.DEFAULT_DECIMAL_PLACES)
- clean_status = tuple()
clean_error = pgettext_lazy(
'Payment form error',
'This payment action can not be performed.')
@@ -268,10 +267,6 @@ class BasePaymentForm(forms.Form):
self.payment = kwargs.pop('payment')
super().__init__(*args, **kwargs)
- def clean(self):
- if self.payment.charge_status not in self.clean_status:
- raise forms.ValidationError(self.clean_error)
-
def payment_error(self, message):
self.add_error(
None, pgettext_lazy(
@@ -289,27 +284,28 @@ class BasePaymentForm(forms.Form):
class CapturePaymentForm(BasePaymentForm):
- clean_status = (ChargeStatus.NOT_CHARGED, )
clean_error = pgettext_lazy(
'Payment form error',
'Only pre-authorized payments can be captured')
+ def clean(self):
+ if not self.payment.can_capture():
+ raise forms.ValidationError(self.clean_error)
+
def capture(self):
return self.try_payment_action(gateway_capture)
class RefundPaymentForm(BasePaymentForm):
- clean_status = (
- ChargeStatus.PARTIALLY_CHARGED,
- ChargeStatus.FULLY_CHARGED,
- ChargeStatus.PARTIALLY_REFUNDED)
clean_error = pgettext_lazy(
'Payment form error',
'Only confirmed payments can be refunded')
def clean(self):
- super().clean()
+ if not self.payment.can_refund():
+ raise forms.ValidationError(self.clean_error)
+
if self.payment.gateway == CustomPaymentChoices.MANUAL:
raise forms.ValidationError(
pgettext_lazy(
@@ -322,7 +318,6 @@ class RefundPaymentForm(BasePaymentForm):
class VoidPaymentForm(BasePaymentForm):
- clean_status = (ChargeStatus.NOT_CHARGED, )
clean_error = pgettext_lazy(
'Payment form error',
'Only pre-authorized payments can be voided')
@@ -334,6 +329,10 @@ class VoidPaymentForm(BasePaymentForm):
# since there is no amount argument for void operation
self.fields.pop('amount')
+ def clean(self):
+ if not self.payment.can_void():
+ raise forms.ValidationError(self.clean_error)
+
def void(self):
try:
gateway_void(self.payment)