Skip to content

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

Prevent race condition between TransactionEventReport and checkout completion (#18642)

Cezary Miącze12689231d3b
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_event_report.py b/saleor/graphql/payment/mutations/transaction/transaction_event_report.py
index a23e53beed..3809914215 100644
--- a/saleor/graphql/payment/mutations/transaction/transaction_event_report.py
+++ b/saleor/graphql/payment/mutations/transaction/transaction_event_report.py
@@ -5,6 +5,7 @@ from django.core.exceptions import ValidationError
from django.utils import timezone
from .....app.models import App
+from .....checkout.models import Checkout
from .....core.exceptions import PermissionDenied
from .....core.prices import quantize_price
from .....core.tracing import traced_atomic_transaction
@@ -372,7 +373,6 @@ class TransactionEventReport(DeprecatedModelMutation):
transaction_event = cls.construct_instance(
transaction_event, transaction_event_data
)
- source_object = transaction.order or transaction.checkout
metadata_collection = cls.create_metadata_from_graphql_input(
transaction_metadata, error_field_name="metadata"
@@ -477,6 +477,18 @@ class TransactionEventReport(DeprecatedModelMutation):
transaction.save(update_fields=updated_fields)
app_identifier = app_identifier or transaction.app_identifier
+
+ source_object: Checkout | order_models.Order | None = None
+ if transaction.checkout_id:
+ source_object = Checkout.objects.filter(pk=transaction.checkout_id).first()
+ if not source_object:
+ # Prevent race condition between TransactionEventReport and checkout completion
+ source_object = order_models.Order.objects.filter(
+ pk__in=payment_models.TransactionItem.objects.filter(
+ pk=transaction.pk
+ ).values_list("order_id", flat=True)
+ ).first()
+
if app_identifier and source_object:
invalidate_cache_for_stored_payment_methods_if_needed(
transaction_event, source_object, app_identifier