Skip to content

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

Do not allow to create payment if checkout already has a transaction (#18069)

Marcin Wcisło7b108c39541f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cba4cff40a..514eeaf5cb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ All notable, unreleased changes to this project will be documented in this file.
- No manual charge (`Transaction` or `Payment`) object will be created.
- The `OrderEvents.ORDER_MARKED_AS_PAID` event will no longer be emitted.
- Logic associated with `WebhookEventAsyncType.CHECKOUT_FULLY_PAID` event will no longer be triggered when creating a transaction event from webhook response for checkouts with having total gross being 0. At the point of creating the transaction event checkout is already considered fully paid.
+- Creating a Payment (old API) for a Checkout object with an existing Transaction (new API) is no longer permitted as it leads to inconsistent behavior.
### GraphQL API
- You can now filter and search orders using the new `where` and `search` fields on the `pages` query.
diff --git a/saleor/graphql/payment/mutations/payment/checkout_payment_create.py b/saleor/graphql/payment/mutations/payment/checkout_payment_create.py
index 976cadd36a..21312dcd2a 100644
--- a/saleor/graphql/payment/mutations/payment/checkout_payment_create.py
+++ b/saleor/graphql/payment/mutations/payment/checkout_payment_create.py
@@ -16,6 +16,7 @@ from .....core.utils import get_client_ip
from .....core.utils.url import validate_storefront_url
from .....payment import StorePaymentMethod
from .....payment.error_codes import PaymentErrorCode
+from .....payment.models import TransactionItem
from .....payment.utils import create_payment, is_currency_supported
from ....account.i18n import I18nMixin
from ....checkout.mutations.utils import get_checkout
@@ -202,6 +203,16 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
code=PaymentErrorCode.CHECKOUT_EMAIL_NOT_SET.value,
)
+ @classmethod
+ def validate_checkout_has_no_transactions(
+ cls, checkout: "checkout_models.Checkout"
+ ):
+ if TransactionItem.objects.filter(checkout=checkout).exists():
+ raise ValidationError(
+ "Mixing Payments and Transactions within one Checkout object is not allowed.",
+ code=PaymentErrorCode.CHECKOUT_HAS_TRANSACTION.value,
+ )
+
@classmethod
def perform_mutation( # type: ignore[override]
cls,
@@ -215,6 +226,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
token=None,
):
checkout = get_checkout(cls, info, checkout_id=checkout_id, token=token, id=id)
+ cls.validate_checkout_has_no_transactions(checkout)
use_legacy_error_flow_for_checkout = (
checkout.channel.use_legacy_error_flow_for_checkout
diff --git a/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py b/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py
index 566b379777..896a2d875b 100644
--- a/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py
+++ b/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py
@@ -889,3 +889,45 @@ def test_checkout_payment_create_checkout_locked_time_pass(
data = content["data"]["checkoutPaymentCreate"]
assert not data["errors"]
assert data["payment"]
+
+
+def test_checkout_payment_create_is_not_allowed_when_checkout_has_transaction(
+ user_api_client,
+ checkout_without_shipping_required,
+ address,
+ transaction_item_generator,
+):
+ # given
+ checkout = checkout_without_shipping_required
+ checkout.billing_address = address
+ checkout.save()
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ total = calculations.calculate_checkout_total(
+ manager=manager, checkout_info=checkout_info, lines=lines, address=address
+ )
+
+ transaction_item_generator(
+ checkout_id=checkout.pk, charged_value=total.gross.amount
+ )
+
+ # when
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "input": {
+ "gateway": DUMMY_GATEWAY,
+ "token": "sample-token",
+ "amount": total.gross.amount,
+ },
+ }
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutPaymentCreate"]
+ errors = data["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] is None
+ assert errors[0]["code"] == PaymentErrorCode.CHECKOUT_HAS_TRANSACTION.name
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 594a9c43c4..900c2b3a6e 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -23141,6 +23141,7 @@ enum PaymentErrorCode @doc(category: "Payments") {
UNAVAILABLE_VARIANT_IN_CHANNEL
NO_CHECKOUT_LINES
CHECKOUT_COMPLETION_IN_PROGRESS
+ CHECKOUT_HAS_TRANSACTION
}
"""
diff --git a/saleor/payment/error_codes.py b/saleor/payment/error_codes.py
index e8f2775498..7a15be1b8e 100644
--- a/saleor/payment/error_codes.py
+++ b/saleor/payment/error_codes.py
@@ -20,6 +20,7 @@ class PaymentErrorCode(Enum):
UNAVAILABLE_VARIANT_IN_CHANNEL = "unavailable_variant_in_channel"
NO_CHECKOUT_LINES = "no_checkout_lines"
CHECKOUT_COMPLETION_IN_PROGRESS = "checkout_completion_in_progress"
+ CHECKOUT_HAS_TRANSACTION = "checkout_has_transaction"
class TransactionCreateErrorCode(Enum):
diff --git a/saleor/tests/e2e/checkout/test_checkout_create_order_transactions_and_active_payment.py b/saleor/tests/e2e/checkout/test_checkout_create_order_transactions_and_active_payment.py
index bd44208064..694ec8feea 100644
--- a/saleor/tests/e2e/checkout/test_checkout_create_order_transactions_and_active_payment.py
+++ b/saleor/tests/e2e/checkout/test_checkout_create_order_transactions_and_active_payment.py
@@ -1,17 +1,16 @@
import pytest
+from ....payment.error_codes import PaymentErrorCode
from ..apps.utils import add_app
-from ..orders.utils import order_query
from ..product.utils.preparing_product import prepare_product
from ..shop.utils.preparing_shop import prepare_default_shop
from ..transactions.utils import transaction_initialize
from ..utils import assign_permissions
from .utils import (
- checkout_complete,
checkout_create,
checkout_delivery_method_update,
- checkout_dummy_payment_create,
)
+from .utils.checkout_payment_create import raw_checkout_dummy_payment_create
@pytest.mark.e2e
@@ -97,7 +96,6 @@ def test_complete_checkout_with_transaction_and_active_payment_CORE_1601(
)
# Step 2 - Set DeliveryMethod for checkout.
total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
- subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]
# Step 3 - Initialize transaction
transaction_initialize(
@@ -109,24 +107,14 @@ def test_complete_checkout_with_transaction_and_active_payment_CORE_1601(
)
# Step 4 - Create payment for checkout
- checkout_dummy_payment_create(
- e2e_not_logged_api_client, checkout_id, total_gross_amount
- )
-
- # Step 5 - Complete checkout.
- order_data = checkout_complete(e2e_not_logged_api_client, checkout_id)
-
- order_id = order_data["id"]
-
- order_line = order_data["lines"][0]
- assert order_data["status"] == "UNFULFILLED"
- assert order_data["total"]["gross"]["amount"] == total_gross_amount
- assert order_data["subtotal"]["gross"]["amount"] == subtotal_gross_amount
- assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(
- product_variant_price
+ checkout_payment_create_response = raw_checkout_dummy_payment_create(
+ e2e_not_logged_api_client,
+ checkout_id,
+ total_gross_amount,
+ token="fully_charged",
)
- # Step 6gst - check if order has transactions and payments objects
- order_data = order_query(e2e_staff_api_client, order_id)
- assert order_data["transactions"]
- assert order_data["payments"]
+ errors = checkout_payment_create_response["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] is None
+ assert errors[0]["code"] == PaymentErrorCode.CHECKOUT_HAS_TRANSACTION.name