Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix incorrect calculations for transactions (#12809)
Maciej Korycinski0ea318790286
diff --git a/saleor/payment/tests/test_transaction_item_calculations.py b/saleor/payment/tests/test_transaction_item_calculations.pyindex 3a7a02de05..f9ccf1beba 100644--- a/saleor/payment/tests/test_transaction_item_calculations.py+++ b/saleor/payment/tests/test_transaction_item_calculations.py@@ -597,7 +597,9 @@ def test_with_only_refund_success_event(# thentransaction.refresh_from_db()- _assert_amounts(transaction, refunded_value=refunded_value)+ _assert_amounts(+ transaction, refunded_value=refunded_value, charged_value=-refunded_value+ )def test_with_only_refund_request_event(@@ -624,7 +626,11 @@ def test_with_only_refund_request_event(# thentransaction.refresh_from_db()- _assert_amounts(transaction, refund_pending_value=refund_pending_value)+ _assert_amounts(+ transaction,+ refund_pending_value=refund_pending_value,+ charged_value=-refund_pending_value,+ )def test_with_only_refund_failure_event(@@ -681,6 +687,7 @@ def test_with_refund_request_and_success_events(transaction,refund_pending_value=Decimal("0"),refunded_value=refund_value,+ charged_value=-refund_value,)@@ -765,6 +772,7 @@ def test_with_refund_success_and_older_failure_events(transaction,refund_pending_value=Decimal("0"),refunded_value=refund_value,+ charged_value=-refund_value,)@@ -795,6 +803,7 @@ def test_with_refund_request_and_success_events_different_psp_references(transaction,refund_pending_value=first_refund_value,refunded_value=second_refund_value,+ charged_value=-(first_refund_value + second_refund_value),)diff --git a/saleor/payment/transaction_item_calculations.py b/saleor/payment/transaction_item_calculations.pyindex df147acfcc..aa5f6fa2ac 100644--- a/saleor/payment/transaction_item_calculations.py+++ b/saleor/payment/transaction_item_calculations.py@@ -378,43 +378,10 @@ def recalculate_transaction_amounts(transaction: TransactionItem, save: bool = T"""calculate_transaction_amount_based_on_events(transaction)- events: Iterable[TransactionEvent] = transaction.events.order_by(- "created_at"- ).exclude(include_in_calculations=False)-- action_map = _initilize_action_map(events)- _set_transaction_amounts_to_zero(transaction)-- _handle_events_without_psp_reference(transaction, action_map.without_psp_reference)-- for authorize_events in action_map.authorization.values():- _recalculate_authorization_amounts(transaction, authorize_events)-- for charge_events in action_map.charge.values():- _recalculate_charge_amounts(transaction, charge_events)-- for refund_events in action_map.refund.values():- _recalculate_refund_amounts(transaction, refund_events)-- for cancel_events in action_map.cancel.values():- _recalculate_cancel_amounts(transaction, cancel_events)-transaction.authorized_value = max(transaction.authorized_value, Decimal("0"))- transaction.charged_value = max(transaction.charged_value, Decimal("0"))- transaction.refunded_value = max(transaction.refunded_value, Decimal("0"))- transaction.canceled_value = max(transaction.canceled_value, Decimal("0"))transaction.authorize_pending_value = max(transaction.authorize_pending_value, Decimal("0"))- transaction.charge_pending_value = max(- transaction.charge_pending_value, Decimal("0")- )- transaction.refund_pending_value = max(- transaction.refund_pending_value, Decimal("0")- )- transaction.cancel_pending_value = max(- transaction.cancel_pending_value, Decimal("0")- )if save:transaction.save(