Skip to content

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

Fix incorrect default value for StorePaymentMethodEnum (#9943)

Maciej Korycinski63b7903e4c1b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 59380aa5e0..e4391ddf1a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@ All notable, unreleased changes to this project will be documented in this file.
### Other changes
- Fix inaccurate tax calculations - #9799 by @IKarbowiak
+- Fix incorrect default value used in `PaymentInput.storePaymentMethod` - #9943 by @korycins
- Introduce plain text attribute - #9907 by @IKarbowiak
- Stop auto-assigning default addresses to checkout - #9933 by @SzymJ
diff --git a/saleor/graphql/payment/enums.py b/saleor/graphql/payment/enums.py
index a30ebc1044..b6e7f6d412 100644
--- a/saleor/graphql/payment/enums.py
+++ b/saleor/graphql/payment/enums.py
@@ -1,12 +1,13 @@
import graphene
-from ...graphql.core.enums import to_enum
from ...payment import (
ChargeStatus,
+ StorePaymentMethod,
TransactionAction,
TransactionKind,
TransactionStatus,
)
+from ..core.enums import to_enum
TransactionKindEnum = to_enum(TransactionKind, type_name="TransactionKind")
PaymentChargeStatusEnum = to_enum(ChargeStatus, type_name="PaymentChargeStatusEnum")
@@ -35,3 +36,27 @@ class OrderAction(graphene.Enum):
if self == OrderAction.VOID:
return "Represents a void action."
raise ValueError("Unsupported enum value: %s" % self.value)
+
+
+def description(enum):
+ if enum is None:
+ return "Enum representing the type of a payment storage in a gateway."
+ elif enum == StorePaymentMethodEnum.NONE:
+ return "Storage is disabled. The payment is not stored."
+ elif enum == StorePaymentMethodEnum.ON_SESSION:
+ return (
+ "On session storage type. "
+ "The payment is stored only to be reused when "
+ "the customer is present in the checkout flow."
+ )
+ elif enum == StorePaymentMethodEnum.OFF_SESSION:
+ return (
+ "Off session storage type. "
+ "The payment is stored to be reused even if the customer is absent."
+ )
+ return None
+
+
+StorePaymentMethodEnum = to_enum(
+ StorePaymentMethod, type_name="StorePaymentMethodEnum", description=description
+)
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index fa4222152d..3171334a91 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -43,14 +43,13 @@ from ..core.descriptions import (
DEPRECATED_IN_3X_INPUT,
PREVIEW_FEATURE,
)
-from ..core.enums import to_enum
from ..core.fields import JSONString
from ..core.mutations import BaseMutation
from ..core.scalars import UUID, PositiveDecimal
from ..core.types import common as common_types
from ..meta.mutations import MetadataInput
from ..utils import get_user_or_app_from_context
-from .enums import TransactionActionEnum, TransactionStatusEnum
+from .enums import StorePaymentMethodEnum, TransactionActionEnum, TransactionStatusEnum
from .types import Payment, PaymentInitialized, TransactionItem
from .utils import metadata_contains_empty_key
@@ -66,30 +65,6 @@ def add_to_order_total_authorized_and_total_charged(
)
-def description(enum):
- if enum is None:
- return "Enum representing the type of a payment storage in a gateway."
- elif enum == StorePaymentMethodEnum.NONE:
- return "Storage is disabled. The payment is not stored."
- elif enum == StorePaymentMethodEnum.ON_SESSION:
- return (
- "On session storage type. "
- "The payment is stored only to be reused when "
- "the customer is present in the checkout flow."
- )
- elif enum == StorePaymentMethodEnum.OFF_SESSION:
- return (
- "Off session storage type. "
- "The payment is stored to be reused even if the customer is absent."
- )
- return None
-
-
-StorePaymentMethodEnum = to_enum(
- StorePaymentMethod, type_name="StorePaymentMethodEnum", description=description
-)
-
-
class PaymentInput(graphene.InputObjectType):
gateway = graphene.Field(
graphene.String,
@@ -122,7 +97,7 @@ class PaymentInput(graphene.InputObjectType):
store_payment_method = StorePaymentMethodEnum(
description="Payment store type." + ADDED_IN_31,
required=False,
- default_value=StorePaymentMethod.NONE,
+ default_value=StorePaymentMethodEnum.NONE.name,
)
metadata = common_types.NonNullList(
MetadataInput,
@@ -332,6 +307,9 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
payment = None
if amount != 0:
+ store_payment_method = (
+ data.get("store_payment_method") or StorePaymentMethod.NONE
+ )
payment = create_payment(
gateway=gateway,
payment_token=data.get("token", ""),
@@ -343,7 +321,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
customer_ip_address=get_client_ip(info.context),
checkout=checkout,
return_url=data.get("return_url"),
- store_payment_method=data["store_payment_method"],
+ store_payment_method=store_payment_method,
metadata=metadata,
)
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 041d2c9064..0e9dc62820 100644
--- a/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py
+++ b/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py
@@ -8,6 +8,7 @@ from .....checkout import calculations
from .....checkout.error_codes import CheckoutErrorCode
from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from .....checkout.utils import add_variant_to_checkout
+from .....payment import StorePaymentMethod
from .....payment.error_codes import PaymentErrorCode
from .....payment.interface import StorePaymentMethodEnum
from .....payment.models import ChargeStatus, Payment
@@ -473,6 +474,40 @@ def test_create_payment_with_store(
assert payment.store_payment_method == store.lower()
+def test_create_payment_with_store_as_none(
+ user_api_client, checkout_without_shipping_required, address
+):
+ # given
+ store = None
+ checkout = checkout_without_shipping_required
+ checkout.billing_address = address
+ checkout.save()
+
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ total = calculations.checkout_total(
+ manager=manager, checkout_info=checkout_info, lines=lines, address=address
+ )
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "input": {
+ "gateway": DUMMY_GATEWAY,
+ "token": "sample-token",
+ "amount": total.gross.amount,
+ "storePaymentMethod": store,
+ },
+ }
+
+ # when
+ user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
+ checkout.refresh_from_db()
+ payment = checkout.payments.first()
+ assert payment.store_payment_method == StorePaymentMethod.NONE
+
+
@pytest.mark.parametrize(
"metadata", [[{"key": f"key{i}", "value": f"value{i}"} for i in range(5)], [], None]
)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index e10c1d9796..5e3845bf1b 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -19080,7 +19080,7 @@ input PaymentInput {
Added in Saleor 3.1.
"""
- storePaymentMethod: StorePaymentMethodEnum = none
+ storePaymentMethod: StorePaymentMethodEnum = NONE
"""
User public metadata.