Skip to content

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

Support partially charged and partially refunded in payment and order module

Unknown2042903d3b00
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 50157608ad..3df24f0ec7 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -297,7 +297,7 @@ class CapturePaymentForm(ManagePaymentForm):
class RefundPaymentForm(ManagePaymentForm):
- clean_status = (ChargeStatus.PARTIAL_CHARGED, ChargeStatus.FULLY_CHARGED)
+ clean_status = (ChargeStatus.PARTIALLY_CHARGED, ChargeStatus.FULLY_CHARGED)
clean_error = pgettext_lazy('Payment form error',
'Only confirmed payments can be refunded')
diff --git a/saleor/order/models.py b/saleor/order/models.py
index d73dd58714..77daacd9fc 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -49,11 +49,12 @@ class OrderQueryset(models.QuerySet):
"""Return orders with payments to capture.
Orders ready to capture are those which are not draft or canceled and
- have a preauthorized payment.
+ have a preauthorized payment. The preauthorized payment can not
+ already be partially or fully captured.
"""
qs = self.filter(
- payments__is_active=True, payments__charge_status__in=[
- ChargeStatus.NOT_CHARGED, ChargeStatus.CHARGED])
+ payments__is_active=True,
+ payments__charge_status=ChargeStatus.NOT_CHARGED)
qs = qs.exclude(status={OrderStatus.DRAFT, OrderStatus.CANCELED})
return qs.distinct()
@@ -150,8 +151,13 @@ class Order(models.Model):
return self.user.email if self.user else self.user_email
def _total_paid(self):
+ # Get total paid amount from partially charged,
+ # fully charged and partially refunded payments
payments = self.payments.filter(
- charge_status=ChargeStatus.CHARGED)
+ charge_status__in=[
+ ChargeStatus.PARTIALLY_CHARGED,
+ ChargeStatus.FULLY_CHARGED,
+ ChargeStatus.PARTIALLY_REFUNDED])
total_captured = [
payment.get_captured_amount() for payment in payments]
total_paid = sum(total_captured, ZERO_TAXED_MONEY)
@@ -265,7 +271,10 @@ class Order(models.Model):
@property
def total_captured(self):
payment = self.get_last_payment()
- if payment and payment.charge_status == ChargeStatus.CHARGED:
+ if payment and payment.charge_status in (
+ ChargeStatus.PARTIALLY_CHARGED,
+ ChargeStatus.FULLY_CHARGED,
+ ChargeStatus.PARTIALLY_REFUNDED):
return Money(payment.captured_amount, payment.currency)
return zero_money()
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 08f857e157..475a6e9979 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -111,7 +111,11 @@ def cancel_order(order, restock):
payments = order.payments.filter(
is_active=True,
- charge_status__in=[ChargeStatus.NOT_CHARGED, ChargeStatus.CHARGED])
+ charge_status__in=[
+ ChargeStatus.NOT_CHARGED,
+ ChargeStatus.PARTIALLY_CHARGED,
+ ChargeStatus.FULLY_CHARGED,
+ ChargeStatus.PARTIALLY_REFUNDED])
for payment in payments:
if payment.can_refund():
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index ee89694a08..2f88303776 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -149,16 +149,19 @@ class Payment(models.Model):
def can_charge(self):
not_fully_charged = (
- self.charge_status == ChargeStatus.CHARGED
- and self.get_total() > self.get_captured_amount())
+ self.charge_status == ChargeStatus.PARTIALLY_CHARGED)
return self.is_active and (self.not_charged or not_fully_charged)
def can_void(self):
return self.is_active and self.not_charged and self.is_authorized
def can_refund(self):
+ can_refund_charge_status = (
+ ChargeStatus.PARTIALLY_CHARGED,
+ ChargeStatus.FULLY_CHARGED,
+ ChargeStatus.PARTIALLY_REFUNDED)
return (
- self.is_active and self.charge_status == ChargeStatus.CHARGED
+ self.is_active and self.charge_status in can_refund_charge_status
and self.gateway != CustomPaymentChoices.MANUAL)
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index aaca243a6e..b6834dad94 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -161,7 +161,7 @@ def mark_order_as_paid(order: Order, request_user: User):
billing_address=order.billing_address,
total=order.total.gross.amount,
order=order)
- payment.charge_status = ChargeStatus.CHARGED
+ payment.charge_status = ChargeStatus.FULLY_CHARGED
payment.captured_amount = order.total.gross.amount
payment.save(update_fields=['captured_amount', 'charge_status'])
order.events.create(
@@ -352,8 +352,14 @@ def _gateway_postprocess(transaction, payment):
transaction_kind = transaction.kind
if transaction_kind in [TransactionKind.CHARGE, TransactionKind.CAPTURE]:
- payment.charge_status = ChargeStatus.CHARGED
payment.captured_amount += transaction.amount
+
+ # Set payment charge status to fully charged
+ # only if there is no more amount needs to charge
+ payment.charge_status = ChargeStatus.PARTIALLY_CHARGED
+ if payment.get_charge_amount() <= 0:
+ payment.charge_status = ChargeStatus.FULLY_CHARGED
+
payment.save(update_fields=['charge_status', 'captured_amount'])
order = payment.order
if order and order.is_fully_paid():