Skip to content

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

Add razorpay gateway plugin

Jakub Draganek806b452c1d50
diff --git a/saleor/payment/gateways/braintree/plugin.py b/saleor/payment/gateways/braintree/plugin.py
index a436d80219..1d1d6fb4e8 100644
--- a/saleor/payment/gateways/braintree/plugin.py
+++ b/saleor/payment/gateways/braintree/plugin.py
@@ -121,7 +121,7 @@ class BraintreeGatewayPlugin(BasePlugin):
connection_params=gateway_config["connection_params"],
store_customer=gateway_config["store_card"],
)
- self.active = GATEWAY_NAME in settings.CHECKOUT_PAYMENT_GATEWAYS
+ self.active = False
@classmethod
def _get_default_configuration(cls):
diff --git a/saleor/payment/gateways/razorpay/plugin.py b/saleor/payment/gateways/razorpay/plugin.py
new file mode 100644
index 0000000000..5e607cd302
--- /dev/null
+++ b/saleor/payment/gateways/razorpay/plugin.py
@@ -0,0 +1,127 @@
+from typing import TYPE_CHECKING
+
+from django.conf import settings
+from django.utils.translation import pgettext_lazy
+
+from saleor.extensions import ConfigurationTypeField
+from saleor.extensions.base_plugin import BasePlugin
+
+from . import GatewayConfig, capture, process_payment, refund
+
+GATEWAY_NAME = "razorpay"
+
+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
+ CONFIG_STRUCTURE = {
+ "Public API key": {
+ "type": ConfigurationTypeField.STRING,
+ "help_text": pgettext_lazy("Plugin help text", "Provide public API key"),
+ "label": pgettext_lazy("Plugin label", "Public API key"),
+ },
+ "Secret API key": {
+ "type": ConfigurationTypeField.STRING,
+ "help_text": pgettext_lazy(
+ "Plugin help text", "Provide Stripe secret API key"
+ ),
+ "label": pgettext_lazy("Plugin label", "Secret API key"),
+ },
+ "Store customers card": {
+ "type": ConfigurationTypeField.BOOLEAN,
+ "help_text": pgettext_lazy(
+ "Plugin help text",
+ "Determines if Saleor should store cards on payments"
+ "in Stripe customer.",
+ ),
+ "label": pgettext_lazy("Plugin label", "Store customers card"),
+ },
+ "Automatic payment capture": {
+ "type": ConfigurationTypeField.BOOLEAN,
+ "help_text": pgettext_lazy(
+ "Plugin help text",
+ "Determines if Saleor should automaticaly capture payments.",
+ ),
+ "label": pgettext_lazy("Plugin label", "Automatic payment capture"),
+ },
+ }
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.config = None
+
+ def _initialize_plugin_configuration(self):
+ super()._initialize_plugin_configuration()
+
+ if self._cached_config and self._cached_config.configuration:
+ configuration = self._cached_config.configuration
+
+ self.config = GatewayConfig(
+ gateway_name=GATEWAY_NAME,
+ auto_capture=configuration["Automatic payment capture"],
+ connection_params={
+ "public_key": configuration["Public API key"],
+ "private_key": configuration["Secret API key"],
+ },
+ template_path="",
+ store_customer=configuration["Store customers card"],
+ )
+ else:
+ # This should be removed after we drop payment configs in settings
+ gateway_config = settings.PAYMENT_GATEWAYS[GATEWAY_NAME]["config"]
+ self.config = GatewayConfig(
+ gateway_name=GATEWAY_NAME,
+ auto_capture=gateway_config["auto_capture"],
+ template_path=gateway_config["template_path"],
+ connection_params=gateway_config["connection_params"],
+ store_customer=gateway_config["store_card"],
+ )
+ self.active = GATEWAY_NAME in settings.CHECKOUT_PAYMENT_GATEWAYS
+
+ @classmethod
+ def _get_default_configuration(cls):
+ defaults = {
+ "name": cls.PLUGIN_NAME,
+ "description": "",
+ "active": False,
+ "configuration": [
+ {"name": "Public API key", "value": ""},
+ {"name": "Secret API key", "value": ""},
+ {"name": "Store customers card", "value": False},
+ {"name": "Automatic payment capture", "value": True},
+ ],
+ }
+ return defaults
+
+ def _get_gateway_config(self):
+ return self.config
+
+ @require_active_plugin
+ def capture_payment(
+ self, payment_information: "PaymentData", previous_value
+ ) -> "GatewayResponse":
+ return capture(payment_information, self._get_gateway_config())
+
+ @require_active_plugin
+ def refund_payment(
+ self, payment_information: "PaymentData", previous_value
+ ) -> "GatewayResponse":
+ return refund(payment_information, self._get_gateway_config())
+
+ @require_active_plugin
+ def process_payment(
+ self, payment_information: "PaymentData", previous_value
+ ) -> "GatewayResponse":
+ return process_payment(payment_information, self._get_gateway_config())
diff --git a/saleor/payment/gateways/stripe/plugin.py b/saleor/payment/gateways/stripe/plugin.py
index 317d49012a..71c3dd2c19 100644
--- a/saleor/payment/gateways/stripe/plugin.py
+++ b/saleor/payment/gateways/stripe/plugin.py
@@ -98,7 +98,7 @@ class StripeGatewayPlugin(BasePlugin):
connection_params=gateway_config["connection_params"],
store_customer=gateway_config["store_card"],
)
- self.active = GATEWAY_NAME in settings.CHECKOUT_PAYMENT_GATEWAYS
+ self.active = False
@classmethod
def _get_default_configuration(cls):
diff --git a/saleor/settings.py b/saleor/settings.py
index 407dacf476..38191959f7 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -683,6 +683,7 @@ PAYMENT_PLUGINS = [
"saleor.payment.gateways.dummy.plugin.DummyGatewayPlugin",
"saleor.payment.gateways.stripe.plugin.StripeGatewayPlugin",
"saleor.payment.gateways.braintree.plugin.BraintreeGatewayPlugin",
+ "saleor.payment.gateways.razorpay.plugin.RazorpayGatewayPlugin",
]
PLUGINS = os.environ.get("PLUGINS", [])
PLUGINS += PAYMENT_PLUGINS