Skip to content

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

Refactor after review

Maciej Korycinskic56ac0520e1a
diff --git a/saleor/graphql/checkout/utils.py b/saleor/checkout/checkout_cleaner.py
similarity index 83%
rename from saleor/graphql/checkout/utils.py
rename to saleor/checkout/checkout_cleaner.py
index c774daa2fe..e370dd8cc9 100644
--- a/saleor/graphql/checkout/utils.py
+++ b/saleor/checkout/checkout_cleaner.py
@@ -1,12 +1,13 @@
-from typing import Iterable, Type, Union
+from typing import Iterable, Optional, Type, Union
from django.core.exceptions import ValidationError
-from ...checkout.error_codes import CheckoutErrorCode
-from ...checkout.models import Checkout, CheckoutLine
-from ...checkout.utils import is_fully_paid, is_valid_shipping_method
-from ...discount import DiscountInfo
-from ...payment.error_codes import PaymentErrorCode
+from ..discount import DiscountInfo
+from ..payment import gateway, models as payment_models
+from ..payment.error_codes import PaymentErrorCode
+from .error_codes import CheckoutErrorCode
+from .models import Checkout, CheckoutLine
+from .utils import is_fully_paid, is_valid_shipping_method
def clean_checkout_shipping(
@@ -65,9 +66,11 @@ def clean_checkout_payment(
lines: Iterable[CheckoutLine],
discounts: Iterable[DiscountInfo],
error_code: Type[CheckoutErrorCode],
+ last_payment: Optional[payment_models.Payment],
):
clean_billing_address(checkout, error_code)
if not is_fully_paid(checkout, lines, discounts):
+ gateway.payment_refund_or_void(last_payment)
raise ValidationError(
"Provided payment methods can not cover the checkout's total amount",
code=error_code.CHECKOUT_NOT_FULLY_PAID.value,
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index db7ae0bf4f..fde8370ba9 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -10,31 +10,27 @@ from ..core.taxes import TaxError
from ..core.utils.url import validate_storefront_url
from ..discount import DiscountInfo
from ..discount.models import NotApplicable
-from ..graphql.checkout.utils import ( # TODO move from graphql
- clean_checkout_payment,
- clean_checkout_shipping,
-)
from ..order import models as order_models
from ..order.models import Order
from ..payment import PaymentError, gateway
from ..payment.models import Payment, Transaction
from ..payment.utils import store_customer_id
from . import models
-from .calculations import calculate_checkout_total_with_gift_cards
+from .checkout_cleaner import clean_checkout_payment, clean_checkout_shipping
from .error_codes import CheckoutErrorCode
from .utils import abort_order_data, create_order, get_order, prepare_order_data
@transaction.atomic
-def prepare_checkout(checkout: models.Checkout, discounts, tracking_code, redirect_url):
+def prepare_checkout(
+ checkout: models.Checkout, discounts, tracking_code, redirect_url, payment
+):
lines = list(checkout)
clean_checkout_shipping(checkout, lines, discounts, CheckoutErrorCode)
- clean_checkout_payment(checkout, lines, discounts, CheckoutErrorCode)
-
- payment = checkout.get_last_active_payment()
-
- validate_payment_amount(discounts, payment, checkout)
+ clean_checkout_payment(
+ checkout, lines, discounts, CheckoutErrorCode, last_payment=payment
+ )
if redirect_url:
try:
@@ -70,16 +66,6 @@ def convert_checkout_to_order(
return order
-def validate_payment_amount(discounts, payment, checkout):
- checkout_total = calculate_checkout_total_with_gift_cards(checkout, discounts)
- if payment.total != checkout_total.gross.amount:
- gateway.payment_refund_or_void(payment)
- raise ValidationError(
- "Payment does not cover all checkout value.",
- code=CheckoutErrorCode.CHECKOUT_NOT_FULLY_PAID.value,
- )
-
-
def get_order_data(checkout: models.Checkout, discounts: List[DiscountInfo]) -> dict:
try:
with transaction.atomic():
@@ -140,14 +126,16 @@ def complete_checkout(
order = get_order(checkout.token)
if order:
return order, action_required, action_data
+
+ payment = checkout.get_last_active_payment()
prepare_checkout(
checkout=checkout,
discounts=discounts,
tracking_code=tracking_code,
redirect_url=redirect_url,
+ payment=payment,
)
- payment = checkout.get_last_active_payment()
try:
order_data = get_order_data(checkout, discounts)
except ValidationError as error:
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index fa049c3967..0ef1f57fad 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -11,6 +11,10 @@ from prices import Money, TaxedMoney
from ....account.models import User
from ....checkout import calculations
+from ....checkout.checkout_cleaner import (
+ clean_checkout_payment,
+ clean_checkout_shipping,
+)
from ....checkout.error_codes import CheckoutErrorCode
from ....checkout.models import Checkout
from ....checkout.utils import add_variant_to_checkout
@@ -25,7 +29,6 @@ from ..mutations import (
clean_shipping_method,
update_checkout_shipping_method_if_invalid,
)
-from ..utils import clean_checkout_payment, clean_checkout_shipping
def test_clean_shipping_method_after_shipping_address_changes_stay_the_same(
@@ -2116,7 +2119,9 @@ def test_clean_checkout(checkout_with_item, payment_dummy, address, shipping_met
# Shouldn't raise any errors
lines = list(checkout)
clean_checkout_shipping(checkout, lines, None, CheckoutErrorCode)
- clean_checkout_payment(checkout, lines, None, CheckoutErrorCode)
+ clean_checkout_payment(
+ checkout, lines, None, CheckoutErrorCode, last_payment=payment
+ )
def test_clean_checkout_no_shipping_method(checkout_with_item, address):
@@ -2166,9 +2171,12 @@ def test_clean_checkout_no_billing_address(
checkout.shipping_address = address
checkout.shipping_method = shipping_method
checkout.save()
+ payment = checkout.get_last_active_payment()
with pytest.raises(ValidationError) as e:
- clean_checkout_payment(checkout, list(checkout), None, CheckoutErrorCode)
+ clean_checkout_payment(
+ checkout, list(checkout), None, CheckoutErrorCode, last_payment=payment
+ )
msg = "Billing address is not set"
assert e.value.error_dict["billing_address"][0].message == msg
@@ -2179,9 +2187,12 @@ def test_clean_checkout_no_payment(checkout_with_item, shipping_method, address)
checkout.shipping_method = shipping_method
checkout.billing_address = address
checkout.save()
+ payment = checkout.get_last_active_payment()
with pytest.raises(ValidationError) as e:
- clean_checkout_payment(checkout, list(checkout), None, CheckoutErrorCode)
+ clean_checkout_payment(
+ checkout, list(checkout), None, CheckoutErrorCode, last_payment=payment
+ )
msg = "Provided payment methods can not cover the checkout's total amount"
assert e.value.error_list[0].message == msg
diff --git a/saleor/graphql/checkout/tests/test_checkout_complete.py b/saleor/graphql/checkout/tests/test_checkout_complete.py
index 5120d6f9d7..973b557011 100644
--- a/saleor/graphql/checkout/tests/test_checkout_complete.py
+++ b/saleor/graphql/checkout/tests/test_checkout_complete.py
@@ -741,7 +741,7 @@ def test_checkout_complete_payment_payment_total_different_than_checkout(
payment = payment_dummy
payment.is_active = True
payment.order = None
- payment.total = total.gross.amount + Decimal(10)
+ payment.total = total.gross.amount - Decimal(10)
payment.currency = total.gross.currency
payment.checkout = checkout
payment.save()
@@ -750,7 +750,6 @@ def test_checkout_complete_payment_payment_total_different_than_checkout(
orders_count = Order.objects.count()
checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
variables = {"checkoutId": checkout_id, "redirectUrl": "https://www.example.com"}
- response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
# when
response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index 9f5bc2451d..c7680f519b 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -3,11 +3,11 @@ from django.conf import settings
from django.core.exceptions import ValidationError
from ...checkout.calculations import calculate_checkout_total_with_gift_cards
+from ...checkout.checkout_cleaner import clean_billing_address, clean_checkout_shipping
from ...checkout.utils import cancel_active_payments
from ...core.permissions import OrderPermissions
from ...core.utils import get_client_ip
from ...core.utils.url import validate_storefront_url
-from ...graphql.checkout.utils import clean_billing_address, clean_checkout_shipping
from ...payment import PaymentError, gateway, models
from ...payment.error_codes import PaymentErrorCode
from ...payment.utils import create_payment, is_currency_supported
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 0191596823..9a3b29553a 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -271,7 +271,9 @@ def _validate_refund_amount(payment: Payment, amount: Decimal):
raise PaymentError("Cannot refund more than captured.")
-def payment_refund_or_void(payment):
+def payment_refund_or_void(payment: Optional[Payment]):
+ if payment is None:
+ return
if payment.can_refund():
refund(payment)
elif payment.can_void():