Skip to content

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

Remove payment gateways forms

IKarbowiak9f8a9ffc4036
diff --git a/saleor/core/payments.py b/saleor/core/payments.py
index af1fecad2e..999aef1156 100644
--- a/saleor/core/payments.py
+++ b/saleor/core/payments.py
@@ -46,12 +46,6 @@ class PaymentInterface(ABC):
) -> "GatewayResponse":
pass
- @abstractmethod
- def create_payment_form(
- self, data, gateway: str, payment_information: "PaymentData"
- ) -> "GatewayResponse":
- pass
-
@abstractmethod
def get_client_token(self, gateway: str, token_config: "TokenConfig") -> str:
pass
diff --git a/saleor/extensions/base_plugin.py b/saleor/extensions/base_plugin.py
index 64f0e0b37f..747815a4f8 100644
--- a/saleor/extensions/base_plugin.py
+++ b/saleor/extensions/base_plugin.py
@@ -299,9 +299,6 @@ class BasePlugin:
) -> List["CustomerSource"]:
return NotImplemented
- def create_form(self, data, payment_information, previous_value):
- return NotImplemented
-
def get_client_token(self, token_config, previous_value):
return NotImplemented
diff --git a/saleor/extensions/manager.py b/saleor/extensions/manager.py
index 09f93a9bdd..f077249539 100644
--- a/saleor/extensions/manager.py
+++ b/saleor/extensions/manager.py
@@ -252,12 +252,6 @@ class ExtensionsManager(PaymentInterface):
method_name = "process_payment"
return self.__run_payment_method(gateway, method_name, payment_information)
- def create_payment_form(self, data, gateway, payment_information):
- method_name = "create_form"
- return self.__run_payment_method(
- gateway, method_name, payment_information, data=data
- )
-
def get_client_token(self, gateway, token_config: "TokenConfig") -> str:
method_name = "get_client_token"
default_value = None
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index dfbd40f8a4..31b1aa8b24 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -191,13 +191,6 @@ def confirm(payment: Payment) -> Transaction:
)
-@require_active_payment
-def create_payment_form(payment: Payment, data) -> forms.Form:
- plugin_manager = get_extensions_manager()
- payment_data = create_payment_information(payment)
- return plugin_manager.create_payment_form(data, payment.gateway, payment_data)
-
-
def list_payment_sources(gateway: str, customer_id: str) -> List["CustomerSource"]:
plugin_manager = get_extensions_manager()
return plugin_manager.list_payment_sources(gateway, customer_id)
diff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.py
index 5d15737fe0..c3613624b9 100644
--- a/saleor/payment/gateways/braintree/__init__.py
+++ b/saleor/payment/gateways/braintree/__init__.py
@@ -14,7 +14,6 @@ from ...interface import (
TokenConfig,
)
from .errors import DEFAULT_ERROR_MESSAGE, BraintreeException
-from .forms import BraintreePaymentForm
# Error codes whitelist should be a dict of code: error_msg_override
# if no error_msg_override is provided,
@@ -90,10 +89,6 @@ def extract_gateway_response(braintree_result) -> Dict:
}
-def create_form(data, payment_information):
- return BraintreePaymentForm(data=data, payment_information=payment_information)
-
-
def get_braintree_gateway(sandbox_mode, merchant_id, public_key, private_key):
if not all([merchant_id, private_key, public_key]):
raise ImproperlyConfigured("Incorrectly configured Braintree gateway.")
diff --git a/saleor/payment/gateways/braintree/forms.py b/saleor/payment/gateways/braintree/forms.py
deleted file mode 100644
index f920d386d4..0000000000
--- a/saleor/payment/gateways/braintree/forms.py
+++ /dev/null
@@ -1,40 +0,0 @@
-from django import forms
-from django.core.exceptions import ValidationError
-from django.utils.translation import pgettext_lazy
-
-from ...error_codes import PaymentErrorCode
-from ...interface import PaymentData
-
-
-class BraintreePaymentForm(forms.Form):
- amount = forms.DecimalField()
-
- # Unique transaction identifier returned by Braintree
- # for testing in the sandbox mode please refer to
- # https://developers.braintreepayments.com/reference/general/testing/python#nonces-representing-cards
- # as it's values should be hardcoded to simulate each payment gateway
- # response
- payment_method_nonce = forms.CharField()
-
- def __init__(self, payment_information: PaymentData, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.payment_information = payment_information
- self.fields["amount"].initial = payment_information.amount
-
- def clean(self):
- cleaned_data = super().clean()
- # Amount is sent client-side
- # authorizing different amount than payments' total could happen only
- # when manually adjusting the template value as we do not allow
- # partial-payments at this moment, error is returned instead.
- amount = cleaned_data.get("amount")
- if amount and amount != self.payment_information.amount:
- msg = pgettext_lazy(
- "payment error",
- "Unable to process transaction. Please try again in a moment",
- )
- raise ValidationError(msg, code=PaymentErrorCode.PAYMENT_ERROR)
- return cleaned_data
-
- def get_payment_token(self):
- return self.cleaned_data["payment_method_nonce"]
diff --git a/saleor/payment/gateways/braintree/plugin.py b/saleor/payment/gateways/braintree/plugin.py
index dd396f0d7a..f7e5eae487 100644
--- a/saleor/payment/gateways/braintree/plugin.py
+++ b/saleor/payment/gateways/braintree/plugin.py
@@ -9,7 +9,6 @@ from . import (
GatewayConfig,
authorize,
capture,
- create_form,
get_client_token,
list_client_sources,
process_payment,
@@ -21,7 +20,6 @@ GATEWAY_NAME = "Braintree"
if TYPE_CHECKING:
from . import GatewayResponse, PaymentData, TokenConfig
- from django import forms
def require_active_plugin(fn):
@@ -191,12 +189,6 @@ class BraintreeGatewayPlugin(BasePlugin):
previous_value.extend(sources)
return previous_value
- @require_active_plugin
- def create_form(
- self, data, payment_information: "PaymentData", previous_value
- ) -> "forms.Form":
- return create_form(data, payment_information)
-
@require_active_plugin
def get_client_token(self, token_config: "TokenConfig", previous_value):
return get_client_token(self._get_gateway_config(), token_config)
diff --git a/saleor/payment/gateways/dummy/__init__.py b/saleor/payment/gateways/dummy/__init__.py
index dfa7e8484b..ee4eae71c5 100644
--- a/saleor/payment/gateways/dummy/__init__.py
+++ b/saleor/payment/gateways/dummy/__init__.py
@@ -2,7 +2,6 @@ import uuid
from ... import ChargeStatus, TransactionKind
from ...interface import GatewayConfig, GatewayResponse, PaymentData
-from .forms import DummyPaymentForm
def dummy_success():
@@ -13,10 +12,6 @@ def get_client_token(**_):
return str(uuid.uuid4())
-def create_form(data, payment_information, connection_params):
- return DummyPaymentForm(data=data)
-
-
def authorize(
payment_information: PaymentData, config: GatewayConfig
) -> GatewayResponse:
diff --git a/saleor/payment/gateways/dummy/forms.py b/saleor/payment/gateways/dummy/forms.py
deleted file mode 100644
index dba24a0f7e..0000000000
--- a/saleor/payment/gateways/dummy/forms.py
+++ /dev/null
@@ -1,42 +0,0 @@
-from django import forms
-from django.utils.translation import pgettext_lazy, ugettext_lazy as _
-
-from ... import ChargeStatus
-
-
-class DummyPaymentForm(forms.Form):
- charge_status = forms.ChoiceField(
- label=pgettext_lazy("Payment status form field", "Payment status"),
- choices=ChargeStatus.CHOICES,
- initial=ChargeStatus.NOT_CHARGED,
- widget=forms.RadioSelect,
- )
-
- def clean(self):
- cleaned_data = super(DummyPaymentForm, self).clean()
-
- # Partially refunded is not supported directly
- # since only last transaction of call_gateway will be processed
- charge_status = cleaned_data["charge_status"]
- if charge_status in [
- ChargeStatus.PARTIALLY_CHARGED,
- ChargeStatus.PARTIALLY_REFUNDED,
- ]:
- raise forms.ValidationError(
- _(
- "Setting charge status to {} directly "
- "is not supported. Please use the dashboard to "
- "refund partially.".format(charge_status)
- ),
- code="invalid_charge_status",
- )
-
- return cleaned_data
-
- def get_payment_token(self):
- """Return selected charge status instead of token for testing only.
-
- Gateways used for production should return an actual token instead.
- """
- charge_status = self.cleaned_data["charge_status"]
- return charge_status
diff --git a/saleor/payment/gateways/dummy/plugin.py b/saleor/payment/gateways/dummy/plugin.py
index 161e30ae7f..9f9228da94 100644
--- a/saleor/payment/gateways/dummy/plugin.py
+++ b/saleor/payment/gateways/dummy/plugin.py
@@ -10,7 +10,6 @@ from . import (
authorize,
capture,
confirm,
- create_form,
get_client_token,
process_payment,
refund,
@@ -21,7 +20,6 @@ GATEWAY_NAME = "Dummy"
if TYPE_CHECKING:
from ...interface import GatewayResponse, PaymentData, TokenConfig
- from django import forms
def require_active_plugin(fn):
@@ -141,12 +139,6 @@ class DummyGatewayPlugin(BasePlugin):
) -> "GatewayResponse":
return process_payment(payment_information, self._get_gateway_config())
- @require_active_plugin
- def create_form(
- self, data, payment_information: "PaymentData", previous_value
- ) -> "forms.Form":
- return create_form(data, payment_information, {})
-
@require_active_plugin
def get_client_token(self, token_config: "TokenConfig", previous_value):
return get_client_token()
diff --git a/saleor/payment/gateways/razorpay/__init__.py b/saleor/payment/gateways/razorpay/__init__.py
index 54a60ae503..e6e3e6ea9b 100644
--- a/saleor/payment/gateways/razorpay/__init__.py
+++ b/saleor/payment/gateways/razorpay/__init__.py
@@ -9,7 +9,6 @@ import razorpay.errors
from ... import TransactionKind
from ...interface import GatewayConfig, GatewayResponse, PaymentData
from . import errors
-from .forms import RazorPaymentForm
from .utils import get_amount_for_razorpay, get_error_response
# The list of currencies supported by razorpay
@@ -71,15 +70,6 @@ def clean_razorpay_response(response: Dict):
response["amount"] = Decimal(response["amount"]) / 100
-def create_form(data, payment_information, connection_params):
- """Return the associated razorpay payment form."""
- return RazorPaymentForm(
- data=data,
- payment_information=payment_information,
- connection_params=connection_params,
- )
-
-
def get_client(public_key: str, private_key: str, **_):
"""Create a Razorpay client from set-up application keys."""
razorpay_client = razorpay.Client(auth=(public_key, private_key))
diff --git a/saleor/payment/gateways/razorpay/forms.py b/saleor/payment/gateways/razorpay/forms.py
deleted file mode 100644
index 0f57cf91a6..0000000000
--- a/saleor/payment/gateways/razorpay/forms.py
+++ /dev/null
@@ -1,76 +0,0 @@
-from django import forms
-from django.forms.utils import flatatt
-from django.forms.widgets import HiddenInput
-from django.utils.html import format_html
-from django.utils.translation import pgettext_lazy
-
-from ...interface import PaymentData
-from .utils import get_amount_for_razorpay
-
-CHECKOUT_SCRIPT_URL = "https://checkout.razorpay.com/v1/checkout.js"
-
-PRIMARY_BTN_TEXT = pgettext_lazy(
- "Razorpay payment gateway primary button", "Pay now with Razorpay"
-)
-SECONDARY_TITLE = pgettext_lazy(
- "Razorpay payment gateway secondary title", "Total payment"
-)
-
-
-class RazorPayCheckoutWidget(HiddenInput):
- def __init__(
- self,
- *,
- payment_information: PaymentData,
- public_key: str,
- prefill: bool,
- store_name: str,
- store_image: str,
- **additional_parameters,
- ):
- override_attrs = additional_parameters.get("attrs", None)
- base_attrs = additional_parameters["attrs"] = {
- "src": CHECKOUT_SCRIPT_URL,
- "data-key": public_key,
- "data-buttontext": PRIMARY_BTN_TEXT,
- "data-image": store_image,
- "data-name": store_name,
- "data-description": SECONDARY_TITLE,
- "data-amount": get_amount_for_razorpay(payment_information.amount),
- "data-currency": payment_information.currency,
- }
-
- if prefill:
- customer_name = "%s %s" % (
- payment_information.billing.last_name,
- payment_information.billing.first_name,
- )
- base_attrs.update(
- {
- "data-prefill.name": customer_name,
- "data-prefill.email": payment_information.customer_email,
- }
- )
-
- if override_attrs:
- base_attrs.update(override_attrs)
- super().__init__(attrs=base_attrs)
-
- def render(self, **kwargs):
- attrs = kwargs.setdefault("attrs", {})
- attrs.update(self.attrs)
- return format_html("<script{0}></script>", flatatt(attrs))
-
-
-class RazorPaymentForm(forms.Form):
- razorpay_payment_id = forms.CharField(required=True, widget=forms.HiddenInput)
-
- def __init__(self, payment_information, connection_params, *args, **kwargs):
- super().__init__(*args, **kwargs)
- widget = RazorPayCheckoutWidget(
- payment_information=payment_information, **connection_params
- )
- self.fields["razorpay"] = forms.CharField(widget=widget, required=False)
-
- def get_payment_token(self):
- return self.cleaned_data["razorpay_payment_id"]
diff --git a/saleor/payment/gateways/razorpay/plugin.py b/saleor/payment/gateways/razorpay/plugin.py
index 27730a0db6..4177832c8c 100644
--- a/saleor/payment/gateways/razorpay/plugin.py
+++ b/saleor/payment/gateways/razorpay/plugin.py
@@ -5,13 +5,12 @@ from django.utils.translation import pgettext_lazy
from saleor.extensions import ConfigurationTypeField
from saleor.extensions.base_plugin import BasePlugin
-from . import GatewayConfig, capture, create_form, process_payment, refund
+from . import GatewayConfig, capture, process_payment, refund
GATEWAY_NAME = "Razorpay"
if TYPE_CHECKING:
from . import GatewayResponse, PaymentData
- from django import forms
def require_active_plugin(fn):
@@ -128,16 +127,6 @@ class RazorpayGatewayPlugin(BasePlugin):
) -> "GatewayResponse":
return process_payment(payment_information, self._get_gateway_config())
- @require_active_plugin
- def create_form(
- self, data, payment_information: "PaymentData", previous_value
- ) -> "forms.Form":
- return create_form(
- data,
- payment_information,
- connection_params=self._get_gateway_config().connection_params,
- )
-
@require_active_plugin
def get_payment_config(self, previous_value):
config = self._get_gateway_config()
diff --git a/tests/gateway/test_braintree.py b/tests/gateway/test_braintree.py
index ff39e129a9..ddeec03730 100644
--- a/tests/gateway/test_braintree.py
+++ b/tests/gateway/test_braintree.py
@@ -11,7 +11,6 @@ from saleor.payment.gateways.braintree import (
TransactionKind,
authorize,
capture,
- create_form,
extract_gateway_response,
get_braintree_gateway,
get_client_token,
@@ -25,7 +24,6 @@ from saleor.payment.gateways.braintree.errors import (
DEFAULT_ERROR_MESSAGE,
BraintreeException,
)
-from saleor.payment.gateways.braintree.forms import BraintreePaymentForm
from saleor.payment.interface import (
CreditCardInfo,
CustomerSource,
@@ -237,29 +235,6 @@ def test_get_client_token_with_no_customer_id_when_disabled(
assert token == expected_token
-def test_braintree_payment_form_incorrect_amount(payment_dummy):
- amount = Decimal("0.01")
- data = {"amount": amount, "payment_method_nonce": "fake-nonce"}
- assert amount != payment_dummy.total
- payment_info = create_payment_information(payment_dummy)
-
- form = BraintreePaymentForm(data=data, payment_information=payment_info)
- assert not form.is_valid()
- assert form.non_field_errors
-
-
-def test_braintree_payment_form(payment_dummy):
- payment = payment_dummy
-
- data = {"amount": payment.total, "payment_method_nonce": "fake-nonce"}
- payment_info = create_payment_information(payment)
-
- form = create_form(data=data, payment_information=payment_info)
-
- assert isinstance(form, BraintreePaymentForm)
- assert form.is_valid()
-
-
@pytest.mark.integration
@patch("saleor.payment.gateways.braintree.get_braintree_gateway")
def test_authorize_error_response(
diff --git a/tests/gateway/test_dummy.py b/tests/gateway/test_dummy.py
index e42c522d47..138e4fd4a9 100644
--- a/tests/gateway/test_dummy.py
+++ b/tests/gateway/test_dummy.py
@@ -212,22 +212,3 @@ def test_refund_gateway_error(payment_txn_captured, monkeypatch):
assert txn.payment == payment
assert payment.charge_status == ChargeStatus.FULLY_CHARGED
assert payment.captured_amount == Decimal("80.00")
-
-
- "kind, charge_status",
- (
- (TransactionKind.AUTH, ChargeStatus.NOT_CHARGED),
- (TransactionKind.CAPTURE, ChargeStatus.FULLY_CHARGED),
- (TransactionKind.REFUND, ChargeStatus.FULLY_REFUNDED),
- ),
-)
-def test_dummy_payment_form(kind, charge_status, payment_dummy):
- payment = payment_dummy
- data = {"charge_status": charge_status}
-
- form = gateway.create_payment_form(payment=payment, data=data)
- assert form.is_valid()
- gateway.process_payment(payment=payment, token=form.get_payment_token())
- payment.refresh_from_db()
- assert payment.transactions.last().kind == kind
diff --git a/tests/gateway/test_razorpay.py b/tests/gateway/test_razorpay.py
index f219a78178..a3351300d7 100644
--- a/tests/gateway/test_razorpay.py
+++ b/tests/gateway/test_razorpay.py
@@ -9,7 +9,6 @@ from saleor.payment.gateways.razorpay import (
capture,
check_payment_supported,
clean_razorpay_response,
- create_form,
errors,
get_amount_for_razorpay,
get_client,
@@ -17,10 +16,6 @@ from saleor.payment.gateways.razorpay import (
logger,
refund,
)
-from saleor.payment.gateways.razorpay.forms import (
- RazorPayCheckoutWidget,
- RazorPaymentForm,
-)
from saleor.payment.interface import GatewayConfig
from saleor.payment.utils import create_payment_information
@@ -73,50 +68,6 @@ def charged_payment(razorpay_payment):
return razorpay_payment
-def test_checkout_widget_render_without_prefill(razorpay_payment, gateway_config):
- gateway_config.connection_params["prefill"] = False
- payment_info = create_payment_information(razorpay_payment)
- widget = RazorPayCheckoutWidget(
- payment_information=payment_info,
- attrs={"data-custom": "123"},
- **gateway_config.connection_params,
- )
- assert widget.render() == (
- '<script data-amount="9840" data-buttontext="Pay now with Razorpay" '
- 'data-currency="INR" data-custom="123" '
- 'data-description="Total payment" '
- 'data-image="image.png" data-key="public" data-name="Saleor" '
- 'src="https://checkout.razorpay.com/v1/checkout.js"></script>'
- )
-
-
-def test_checkout_widget_render_with_prefill(razorpay_payment, gateway_config):
- payment_info = create_payment_information(razorpay_payment)
- widget = RazorPayCheckoutWidget(
- payment_information=payment_info, **gateway_config.connection_params
- )
- assert widget.render() == (
- '<script data-amount="9840" data-buttontext="Pay now with Razorpay" '
- 'data-currency="INR" data-description="Total payment" '
- 'data-image="image.png" data-key="public" data-name="Saleor" '
- 'data-prefill.email="[email protected]" '
- 'data-prefill.name="Doe John" '
- 'src="https://checkout.razorpay.com/v1/checkout.js"></script>'
- )
-
-
-def test_checkout_form(razorpay_payment, gateway_config):
- payment_info = create_payment_information(razorpay_payment)
- form = create_form(
- data={"razorpay_payment_id": "123"},
- payment_information=payment_info,
- connection_params=gateway_config.connection_params,
- )
-
- assert isinstance(form, RazorPaymentForm)
- assert form.is_valid()
-
-
def test_check_payment_supported(razorpay_payment):
payment_info = create_payment_information(razorpay_payment)
found_error = check_payment_supported(payment_info)