Skip to content

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

Apply changes after review

Maciej Korycinskia861daee8a94
diff --git a/docs/guides/payments.rst b/docs/guides/payments.rst
index 27afaa932c..61cd55d20d 100644
--- a/docs/guides/payments.rst
+++ b/docs/guides/payments.rst
@@ -39,7 +39,7 @@ Example
.. note::
- All the below functions receive ``payment_information`` as a dataclass: ``PaymentData`` and ``config`` as a dataclass: ``ConfigData``.
+ All the below functions receive ``payment_information`` as a dataclass: ``PaymentData`` and ``config`` as a dataclass: ``GatewayConfig``.
Those functions should return a response as a dataclass: ``GatewayResponse``.
The description of the given structures can be found below.
@@ -57,7 +57,7 @@ Example
def authorize(
payment_information: PaymentData,
- config: ConfigData) -> GatewayResponse:
+ config: GatewayConfig) -> GatewayResponse:
# Handle connecting to the gateway and sending the auth request here
response = gateway.authorize(token=payment_information.token)
@@ -86,7 +86,7 @@ Example
def refund(
payment_information: PaymentData,
- config: ConfigData) -> GatewayResponse:
+ config: GatewayConfig) -> GatewayResponse:
# Handle connecting to the gateway and sending the refund request here
response = gateway.refund(token=payment_information.token)
@@ -115,7 +115,7 @@ Example
def capture(
payment_information: PaymentData,
- config: ConfigData) -> GatewayResponse:
+ config: GatewayConfig) -> GatewayResponse:
# Handle connecting to the gateway and sending the capture request here
response = gateway.capture(token=payment_information.token)
@@ -144,7 +144,7 @@ Example
def void(
payment_information: PaymentData,
- config: ConfigData) -> GatewayResponse:
+ config: GatewayConfig) -> GatewayResponse:
# Handle connecting to the gateway and sending the void request here
response = gateway.void(token=payment_information.token)
@@ -173,7 +173,7 @@ Example
def charge(
payment_information: PaymentData,
- config: ConfigData) -> GatewayResponse:
+ config: GatewayConfig) -> GatewayResponse:
# Handle connecting to the gateway and sending the charge request here
response = gateway.charge(
@@ -206,7 +206,7 @@ Example
def process_payment(
payment_information: PaymentData,
- config: ConfigData) -> GatewayResponse:
+ config: GatewayConfig) -> GatewayResponse:
# Authorize, update the token, then capture
authorize_response = authorize(
@@ -221,13 +221,13 @@ Example
Parameters
^^^^^^^^^^
-+-------------------------+-----------------+-----------------------------------------------------------------------------+
-| name | type | description |
-+-------------------------+-----------------+-----------------------------------------------------------------------------+
-| ``payment_information`` | ``PaymentData`` | Payment information, containing the token, amount, currency and billing. |
-+-------------------------+-----------------+-----------------------------------------------------------------------------+
-| ``config`` | ``ConfigData`` | Configuration of the payment gateway. |
-+-------------------------+-----------------+-----------------------------------------------------------------------------+
++-------------------------+-----------------+---------------------------------------------------------------------------+
+| name | type | description |
++-------------------------+-----------------+---------------------------------------------------------------------------+
+| ``payment_information`` | ``PaymentData`` | Payment information, containing the token, amount, currency and billing. |
++-------------------------+-----------------+---------------------------------------------------------------------------+
+| ``config`` | ``GatewayConfig`` | Configuration of the payment gateway. |
++-------------------------+-----------------+---------------------------------------------------------------------------+
PaymentData
"""""""""""
@@ -280,7 +280,7 @@ AddressData
| phone | ``str`` |
+------------------+---------+
-ConfigData
+GatewayConfig
""""""""""
+---------------------+-----------+---------------------------------------------------------------------------------------------------------+
@@ -288,7 +288,7 @@ ConfigData
+---------------------+-----------+---------------------------------------------------------------------------------------------------------+
| auto_capture | ``bool`` | Define if gateway should also capture funds from the card. If false, payment should be only authorized |
+---------------------+-----------+---------------------------------------------------------------------------------------------------------+
-| template_path | ``str`` | Should specify a path to a template that will be rendered for the checkout. |
+| template_path | ``str`` | Path to a template that will be rendered for the checkout. tests/gateway/test_braintree.py |
+---------------------+-----------+---------------------------------------------------------------------------------------------------------+
| connection_params | ``Dict`` | List of parameters used for connecting to the payment’s gateway. |
+---------------------+-----------+---------------------------------------------------------------------------------------------------------+
@@ -416,7 +416,7 @@ Adding new payment gateway to the settings
.. code-block:: python
PAYMENT_GATEWAYS = {
- 'braintree': {
+ "braintree": {
"module": "saleor.payment.gateways.braintree",
"config": {
"auto_capture": True,
diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.py
index 7551727a68..210c5b33ca 100644
--- a/saleor/payment/__init__.py
+++ b/saleor/payment/__init__.py
@@ -5,7 +5,7 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import pgettext_lazy
-from .interface import ConfigData
+from .interface import GatewayConfig
class PaymentError(Exception):
@@ -120,8 +120,13 @@ def get_payment_gateway(gateway_name):
settings.PAYMENT_GATEWAYS[gateway_name]["module"]
)
+ if "config" not in settings.PAYMENT_GATEWAYS[gateway_name]:
+ raise ImproperlyConfigured(
+ "Payment gateway %s should have own configuration" % gateway_name
+ )
+
gateway_config = settings.PAYMENT_GATEWAYS[gateway_name]["config"]
- config = ConfigData(
+ config = GatewayConfig(
auto_capture=gateway_config["auto_capture"],
template_path=gateway_config["template_path"],
connection_params=gateway_config["connection_params"],
diff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.py
index c7f3f3365a..58ff9f23ab 100644
--- a/saleor/payment/gateways/braintree/__init__.py
+++ b/saleor/payment/gateways/braintree/__init__.py
@@ -6,7 +6,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import pgettext_lazy
from ... import TransactionKind
-from ...interface import ConfigData, GatewayResponse, PaymentData
+from ...interface import GatewayConfig, GatewayResponse, PaymentData
from .errors import DEFAULT_ERROR_MESSAGE, BraintreeException
from .forms import BraintreePaymentForm
@@ -115,7 +115,9 @@ def get_client_token(connection_params: Dict[str, Any]) -> str:
return client_token
-def authorize(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def authorize(
+ payment_information: PaymentData, config: GatewayConfig
+) -> GatewayResponse:
gateway = get_braintree_gateway(**config.connection_params)
try:
@@ -149,7 +151,7 @@ def authorize(payment_information: PaymentData, config: ConfigData) -> GatewayRe
)
-def capture(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
gateway = get_braintree_gateway(**config.connection_params)
try:
@@ -176,7 +178,7 @@ def capture(payment_information: PaymentData, config: ConfigData) -> GatewayResp
)
-def void(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def void(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
gateway = get_braintree_gateway(**config.connection_params)
try:
@@ -200,7 +202,7 @@ def void(payment_information: PaymentData, config: ConfigData) -> GatewayRespons
)
-def refund(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def refund(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
gateway = get_braintree_gateway(**config.connection_params)
try:
@@ -228,7 +230,7 @@ def refund(payment_information: PaymentData, config: ConfigData) -> GatewayRespo
def process_payment(
- payment_information: PaymentData, config: ConfigData
+ payment_information: PaymentData, config: GatewayConfig
) -> GatewayResponse:
auth_resp = authorize(payment_information, config)
return auth_resp
diff --git a/saleor/payment/gateways/dummy/__init__.py b/saleor/payment/gateways/dummy/__init__.py
index cff42e5082..d3ba929785 100644
--- a/saleor/payment/gateways/dummy/__init__.py
+++ b/saleor/payment/gateways/dummy/__init__.py
@@ -1,7 +1,7 @@
import uuid
from ... import ChargeStatus, TransactionKind
-from ...interface import ConfigData, GatewayResponse, PaymentData
+from ...interface import GatewayConfig, GatewayResponse, PaymentData
from .forms import DummyPaymentForm
@@ -17,7 +17,9 @@ def create_form(data, payment_information, connection_params):
return DummyPaymentForm(data=data)
-def authorize(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def authorize(
+ payment_information: PaymentData, config: GatewayConfig
+) -> GatewayResponse:
success = dummy_success()
error = None
if not success:
@@ -32,7 +34,7 @@ def authorize(payment_information: PaymentData, config: ConfigData) -> GatewayRe
)
-def void(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def void(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
error = None
success = dummy_success()
if not success:
@@ -47,7 +49,7 @@ def void(payment_information: PaymentData, config: ConfigData) -> GatewayRespons
)
-def capture(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
"""Perform capture transaction."""
error = None
success = dummy_success()
@@ -64,7 +66,7 @@ def capture(payment_information: PaymentData, config: ConfigData) -> GatewayResp
)
-def refund(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def refund(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
error = None
success = dummy_success()
if not success:
@@ -80,7 +82,7 @@ def refund(payment_information: PaymentData, config: ConfigData) -> GatewayRespo
def process_payment(
- payment_information: PaymentData, config: ConfigData
+ payment_information: PaymentData, config: GatewayConfig
) -> GatewayResponse:
"""Process the payment."""
token = payment_information.token
diff --git a/saleor/payment/gateways/razorpay/__init__.py b/saleor/payment/gateways/razorpay/__init__.py
index 07956d0442..18cf9a1dcd 100644
--- a/saleor/payment/gateways/razorpay/__init__.py
+++ b/saleor/payment/gateways/razorpay/__init__.py
@@ -7,7 +7,7 @@ import razorpay
import razorpay.errors
from ... import TransactionKind
-from ...interface import ConfigData, GatewayResponse, PaymentData
+from ...interface import GatewayConfig, GatewayResponse, PaymentData
from . import errors
from .forms import RazorPaymentForm
from .utils import get_amount_for_razorpay, get_error_response
@@ -86,7 +86,7 @@ def get_client_token(**_):
return str(uuid.uuid4())
-def capture(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
"""Capture a authorized payment using the razorpay client.
But it first check if the given payment instance is supported
by the gateway.
@@ -122,7 +122,7 @@ def capture(payment_information: PaymentData, config: ConfigData) -> GatewayResp
)
-def refund(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def refund(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
"""Refund a payment using the razorpay client.
But it first check if the given payment instance is supported
@@ -156,6 +156,6 @@ def refund(payment_information: PaymentData, config: ConfigData) -> GatewayRespo
def process_payment(
- payment_information: PaymentData, config: ConfigData
+ payment_information: PaymentData, config: GatewayConfig
) -> GatewayResponse:
return capture(payment_information=payment_information, config=config)
diff --git a/saleor/payment/gateways/stripe/__init__.py b/saleor/payment/gateways/stripe/__init__.py
index 6d93ba652a..47ced0388e 100644
--- a/saleor/payment/gateways/stripe/__init__.py
+++ b/saleor/payment/gateways/stripe/__init__.py
@@ -3,7 +3,7 @@ from typing import Dict
import stripe
from ... import TransactionKind
-from ...interface import ConfigData, GatewayResponse, PaymentData
+from ...interface import GatewayConfig, GatewayResponse, PaymentData
from .forms import StripePaymentModalForm
from .utils import (
get_amount_for_stripe,
@@ -22,7 +22,9 @@ def get_client_token(**_):
return
-def authorize(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def authorize(
+ payment_information: PaymentData, config: GatewayConfig
+) -> GatewayResponse:
client, error = _get_client(**config.connection_params), None
try:
@@ -47,7 +49,7 @@ def authorize(payment_information: PaymentData, config: ConfigData) -> GatewayRe
)
-def capture(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
client, error = _get_client(**config.connection_params), None
# Get amount from argument or payment, and convert to stripe's amount
@@ -71,7 +73,7 @@ def capture(payment_information: PaymentData, config: ConfigData) -> GatewayResp
)
-def refund(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def refund(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
client, error = _get_client(**config.connection_params), None
# Get amount from payment, and convert to stripe's amount
@@ -95,7 +97,7 @@ def refund(payment_information: PaymentData, config: ConfigData) -> GatewayRespo
)
-def void(payment_information: PaymentData, config: ConfigData) -> GatewayResponse:
+def void(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
client, error = _get_client(**config.connection_params), None
try:
@@ -116,7 +118,7 @@ def void(payment_information: PaymentData, config: ConfigData) -> GatewayRespons
def process_payment(
- payment_information: PaymentData, config: ConfigData
+ payment_information: PaymentData, config: GatewayConfig
) -> GatewayResponse:
# Stripe supports capture during authorize process. No need to run other steps.
# If 'config.auto_capture is True', it will also capture funds from the card.
diff --git a/saleor/payment/interface.py b/saleor/payment/interface.py
index 286c4b2915..377a84211a 100644
--- a/saleor/payment/interface.py
+++ b/saleor/payment/interface.py
@@ -50,7 +50,7 @@ class PaymentData:
@dataclass
-class ConfigData:
+class GatewayConfig:
"""Dataclass for storing gateway config data. Used for unifying the
representation of config data. It is required to communicate between
Saleor and given payment gateway."""
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index 44bf7f60d2..1d6f96af63 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -11,7 +11,13 @@ from prices import Money
from ..checkout.models import Checkout
from ..core.utils.taxes import zero_money
from ..order.models import Order
-from . import ChargeStatus, CustomPaymentChoices, TransactionError, TransactionKind
+from . import (
+ ChargeStatus,
+ CustomPaymentChoices,
+ TransactionError,
+ TransactionKind,
+ get_payment_gateway,
+)
class Payment(models.Model):
@@ -157,8 +163,8 @@ class Payment(models.Model):
if not (self.is_active and self.not_charged):
return False
- gateway_config = settings.PAYMENT_GATEWAYS[self.gateway]["config"]
- if gateway_config["auto_capture"]:
+ _, gateway_config = get_payment_gateway(self.gateway)
+ if gateway_config.auto_capture:
return self.is_authorized
return True
diff --git a/tests/gateway/test_braintree.py b/tests/gateway/test_braintree.py
index 10115ee9e9..2fd242ab73 100644
--- a/tests/gateway/test_braintree.py
+++ b/tests/gateway/test_braintree.py
@@ -29,7 +29,7 @@ from saleor.payment.gateways.braintree.errors import (
BraintreeException,
)
from saleor.payment.gateways.braintree.forms import BraintreePaymentForm
-from saleor.payment.interface import ConfigData
+from saleor.payment.interface import GatewayConfig
from saleor.payment.utils import create_payment_information
INCORRECT_TOKEN_ERROR = (
@@ -89,7 +89,7 @@ def braintree_not_found_error():
@pytest.fixture
def gateway_config():
- return ConfigData(
+ return GatewayConfig(
template_path="template.html",
auto_capture=False,
connection_params={
diff --git a/tests/gateway/test_razorpay.py b/tests/gateway/test_razorpay.py
index 7f5d003ce3..f5df87be2a 100644
--- a/tests/gateway/test_razorpay.py
+++ b/tests/gateway/test_razorpay.py
@@ -21,7 +21,7 @@ from saleor.payment.gateways.razorpay.forms import (
RazorPayCheckoutWidget,
RazorPaymentForm,
)
-from saleor.payment.interface import ConfigData
+from saleor.payment.interface import GatewayConfig
from saleor.payment.utils import create_payment_information
TRANSACTION_AMOUNT = Decimal("61.33")
@@ -29,7 +29,7 @@ TRANSACTION_AMOUNT = Decimal("61.33")
@pytest.fixture
def gateway_config():
- return ConfigData(
+ return GatewayConfig(
auto_capture=False,
template_path="template.html",
connection_params={
diff --git a/tests/gateway/test_stripe.py b/tests/gateway/test_stripe.py
index 59ecdc43d8..6f9d72a6cd 100644
--- a/tests/gateway/test_stripe.py
+++ b/tests/gateway/test_stripe.py
@@ -32,7 +32,7 @@ from saleor.payment.gateways.stripe.utils import (
get_payment_billing_fullname,
shipping_to_stripe_dict,
)
-from saleor.payment.interface import AddressData, ConfigData
+from saleor.payment.interface import AddressData, GatewayConfig
from saleor.payment.utils import create_payment_information
TRANSACTION_AMOUNT = Decimal(42.42)
@@ -45,7 +45,7 @@ ERROR_MESSAGE = "error-message"
@pytest.fixture()
def gateway_config():
- return ConfigData(
+ return GatewayConfig(
auto_capture=False,
template_path="template.html",
connection_params={
diff --git a/tests/test_payment.py b/tests/test_payment.py
index da6c9c29f1..064487af55 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -16,7 +16,7 @@ from saleor.payment import (
TransactionKind,
get_payment_gateway,
)
-from saleor.payment.interface import ConfigData, GatewayResponse
+from saleor.payment.interface import GatewayConfig, GatewayResponse
from saleor.payment.models import Payment
from saleor.payment.utils import (
ALLOWED_GATEWAY_KINDS,
@@ -73,7 +73,7 @@ def transaction_data(payment_dummy, gateway_response):
@pytest.fixture
def gateway_config():
- return ConfigData(
+ return GatewayConfig(
auto_capture=True,
template_path="template.html",
connection_params={"secret-key": "nobodylikesspanishinqusition"},
@@ -497,8 +497,7 @@ def test_gateway_charge_failed(
def test_gateway_charge_errors(payment_dummy, transaction_token, settings):
payment = payment_dummy
- settings.PAYMENT_GATEWAYS = {payment.gateway: {"config": {"auto_capture": False}}}
-
+ gateway_authorize(payment, transaction_token)
with pytest.raises(PaymentError) as exc:
gateway_capture(payment, Decimal("0"))
assert exc.value.message == "Amount should be a positive number."
@@ -657,27 +656,6 @@ def test_gateway_refund_errors(payment_txn_captured):
assert exc.value.message == "This payment cannot be refunded."
[email protected]("gateway_name", dj_settings.PAYMENT_GATEWAYS.keys())
-def test_payment_gateway_templates_exists(gateway_name):
- """Test if for each payment gateway there's a corresponding
- template for the old checkout.
- """
- template = PAYMENT_TEMPLATE % gateway_name
- get_template(template)
-
-
[email protected]("gateway_name", dj_settings.PAYMENT_GATEWAYS.keys())
-def test_payment_gateway_form_exists(gateway_name, payment_dummy):
- """Test if for each payment gateway there's a corresponding
- form for the old checkout.
-
- An error will be raised if it's missing.
- """
- payment_gateway, gateway_config = get_payment_gateway(gateway_name)
- payment_info = create_payment_information(payment_dummy)
- payment_gateway.create_form(None, payment_info, gateway_config.connection_params)
-
-
def test_clean_authorize():
payment = Mock(can_authorize=Mock(return_value=True))
clean_authorize(payment)