Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
List available payment plugins
Jakub Draganek08685ad7a676
diff --git a/saleor/core/payments.py b/saleor/core/payments.pyindex f36d83a973..aff7784543 100644--- a/saleor/core/payments.py+++ b/saleor/core/payments.py@@ -19,6 +19,10 @@ class Gateway(Enum):class PaymentInterface(ABC):+ @abstractmethod+ def list_payment_gateways(self) -> List[Gateway]:+ pass+@abstractmethoddef authorize_payment(self, gateway: Gateway, payment_information: "PaymentData"diff --git a/saleor/extensions/manager.py b/saleor/extensions/manager.pyindex 2a9bcee73a..f904e4948f 100644--- a/saleor/extensions/manager.py+++ b/saleor/extensions/manager.py@@ -232,6 +232,14 @@ class ExtensionsManager(PaymentInterface):"list_payment_sources", default_value, customer_id=customer_id)+ def list_payment_gateways(self) -> List["Gateway"]:+ PAYMENT_METHOD = "process_payment"+ return [+ plugin.PLUGIN_NAME+ for plugin in self.plugins+ if PAYMENT_METHOD in type(plugin).__dict__+ ]+def __run_payment_method(self,gateway: Gateway,diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.pyindex f10891a041..878886bdec 100644--- a/saleor/payment/gateway.py+++ b/saleor/payment/gateway.py@@ -1,5 +1,6 @@import loggingfrom decimal import Decimal+from typing import Listfrom django import formsfrom django.core.exceptions import ImproperlyConfigured@@ -207,6 +208,11 @@ def get_client_token(payment: Payment) -> str:return plugin_manager.get_client_token(gateway, payment_data)+def list_gateways() -> List[Gateway]:+ plugin_manager = get_extensions_manager()+ return plugin_manager.list_payment_gateways()++def _get_gateway(payment: Payment) -> Gateway:try:gateway = Gateway(payment.gateway)diff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 104bda4c4c..7432af1b51 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -1,7 +1,6 @@import jsonimport loggingfrom decimal import Decimal-from functools import wrapsfrom typing import Dict, Listfrom django.conf import settings@@ -19,7 +18,6 @@ from . import (ChargeStatus,CustomPaymentChoices,GatewayError,- OperationType,PaymentError,TransactionKind,get_payment_gateway,diff --git a/tests/extensions/test_manager.py b/tests/extensions/test_manager.pyindex 7efafd8d98..5486a96c65 100644--- a/tests/extensions/test_manager.py+++ b/tests/extensions/test_manager.py@@ -281,3 +281,13 @@ def test_manager_save_plugin_configuration():manager.save_plugin_configuration("Sample Plugin", {"active": False})configuration.refresh_from_db()assert not configuration.active+++def test_manager_serve_list_of_payment_gateways():+ SamplePlugin.process_payment = lambda: True+ plugins = [+ "tests.extensions.test_manager.SamplePlugin",+ "tests.extensions.test_manager.SamplePlugin1",+ ]+ manager = ExtensionsManager(plugins=plugins)+ assert manager.list_payment_gateways() == [SamplePlugin.PLUGIN_NAME]diff --git a/tests/gateway/test_gateway.py b/tests/gateway/test_gateway.pyindex bd5ce34b69..01d623131f 100644--- a/tests/gateway/test_gateway.py+++ b/tests/gateway/test_gateway.py@@ -248,3 +248,11 @@ def test_confirm_payment(mock_payment_interface, payment_txn_preauth):assert transaction.kind == TransactionKind.CONFIRMassert transaction.currency == "usd"assert transaction.gateway_response == RAW_RESPONSE+++def test_list_gateways(mock_payment_interface):+ gateways = [Gateway.STRIPE, Gateway.BRAINTREE]+ mock_payment_interface.list_payment_gateways.return_value = gateways+ lst = gateway.list_gateways()+ mock_payment_interface.list_payment_gateways.assert_called_once()+ assert lst == gateways