Skip to content

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

Implement `can_charge` and implement few tests for `Payment.can_*`

NyanKiyoshidc885fdacafd
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 378944514a..b5c00114fa 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -218,6 +218,15 @@ class Order(models.Model):
OrderStatus.DRAFT, OrderStatus.CANCELED}
return payment.can_capture() and order_status_ok
+ def can_charge(self, payment=None):
+ if not payment:
+ payment = self.get_last_payment()
+ if not payment:
+ return False
+ order_status_ok = self.status not in {
+ OrderStatus.DRAFT, OrderStatus.CANCELED}
+ return payment.can_charge() and order_status_ok
+
def can_void(self, payment=None):
if not payment:
payment = self.get_last_payment()
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index a9fe565b98..10eea91a55 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -142,6 +142,9 @@ class Payment(models.Model):
and self.get_total() > self.get_captured_amount())
return self.is_active and not_charged or not_fully_charged
+ def can_charge(self):
+ return self.can_capture()
+
def can_void(self):
return (
self.is_active and self.charge_status == ChargeStatus.NOT_CHARGED)
diff --git a/tests/test_payment.py b/tests/test_payment.py
index d083fb1e09..5fecf485de 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -11,6 +11,7 @@ from saleor.order import OrderEvents, OrderEventsEmails
from saleor.order.views import PAYMENT_TEMPLATE
from saleor.payment import (
ChargeStatus, PaymentError, TransactionKind, get_payment_gateway)
+from saleor.payment.models import Payment
from saleor.payment.utils import (
clean_authorize, clean_capture, create_payment, create_transaction,
gateway_authorize, gateway_capture, gateway_get_client_token,
@@ -493,3 +494,74 @@ def test_clean_capture():
total=amount,
captured_amount=Decimal('0.00'))
clean_capture(payment, amount)
+
+
+def test_can_authorize(payment_dummy: Payment):
+ assert payment_dummy.charge_status == ChargeStatus.NOT_CHARGED
+
+ payment_dummy.is_active = False
+ assert not payment_dummy.can_authorize()
+
+ payment_dummy.is_active = True
+ assert payment_dummy.can_authorize()
+
+ payment_dummy.charge_status = ChargeStatus.CHARGED
+ assert not payment_dummy.can_authorize()
+
+
+def test_can_capture(payment_dummy: Payment):
+ assert payment_dummy.charge_status == ChargeStatus.NOT_CHARGED
+
+ payment_dummy.is_active = False
+ assert not payment_dummy.can_capture()
+
+ payment_dummy.is_active = True
+ assert payment_dummy.can_capture()
+
+ payment_dummy.charge_status = ChargeStatus.CHARGED
+ assert payment_dummy.can_capture()
+
+ payment_dummy.captured_amount = payment_dummy.total
+ assert not payment_dummy.can_capture()
+
+
+def test_can_charge(payment_dummy: Payment):
+ assert payment_dummy.charge_status == ChargeStatus.NOT_CHARGED
+
+ payment_dummy.is_active = False
+ assert not payment_dummy.can_charge()
+
+ payment_dummy.is_active = True
+ assert payment_dummy.can_charge()
+
+ payment_dummy.charge_status = ChargeStatus.CHARGED
+ assert payment_dummy.can_charge()
+
+ payment_dummy.captured_amount = payment_dummy.total
+ assert not payment_dummy.can_charge()
+
+
+def test_can_void(payment_dummy: Payment):
+ assert payment_dummy.charge_status == ChargeStatus.NOT_CHARGED
+
+ payment_dummy.is_active = False
+ assert not payment_dummy.can_void()
+
+ payment_dummy.is_active = True
+ assert payment_dummy.can_void()
+
+ payment_dummy.charge_status = ChargeStatus.CHARGED
+ assert not payment_dummy.can_void()
+
+
+def test_can_refund(payment_dummy: Payment):
+ assert payment_dummy.charge_status == ChargeStatus.NOT_CHARGED
+
+ payment_dummy.is_active = False
+ assert not payment_dummy.can_refund()
+
+ payment_dummy.is_active = True
+ assert not payment_dummy.can_refund()
+
+ payment_dummy.charge_status = ChargeStatus.CHARGED
+ assert payment_dummy.can_refund()