Skip to content

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

Clean up code

Maciej Korycinskif3a1ec65571f
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index bde9d39ba6..db7ae0bf4f 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -54,6 +54,7 @@ def prepare_checkout(checkout: models.Checkout, discounts, tracking_code, redire
to_update.append("tracking_code")
if to_update:
+ to_update.append("last_change")
checkout.save(update_fields=to_update)
@@ -70,10 +71,8 @@ def convert_checkout_to_order(
def validate_payment_amount(discounts, payment, checkout):
- if (
- payment.total
- != calculate_checkout_total_with_gift_cards(checkout, discounts).gross.amount
- ):
+ 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.",
diff --git a/saleor/graphql/checkout/tests/test_checkout_complete.py b/saleor/graphql/checkout/tests/test_checkout_complete.py
index bd1f448ab1..f457d960c0 100644
--- a/saleor/graphql/checkout/tests/test_checkout_complete.py
+++ b/saleor/graphql/checkout/tests/test_checkout_complete.py
@@ -59,6 +59,26 @@ ACTION_REQUIRED_GATEWAY_RESPONSE = GatewayResponse(
)
+def test_checkout_complete_order_already_exists(
+ user_api_client, order_with_lines, checkout_with_gift_card,
+):
+ checkout = checkout_with_gift_card
+ orders_count = Order.objects.count()
+ order_with_lines.checkout_token = checkout.pk
+ order_with_lines.save()
+ 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)
+
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["checkoutErrors"]
+
+ order_token = data["order"]["token"]
+ assert Order.objects.count() == orders_count
+ assert order_with_lines.token == order_token
+
+
@pytest.mark.integration
def test_checkout_complete(
user_api_client,
diff --git a/saleor/payment/gateways/adyen/utils.py b/saleor/payment/gateways/adyen/utils.py
index 75d7090c7a..22599935de 100644
--- a/saleor/payment/gateways/adyen/utils.py
+++ b/saleor/payment/gateways/adyen/utils.py
@@ -6,15 +6,12 @@ from typing import Any, Callable, Dict, Optional
import Adyen
from babel.numbers import get_currency_precision
from django.conf import settings
-from django.contrib.auth.models import AnonymousUser
from django_countries.fields import Country
from ....checkout.calculations import checkout_line_total, checkout_total
from ....checkout.models import Checkout
-from ....checkout.utils import create_order, is_fully_paid, prepare_order_data
from ....core.prices import quantize_price
from ....discount.utils import fetch_active_discounts
-from ....order.models import Order
from ....payment.models import Payment
from ... import PaymentError
from ...interface import PaymentData, PaymentMethodInfo
@@ -284,21 +281,3 @@ def get_payment_method_info(
type="card" if payment_method == "scheme" else payment_method,
)
return payment_method_info
-
-
-def create_order_from_checkout(checkout: "Checkout") -> "Order":
- discounts = fetch_active_discounts()
- lines = list(checkout)
-
- if not is_fully_paid(checkout, lines, discounts):
- raise PaymentError(
- "Provided payment methods can not cover the checkout's total amount"
- )
-
- order_data = prepare_order_data(
- checkout=checkout, lines=lines, discounts=discounts,
- )
- user = checkout.user or AnonymousUser()
- return create_order(
- checkout=checkout, order_data=order_data, user=user # type: ignore
- )