Skip to content

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

Refactor list_payment_gateways and list_payment_plugin, add get_supported_currencies to BasePlugin

IKarbowiak147aa1242113
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 81bcbbbd51..cd4a4249aa 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -551,10 +551,8 @@ def expected_dummy_gateway():
return {
"id": "mirumee.payments.dummy",
"name": "Dummy",
- "config": [
- {"field": "store_customer_card", "value": "false"},
- {"field": "supported_currencies", "value": "['USD']"},
- ],
+ "config": [{"field": "store_customer_card", "value": "false"}],
+ "currencies": ["USD"],
}
@@ -568,6 +566,7 @@ query getCheckoutPayments($token: UUID!) {
field
value
}
+ currencies
}
}
}
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 27086575eb..4c4d93c9df 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -35,6 +35,11 @@ class PaymentGateway(graphene.ObjectType):
required=True,
description="Payment gateway client configuration.",
)
+ currencies = graphene.List(
+ graphene.String,
+ required=True,
+ description="Payment gateway supported currencies.",
+ )
class Meta:
description = (
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index ec8eaecb12..400d4ec1ed 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3119,6 +3119,7 @@ type PaymentGateway {
name: String!
id: ID!
config: [GatewayConfigLine!]!
+ currencies: [String]!
}
input PaymentInput {
diff --git a/saleor/payment/gateways/braintree/plugin.py b/saleor/payment/gateways/braintree/plugin.py
index ac3118876d..e35f886c97 100644
--- a/saleor/payment/gateways/braintree/plugin.py
+++ b/saleor/payment/gateways/braintree/plugin.py
@@ -153,12 +153,15 @@ class BraintreeGatewayPlugin(BasePlugin):
def get_client_token(self, token_config: "TokenConfig", previous_value):
return get_client_token(self._get_gateway_config(), token_config)
+ @require_active_plugin
+ def get_supported_currencies(self, previous_value):
+ config = self._get_gateway_config()
+ return get_supported_currencies(config, GATEWAY_NAME)
+
@require_active_plugin
def get_payment_config(self, previous_value):
config = self._get_gateway_config()
- currencies = get_supported_currencies(config, GATEWAY_NAME)
return [
{"field": "store_customer_card", "value": config.store_customer},
{"field": "client_token", "value": get_client_token(config=config)},
- {"field": "supported_currencies", "value": currencies},
]
diff --git a/saleor/payment/gateways/dummy/plugin.py b/saleor/payment/gateways/dummy/plugin.py
index 37ac3faa05..c9f29ae67a 100644
--- a/saleor/payment/gateways/dummy/plugin.py
+++ b/saleor/payment/gateways/dummy/plugin.py
@@ -114,11 +114,12 @@ class DummyGatewayPlugin(BasePlugin):
def get_client_token(self, token_config: "TokenConfig", previous_value):
return get_client_token()
+ @require_active_plugin
+ def get_supported_currencies(self, previous_value):
+ config = self._get_gateway_config()
+ return get_supported_currencies(config, GATEWAY_NAME)
+
@require_active_plugin
def get_payment_config(self, previous_value):
config = self._get_gateway_config()
- currencies = get_supported_currencies(config, GATEWAY_NAME)
- return [
- {"field": "store_customer_card", "value": config.store_customer},
- {"field": "supported_currencies", "value": currencies},
- ]
+ return [{"field": "store_customer_card", "value": config.store_customer}]
diff --git a/saleor/payment/gateways/razorpay/plugin.py b/saleor/payment/gateways/razorpay/plugin.py
index 87c002998c..4567be4406 100644
--- a/saleor/payment/gateways/razorpay/plugin.py
+++ b/saleor/payment/gateways/razorpay/plugin.py
@@ -100,11 +100,12 @@ class RazorpayGatewayPlugin(BasePlugin):
) -> "GatewayResponse":
return process_payment(payment_information, self._get_gateway_config())
+ @require_active_plugin
+ def get_supported_currencies(self, previous_value):
+ config = self._get_gateway_config()
+ return get_supported_currencies(config, GATEWAY_NAME)
+
@require_active_plugin
def get_payment_config(self, previous_value):
config = self._get_gateway_config()
- currencies = get_supported_currencies(config, GATEWAY_NAME)
- return [
- {"field": "api_key", "value": config.connection_params["public_key"]},
- {"field": "supported_currencies", "value": currencies},
- ]
+ return [{"field": "api_key", "value": config.connection_params["public_key"]}]
diff --git a/saleor/payment/gateways/stripe/plugin.py b/saleor/payment/gateways/stripe/plugin.py
index 5177c5d507..a49bf77e63 100644
--- a/saleor/payment/gateways/stripe/plugin.py
+++ b/saleor/payment/gateways/stripe/plugin.py
@@ -127,12 +127,15 @@ class StripeGatewayPlugin(BasePlugin):
previous_value.extend(sources)
return previous_value
+ @require_active_plugin
+ def get_supported_currencies(self, previous_value):
+ config = self._get_gateway_config()
+ return get_supported_currencies(config, GATEWAY_NAME)
+
@require_active_plugin
def get_payment_config(self, previous_value):
config = self._get_gateway_config()
- currencies = get_supported_currencies(config, GATEWAY_NAME)
return [
{"field": "api_key", "value": config.connection_params["public_key"]},
{"field": "store_customer_card", "value": config.store_customer},
- {"field": "supported_currencies", "value": currencies},
]
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 845f871436..02f698aaf9 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -331,6 +331,9 @@ class BasePlugin:
def get_payment_config(self, previous_value):
return NotImplemented
+ def get_supported_currencies(self, previous_value):
+ return NotImplemented
+
@classmethod
def _update_config_items(
cls, configuration_to_update: List[dict], current_config: List[dict]
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index b21e81ed75..b970ea2548 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -1,5 +1,5 @@
from decimal import Decimal
-from typing import TYPE_CHECKING, Any, Iterable, List, Optional, Union
+from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Union
import opentracing
from django.conf import settings
@@ -309,57 +309,47 @@ class PluginsManager(PaymentInterface):
plugins = self.plugins
return [plugin for plugin in plugins if plugin.active]
- def list_payment_plugin_names(self, active_only: bool = False) -> List[tuple]:
+ def list_payment_plugin(self, active_only: bool = False) -> Dict[str, "BasePlugin"]:
payment_method = "process_payment"
plugins = self.plugins
if active_only:
plugins = self.get_active_plugins()
- return [
- (plugin.PLUGIN_ID, plugin.PLUGIN_NAME)
+ return {
+ plugin.PLUGIN_ID: plugin
for plugin in plugins
if payment_method in type(plugin).__dict__
- ]
+ }
def list_payment_gateways(
self, currency: Optional[str] = None, active_only: bool = True
) -> List[dict]:
- payment_plugins = self.list_payment_plugin_names(active_only=active_only)
+ payment_plugins = self.list_payment_plugin(active_only=active_only)
# if currency is given return only gateways which support given currency
- if currency:
- return self.__get_payment_gateways_for_specific_currency(
- currency, payment_plugins
- )
- return [
+ gateways = [
{
"id": plugin_id,
- "name": plugin_name,
- "config": self.__get_payment_config(plugin_id),
+ "name": plugin.PLUGIN_NAME,
+ "config": self.__get_payment_config(plugin),
+ "currencies": self.__get_payment_currencies(plugin),
}
- for plugin_id, plugin_name in payment_plugins
+ for plugin_id, plugin in payment_plugins.items()
]
-
- def __get_payment_gateways_for_specific_currency(
- self, currency: str, payment_plugins: List[tuple]
- ) -> List[dict]:
- """Return only gateways which support given currency."""
- gateways = []
- for plugin_id, plugin_name in payment_plugins:
- config = self.__get_payment_config(plugin_id)
- for item in config:
- if (
- item["field"] == "supported_currencies"
- and currency in item["value"]
- ):
- gateways.append(
- {"id": plugin_id, "name": plugin_name, "config": config}
- )
+ if currency:
+ return [
+ gtw for gtw in gateways if currency in gtw["currencies"] # type: ignore
+ ]
return gateways
- def __get_payment_config(self, gateway: str) -> List[dict]:
+ def __get_payment_currencies(self, gateway: "BasePlugin") -> List[str]:
+ """Return gateway supported currencies."""
+ method_name = "get_supported_currencies"
+ default_value: list = []
+ return self.__run_method_on_single_plugin(gateway, method_name, default_value)
+
+ def __get_payment_config(self, gateway: "BasePlugin") -> List[dict]:
method_name = "get_payment_config"
default_value: list = []
- gtw = self.get_plugin(gateway)
- return self.__run_method_on_single_plugin(gtw, method_name, default_value)
+ return self.__run_method_on_single_plugin(gateway, method_name, default_value)
def __run_payment_method(
self,
diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.py
index c4cbeba904..3f0dd7e787 100644
--- a/saleor/plugins/tests/sample_plugins.py
+++ b/saleor/plugins/tests/sample_plugins.py
@@ -111,14 +111,17 @@ class ActivePaymentGateway(BasePlugin):
PLUGIN_ID = "gateway.active"
CLIENT_CONFIG = [
{"field": "foo", "value": "bar"},
- {"field": "supported_currencies", "value": ["USD"]},
]
PLUGIN_NAME = "braintree"
DEFAULT_ACTIVE = True
+ SUPPORTED_CURRENCIES = ["USD"]
def process_payment(self, payment_information, previous_value):
pass
+ def get_supported_currencies(self, previous_value):
+ return self.SUPPORTED_CURRENCIES
+
def get_payment_config(self, previous_value):
return self.CLIENT_CONFIG
@@ -127,14 +130,17 @@ class ActiveDummyPaymentGateway(BasePlugin):
PLUGIN_ID = "sampleDummy.active"
CLIENT_CONFIG = [
{"field": "foo", "value": "bar"},
- {"field": "supported_currencies", "value": ["PLN", "USD"]},
]
PLUGIN_NAME = "SampleDummy"
DEFAULT_ACTIVE = True
+ SUPPORTED_CURRENCIES = ["PLN", "USD"]
def process_payment(self, payment_information, previous_value):
pass
+ def get_supported_currencies(self, previous_value):
+ return self.SUPPORTED_CURRENCIES
+
def get_payment_config(self, previous_value):
return self.CLIENT_CONFIG
diff --git a/saleor/plugins/tests/test_manager.py b/saleor/plugins/tests/test_manager.py
index 6069d81c74..4669ccdae6 100644
--- a/saleor/plugins/tests/test_manager.py
+++ b/saleor/plugins/tests/test_manager.py
@@ -257,6 +257,7 @@ def test_manager_serve_list_of_payment_gateways():
"id": ActivePaymentGateway.PLUGIN_ID,
"name": ActivePaymentGateway.PLUGIN_NAME,
"config": ActivePaymentGateway.CLIENT_CONFIG,
+ "currencies": ActivePaymentGateway.SUPPORTED_CURRENCIES,
}
plugins = [
"saleor.plugins.tests.sample_plugins.PluginSample",
@@ -273,11 +274,13 @@ def test_manager_serve_list_all_payment_gateways():
"id": ActivePaymentGateway.PLUGIN_ID,
"name": ActivePaymentGateway.PLUGIN_NAME,
"config": ActivePaymentGateway.CLIENT_CONFIG,
+ "currencies": ActivePaymentGateway.SUPPORTED_CURRENCIES,
},
{
"id": InactivePaymentGateway.PLUGIN_ID,
"name": InactivePaymentGateway.PLUGIN_NAME,
"config": [],
+ "currencies": [],
},
]
@@ -295,6 +298,7 @@ def test_manager_serve_list_all_payment_gateways_specified_currency():
"id": ActiveDummyPaymentGateway.PLUGIN_ID,
"name": ActiveDummyPaymentGateway.PLUGIN_NAME,
"config": ActiveDummyPaymentGateway.CLIENT_CONFIG,
+ "currencies": ActiveDummyPaymentGateway.SUPPORTED_CURRENCIES,
},
]
@@ -316,11 +320,13 @@ def test_manager_serve_list_all_payment_gateways_specified_currency_two_gateways
"id": ActivePaymentGateway.PLUGIN_ID,
"name": ActivePaymentGateway.PLUGIN_NAME,
"config": ActivePaymentGateway.CLIENT_CONFIG,
+ "currencies": ActivePaymentGateway.SUPPORTED_CURRENCIES,
},
{
"id": ActiveDummyPaymentGateway.PLUGIN_ID,
"name": ActiveDummyPaymentGateway.PLUGIN_NAME,
"config": ActiveDummyPaymentGateway.CLIENT_CONFIG,
+ "currencies": ActiveDummyPaymentGateway.SUPPORTED_CURRENCIES,
},
]