Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
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.pyindex 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.pyindex 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_emaildef _total_paid(self):+ # Get total paid amount from partially charged,+ # fully charged and partially refunded paymentspayments = 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):@propertydef 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.pyindex 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.pyindex 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_authorizeddef 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_statusand self.gateway != CustomPaymentChoices.MANUAL)diff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 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_CHARGEDpayment.captured_amount = order.total.gross.amountpayment.save(update_fields=['captured_amount', 'charge_status'])order.events.create(@@ -352,8 +352,14 @@ def _gateway_postprocess(transaction, payment):transaction_kind = transaction.kindif transaction_kind in [TransactionKind.CHARGE, TransactionKind.CAPTURE]:- payment.charge_status = ChargeStatus.CHARGEDpayment.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.orderif order and order.is_fully_paid():