Skip to content

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

Refactor listing payment methods

Marcin Gębala91e2826c47f7
diff --git a/saleor/core/payments.py b/saleor/core/payments.py
index 8b2a9c545b..fc0d2bdcc3 100644
--- a/saleor/core/payments.py
+++ b/saleor/core/payments.py
@@ -3,8 +3,6 @@ from typing import TYPE_CHECKING, List, Optional
if TYPE_CHECKING:
# flake8: noqa
- from ..checkout.models import Checkout, CheckoutLine
- from ..discount import DiscountInfo
from ..payment.interface import (
CustomerSource,
GatewayResponse,
@@ -17,14 +15,9 @@ if TYPE_CHECKING:
class PaymentInterface(ABC):
@abstractmethod
def list_payment_gateways(
- self, currency: Optional[str] = None, active_only: bool = True
- ) -> List["PaymentGateway"]:
- pass
-
- @abstractmethod
- def checkout_available_payment_gateways(
self,
- checkout: "Checkout",
+ currency: Optional[str] = None,
+ active_only: bool = True,
) -> List["PaymentGateway"]:
pass
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 3620ae2db6..a869ea6ff8 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -425,7 +425,7 @@ class Checkout(CountableDjangoObjectType):
@staticmethod
def resolve_available_payment_gateways(root: models.Checkout, info):
- return info.context.plugins.checkout_available_payment_gateways(checkout=root)
+ return info.context.plugins.list_payment_gateways(currency=root.currency)
@staticmethod
def resolve_gift_cards(root: models.Checkout, _info):
diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.py
index edbaddb539..41efb26cc0 100644
--- a/saleor/payment/gateways/adyen/plugin.py
+++ b/saleor/payment/gateways/adyen/plugin.py
@@ -253,11 +253,11 @@ class AdyenGatewayPlugin(BasePlugin):
return previous_value
@require_active_plugin
- def get_payment_gateway_for_checkout(
- self,
- checkout: "Checkout",
- previous_value,
- ) -> Optional["PaymentGateway"]:
+ def get_payment_gateways(
+ self, currency: Optional[str], checkout: Optional["Checkout"], previous_value
+ ) -> List["PaymentGateway"]:
+ if not checkout:
+ return []
config = self._get_gateway_config()
request = request_data_for_gateway_config(
@@ -270,7 +270,7 @@ class AdyenGatewayPlugin(BasePlugin):
span.set_tag(opentracing.tags.COMPONENT, "payment")
span.set_tag("service.name", "adyen")
response = api_call(request, self.adyen.checkout.payment_methods)
- return PaymentGateway(
+ gateway = PaymentGateway(
id=self.PLUGIN_ID,
name=self.PLUGIN_NAME,
config=[
@@ -282,6 +282,7 @@ class AdyenGatewayPlugin(BasePlugin):
],
currencies=self.get_supported_currencies([]),
)
+ return [gateway]
@property
def order_auto_confirmation(self):
diff --git a/saleor/payment/gateways/adyen/tests/test_plugin.py b/saleor/payment/gateways/adyen/tests/test_plugin.py
index ec23889a37..5e8df4e251 100644
--- a/saleor/payment/gateways/adyen/tests/test_plugin.py
+++ b/saleor/payment/gateways/adyen/tests/test_plugin.py
@@ -58,9 +58,9 @@ def test_get_payment_gateway_for_checkout(
checkout_with_single_item.billing_address = address
checkout_with_single_item.save()
adyen_plugin = adyen_plugin()
- response = adyen_plugin.get_payment_gateway_for_checkout(
- checkout_with_single_item, None
- )
+ response = adyen_plugin.get_payment_gateways(
+ currency=None, checkout=checkout_with_single_item, previous_value=None
+ )[0]
assert response.id == adyen_plugin.PLUGIN_ID
assert response.name == adyen_plugin.PLUGIN_NAME
config = response.config
diff --git a/saleor/payment/gateways/authorize_net/tests/test_plugin.py b/saleor/payment/gateways/authorize_net/tests/test_plugin.py
index 53581ae0d9..e710b9b715 100644
--- a/saleor/payment/gateways/authorize_net/tests/test_plugin.py
+++ b/saleor/payment/gateways/authorize_net/tests/test_plugin.py
@@ -12,9 +12,9 @@ def test_get_payment_gateway_for_checkout(
):
checkout_with_single_item.billing_address = address
checkout_with_single_item.save()
- response = authorize_net_plugin.get_payment_gateway_for_checkout(
- checkout_with_single_item, None
- )
+ response = authorize_net_plugin.get_payment_gateways(
+ currency=None, checkout=checkout_with_single_item, previous_value=None
+ )[0]
assert response.id == authorize_net_plugin.PLUGIN_ID
assert response.name == authorize_net_plugin.PLUGIN_NAME
config = response.config
@@ -41,8 +41,9 @@ def test_get_payment_gateway_for_checkout_inactive(
authorize_net_plugin, checkout_with_single_item
):
authorize_net_plugin.active = False
- response = authorize_net_plugin.get_payment_gateway_for_checkout(
- checkout_with_single_item, None
+ currency = checkout_with_single_item.currency
+ response = authorize_net_plugin.get_payment_gateways(
+ currency=currency, checkout=checkout_with_single_item, previous_value=None
)
assert not response
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index a4b50cbf72..4a435c56d3 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -613,28 +613,22 @@ class BasePlugin:
def token_is_required_as_payment_input(self, previous_value):
return previous_value
- def get_payment_gateway(
- self, currency: Optional[str], previous_value
- ) -> Optional["PaymentGateway"]:
+ def get_payment_gateways(
+ self, currency: Optional[str], checkout: Optional["Checkout"], previous_value
+ ) -> List["PaymentGateway"]:
payment_config = self.get_payment_config(previous_value)
payment_config = payment_config if payment_config != NotImplemented else []
currencies = self.get_supported_currencies(previous_value=[])
currencies = currencies if currencies != NotImplemented else []
if currency and currency not in currencies:
- return None
- return PaymentGateway(
+ return []
+ gateway = PaymentGateway(
id=self.PLUGIN_ID,
name=self.PLUGIN_NAME,
config=payment_config,
currencies=currencies,
)
-
- def get_payment_gateway_for_checkout(
- self,
- checkout: "Checkout",
- previous_value,
- ) -> Optional["PaymentGateway"]:
- return self.get_payment_gateway(checkout.currency, previous_value)
+ return [gateway]
@classmethod
def _update_config_items(
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index b87e9a8475..9adf3e5eaa 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -593,15 +593,19 @@ class PluginsManager(PaymentInterface):
}
def list_payment_gateways(
- self, currency: Optional[str] = None, active_only: bool = True
+ self,
+ currency: Optional[str] = None,
+ active_only: bool = True,
) -> List["PaymentGateway"]:
payment_plugins = self.list_payment_plugin(active_only=active_only)
# if currency is given return only gateways which support given currency
gateways = []
for plugin in payment_plugins.values():
- gateway = plugin.get_payment_gateway(currency=currency, previous_value=None)
- if gateway:
- gateways.append(gateway)
+ gateways.extend(
+ plugin.get_payment_gateways(
+ currency=currency, checkout=None, previous_value=None
+ )
+ )
return gateways
def list_external_authentications(self, active_only: bool = True) -> List[dict]:
@@ -615,20 +619,6 @@ class PluginsManager(PaymentInterface):
if auth_basic_method in type(plugin).__dict__
]
- def checkout_available_payment_gateways(
- self,
- checkout: "Checkout",
- ) -> List["PaymentGateway"]:
- payment_plugins = self.list_payment_plugin(active_only=True)
- gateways = []
- for plugin in payment_plugins.values():
- gateway = plugin.get_payment_gateway_for_checkout(
- checkout, previous_value=None
- )
- if gateway:
- gateways.append(gateway)
- return gateways
-
def __run_payment_method(
self,
gateway: str,