Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add cancel_active_payments checkout utils and move is_fully_paid tests to correct file
IKarbowiak1b2715e060b5
diff --git a/saleor/checkout/tests/test_checkout.py b/saleor/checkout/tests/test_checkout.pyindex e2c73e2b82..41853958f2 100644--- a/saleor/checkout/tests/test_checkout.py+++ b/saleor/checkout/tests/test_checkout.py@@ -27,12 +27,14 @@ from ..models import Checkoutfrom ..utils import (add_variant_to_checkout,add_voucher_to_checkout,+ cancel_active_payments,change_billing_address_in_checkout,change_shipping_address_in_checkout,clear_shipping_method,create_order,get_voucher_discount_for_checkout,get_voucher_for_checkout,+ is_fully_paid,is_valid_shipping_method,prepare_order_data,recalculate_checkout_discount,@@ -1420,3 +1422,72 @@ def test_get_last_active_payments(checkout_with_payments):# thenassert last_payment.pk == payment.pk+++def test_is_fully_paid(checkout_with_item, payment_dummy):+ checkout = checkout_with_item+ total = calculations.checkout_total(checkout=checkout, lines=list(checkout))+ payment = payment_dummy+ payment.is_active = True+ payment.order = None+ payment.total = total.gross.amount+ payment.currency = total.gross.currency+ payment.checkout = checkout+ payment.save()+ is_paid = is_fully_paid(checkout, list(checkout), None)+ assert is_paid+++def test_is_fully_paid_many_payments(checkout_with_item, payment_dummy):+ checkout = checkout_with_item+ total = calculations.checkout_total(checkout=checkout, lines=list(checkout))+ payment = payment_dummy+ payment.is_active = True+ payment.order = None+ payment.total = total.gross.amount - 1+ payment.currency = total.gross.currency+ payment.checkout = checkout+ payment.save()+ payment2 = payment_dummy+ payment2.pk = None+ payment2.is_active = True+ payment2.order = None+ payment2.total = 1+ payment2.currency = total.gross.currency+ payment2.checkout = checkout+ payment2.save()+ is_paid = is_fully_paid(checkout, list(checkout), None)+ assert is_paid+++def test_is_fully_paid_partially_paid(checkout_with_item, payment_dummy):+ checkout = checkout_with_item+ total = calculations.checkout_total(checkout=checkout, lines=list(checkout))+ payment = payment_dummy+ payment.is_active = True+ payment.order = None+ payment.total = total.gross.amount - 1+ payment.currency = total.gross.currency+ payment.checkout = checkout+ payment.save()+ is_paid = is_fully_paid(checkout, list(checkout), None)+ assert not is_paid+++def test_is_fully_paid_no_payment(checkout_with_item):+ checkout = checkout_with_item+ is_paid = is_fully_paid(checkout, list(checkout), None)+ assert not is_paid+++def test_cancel_active_payments(checkout_with_payments):+ # given+ checkout = checkout_with_payments+ count_active = checkout.payments.filter(is_active=True).count()+ assert count_active != 0++ # when+ cancel_active_payments(checkout)++ # then+ assert checkout.payments.filter(is_active=True).count() == 0diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 0a08fbba3d..1efa5649c1 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -6,6 +6,7 @@ from typing import Iterable, List, Optional, Tuplefrom django.core.exceptions import ValidationErrorfrom django.db import transactionfrom django.db.models import Max, Min, Sum+from django.db.models.functions import Coalescefrom django.utils import timezonefrom django.utils.encoding import smart_textfrom django.utils.translation import get_language@@ -760,8 +761,9 @@ def is_fully_paid(Note that these payments may not be captured or charged at all."""- payments = [payment for payment in checkout.payments.all() if payment.is_active]- total_paid = sum([p.total for p in payments])+ total_paid = checkout.payments.filter(is_active=True).aggregate(+ total_sum=Coalesce(Sum("total"), 0)+ )["total_sum"]checkout_total = (calculations.checkout_total(checkout=checkout, lines=lines, discounts=discounts)- checkout.get_total_gift_cards_balance()@@ -804,3 +806,7 @@ def clean_checkout("Provided payment methods can not cover the checkout's total amount",code=CheckoutErrorCode.CHECKOUT_NOT_FULLY_PAID.value,)+++def cancel_active_payments(checkout: Checkout):+ checkout.payments.filter(is_active=True).update(is_active=False)diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.pyindex bb7d9e4090..7661c0a6ac 100644--- a/saleor/graphql/checkout/tests/test_checkout.py+++ b/saleor/graphql/checkout/tests/test_checkout.py@@ -13,7 +13,7 @@ from ....account.models import Userfrom ....checkout import calculationsfrom ....checkout.error_codes import CheckoutErrorCodefrom ....checkout.models import Checkout-from ....checkout.utils import add_variant_to_checkout, is_fully_paid+from ....checkout.utils import add_variant_to_checkoutfrom ....core.payments import PaymentInterfacefrom ....core.taxes import zero_moneyfrom ....order.models import Order@@ -2703,59 +2703,3 @@ def test_clean_checkout_no_payment(checkout_with_item, shipping_method, address)msg = "Provided payment methods can not cover the checkout's total amount"assert e.value.error_list[0].message == msg---def test_is_fully_paid(checkout_with_item, payment_dummy):- checkout = checkout_with_item- total = calculations.checkout_total(checkout=checkout, lines=list(checkout))- payment = payment_dummy- payment.is_active = True- payment.order = None- payment.total = total.gross.amount- payment.currency = total.gross.currency- payment.checkout = checkout- payment.save()- is_paid = is_fully_paid(checkout, list(checkout), None)- assert is_paid---def test_is_fully_paid_many_payments(checkout_with_item, payment_dummy):- checkout = checkout_with_item- total = calculations.checkout_total(checkout=checkout, lines=list(checkout))- payment = payment_dummy- payment.is_active = True- payment.order = None- payment.total = total.gross.amount - 1- payment.currency = total.gross.currency- payment.checkout = checkout- payment.save()- payment2 = payment_dummy- payment2.pk = None- payment2.is_active = True- payment2.order = None- payment2.total = 1- payment2.currency = total.gross.currency- payment2.checkout = checkout- payment2.save()- is_paid = is_fully_paid(checkout, list(checkout), None)- assert is_paid---def test_is_fully_paid_partially_paid(checkout_with_item, payment_dummy):- checkout = checkout_with_item- total = calculations.checkout_total(checkout=checkout, lines=list(checkout))- payment = payment_dummy- payment.is_active = True- payment.order = None- payment.total = total.gross.amount - 1- payment.currency = total.gross.currency- payment.checkout = checkout- payment.save()- is_paid = is_fully_paid(checkout, list(checkout), None)- assert not is_paid---def test_is_fully_paid_no_payment(checkout_with_item):- checkout = checkout_with_item- is_paid = is_fully_paid(checkout, list(checkout), None)- assert not is_paid