Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Apply changes after review
Maciej Korycinskia861daee8a94
diff --git a/docs/guides/payments.rst b/docs/guides/payments.rstindex 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 @@ Exampledef authorize(payment_information: PaymentData,- config: ConfigData) -> GatewayResponse:+ config: GatewayConfig) -> GatewayResponse:# Handle connecting to the gateway and sending the auth request hereresponse = gateway.authorize(token=payment_information.token)@@ -86,7 +86,7 @@ Exampledef refund(payment_information: PaymentData,- config: ConfigData) -> GatewayResponse:+ config: GatewayConfig) -> GatewayResponse:# Handle connecting to the gateway and sending the refund request hereresponse = gateway.refund(token=payment_information.token)@@ -115,7 +115,7 @@ Exampledef capture(payment_information: PaymentData,- config: ConfigData) -> GatewayResponse:+ config: GatewayConfig) -> GatewayResponse:# Handle connecting to the gateway and sending the capture request hereresponse = gateway.capture(token=payment_information.token)@@ -144,7 +144,7 @@ Exampledef void(payment_information: PaymentData,- config: ConfigData) -> GatewayResponse:+ config: GatewayConfig) -> GatewayResponse:# Handle connecting to the gateway and sending the void request hereresponse = gateway.void(token=payment_information.token)@@ -173,7 +173,7 @@ Exampledef charge(payment_information: PaymentData,- config: ConfigData) -> GatewayResponse:+ config: GatewayConfig) -> GatewayResponse:# Handle connecting to the gateway and sending the charge request hereresponse = gateway.charge(@@ -206,7 +206,7 @@ Exampledef process_payment(payment_information: PaymentData,- config: ConfigData) -> GatewayResponse:+ config: GatewayConfig) -> GatewayResponse:# Authorize, update the token, then captureauthorize_response = authorize(@@ -221,13 +221,13 @@ ExampleParameters^^^^^^^^^^-+-------------------------+-----------------+-----------------------------------------------------------------------------+-| 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:: pythonPAYMENT_GATEWAYS = {- 'braintree': {+ "braintree": {"module": "saleor.payment.gateways.braintree","config": {"auto_capture": True,diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.pyindex 7551727a68..210c5b33ca 100644--- a/saleor/payment/__init__.py+++ b/saleor/payment/__init__.py@@ -5,7 +5,7 @@ from django.conf import settingsfrom django.core.exceptions import ImproperlyConfiguredfrom django.utils.translation import pgettext_lazy-from .interface import ConfigData+from .interface import GatewayConfigclass 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__.pyindex 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 ImproperlyConfiguredfrom django.utils.translation import pgettext_lazyfrom ... import TransactionKind-from ...interface import ConfigData, GatewayResponse, PaymentData+from ...interface import GatewayConfig, GatewayResponse, PaymentDatafrom .errors import DEFAULT_ERROR_MESSAGE, BraintreeExceptionfrom .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) -> GatewayRespodef process_payment(- payment_information: PaymentData, config: ConfigData+ payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:auth_resp = authorize(payment_information, config)return auth_respdiff --git a/saleor/payment/gateways/dummy/__init__.py b/saleor/payment/gateways/dummy/__init__.pyindex cff42e5082..d3ba929785 100644--- a/saleor/payment/gateways/dummy/__init__.py+++ b/saleor/payment/gateways/dummy/__init__.py@@ -1,7 +1,7 @@import uuidfrom ... import ChargeStatus, TransactionKind-from ...interface import ConfigData, GatewayResponse, PaymentData+from ...interface import GatewayConfig, GatewayResponse, PaymentDatafrom .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 = Noneif 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 = Nonesuccess = 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 = Nonesuccess = 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 = Nonesuccess = dummy_success()if not success:@@ -80,7 +82,7 @@ def refund(payment_information: PaymentData, config: ConfigData) -> GatewayRespodef process_payment(- payment_information: PaymentData, config: ConfigData+ payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:"""Process the payment."""token = payment_information.tokendiff --git a/saleor/payment/gateways/razorpay/__init__.py b/saleor/payment/gateways/razorpay/__init__.pyindex 07956d0442..18cf9a1dcd 100644--- a/saleor/payment/gateways/razorpay/__init__.py+++ b/saleor/payment/gateways/razorpay/__init__.py@@ -7,7 +7,7 @@ import razorpayimport razorpay.errorsfrom ... import TransactionKind-from ...interface import ConfigData, GatewayResponse, PaymentData+from ...interface import GatewayConfig, GatewayResponse, PaymentDatafrom . import errorsfrom .forms import RazorPaymentFormfrom .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 supportedby 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) -> GatewayRespodef 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__.pyindex 6d93ba652a..47ced0388e 100644--- a/saleor/payment/gateways/stripe/__init__.py+++ b/saleor/payment/gateways/stripe/__init__.py@@ -3,7 +3,7 @@ from typing import Dictimport stripefrom ... import TransactionKind-from ...interface import ConfigData, GatewayResponse, PaymentData+from ...interface import GatewayConfig, GatewayResponse, PaymentDatafrom .forms import StripePaymentModalFormfrom .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), Nonetry:@@ -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), Nonetry:@@ -116,7 +118,7 @@ def void(payment_information: PaymentData, config: ConfigData) -> GatewayResponsdef 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.pyindex 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 therepresentation of config data. It is required to communicate betweenSaleor and given payment gateway."""diff --git a/saleor/payment/models.py b/saleor/payment/models.pyindex 44bf7f60d2..1d6f96af63 100644--- a/saleor/payment/models.py+++ b/saleor/payment/models.py@@ -11,7 +11,13 @@ from prices import Moneyfrom ..checkout.models import Checkoutfrom ..core.utils.taxes import zero_moneyfrom ..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_authorizedreturn Truediff --git a/tests/gateway/test_braintree.py b/tests/gateway/test_braintree.pyindex 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 GatewayConfigfrom saleor.payment.utils import create_payment_informationINCORRECT_TOKEN_ERROR = (@@ -89,7 +89,7 @@ def braintree_not_found_error():@pytest.fixturedef 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.pyindex 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 GatewayConfigfrom saleor.payment.utils import create_payment_informationTRANSACTION_AMOUNT = Decimal("61.33")@@ -29,7 +29,7 @@ TRANSACTION_AMOUNT = Decimal("61.33")@pytest.fixturedef 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.pyindex 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, GatewayConfigfrom saleor.payment.utils import create_payment_informationTRANSACTION_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.pyindex 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, GatewayResponsefrom saleor.payment.models import Paymentfrom saleor.payment.utils import (ALLOWED_GATEWAY_KINDS,@@ -73,7 +73,7 @@ def transaction_data(payment_dummy, gateway_response):@pytest.fixturedef 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)