Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Validate positive amount in mutations, move validation to decorator
Pacu26e90e2568a11
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.pyindex f8e596fa63..ae21ba3873 100644--- a/saleor/graphql/order/mutations/orders.py+++ b/saleor/graphql/order/mutations/orders.py@@ -317,8 +317,11 @@ class OrderCapture(BaseMutation):@classmethod@permission_required('order.manage_orders')def mutate(cls, root, info, id, amount):- # FIXME we should validate if amount is positive numbererrors = []+ if amount <= 0:+ cls.add_error('Amount should be a positive number.')+ return OrderCapture(errors=errors)+order = cls.get_node_or_error(info, id, errors, 'id', Order)payment = order.get_last_payment()clean_order_capture(payment, amount, errors)@@ -375,8 +378,11 @@ class OrderRefund(BaseMutation):@classmethod@permission_required('order.manage_orders')def mutate(cls, root, info, id, amount):- # FIXME we should validate if amount is positive numbererrors = []+ if amount <= 0:+ cls.add_error('Amount should be a positive number.')+ return OrderRefund(errors=errors)+order = cls.get_node_or_error(info, id, errors, 'id', Order)if order:payment = order.get_last_payment()diff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 717368a343..8cda6c9db0 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -40,10 +40,24 @@ def validate_payment_method(view):def func(payment_method: PaymentMethod, *args, **kwargs):if not payment_method.is_active:raise PaymentError('This payment method is no longer active.')- return view(payment_method, *args, **kwargs)+ return view(payment_method=payment_method, *args, **kwargs)return func+def validate_positive_amount(view):+ """Decorate a view to check if payment method is active, so any actions+ can be performed on it.+ """+ @wraps(view)+ def func(amount: Decimal, *args, **kwargs):+ if amount <= 0:+ raise PaymentError('Amount should be a positive number.')+ amount = Money(amount, currency=settings.DEFAULT_CURRENCY)+ return view(*args, amount=amount, **kwargs)+ return func+++def create_payment_method(**payment_data):payment_method, _ = PaymentMethod.objects.get_or_create(**payment_data)return payment_method@@ -91,12 +105,10 @@ def gateway_authorize(@validate_payment_method+@validate_positive_amountdef gateway_capture(payment_method: PaymentMethod,amount: Decimal) -> Transaction:- if amount <= 0:- raise PaymentError('Amount should be a positive number.')- amount = Money(amount, currency=settings.DEFAULT_CURRENCY)if payment_method.charge_status not in {ChargeStatus.CHARGED,ChargeStatus.NOT_CHARGED}:@@ -139,12 +151,10 @@ def gateway_void(payment_method) -> Transaction:@validate_payment_method+@validate_positive_amountdef gateway_refund(payment_method,amount: Decimal) -> Transaction:- if amount <= 0:- raise PaymentError('Amount should be a positive number.')- amount = Money(amount, currency=settings.DEFAULT_CURRENCY)if amount > payment_method.captured_amount:raise PaymentError('Cannot refund more than captured')if not payment_method.charge_status == ChargeStatus.CHARGED: