Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add missing lock to avoid deadlocks between checkout process and payment plugins (#14893)
Maciej Korycinskibdd1f96a57ea
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.pyindex 94e7b92f4b..c45afab9a0 100644--- a/saleor/checkout/complete_checkout.py+++ b/saleor/checkout/complete_checkout.py@@ -1,13 +1,8 @@+import loggingfrom collections.abc import Iterablefrom datetime import timedeltafrom decimal import Decimal-from typing import (- TYPE_CHECKING,- Any,- Optional,- Union,- cast,-)+from typing import TYPE_CHECKING, Any, Optional, Union, castfrom uuid import UUIDfrom django.conf import settings@@ -98,6 +93,8 @@ if TYPE_CHECKING:from ..plugins.manager import PluginsManagerfrom ..site.models import SiteSettings+logger = logging.getLogger(__name__)+def _process_voucher_data_for_order(checkout_info: "CheckoutInfo") -> dict:"""Fetch, process and return voucher/discount data from checkout.@@ -1506,29 +1503,35 @@ def complete_checkout_with_payment(voucher = checkout_info.vouchervoucher_code = checkout_info.voucher_codeif payment:- txn = _process_payment(- payment=payment,- customer_id=customer_id,- store_source=store_source,- payment_data=payment_data,- order_data=order_data,- manager=manager,- channel_slug=channel_slug,- voucher_code=checkout_info.voucher_code,- voucher=checkout_info.voucher,- )-- # As payment processing might take a while, we need to check if the payment- # doesn't become inactive in the meantime. If it's inactive we need to refund- # the payment.- payment.refresh_from_db()- if not payment.is_active:- gateway.payment_refund_or_void(payment, manager, channel_slug=channel_slug)- raise ValidationError(- f"The payment with pspReference: {payment.psp_reference} is inactive.",- code=CheckoutErrorCode.INACTIVE_PAYMENT.value,+ with transaction_with_commit_on_errors():+ Checkout.objects.select_for_update().filter(pk=checkout_pk).first()+ payment = Payment.objects.select_for_update().get(id=payment.id)+ txn = _process_payment(+ payment=payment,+ customer_id=customer_id,+ store_source=store_source,+ payment_data=payment_data,+ order_data=order_data,+ manager=manager,+ channel_slug=channel_slug,+ voucher_code=checkout_info.voucher_code,+ voucher=checkout_info.voucher,)+ # As payment processing might take a while, we need to check if the payment+ # doesn't become inactive in the meantime. If it's inactive we need to+ # refund the payment.+ payment.refresh_from_db()+ if not payment.is_active:+ gateway.payment_refund_or_void(+ payment, manager, channel_slug=channel_slug+ )+ raise ValidationError(+ f"The payment with pspReference: {payment.psp_reference} is "+ "inactive.",+ code=CheckoutErrorCode.INACTIVE_PAYMENT.value,+ )+with transaction_with_commit_on_errors():checkout = (Checkout.objects.select_for_update()diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.pyindex a21b4d83f4..0f3e304341 100644--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py@@ -4331,8 +4331,8 @@ def test_checkout_complete_payment_create_create_run_in_meantime(user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)# when- with before_after.before(- "saleor.checkout.complete_checkout._get_order_data",+ with before_after.after(+ "saleor.checkout.complete_checkout._process_payment",call_payment_create_mutation,):response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)