Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix fetch checkout info resolver for checkout (#13755)
Jakub82c56bde0e34
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2cebff552c..d2e73638e1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -193,6 +193,9 @@ Shipping methods can be removed by the user after it has been assigned to a chec- `preprocess_order_creation`This breaking change affect any custom plugins in open-source Saleor, if they override any of the above mentioned methods.+- The signature of the `list_payment_gateways` manager method has changed. It may affect Saleor open-source users, who maintain plugins overriding this method. Changes:+ - The `checkout: Checkout` argument was removed+ - `checkout_info: CheckoutInfo` and `checkout_lines: Iterable[CheckoutLineInfo]` arguments were added instead### GraphQL APIdiff --git a/saleor/core/payments.py b/saleor/core/payments.pyindex 64f66b6218..c8ed53659b 100644--- a/saleor/core/payments.py+++ b/saleor/core/payments.py@@ -1,8 +1,9 @@from abc import ABC, abstractmethod-from typing import TYPE_CHECKING, List, Optional+from typing import TYPE_CHECKING, Iterable, List, Optional++from ..checkout.fetch import CheckoutInfo, CheckoutLineInfoif TYPE_CHECKING:- from ..checkout.models import Checkoutfrom ..payment.interface import (CustomerSource,GatewayResponse,@@ -17,7 +18,8 @@ class PaymentInterface(ABC):def list_payment_gateways(self,currency: Optional[str] = None,- checkout: Optional["Checkout"] = None,+ checkout_info: Optional["CheckoutInfo"] = None,+ checkout_lines: Optional[Iterable["CheckoutLineInfo"]] = None,channel_slug: Optional[str] = None,active_only: bool = True,) -> List["PaymentGateway"]:diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.pyindex 75113a0bac..7a261d4334 100644--- a/saleor/graphql/checkout/tests/test_checkout.py+++ b/saleor/graphql/checkout/tests/test_checkout.py@@ -203,6 +203,33 @@ def test_checkout_available_payment_gateways(][email protected]("saleor.plugins.manager.PluginsManager.list_payment_gateways")+def test_checkout_available_payment_gateways_valid_info_sent(+ mocked_list_gateways,+ api_client,+ checkout_with_item,+ checkout_info,+ checkout_lines_info,+):+ # given+ checkout = checkout_with_item+ channel_slug = checkout.channel.slug+ currency = checkout.currency+ query = GET_CHECKOUT_PAYMENTS_QUERY+ variables = {"id": to_global_id_or_none(checkout_with_item)}++ # when+ api_client.post_graphql(query, variables)++ # then+ mocked_list_gateways.assert_called_with(+ currency=currency,+ checkout_info=checkout_info,+ checkout_lines=checkout_lines_info,+ channel_slug=channel_slug,+ )++def test_checkout_available_payment_gateways_currency_specified_USD(api_client,checkout_with_item,diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.pyindex ae3a6875eb..afa54b7b12 100644--- a/saleor/graphql/checkout/types.py+++ b/saleor/graphql/checkout/types.py@@ -937,10 +937,24 @@ class Checkout(ModelObjectType[models.Checkout]):@prevent_sync_event_circular_query@plugin_manager_promise_callbackdef resolve_available_payment_gateways(- root: models.Checkout, _info: ResolveInfo, manager+ root: models.Checkout, info: ResolveInfo, manager):- return manager.list_payment_gateways(- currency=root.currency, checkout=root, channel_slug=root.channel.slug+ checkout_info = CheckoutInfoByCheckoutTokenLoader(info.context).load(root.token)+ checkout_lines_info = CheckoutLinesInfoByCheckoutTokenLoader(info.context).load(+ root.token+ )++ def get_available_payment_gateways(results):+ (checkout, lines_info) = results+ return manager.list_payment_gateways(+ currency=root.currency,+ checkout_info=checkout,+ checkout_lines=lines_info,+ channel_slug=root.channel.slug,+ )++ return Promise.all([checkout_info, checkout_lines_info]).then(+ get_available_payment_gateways)@staticmethoddiff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.pyindex a1dff0a752..0f14fe5825 100644--- a/saleor/payment/gateways/adyen/plugin.py+++ b/saleor/payment/gateways/adyen/plugin.py@@ -1,5 +1,5 @@import json-from typing import List, Optional+from typing import Iterable, List, Optionalfrom urllib.parse import urlencode, urljoinimport opentracing@@ -11,7 +11,7 @@ from django.http import HttpResponse, HttpResponseNotFoundfrom django.urls import reversefrom requests.exceptions import SSLError-from ....checkout.models import Checkout+from ....checkout.fetch import CheckoutInfo, CheckoutLineInfofrom ....core.utils import build_absolute_urifrom ....core.utils.url import prepare_urlfrom ....order.events import external_notification_event@@ -299,7 +299,11 @@ class AdyenGatewayPlugin(BasePlugin):return previous_valuedef get_payment_gateways(- self, currency: Optional[str], checkout: Optional["Checkout"], previous_value+ self,+ currency: Optional[str],+ checkout_info: Optional["CheckoutInfo"],+ checkout_lines: Optional[Iterable["CheckoutLineInfo"]],+ previous_value,) -> List["PaymentGateway"]:"""Fetch current configuration for given checkout.@@ -321,11 +325,13 @@ class AdyenGatewayPlugin(BasePlugin):}]- if checkout:+ if checkout_info:# If checkout is available, fetch available payment methods from Adyen API# and append them to the config object returned for the gateway.request = request_data_for_gateway_config(- checkout, local_config.connection_params["merchant_account"]+ checkout_info,+ checkout_lines,+ local_config.connection_params["merchant_account"],)with opentracing.global_tracer().start_active_span("adyen.checkout.payment_methods"diff --git a/saleor/payment/gateways/adyen/tests/test_plugin.py b/saleor/payment/gateways/adyen/tests/test_plugin.pyindex 1cc593b15f..efdcdda36d 100644--- a/saleor/payment/gateways/adyen/tests/test_plugin.py+++ b/saleor/payment/gateways/adyen/tests/test_plugin.py@@ -54,13 +54,16 @@ def test_process_additional_action(@pytest.mark.vcrdef test_get_payment_gateway_for_checkout(- adyen_plugin, checkout_with_single_item, address+ adyen_plugin, checkout_with_single_item, checkout_info, address, checkout_lines_info):checkout_with_single_item.billing_address = addresscheckout_with_single_item.save()adyen_plugin = adyen_plugin()response = adyen_plugin.get_payment_gateways(- currency=None, checkout=checkout_with_single_item, previous_value=None+ currency=None,+ checkout_info=checkout_info,+ checkout_lines=checkout_lines_info,+ previous_value=None,)[0]assert response.id == adyen_plugin.PLUGIN_IDassert response.name == adyen_plugin.PLUGIN_NAMEdiff --git a/saleor/payment/gateways/adyen/tests/utils/test_common.py b/saleor/payment/gateways/adyen/tests/utils/test_common.pyindex cb1ad391a2..416da7c52b 100644--- a/saleor/payment/gateways/adyen/tests/utils/test_common.py+++ b/saleor/payment/gateways/adyen/tests/utils/test_common.py@@ -5,6 +5,7 @@ from unittest import mockimport pytestfrom prices import Money, TaxedMoney+from saleor.checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom saleor.core.prices import quantize_pricefrom saleor.payment import PaymentErrorfrom saleor.payment.gateways.adyen.utils.common import (@@ -18,6 +19,7 @@ from saleor.payment.gateways.adyen.utils.common import ()from saleor.payment.interface import PaymentMethodInfofrom saleor.payment.utils import price_from_minor_unit, price_to_minor_unit+from saleor.plugins.manager import get_plugins_managerfrom ...utils.common import prepare_address_request_data@@ -709,10 +711,12 @@ def test_request_data_for_gateway_config(checkout_with_item, address):# givencheckout_with_item.billing_address = addressmerchant_account = "test_account"-+ manager = get_plugins_manager()+ lines_info, _ = fetch_checkout_lines(checkout_with_item)+ checkout_info = fetch_checkout_info(checkout_with_item, lines_info, manager)# whenresponse_config = request_data_for_gateway_config(- checkout_with_item, merchant_account+ checkout_info, lines_info, merchant_account)# then@@ -727,9 +731,14 @@ def test_request_data_for_gateway_config(checkout_with_item, address):def test_request_data_for_gateway_config_no_country(checkout, address, settings):# givenmerchant_account = "test_account"+ manager = get_plugins_manager()+ lines_info, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines_info, manager)# when- response_config = request_data_for_gateway_config(checkout, merchant_account)+ response_config = request_data_for_gateway_config(+ checkout_info, lines_info, merchant_account+ )# thenassert response_config == {diff --git a/saleor/payment/gateways/adyen/utils/common.py b/saleor/payment/gateways/adyen/utils/common.pyindex 4bbc7ea890..e926f152cc 100644--- a/saleor/payment/gateways/adyen/utils/common.py+++ b/saleor/payment/gateways/adyen/utils/common.py@@ -1,7 +1,7 @@import jsonimport loggingfrom decimal import Decimal-from typing import TYPE_CHECKING, Any, Callable, Dict, Optional+from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, Optionalimport Adyenimport opentracing@@ -16,7 +16,12 @@ from .....checkout.calculations import (checkout_shipping_price,checkout_total,)-from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines+from .....checkout.fetch import (+ CheckoutInfo,+ CheckoutLineInfo,+ fetch_checkout_info,+ fetch_checkout_lines,+)from .....checkout.models import Checkoutfrom .....checkout.utils import get_checkout_metadata, is_shipping_requiredfrom .....payment.models import Payment@@ -328,12 +333,14 @@ def get_shopper_locale_value(country_code: str):def request_data_for_gateway_config(- checkout: "Checkout", merchant_account+ checkout_info: "CheckoutInfo",+ lines: Optional[Iterable[CheckoutLineInfo]],+ merchant_account,) -> Dict[str, Any]:manager = get_plugins_manager()- address = checkout.billing_address or checkout.shipping_address- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, manager)+ checkout = checkout_info.checkout+ address = checkout_info.billing_address or checkout_info.shipping_address+ lines = lines or []total = checkout_total(manager=manager,checkout_info=checkout_info,diff --git a/saleor/payment/gateways/authorize_net/tests/test_plugin.py b/saleor/payment/gateways/authorize_net/tests/test_plugin.pyindex 600253d2a4..f43d4c8a73 100644--- a/saleor/payment/gateways/authorize_net/tests/test_plugin.py+++ b/saleor/payment/gateways/authorize_net/tests/test_plugin.py@@ -8,12 +8,19 @@ from ..plugin import AuthorizeNetGatewayPlugindef test_get_payment_gateway_for_checkout(- authorize_net_plugin, checkout_with_single_item, address+ authorize_net_plugin,+ checkout_with_single_item,+ address,+ checkout_info,+ checkout_lines_info,):checkout_with_single_item.billing_address = addresscheckout_with_single_item.save()response = authorize_net_plugin.get_payment_gateways(- currency=None, checkout=checkout_with_single_item, previous_value=None+ currency=None,+ checkout_info=checkout_info,+ checkout_lines=checkout_lines_info,+ previous_value=None,)[0]assert response.id == authorize_net_plugin.PLUGIN_IDassert response.name == authorize_net_plugin.PLUGIN_NAME@@ -38,12 +45,15 @@ def test_get_payment_gateway_for_checkout(def test_get_payment_gateway_for_checkout_inactive(- authorize_net_plugin, checkout_with_single_item+ authorize_net_plugin, checkout_with_single_item, checkout_info, checkout_lines_info):authorize_net_plugin.active = Falsecurrency = checkout_with_single_item.currencyresponse = authorize_net_plugin.get_payment_gateways(- currency=currency, checkout=checkout_with_single_item, previous_value=None+ currency=currency,+ checkout_info=checkout_info,+ checkout_lines=checkout_lines_info,+ previous_value=None,)assert not responsediff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.pyindex 357d60d779..a9c6f152e8 100644--- a/saleor/plugins/base_plugin.py+++ b/saleor/plugins/base_plugin.py@@ -1163,7 +1163,11 @@ class BasePlugin:return previous_valuedef get_payment_gateways(- self, currency: Optional[str], checkout: Optional["Checkout"], previous_value+ self,+ currency: Optional[str],+ checkout_info: Optional["CheckoutInfo"],+ checkout_lines: Optional[Iterable["CheckoutLineInfo"]],+ previous_value,) -> List["PaymentGateway"]:payment_config = (self.get_payment_config(previous_value)diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.pyindex cd92e19aeb..8494f6c780 100644--- a/saleor/plugins/manager.py+++ b/saleor/plugins/manager.py@@ -1585,11 +1585,12 @@ class PluginsManager(PaymentInterface):def list_payment_gateways(self,currency: Optional[str] = None,- checkout: Optional["Checkout"] = None,+ checkout_info: Optional["CheckoutInfo"] = None,+ checkout_lines: Optional[Iterable["CheckoutLineInfo"]] = None,channel_slug: Optional[str] = None,active_only: bool = True,) -> List["PaymentGateway"]:- channel_slug = checkout.channel.slug if checkout else channel_slug+ channel_slug = checkout_info.channel.slug if checkout_info else channel_slugplugins = self.get_plugins(channel_slug=channel_slug, active_only=active_only)payment_plugins = [plugin for plugin in plugins if "process_payment" in type(plugin).__dict__@@ -1600,7 +1601,10 @@ class PluginsManager(PaymentInterface):for plugin in payment_plugins:gateways.extend(plugin.get_payment_gateways(- currency=currency, checkout=checkout, previous_value=None+ currency=currency,+ checkout_info=checkout_info,+ checkout_lines=checkout_lines,+ previous_value=None,))return gatewaysdiff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.pyindex c09c1cb656..a6e3c02e01 100644--- a/saleor/plugins/webhook/plugin.py+++ b/saleor/plugins/webhook/plugin.py@@ -1,11 +1,22 @@import jsonimport loggingfrom decimal import Decimal-from typing import TYPE_CHECKING, Any, DefaultDict, Final, List, Optional, Set, Union+from typing import (+ TYPE_CHECKING,+ Any,+ DefaultDict,+ Final,+ Iterable,+ List,+ Optional,+ Set,+ Union,+)import graphenefrom ...app.models import App+from ...checkout.fetch import CheckoutInfo, CheckoutLineInfofrom ...checkout.models import Checkoutfrom ...core import EventDeliveryStatusfrom ...core.models import EventDelivery@@ -2023,11 +2034,15 @@ class WebhookPlugin(BasePlugin):def get_payment_gateways(self,currency: Optional[str],- checkout: Optional["Checkout"],+ checkout_info: Optional["CheckoutInfo"],+ checkout_lines: Optional[Iterable["CheckoutLineInfo"]],previous_value,**kwargs) -> List["PaymentGateway"]:gateways = []+ checkout = None+ if checkout_info:+ checkout = checkout_info.checkoutevent_type = WebhookEventSyncType.PAYMENT_LIST_GATEWAYSwebhooks = get_webhooks_for_event(event_type)for webhook in webhooks:diff --git a/saleor/plugins/webhook/tests/test_payment_webhook.py b/saleor/plugins/webhook/tests/test_payment_webhook.pyindex 77f5acaa39..e769dba422 100644--- a/saleor/plugins/webhook/tests/test_payment_webhook.py+++ b/saleor/plugins/webhook/tests/test_payment_webhook.py@@ -286,7 +286,7 @@ def test_get_payment_gateways(}]mock_send_request.return_value = mock_json_response- response_data = plugin.get_payment_gateways("USD", None, None)+ response_data = plugin.get_payment_gateways("USD", None, None, None)expected_response_1 = parse_list_payment_gateways_response(mock_json_response, payment_app)@@ -319,7 +319,7 @@ def test_get_payment_gateways_with_transactions(plugin = webhook_plugin()# when- response_data = plugin.get_payment_gateways("USD", None, None)+ response_data = plugin.get_payment_gateways("USD", None, None, None)# thenassert len(response_data) == 1@@ -350,7 +350,7 @@ def test_get_payment_gateways_with_transactions_and_app_without_identifier(plugin = webhook_plugin()# when- response_data = plugin.get_payment_gateways("USD", None, None)+ response_data = plugin.get_payment_gateways("USD", None, None, None)# thenassert len(response_data) == 0@@ -386,7 +386,7 @@ def test_get_payment_gateways_multiple_webhooks_in_the_same_app(mock_send_request.return_value = mock_json_response# when- response_data = plugin.get_payment_gateways("USD", None, None)+ response_data = plugin.get_payment_gateways("USD", None, None, None)# thenexpected_response_1 = parse_list_payment_gateways_response(@@ -414,14 +414,14 @@ def test_get_payment_gateways_filters_out_unsupported_currencies(}]mock_send_request.return_value = mock_json_response- response_data = plugin.get_payment_gateways("PLN", None, None)+ response_data = plugin.get_payment_gateways("PLN", None, None, None)assert response_data == []@mock.patch("saleor.plugins.webhook.tasks.send_webhook_request_sync")@mock.patch("saleor.plugins.webhook.plugin.generate_list_gateways_payload")def test_get_payment_gateways_for_checkout(- mock_generate_payload, mock_send_request, checkout, payment_app, webhook_plugin+ mock_generate_payload, mock_send_request, checkout_info, payment_app, webhook_plugin):plugin = webhook_plugin()mock_json_response = [@@ -434,8 +434,8 @@ def test_get_payment_gateways_for_checkout(]mock_send_request.return_value = mock_json_responsemock_generate_payload.return_value = ""- plugin.get_payment_gateways("USD", checkout, None)- assert mock_generate_payload.call_args[0][1] == checkout+ plugin.get_payment_gateways("USD", checkout_info, None, None)+ assert mock_generate_payload.call_args[0][1] == checkout_info.checkout@pytest.mark.parametrize(