Skip to content

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

Add transaction items limit to TransactionSessionBase (#15795)

Tomasz Szymańskiac1920a8d37a
diff --git a/saleor/graphql/payment/mutations/base.py b/saleor/graphql/payment/mutations/base.py
index e587e68ecd..c2a525bcb1 100644
--- a/saleor/graphql/payment/mutations/base.py
+++ b/saleor/graphql/payment/mutations/base.py
@@ -1,6 +1,7 @@
from decimal import Decimal
from typing import TYPE_CHECKING, Optional, Union
+from django.conf import settings
from django.core.exceptions import ValidationError
from graphql import GraphQLError
@@ -8,6 +9,7 @@ from ....checkout import models as checkout_models
from ....checkout.calculations import fetch_checkout_data
from ....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from ....order import models as order_models
+from ...core.enums import TransactionInitializeErrorCode
from ...core.mutations import BaseMutation
from ...core.utils import from_global_id_or_error
@@ -74,6 +76,21 @@ class TransactionSessionBase(BaseMutation):
)
}
)
+
+ if (
+ source_object.payment_transactions.count()
+ >= settings.TRANSACTION_ITEMS_LIMIT
+ ):
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ f"{source_object_type} transactions limit of "
+ f"{settings.TRANSACTION_ITEMS_LIMIT} reached.",
+ code=TransactionInitializeErrorCode.INVALID.value,
+ )
+ }
+ )
+
return source_object
@classmethod
diff --git a/saleor/graphql/payment/mutations/transaction/payment_gateway_initialize.py b/saleor/graphql/payment/mutations/transaction/payment_gateway_initialize.py
index 093619d415..ef3d4e6988 100644
--- a/saleor/graphql/payment/mutations/transaction/payment_gateway_initialize.py
+++ b/saleor/graphql/payment/mutations/transaction/payment_gateway_initialize.py
@@ -1,4 +1,5 @@
import graphene
+from django.conf import settings
from .....payment.interface import PaymentGatewayData
from ....core.descriptions import ADDED_IN_313, PREVIEW_FEATURE
@@ -65,7 +66,10 @@ class PaymentGatewayInitialize(TransactionSessionBase):
"Initializes a payment gateway session. It triggers the webhook "
"`PAYMENT_GATEWAY_INITIALIZE_SESSION`, to the requested `paymentGateways`. "
"If `paymentGateways` is not provided, the webhook will be send to all "
- "subscribed payment gateways." + ADDED_IN_313 + PREVIEW_FEATURE
+ "subscribed payment gateways. There is a limit of "
+ f"{settings.TRANSACTION_ITEMS_LIMIT} transaction items per checkout / order."
+ + ADDED_IN_313
+ + PREVIEW_FEATURE
)
error_type_class = common_types.PaymentGatewayInitializeError
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_initialize.py b/saleor/graphql/payment/mutations/transaction/transaction_initialize.py
index 2f9057f838..fce0e8285c 100644
--- a/saleor/graphql/payment/mutations/transaction/transaction_initialize.py
+++ b/saleor/graphql/payment/mutations/transaction/transaction_initialize.py
@@ -2,6 +2,7 @@ import uuid
from typing import Optional
import graphene
+from django.conf import settings
from django.core.exceptions import ValidationError
from .....app.models import App
@@ -93,8 +94,8 @@ class TransactionInitialize(TransactionSessionBase):
description = (
"Initializes a transaction session. It triggers the webhook "
"`TRANSACTION_INITIALIZE_SESSION`, to the requested `paymentGateways`. "
- + ADDED_IN_313
- + PREVIEW_FEATURE
+ f"There is a limit of {settings.TRANSACTION_ITEMS_LIMIT} transaction "
+ "items per checkout / order." + ADDED_IN_313 + PREVIEW_FEATURE
)
error_type_class = common_types.TransactionInitializeError
diff --git a/saleor/graphql/payment/tests/mutations/test_payment_gateway_initialize.py b/saleor/graphql/payment/tests/mutations/test_payment_gateway_initialize.py
index 8bcd84b9e6..6e12bab11a 100644
--- a/saleor/graphql/payment/tests/mutations/test_payment_gateway_initialize.py
+++ b/saleor/graphql/payment/tests/mutations/test_payment_gateway_initialize.py
@@ -1,10 +1,14 @@
from decimal import Decimal
from unittest import mock
+from django.conf import settings
+from django.test import override_settings
+
from .....checkout.calculations import fetch_checkout_data
from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from .....payment.interface import PaymentGatewayData
-from ....core.enums import PaymentGatewayConfigErrorCode
+from .....payment.models import TransactionItem
+from ....core.enums import PaymentGatewayConfigErrorCode, TransactionInitializeErrorCode
from ....core.utils import to_global_id_or_none
from ....tests.utils import get_graphql_content
@@ -77,6 +81,40 @@ def test_for_checkout_without_payment_gateways(
mocked_initialize.assert_called_once_with(checkout.total.gross.amount, [], checkout)
+@override_settings(TRANSACTION_ITEMS_LIMIT=3)
+def test_for_checkout_transactions_limit_on_gateway_initialize(
+ user_api_client, checkout_with_prices
+):
+ # given
+ TransactionItem.objects.bulk_create(
+ [
+ TransactionItem(
+ checkout=checkout_with_prices, currency=checkout_with_prices.currency
+ )
+ for _ in range(settings.TRANSACTION_ITEMS_LIMIT)
+ ]
+ )
+
+ variables = {
+ "id": to_global_id_or_none(checkout_with_prices),
+ "paymentGateways": None,
+ }
+
+ # when
+ response = user_api_client.post_graphql(PAYMENT_GATEWAY_INITIALIZE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["paymentGatewayInitialize"]
+ assert data["errors"]
+ error = data["errors"][0]
+ assert error["code"] == TransactionInitializeErrorCode.INVALID.name
+ assert error["field"] == "id"
+ assert error["message"] == (
+ "Checkout transactions limit of " f"{settings.TRANSACTION_ITEMS_LIMIT} reached."
+ )
+
+
@mock.patch("saleor.plugins.manager.PluginsManager.payment_gateway_initialize_session")
def test_for_order_without_payment_gateways(
mocked_initialize,
diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py b/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py
index 59a944ec4c..939bd038ab 100644
--- a/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py
+++ b/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py
@@ -3,6 +3,8 @@ from unittest import mock
import pytest
import pytz
+from django.conf import settings
+from django.test import override_settings
from django.utils import timezone
from freezegun import freeze_time
@@ -18,6 +20,7 @@ from .....payment.interface import (
TransactionSessionData,
TransactionSessionResult,
)
+from .....payment.models import TransactionItem
from ....channel.enums import TransactionFlowStrategyEnum
from ....core.enums import TransactionInitializeErrorCode
from ....core.utils import to_global_id_or_none
@@ -234,6 +237,42 @@ def test_for_checkout_without_payment_gateway_data(
assert checkout.authorize_status == CheckoutAuthorizeStatus.PARTIAL
+@override_settings(TRANSACTION_ITEMS_LIMIT=3)
+def test_for_checkout_transactions_limit_on_transaction_initialize(
+ user_api_client, checkout_with_prices
+):
+ # given
+ TransactionItem.objects.bulk_create(
+ [
+ TransactionItem(
+ checkout=checkout_with_prices, currency=checkout_with_prices.currency
+ )
+ for _ in range(settings.TRANSACTION_ITEMS_LIMIT)
+ ]
+ )
+
+ variables = {
+ "action": None,
+ "amount": 99,
+ "id": to_global_id_or_none(checkout_with_prices),
+ "paymentGateway": {"id": "any", "data": None},
+ }
+
+ # when
+ response = user_api_client.post_graphql(TRANSACTION_INITIALIZE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["transactionInitialize"]
+ assert data["errors"]
+ error = data["errors"][0]
+ assert error["code"] == TransactionInitializeErrorCode.INVALID.name
+ assert error["field"] == "id"
+ assert error["message"] == (
+ "Checkout transactions limit of " f"{settings.TRANSACTION_ITEMS_LIMIT} reached."
+ )
+
+
@mock.patch("saleor.plugins.manager.PluginsManager.transaction_initialize_session")
def test_for_checkout_with_idempotency_key(
mocked_initialize,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 1590534ca4..76a5040265 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -16958,7 +16958,7 @@ type Mutation {
): TransactionEventReport @doc(category: "Payments")
"""
- Initializes a payment gateway session. It triggers the webhook `PAYMENT_GATEWAY_INITIALIZE_SESSION`, to the requested `paymentGateways`. If `paymentGateways` is not provided, the webhook will be send to all subscribed payment gateways.
+ Initializes a payment gateway session. It triggers the webhook `PAYMENT_GATEWAY_INITIALIZE_SESSION`, to the requested `paymentGateways`. If `paymentGateways` is not provided, the webhook will be send to all subscribed payment gateways. There is a limit of 100 transaction items per checkout / order.
Added in Saleor 3.13.
@@ -16978,7 +16978,7 @@ type Mutation {
): PaymentGatewayInitialize @doc(category: "Payments")
"""
- Initializes a transaction session. It triggers the webhook `TRANSACTION_INITIALIZE_SESSION`, to the requested `paymentGateways`.
+ Initializes a transaction session. It triggers the webhook `TRANSACTION_INITIALIZE_SESSION`, to the requested `paymentGateways`. There is a limit of 100 transaction items per checkout / order.
Added in Saleor 3.13.
@@ -24822,7 +24822,7 @@ enum TransactionEventReportErrorCode @doc(category: "Payments") {
}
"""
-Initializes a payment gateway session. It triggers the webhook `PAYMENT_GATEWAY_INITIALIZE_SESSION`, to the requested `paymentGateways`. If `paymentGateways` is not provided, the webhook will be send to all subscribed payment gateways.
+Initializes a payment gateway session. It triggers the webhook `PAYMENT_GATEWAY_INITIALIZE_SESSION`, to the requested `paymentGateways`. If `paymentGateways` is not provided, the webhook will be send to all subscribed payment gateways. There is a limit of 100 transaction items per checkout / order.
Added in Saleor 3.13.
@@ -24892,7 +24892,7 @@ input PaymentGatewayToInitialize @doc(category: "Payments") {
}
"""
-Initializes a transaction session. It triggers the webhook `TRANSACTION_INITIALIZE_SESSION`, to the requested `paymentGateways`.
+Initializes a transaction session. It triggers the webhook `TRANSACTION_INITIALIZE_SESSION`, to the requested `paymentGateways`. There is a limit of 100 transaction items per checkout / order.
Added in Saleor 3.13.
diff --git a/saleor/settings.py b/saleor/settings.py
index b69ebaecc3..94180e45c8 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -934,3 +934,8 @@ GIFTS_LIMIT_PER_RULE = os.environ.get("GIFTS_LIMIT_PER_RULE", 500)
ENABLE_LIMITING_WEBHOOKS_FOR_IDENTICAL_PAYLOADS = get_bool_from_env(
"ENABLE_LIMITING_WEBHOOKS_FOR_IDENTICAL_PAYLOADS", False
)
+
+
+# Transaction items limit for PaymentGatewayInitialize / TransactionInitialize.
+# That setting limits the allowed number of transaction items for single entity.
+TRANSACTION_ITEMS_LIMIT = 100