Skip to content

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

Add docstring to get_supported_currencies and create is_currency_supported utility function

IKarbowiak1ffced3b863e
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index 23e7708f30..189fcc81e9 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -9,8 +9,7 @@ from ...core.utils import get_client_ip
from ...graphql.checkout.utils import clean_billing_address, clean_checkout_shipping
from ...payment import PaymentError, gateway, models
from ...payment.error_codes import PaymentErrorCode
-from ...payment.utils import create_payment
-from ...plugins.manager import get_plugins_manager
+from ...payment.utils import create_payment, is_currency_supported
from ..account.i18n import I18nMixin
from ..account.types import AddressInput
from ..checkout.types import Checkout
@@ -108,10 +107,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
@classmethod
def validate_gateway(cls, gateway_id, currency):
- available_gateways = get_plugins_manager().list_payment_gateways(
- currency=currency
- )
- if not any([gateway["id"] == gateway_id for gateway in available_gateways]):
+ if not is_currency_supported(currency, gateway_id):
raise ValidationError(
{
"gateway": ValidationError(
diff --git a/saleor/payment/gateways/utils.py b/saleor/payment/gateways/utils.py
index d0312b87ad..b37aede819 100644
--- a/saleor/payment/gateways/utils.py
+++ b/saleor/payment/gateways/utils.py
@@ -8,6 +8,11 @@ if TYPE_CHECKING:
def get_supported_currencies(config: "GatewayConfig", gateway_name: str) -> List[str]:
+ """Return supported currencies for given gateway configuration.
+
+ If supported currencies are not specified, the default currency is used
+ and a warning is raised.
+ """
supp_currencies = config.supported_currencies
if not supp_currencies:
currencies = [settings.DEFAULT_CURRENCY]
diff --git a/saleor/payment/tests/test_payment.py b/saleor/payment/tests/test_payment.py
index 6a2be762f4..02daf3f1df 100644
--- a/saleor/payment/tests/test_payment.py
+++ b/saleor/payment/tests/test_payment.py
@@ -15,6 +15,7 @@ from ..utils import (
create_payment,
create_payment_information,
create_transaction,
+ is_currency_supported,
validate_gateway_response,
)
@@ -456,3 +457,21 @@ def test_validate_gateway_response_not_json_serializable(gateway_response):
validate_gateway_response(gateway_response)
assert str(e.value) == "Gateway response needs to be json serializable"
+
+
+ "currency, exp_response", [("EUR", True), ("USD", True), ("PLN", False)],
+)
+def test_is_currency_supported(currency, exp_response, gateway_config, monkeypatch):
+ # given
+ gateway_config.supported_currencies = "USD, EUR"
+ monkeypatch.setattr(
+ "saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin._get_gateway_config",
+ lambda _: gateway_config,
+ )
+
+ # when
+ response = is_currency_supported(currency, "mirumee.payments.dummy")
+
+ # then
+ assert response == exp_response
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 9f7d422afe..ae5292418e 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -11,6 +11,7 @@ from ..account.models import User
from ..checkout.models import Checkout
from ..order.actions import handle_fully_paid_order
from ..order.models import Order
+from ..plugins.manager import get_plugins_manager
from . import ChargeStatus, GatewayError, PaymentError, TransactionKind
from .error_codes import PaymentErrorCode
from .interface import AddressData, GatewayResponse, PaymentData
@@ -280,3 +281,9 @@ def get_payment_token(payment: Payment):
if auth_transaction is None:
raise PaymentError("Cannot process unauthorized transaction")
return auth_transaction.token
+
+
+def is_currency_supported(currency: str, gateway_id: str):
+ """Return true if the given gateway supports given currency."""
+ available_gateways = get_plugins_manager().list_payment_gateways(currency=currency)
+ return any([gateway["id"] == gateway_id for gateway in available_gateways])