Skip to content

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

Run create_form on payment plugin

Jakub Draganek162dfcd06a33
diff --git a/saleor/core/payments.py b/saleor/core/payments.py
index 2726cac06d..204dba31f3 100644
--- a/saleor/core/payments.py
+++ b/saleor/core/payments.py
@@ -55,6 +55,12 @@ class PaymentInterface(ABC):
) -> "GatewayResponse":
pass
+ @abstractmethod
+ def create_payment_form(
+ self, data, gateway: Gateway, payment_information: "PaymentData"
+ ) -> "GatewayResponse":
+ pass
+
@abstractmethod
def list_payment_sources(self, customer_id: str) -> List["CustomerSource"]:
pass
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index f5b98b973a..f2cd5ac87d 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -26,10 +26,7 @@ from ...order.utils import (
recalculate_order,
)
from ...payment import ChargeStatus, CustomPaymentChoices, PaymentError, gateway
-from ...payment.utils import (
- clean_mark_order_as_paid,
- mark_order_as_paid,
-)
+from ...payment.utils import clean_mark_order_as_paid, mark_order_as_paid
from ...product.models import Product, ProductVariant
from ...product.utils import allocate_stock, deallocate_stock
from ...shipping.models import ShippingMethod
diff --git a/saleor/extensions/base_plugin.py b/saleor/extensions/base_plugin.py
index 7fc94ae316..3782ccc0c5 100644
--- a/saleor/extensions/base_plugin.py
+++ b/saleor/extensions/base_plugin.py
@@ -177,6 +177,9 @@ class BasePlugin:
) -> List["CustomerSource"]:
pass
+ def create_form(self, data, payment_information):
+ pass
+
@classmethod
def _update_config_items(
cls, configuration_to_update: List[dict], current_config: List[dict]
diff --git a/saleor/extensions/manager.py b/saleor/extensions/manager.py
index 422bc0f289..7873084f93 100644
--- a/saleor/extensions/manager.py
+++ b/saleor/extensions/manager.py
@@ -216,6 +216,12 @@ class ExtensionsManager(PaymentInterface):
method_name = "process_payment"
return self.__run_payment_method(gateway, method_name, payment_information)
+ def create_payment_form(self, gateway, data, payment_information):
+ method_name = "create_form"
+ return self.__run_payment_method(
+ gateway, method_name, payment_information, data=data
+ )
+
def list_payment_sources(self, customer_id: str) -> List["CustomerSource"]:
default_value = []
return self.__run_method_on_plugins(
@@ -223,14 +229,22 @@ class ExtensionsManager(PaymentInterface):
)
def __run_payment_method(
- self, gateway: Gateway, method_name: str, payment_information: "PaymentData"
+ self,
+ gateway: Gateway,
+ method_name: str,
+ payment_information: "PaymentData",
+ **kwargs,
) -> Optional["GatewayResposne"]:
default_value = None
gateway_name = gateway.value
gtw = self.get_plugin(gateway_name)
if gtw is not None:
return self.__run_method_on_single_plugin(
- gtw, method_name, default_value, payment_information=payment_information
+ gtw,
+ method_name,
+ default_value,
+ payment_information=payment_information,
+ **kwargs,
)
raise Exception(f"Payment plugin {gateway_name} is inaccessible!")
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index b38d342bac..9335554c0f 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -11,10 +11,7 @@ from ....order.utils import (
recalculate_order,
)
from ....payment import CustomPaymentChoices, PaymentError, gateway
-from ....payment.utils import (
- clean_mark_order_as_paid,
- mark_order_as_paid,
-)
+from ....payment.utils import clean_mark_order_as_paid, mark_order_as_paid
from ...account.types import AddressInput
from ...core.mutations import BaseMutation
from ...core.scalars import Decimal
diff --git a/saleor/order/views.py b/saleor/order/views.py
index 4e742b38e4..047ff65333 100644
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -14,11 +14,8 @@ from ..account.forms import LoginForm
from ..account.models import User
from ..core.utils import get_client_ip
from ..payment import ChargeStatus, TransactionKind, get_payment_gateway
-from ..payment.utils import (
- create_payment,
- create_payment_information,
- gateway_process_payment,
-)
+from ..payment import gateway as payment_gateway
+from ..payment.utils import create_payment, create_payment_information
from . import FulfillmentStatus
from .forms import CustomerNoteForm, PasswordForm, PaymentDeleteForm, PaymentsForm
from .models import Order
@@ -92,8 +89,7 @@ def payment(request, token):
@check_order_status
def start_payment(request, order, gateway):
- payment_gateway, gateway_config = get_payment_gateway(gateway)
- connection_params = gateway_config.connection_params
+ _, gateway_config = get_payment_gateway(gateway)
extra_data = {"customer_user_agent": request.META.get("HTTP_USER_AGENT")}
with transaction.atomic():
payment = create_payment(
@@ -113,16 +109,11 @@ def start_payment(request, order, gateway):
):
return redirect(order.get_absolute_url())
- payment_info = create_payment_information(payment)
- form = payment_gateway.create_form(
- data=request.POST or None,
- payment_information=payment_info,
- connection_params=connection_params,
- )
+ form = payment_gateway.create_payment_form(payment, data=request.POST or None)
if form.is_valid():
try:
- gateway_process_payment(
- payment=payment, payment_token=form.get_payment_token()
+ payment_gateway.process_payment(
+ payment=payment, token=form.get_payment_token()
)
except Exception as exc:
form.add_error(None, str(exc))
@@ -131,11 +122,11 @@ def start_payment(request, order, gateway):
return redirect("order:payment-success", token=order.token)
return redirect(order.get_absolute_url())
- client_token = payment_gateway.get_client_token(config=gateway_config)
+ # client_token = payment_gateway.get_client_token(config=gateway_config)
ctx = {
"form": form,
"payment": payment,
- "client_token": client_token,
+ # "client_token": client_token,
"order": order,
}
return TemplateResponse(request, gateway_config.template_path, ctx)
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 8d920b1da3..23196b46cc 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -1,6 +1,7 @@
import logging
from decimal import Decimal
+from django import forms
from django.core.exceptions import ImproperlyConfigured
from ..core.payments import Gateway
@@ -190,6 +191,14 @@ 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)
+ gateway = _get_gateway(payment)
+ return plugin_manager.create_payment_form(gateway, data, payment_data)
+
+
def _get_gateway(payment: Payment) -> Gateway:
try:
gateway = Gateway(payment.gateway)
diff --git a/saleor/payment/gateways/dummy/plugin.py b/saleor/payment/gateways/dummy/plugin.py
index 45bd8d3685..db307460f8 100644
--- a/saleor/payment/gateways/dummy/plugin.py
+++ b/saleor/payment/gateways/dummy/plugin.py
@@ -6,7 +6,16 @@ from django.utils.translation import pgettext_lazy
from saleor.extensions import ConfigurationTypeField
from saleor.extensions.base_plugin import BasePlugin
-from . import GatewayConfig, authorize, capture, confirm, process_payment, refund, void
+from . import (
+ GatewayConfig,
+ authorize,
+ capture,
+ confirm,
+ create_form,
+ process_payment,
+ refund,
+ void,
+)
GATEWAY_NAME = "dummy"
@@ -115,3 +124,8 @@ class DummyGatewayPlugin(BasePlugin):
self, payment_information: "PaymentData", previous_value
) -> "GatewayResponse":
return process_payment(payment_information, self._get_gateway_config())
+
+ def create_form(
+ self, data, payment_information: "PaymentData", previous_value
+ ) -> "forms.Form":
+ return create_form(data, payment_information, {})
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 2d20986cf9..6578ea394f 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -408,23 +408,6 @@ def gateway_postprocess(transaction, payment):
payment.save(update_fields=changed_fields)
-@require_active_payment
-def gateway_process_payment(
- payment: Payment, payment_token: str, **extras
-) -> Transaction:
- """Perform the whole payment process on a gateway."""
- transaction = call_gateway(
- operation_type=OperationType.PROCESS_PAYMENT,
- payment=payment,
- payment_token=payment_token,
- amount=payment.total,
- **extras,
- )
-
- gateway_postprocess(transaction, payment)
- return transaction
-
-
def fetch_customer_id(user, gateway):
"""Retrieve users customer_id stored for desired gateway."""
key = prepare_namespace_name(gateway)
diff --git a/tests/test_payment.py b/tests/test_payment.py
index 48075834ae..bfca985187 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -33,7 +33,6 @@ from saleor.payment.utils import (
create_payment_information,
create_transaction,
gateway_get_client_token,
- gateway_process_payment,
handle_fully_paid_order,
mark_order_as_paid,
require_active_payment,