Skip to content

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

Extracted require_active_plugin decorator (#6801)

LeOndaz2b1d192bb7ad
diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.py
index e0d5189c82..edbaddb539 100644
--- a/saleor/payment/gateways/adyen/plugin.py
+++ b/saleor/payment/gateways/adyen/plugin.py
@@ -28,7 +28,7 @@ from ...interface import (
PaymentGateway,
)
from ...models import Payment, Transaction
-from ..utils import get_supported_currencies
+from ..utils import get_supported_currencies, require_active_plugin
from .utils.apple_pay import initialize_apple_pay, make_request_to_initialize_apple_pay
from .utils.common import (
AUTH_STATUS,
@@ -50,16 +50,6 @@ WEBHOOK_PATH = "/webhooks"
ADDITIONAL_ACTION_PATH = "/additional-actions"
-def require_active_plugin(fn):
- def wrapped(self, *args, **kwargs):
- previous = kwargs.get("previous_value", None)
- if not self.active:
- return previous
- return fn(self, *args, **kwargs)
-
- return wrapped
-
-
class AdyenGatewayPlugin(BasePlugin):
PLUGIN_ID = "mirumee.payments.adyen"
PLUGIN_NAME = GATEWAY_NAME
diff --git a/saleor/payment/gateways/authorize_net/plugin.py b/saleor/payment/gateways/authorize_net/plugin.py
index ac3d9976d9..d85b5ae038 100644
--- a/saleor/payment/gateways/authorize_net/plugin.py
+++ b/saleor/payment/gateways/authorize_net/plugin.py
@@ -8,7 +8,7 @@ from saleor.plugins.error_codes import PluginErrorCode
from ... import PaymentError
from ...models import Payment
-from ..utils import get_supported_currencies
+from ..utils import get_supported_currencies, require_active_plugin
from . import (
GatewayConfig,
authenticate_test,
@@ -29,16 +29,6 @@ if TYPE_CHECKING:
from . import GatewayResponse, PaymentData
-def require_active_plugin(fn):
- def wrapped(self, *args, **kwargs):
- previous = kwargs.get("previous_value", None)
- if not self.active:
- return previous
- return fn(self, *args, **kwargs)
-
- return wrapped
-
-
class AuthorizeNetGatewayPlugin(BasePlugin):
PLUGIN_NAME = GATEWAY_NAME
PLUGIN_ID = "mirumee.payments.authorize_net"
diff --git a/saleor/payment/gateways/braintree/plugin.py b/saleor/payment/gateways/braintree/plugin.py
index d5103f6d8c..c19403daee 100644
--- a/saleor/payment/gateways/braintree/plugin.py
+++ b/saleor/payment/gateways/braintree/plugin.py
@@ -2,7 +2,7 @@ from typing import TYPE_CHECKING, List
from saleor.plugins.base_plugin import BasePlugin, ConfigurationTypeField
-from ..utils import get_supported_currencies
+from ..utils import get_supported_currencies, require_active_plugin
from . import (
GatewayConfig,
authorize,
@@ -22,16 +22,6 @@ if TYPE_CHECKING:
from . import GatewayResponse, PaymentData, TokenConfig
-def require_active_plugin(fn):
- def wrapped(self, *args, **kwargs):
- previous = kwargs.get("previous_value", None)
- if not self.active:
- return previous
- return fn(self, *args, **kwargs)
-
- return wrapped
-
-
class BraintreeGatewayPlugin(BasePlugin):
PLUGIN_ID = "mirumee.payments.braintree"
PLUGIN_NAME = GATEWAY_NAME
diff --git a/saleor/payment/gateways/dummy/plugin.py b/saleor/payment/gateways/dummy/plugin.py
index 9a1388663b..35db5f002f 100644
--- a/saleor/payment/gateways/dummy/plugin.py
+++ b/saleor/payment/gateways/dummy/plugin.py
@@ -2,7 +2,7 @@ from typing import TYPE_CHECKING
from saleor.plugins.base_plugin import BasePlugin, ConfigurationTypeField
-from ..utils import get_supported_currencies
+from ..utils import get_supported_currencies, require_active_plugin
from . import (
GatewayConfig,
authorize,
@@ -20,16 +20,6 @@ if TYPE_CHECKING:
from ...interface import GatewayResponse, PaymentData, TokenConfig
-def require_active_plugin(fn):
- def wrapped(self, *args, **kwargs):
- previous = kwargs.get("previous_value", None)
- if not self.active:
- return previous
- return fn(self, *args, **kwargs)
-
- return wrapped
-
-
class DummyGatewayPlugin(BasePlugin):
PLUGIN_ID = "mirumee.payments.dummy"
PLUGIN_NAME = GATEWAY_NAME
diff --git a/saleor/payment/gateways/dummy_credit_card/plugin.py b/saleor/payment/gateways/dummy_credit_card/plugin.py
index 9466173991..aee7533aa9 100644
--- a/saleor/payment/gateways/dummy_credit_card/plugin.py
+++ b/saleor/payment/gateways/dummy_credit_card/plugin.py
@@ -2,7 +2,7 @@ from typing import TYPE_CHECKING
from saleor.plugins.base_plugin import BasePlugin, ConfigurationTypeField
-from ..utils import get_supported_currencies
+from ..utils import get_supported_currencies, require_active_plugin
from . import (
GatewayConfig,
authorize,
@@ -20,16 +20,6 @@ if TYPE_CHECKING:
from ...interface import GatewayResponse, PaymentData, TokenConfig
-def require_active_plugin(fn):
- def wrapped(self, *args, **kwargs):
- previous = kwargs.get("previous_value", None)
- if not self.active:
- return previous
- return fn(self, *args, **kwargs)
-
- return wrapped
-
-
class DummyCreditCardGatewayPlugin(BasePlugin):
PLUGIN_ID = "mirumee.payments.dummy_credit_card"
PLUGIN_NAME = GATEWAY_NAME
diff --git a/saleor/payment/gateways/razorpay/plugin.py b/saleor/payment/gateways/razorpay/plugin.py
index 4567be4406..81a9625d90 100644
--- a/saleor/payment/gateways/razorpay/plugin.py
+++ b/saleor/payment/gateways/razorpay/plugin.py
@@ -2,7 +2,7 @@ from typing import TYPE_CHECKING
from saleor.plugins.base_plugin import BasePlugin, ConfigurationTypeField
-from ..utils import get_supported_currencies
+from ..utils import get_supported_currencies, require_active_plugin
from . import GatewayConfig, capture, process_payment, refund
GATEWAY_NAME = "Razorpay"
@@ -11,16 +11,6 @@ if TYPE_CHECKING:
from . import GatewayResponse, PaymentData
-def require_active_plugin(fn):
- def wrapped(self, *args, **kwargs):
- previous = kwargs.get("previous_value", None)
- if not self.active:
- return previous
- return fn(self, *args, **kwargs)
-
- return wrapped
-
-
class RazorpayGatewayPlugin(BasePlugin):
PLUGIN_NAME = GATEWAY_NAME
PLUGIN_ID = "mirumee.payments.razorpay"
diff --git a/saleor/payment/gateways/stripe/plugin.py b/saleor/payment/gateways/stripe/plugin.py
index 35f4f977f6..d44f9fd668 100644
--- a/saleor/payment/gateways/stripe/plugin.py
+++ b/saleor/payment/gateways/stripe/plugin.py
@@ -2,7 +2,7 @@ from typing import TYPE_CHECKING, List
from saleor.plugins.base_plugin import BasePlugin, ConfigurationTypeField
-from ..utils import get_supported_currencies
+from ..utils import get_supported_currencies, require_active_plugin
from . import (
GatewayConfig,
authorize,
@@ -21,16 +21,6 @@ if TYPE_CHECKING:
from . import GatewayResponse, PaymentData
-def require_active_plugin(fn):
- def wrapped(self, *args, **kwargs):
- previous = kwargs.get("previous_value", None)
- if not self.active:
- return previous
- return fn(self, *args, **kwargs)
-
- return wrapped
-
-
class StripeGatewayPlugin(BasePlugin):
PLUGIN_NAME = GATEWAY_NAME
PLUGIN_ID = "mirumee.payments.stripe"
diff --git a/saleor/payment/gateways/utils.py b/saleor/payment/gateways/utils.py
index 3246b16540..dd4810051c 100644
--- a/saleor/payment/gateways/utils.py
+++ b/saleor/payment/gateways/utils.py
@@ -5,6 +5,16 @@ if TYPE_CHECKING:
from ..interface import GatewayConfig
+def require_active_plugin(fn):
+ def wrapped(self, *args, **kwargs):
+ previous = kwargs.get("previous_value", None)
+ if not self.active:
+ return previous
+ return fn(self, *args, **kwargs)
+
+ return wrapped
+
+
def get_supported_currencies(config: "GatewayConfig", gateway_name: str) -> List[str]:
"""Return supported currencies for given gateway configuration.