Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
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.pyindex 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_ipfrom ...graphql.checkout.utils import clean_billing_address, clean_checkout_shippingfrom ...payment import PaymentError, gateway, modelsfrom ...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_supportedfrom ..account.i18n import I18nMixinfrom ..account.types import AddressInputfrom ..checkout.types import Checkout@@ -108,10 +107,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):@classmethoddef 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.pyindex 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_currenciesif not supp_currencies:currencies = [settings.DEFAULT_CURRENCY]diff --git a/saleor/payment/tests/test_payment.py b/saleor/payment/tests/test_payment.pyindex 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_responsediff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 9f7d422afe..ae5292418e 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -11,6 +11,7 @@ from ..account.models import Userfrom ..checkout.models import Checkoutfrom ..order.actions import handle_fully_paid_orderfrom ..order.models import Order+from ..plugins.manager import get_plugins_managerfrom . import ChargeStatus, GatewayError, PaymentError, TransactionKindfrom .error_codes import PaymentErrorCodefrom .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])