Skip to content

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

Replace PaymentGatewayEnum with a string in GraphQL schema

Jakub Draganek9ebd08e1b94d
diff --git a/saleor/extensions/manager.py b/saleor/extensions/manager.py
index 407389d9ed..49718ba2bf 100644
--- a/saleor/extensions/manager.py
+++ b/saleor/extensions/manager.py
@@ -6,17 +6,18 @@ from django.utils.module_loading import import_string
from django_countries.fields import Country
from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange
-from ..core.payments import Gateway, PaymentInterface
+from ..core.payments import PaymentInterface
from ..core.taxes import TaxType, quantize_price
from .models import PluginConfiguration
if TYPE_CHECKING:
from .base_plugin import BasePlugin
from ..checkout.models import Checkout, CheckoutLine
+ from ..core.payments import Gateway
from ..product.models import Product
from ..account.models import Address
from ..order.models import OrderLine, Order
- from ..payment.interface import PaymentData, GatewayResponse, TokenConfig
+ from ..payment.interface import PaymentData, TokenConfig
class ExtensionsManager(PaymentInterface):
@@ -181,38 +182,38 @@ class ExtensionsManager(PaymentInterface):
)
def authorize_payment(
- self, gateway: Gateway, payment_information: "PaymentData"
- ) -> "GatewayResponse":
+ self, gateway: "Gateway", payment_information: "PaymentData"
+ ) -> Optional["GatewayResponse"]:
method_name = "authorize_payment"
return self.__run_payment_method(gateway, method_name, payment_information)
def capture_payment(
- self, gateway: Gateway, payment_information: "PaymentData"
- ) -> "GatewayResponse":
+ self, gateway: "Gateway", payment_information: "PaymentData"
+ ) -> Optional["GatewayResponse"]:
method_name = "capture_payment"
return self.__run_payment_method(gateway, method_name, payment_information)
def refund_payment(
- self, gateway: Gateway, payment_information: "PaymentData"
- ) -> "GatewayResponse":
+ self, gateway: "Gateway", payment_information: "PaymentData"
+ ) -> Optional["GatewayResponse"]:
method_name = "refund_payment"
return self.__run_payment_method(gateway, method_name, payment_information)
def void_payment(
- self, gateway: Gateway, payment_information: "PaymentData"
- ) -> "GatewayResponse":
+ self, gateway: "Gateway", payment_information: "PaymentData"
+ ) -> Optional["GatewayResponse"]:
method_name = "void_payment"
return self.__run_payment_method(gateway, method_name, payment_information)
def confirm_payment(
- self, gateway: Gateway, payment_information: "PaymentData"
- ) -> "GatewayResponse":
+ self, gateway: "Gateway", payment_information: "PaymentData"
+ ) -> Optional["GatewayResponse"]:
method_name = "confirm_payment"
return self.__run_payment_method(gateway, method_name, payment_information)
def process_payment(
- self, gateway: Gateway, payment_information: "PaymentData"
- ) -> "GatewayResponse":
+ self, gateway: "Gateway", payment_information: "PaymentData"
+ ) -> Optional["GatewayResponse"]:
method_name = "process_payment"
return self.__run_payment_method(gateway, method_name, payment_information)
@@ -232,7 +233,7 @@ class ExtensionsManager(PaymentInterface):
)
def list_payment_sources(
- self, gateway: Gateway, customer_id: str
+ self, gateway: "Gateway", customer_id: str
) -> List["CustomerSource"]:
default_value = []
gateway_name = gateway.value
@@ -243,16 +244,16 @@ class ExtensionsManager(PaymentInterface):
)
raise Exception(f"Payment plugin {gateway_name} is inaccessible!")
- def list_payment_gateways(self) -> List[Gateway]:
+ def list_payment_gateways(self) -> List[str]:
payment_method = "process_payment"
return [
- Gateway(plugin.PLUGIN_NAME)
+ plugin.PLUGIN_NAME
for plugin in self.plugins
if payment_method in type(plugin).__dict__
and self.get_plugin_configuration(plugin.PLUGIN_NAME).active
]
- def get_payment_template(self, gateway: Gateway) -> str:
+ def get_payment_template(self, gateway: "Gateway") -> str:
method_name = "get_payment_template"
default_value = None
gateway_name = gateway.value
@@ -261,7 +262,7 @@ class ExtensionsManager(PaymentInterface):
def __run_payment_method(
self,
- gateway: Gateway,
+ gateway: "Gateway",
method_name: str,
payment_information: "PaymentData",
**kwargs,
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 4e9b05117b..c19bf30c59 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -11,7 +11,6 @@ from ..core.types.meta import MetadataObjectType
from ..core.types.money import Money, TaxedMoney
from ..decorators import permission_required
from ..giftcard.types import GiftCard
-from ..payment.enums import PaymentGatewayEnum
from ..shipping.types import ShippingMethod
@@ -49,7 +48,7 @@ class Checkout(MetadataObjectType, CountableDjangoObjectType):
description="Shipping methods that can be used with this order.",
)
available_payment_gateways = graphene.List(
- PaymentGatewayEnum,
+ graphene.String,
description="List of available payment gateways.",
required=True,
)
diff --git a/saleor/graphql/payment/enums.py b/saleor/graphql/payment/enums.py
index fa17a2273e..e09fe54db6 100644
--- a/saleor/graphql/payment/enums.py
+++ b/saleor/graphql/payment/enums.py
@@ -1,10 +1,9 @@
import graphene
from ...graphql.core.enums import to_enum
-from ...payment import GATEWAYS_ENUM, ChargeStatus
+from ...payment import ChargeStatus
PaymentChargeStatusEnum = to_enum(ChargeStatus, type_name="PaymentChargeStatusEnum")
-PaymentGatewayEnum = graphene.Enum.from_enum(GATEWAYS_ENUM)
class OrderAction(graphene.Enum):
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index 4cf1c9e1dc..8fdd3e4318 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -14,13 +14,12 @@ from ..core.mutations import BaseMutation
from ..core.scalars import Decimal
from ..core.types import common as common_types
from ..core.utils import from_global_id_strict_type
-from .enums import PaymentGatewayEnum
from .types import Payment
class PaymentInput(graphene.InputObjectType):
gateway = graphene.Field(
- PaymentGatewayEnum,
+ graphene.String,
description="A gateway to use with that payment.",
required=True,
)
diff --git a/saleor/graphql/payment/schema.py b/saleor/graphql/payment/schema.py
index 4d7ec319af..b518e4fa58 100644
--- a/saleor/graphql/payment/schema.py
+++ b/saleor/graphql/payment/schema.py
@@ -2,7 +2,6 @@ import graphene
from ..core.fields import PrefetchingConnectionField
from ..decorators import permission_required
-from .enums import PaymentGatewayEnum
from .mutations import PaymentCapture, PaymentRefund, PaymentSecureConfirm, PaymentVoid
from .resolvers import resolve_client_token, resolve_payments
from .types import Payment
@@ -19,8 +18,7 @@ class PaymentQueries(graphene.ObjectType):
payments = PrefetchingConnectionField(Payment, description="List of payments")
payment_client_token = graphene.Field(
graphene.String,
- description="Return a new token for the payment gateway.",
- gateway=graphene.Argument(PaymentGatewayEnum, description="A payment gateway."),
+ gateway=graphene.String(required=True, description="A payment gateway."),
)
@permission_required("order.manage_orders")
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 05bbd4e1a9..18ca3e47ab 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -595,7 +595,7 @@ type Checkout implements Node {
privateMeta: [MetaStore]!
meta: [MetaStore]!
availableShippingMethods: [ShippingMethod]!
- availablePaymentGateways: [GatewaysEnum]!
+ availablePaymentGateways: [String]!
email: String!
isShippingRequired: Boolean!
lines: [CheckoutLine]
@@ -1601,13 +1601,6 @@ input FulfillmentUpdateTrackingInput {
notifyCustomer: Boolean
}
-enum GatewaysEnum {
- DUMMY
- BRAINTREE
- RAZORPAY
- STRIPE
-}
-
scalar GenericScalar
type Geolocalization {
@@ -2682,7 +2675,7 @@ enum PaymentErrorCode {
}
input PaymentInput {
- gateway: GatewaysEnum!
+ gateway: String!
token: String!
amount: Decimal
billingAddress: AddressInput
@@ -3285,7 +3278,7 @@ type Query {
reportProductSales(period: ReportingPeriod!, before: String, after: String, first: Int, last: Int): ProductVariantCountableConnection
payment(id: ID!): Payment
payments(before: String, after: String, first: Int, last: Int): PaymentCountableConnection
- paymentClientToken(gateway: GatewaysEnum): String
+ paymentClientToken(gateway: String!): String
page(id: ID, slug: String): Page
pages(query: String, filter: PageFilterInput, before: String, after: String, first: Int, last: Int): PageCountableConnection
homepageEvents(before: String, after: String, first: Int, last: Int): OrderEventCountableConnection
diff --git a/saleor/order/forms.py b/saleor/order/forms.py
index 923c4c6777..20f1e5d835 100644
--- a/saleor/order/forms.py
+++ b/saleor/order/forms.py
@@ -10,10 +10,7 @@ from .models import Order
def get_gateways():
- gateways = [
- (gtw.value, gtw.value.capitalize() + " gateway")
- for gtw in gateway.list_gateways()
- ]
+ gateways = [(gtw, gtw.capitalize() + " gateway") for gtw in gateway.list_gateways()]
return gateways
diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.py
index 5a526c6e40..3690c89305 100644
--- a/saleor/payment/__init__.py
+++ b/saleor/payment/__init__.py
@@ -1,6 +1,5 @@
from enum import Enum
-from django.conf import settings
from django.utils.translation import pgettext_lazy
@@ -10,11 +9,6 @@ class PaymentError(Exception):
self.message = message
-GATEWAYS_ENUM = Enum(
- "GatewaysEnum", {key.upper(): key.lower() for key in settings.PAYMENT_GATEWAYS}
-)
-
-
class GatewayError(IOError):
pass
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 313fd665ce..c271c5d86f 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -213,7 +213,7 @@ def get_client_token(gateway: Gateway, customer_id: str = None) -> str:
return plugin_manager.get_client_token(gateway, token_config)
-def list_gateways() -> List[Gateway]:
+def list_gateways() -> List[str]:
return get_extensions_manager().list_payment_gateways()
diff --git a/saleor/payment/gateways/razorpay/plugin.py b/saleor/payment/gateways/razorpay/plugin.py
index 48ac1321d4..8b07240bf3 100644
--- a/saleor/payment/gateways/razorpay/plugin.py
+++ b/saleor/payment/gateways/razorpay/plugin.py
@@ -5,7 +5,7 @@ from django.utils.translation import pgettext_lazy
from saleor.extensions import ConfigurationTypeField
from saleor.extensions.base_plugin import BasePlugin
-from . import GatewayConfig, capture, process_payment, refund, create_form
+from . import GatewayConfig, capture, create_form, process_payment, refund
GATEWAY_NAME = "razorpay"
diff --git a/saleor/settings.py b/saleor/settings.py
index 67e4c40340..cb6eac1137 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -615,14 +615,6 @@ BRAINTREE = "braintree"
RAZORPAY = "razorpay"
STRIPE = "stripe"
-# TODO: remove this after graphql schema stops generating enum from this
-PAYMENT_GATEWAYS = {
- DUMMY: {"template_path": "order/payment/dummy.html"},
- BRAINTREE: {"template_path": "order/payment/braintree.html"},
- RAZORPAY: {"template_path": "order/payment/razorpay.html"},
- STRIPE: None,
-}
-
GRAPHENE = {
"RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST": True,
"RELAY_CONNECTION_MAX_LIMIT": 100,
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index f24a1c959f..bbcce47f8a 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -379,7 +379,7 @@ def test_checkout_create_check_lines_quantity(
assert data["errors"][0]["field"] == "quantity"
-def test_checkout_available_payment_gateways(api_client, checkout_with_item, settings):
+def test_checkout_available_payment_gateways(api_client, checkout_with_item):
query = """
query getCheckout($token: UUID!) {
checkout(token: $token) {
@@ -391,7 +391,7 @@ def test_checkout_available_payment_gateways(api_client, checkout_with_item, set
response = api_client.post_graphql(query, variables)
content = get_graphql_content(response)
data = content["data"]["checkout"]
- assert data["availablePaymentGateways"] == ["DUMMY"]
+ assert data["availablePaymentGateways"] == ["dummy"]
def test_checkout_available_shipping_methods(
diff --git a/tests/api/test_payment.py b/tests/api/test_payment.py
index ce400085c7..ec3ba34abc 100644
--- a/tests/api/test_payment.py
+++ b/tests/api/test_payment.py
@@ -6,11 +6,7 @@ from prices import TaxedMoney
from saleor.core.payments import Gateway
from saleor.core.utils import get_country_name_by_code
-from saleor.graphql.payment.enums import (
- OrderAction,
- PaymentChargeStatusEnum,
- PaymentGatewayEnum,
-)
+from saleor.graphql.payment.enums import OrderAction, PaymentChargeStatusEnum
from saleor.payment.interface import CreditCardInfo, CustomerSource, TokenConfig
from saleor.payment.models import ChargeStatus, Payment, TransactionKind
from saleor.payment.utils import fetch_customer_id, store_customer_id
@@ -501,7 +497,7 @@ def braintree_customer_id():
def test_store_payment_gateway_meta(customer_user, braintree_customer_id):
- gateway_name = PaymentGatewayEnum.BRAINTREE.name
+ gateway_name = "braintree"
META = {
"payment-gateways": {
gateway_name.upper(): {"customer_id": braintree_customer_id}
diff --git a/tests/extensions/test_manager.py b/tests/extensions/test_manager.py
index 0834cde011..ab84cbe782 100644
--- a/tests/extensions/test_manager.py
+++ b/tests/extensions/test_manager.py
@@ -5,9 +5,7 @@ import pytest
from django_countries.fields import Country
from prices import Money, TaxedMoney
-from saleor.core.payments import Gateway
from saleor.core.taxes import TaxType
-
from saleor.extensions import ConfigurationTypeField
from saleor.extensions.base_plugin import BasePlugin
from saleor.extensions.manager import ExtensionsManager, get_extensions_manager
@@ -415,6 +413,4 @@ def test_manager_serve_list_of_payment_gateways():
"tests.extensions.test_manager.InactivePaymentGateway",
]
manager = ExtensionsManager(plugins=plugins)
- assert manager.list_payment_gateways() == [
- Gateway(ActivePaymentGateway.PLUGIN_NAME)
- ]
+ assert manager.list_payment_gateways() == [ActivePaymentGateway.PLUGIN_NAME]