Skip to content

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

Log the warning when checkout and order are missing for provided payment in stripe webhooks (#16757)

Iga Karbowiakc3cab238d17d
diff --git a/saleor/payment/gateways/stripe/tests/test_webhooks.py b/saleor/payment/gateways/stripe/tests/test_webhooks.py
index 207600ca14..820b3c3ef0 100644
--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py
+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py
@@ -1,4 +1,5 @@
import json
+import logging
from decimal import Decimal
from unittest.mock import Mock, patch
@@ -29,6 +30,7 @@ from ..consts import (
WEBHOOK_SUCCESS_EVENT,
)
from ..webhooks import (
+ _channel_slug_is_different_from_payment_channel_slug,
_finalize_checkout,
_process_payment_with_checkout,
_update_payment_with_new_transaction,
@@ -1720,3 +1722,94 @@ def test_handle_successful_payment_intent_for_checkout_when_already_processing_c
assert payment.order
transaction = payment.transactions.get(kind=TransactionKind.CAPTURE)
assert transaction.token == payment_intent.id
+
+
+def test_channel_slug_is_different_from_payment_channel_slug_for_checkout_false(
+ payment, checkout
+):
+ # given
+ payment.checkout = checkout
+ payment.order = None
+ payment.save(update_fields=["checkout", "order"])
+
+ channel_slug = checkout.channel.slug
+
+ # when
+ result = _channel_slug_is_different_from_payment_channel_slug(channel_slug, payment)
+
+ # then
+ assert result is False
+
+
+def test_channel_slug_is_different_from_payment_channel_slug_for_checkout_true(
+ payment, checkout
+):
+ # given
+ payment.checkout = checkout
+ payment.order = None
+ payment.save(update_fields=["checkout", "order"])
+
+ channel_slug = "test"
+
+ # when
+ result = _channel_slug_is_different_from_payment_channel_slug(channel_slug, payment)
+
+ # then
+ assert result is True
+
+
+def test_channel_slug_is_different_from_payment_channel_slug_for_order_false(
+ payment, order
+):
+ # given
+ payment.checkout = None
+ payment.order = order
+ payment.save(update_fields=["checkout", "order"])
+
+ channel_slug = order.channel.slug
+
+ # when
+ result = _channel_slug_is_different_from_payment_channel_slug(channel_slug, payment)
+
+ # then
+ assert result is False
+
+
+def test_channel_slug_is_different_from_payment_channel_slug_for_order_true(
+ payment, order
+):
+ # given
+ payment.checkout = None
+ payment.order = order
+ payment.save(update_fields=["checkout", "order"])
+
+ channel_slug = "test"
+
+ # when
+ result = _channel_slug_is_different_from_payment_channel_slug(channel_slug, payment)
+
+ # then
+ assert result is True
+
+
+def test_channel_slug_is_different_from_payment_channel_slug_no_order_or_checkout(
+ payment, caplog
+):
+ # given
+ payment.checkout = None
+ payment.order = None
+ payment.save(update_fields=["checkout", "order"])
+ caplog.set_level(logging.WARNING)
+
+ channel_slug = "test"
+
+ # when
+ result = _channel_slug_is_different_from_payment_channel_slug(channel_slug, payment)
+
+ # then
+ assert result is True
+ assert (
+ caplog.records[0].message
+ == "Both payment.checkout and payment.order cannot be None"
+ )
+ assert caplog.records[0].payment_id == payment.id
diff --git a/saleor/payment/gateways/stripe/webhooks.py b/saleor/payment/gateways/stripe/webhooks.py
index 80262bbf2f..7eaa9221b9 100644
--- a/saleor/payment/gateways/stripe/webhooks.py
+++ b/saleor/payment/gateways/stripe/webhooks.py
@@ -115,9 +115,11 @@ def _channel_slug_is_different_from_payment_channel_slug(
elif order is not None:
return channel_slug != order.channel.slug
else:
- raise ValueError(
- "Both payment.checkout and payment.order cannot be None"
- ) # pragma: no cover
+ logger.warning(
+ "Both payment.checkout and payment.order cannot be None",
+ extra={"payment_id": payment.id},
+ )
+ return True
def _get_payment(payment_intent_id: str, with_lock=True) -> Optional[Payment]: