Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
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.pyindex 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 loggingfrom decimal import Decimalfrom 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_cassert payment.ordertransaction = 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.iddiff --git a/saleor/payment/gateways/stripe/webhooks.py b/saleor/payment/gateways/stripe/webhooks.pyindex 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.slugelse:- 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 Truedef _get_payment(payment_intent_id: str, with_lock=True) -> Optional[Payment]: