Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Do not create order in gateways when checkout line variant is deleted (#8945)
Iga Karbowiak8b0cbe2fb72f
diff --git a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_api_response.py b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_api_response.pyindex 3e241c63be..4e4efc96d5 100644--- a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_api_response.py+++ b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_api_response.py@@ -104,7 +104,7 @@ def test_handle_api_response_auto_capture_false_cannot_create_order_void_paymentassert not refund_mock.calledassert payment_adyen_for_checkout.can_void()- assert void_mock.call_count == 2+ assert void_mock.call_count == 1@patch("saleor.payment.gateway.void")@@ -137,7 +137,44 @@ def test_handle_api_response_auto_capture_cannot_create_order_refund_payment(assert not payment_adyen_for_checkout.orderassert payment_adyen_for_checkout.can_refund()- assert refund_mock.call_count == 2+ assert refund_mock.call_count == 1++ assert not payment_adyen_for_checkout.can_void()+ assert not void_mock.called+++@patch("saleor.payment.gateway.void")+@patch("saleor.payment.gateway.refund")+def test_handle_api_response_auto_capture_cannot_create_order_variant_deleted(+ refund_mock, void_mock, payment_adyen_for_checkout, adyen_plugin+):+ payment_adyen_for_checkout.to_confirm = True+ payment_adyen_for_checkout.save(update_fields=["to_confirm"])++ checkout = payment_adyen_for_checkout.checkout+ checkout.lines.first().delete()++ plugin = adyen_plugin(adyen_auto_capture=True)++ adyen_response = AdyenResult(+ {+ "additionalData": {"paymentMethod": "visa"},+ "pspReference": "882635241694695D",+ "resultCode": "Authorised",+ "amount": {"currency": "USD", "value": payment_adyen_for_checkout.total},+ "merchantReference": "UGF5bWVudDoxMDU=",+ }+ )++ handle_api_response(payment_adyen_for_checkout, adyen_response, plugin.channel.slug)++ payment_adyen_for_checkout.refresh_from_db()++ assert payment_adyen_for_checkout.charge_status == ChargeStatus.FULLY_CHARGED+ assert not payment_adyen_for_checkout.order++ assert payment_adyen_for_checkout.can_refund()+ assert refund_mock.call_count == 1assert not payment_adyen_for_checkout.can_void()assert not void_mock.calleddiff --git a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.pyindex a94dfa79ec..ec08158f56 100644--- a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py+++ b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py@@ -196,6 +196,57 @@ def test_handle_authorization_for_checkout(assert external_events.count() == 1+@patch("saleor.payment.gateway.void")+def test_handle_authorization_for_checkout_one_of_variants_deleted(+ void_mock,+ notification,+ adyen_plugin,+ payment_adyen_for_checkout,+ address,+ shipping_method,+):+ checkout = payment_adyen_for_checkout.checkout+ checkout.shipping_address = address+ checkout.shipping_method = shipping_method+ checkout.billing_address = address+ checkout.save()++ payment = payment_adyen_for_checkout+ manager = get_plugins_manager()+ lines = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)+ total = calculations.calculate_checkout_total_with_gift_cards(+ manager, checkout_info, lines, address+ )+ payment.is_active = True+ payment.order = None+ payment.total = total.gross.amount+ payment.currency = total.gross.currency+ payment.to_confirm = True+ payment.save()++ checkout.lines.first().delete()++ payment_id = graphene.Node.to_global_id("Payment", payment.pk)+ notification = notification(+ merchant_reference=payment_id,+ value=price_to_minor_unit(payment.total, payment.currency),+ )+ config = adyen_plugin().config+ handle_authorization(notification, config)++ payment.refresh_from_db()+ assert void_mock.call_count == 1+ assert not payment.order+ assert payment.checkout+ assert payment.transactions.count() == 2+ transaction = payment.transactions.exclude(+ kind=TransactionKind.ACTION_TO_CONFIRM+ ).get()+ assert transaction.is_success is True+ assert transaction.kind == TransactionKind.AUTH++def test_handle_authorization_with_adyen_auto_capture(notification, adyen_plugin, payment_adyen_for_checkout, address, shipping_method):@@ -437,6 +488,66 @@ def test_handle_capture_for_checkout(assert external_events.count() == 1+@patch("saleor.payment.gateway.void")+def test_handle_capture_for_checkout_order_not_created_checkout_line_variant_deleted(+ void_mock,+ notification,+ adyen_plugin,+ payment_adyen_for_checkout,+ address,+ shipping_method,+):+ """+ Ensure that payment is not captured when one of checkout line variant is deleted.+ """++ # given+ checkout = payment_adyen_for_checkout.checkout+ checkout.shipping_address = address+ checkout.shipping_method = shipping_method+ checkout.billing_address = address+ checkout.save()++ payment = payment_adyen_for_checkout+ manager = get_plugins_manager()+ lines = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)+ total = calculations.calculate_checkout_total_with_gift_cards(+ manager, checkout_info, lines, address+ )+ payment.is_active = True+ payment.order = None+ payment.total = total.gross.amount+ payment.currency = total.gross.currency+ payment.to_confirm = True+ payment.save()++ checkout.lines.first().delete()++ payment_id = graphene.Node.to_global_id("Payment", payment.pk)+ notification = notification(+ merchant_reference=payment_id,+ value=price_to_minor_unit(payment.total, payment.currency),+ )+ config = adyen_plugin().config++ # when+ handle_capture(notification, config)++ # then+ payment.refresh_from_db()+ assert void_mock.call_count == 1+ assert not payment.order+ assert payment.checkout++ assert payment.transactions.count() == 2+ transaction = payment.transactions.exclude(+ kind=TransactionKind.ACTION_TO_CONFIRM+ ).get()+ assert transaction.is_success is True+ assert transaction.kind == TransactionKind.AUTH++def test_handle_capture_invalid_payment_id(notification, adyen_plugin, payment_adyen_for_order, caplog):@@ -1015,6 +1126,41 @@ def test_handle_not_created_order_order_created(assert payment_adyen_for_checkout.order+@patch("saleor.payment.gateway.refund")+def test_handle_not_created_order_order_not_created_checkout_line_variant_deleted(+ refund_mock,+ checkout_ready_to_complete,+ payment_adyen_for_checkout,+ adyen_plugin,+ notification,+):+ """Validate if order is not created when one of checkout line variant is deleted."""++ # given+ checkout = payment_adyen_for_checkout.checkout+ checkout.lines.first().variant.delete()++ payment_adyen_for_checkout.charge_status = ChargeStatus.FULLY_CHARGED+ payment_adyen_for_checkout.captured_amount = payment_adyen_for_checkout.total+ payment_adyen_for_checkout.save(update_fields=["charge_status", "captured_amount"])++ adyen_plugin()++ # when+ handle_not_created_order(+ notification(),+ payment_adyen_for_checkout,+ payment_adyen_for_checkout.checkout,+ TransactionKind.CAPTURE,+ get_plugins_manager(),+ )++ # then+ assert refund_mock.call_count == 1+ payment_adyen_for_checkout.refresh_from_db()+ assert not payment_adyen_for_checkout.order++@patch("saleor.payment.gateway.refund")@patch("saleor.checkout.complete_checkout._get_order_data")def test_handle_not_created_order_refund_when_create_order_raises(@@ -1035,7 +1181,7 @@ def test_handle_not_created_order_refund_when_create_order_raises()assert payment_adyen_for_checkout.can_refund()- assert refund_mock.call_count == 2+ assert refund_mock.call_count == 1@patch("saleor.payment.gateway.void")@@ -1058,7 +1204,7 @@ def test_handle_not_created_order_void_when_create_order_raises()assert payment_adyen_for_checkout.can_void()- assert void_mock.call_count == 2+ assert void_mock.call_count == 1@pytest.mark.parametrize(@@ -1139,7 +1285,7 @@ def test_handle_not_created_order_success_transaction_create_order_raises_and_reassert all_payment_transactions[1].kind == TransactionKind.AUTHassert payment_adyen_for_checkout.can_refund()- assert refund_mock.call_count == 2+ assert refund_mock.call_count == 1@patch("saleor.payment.gateway.void")@@ -1171,7 +1317,7 @@ def test_handle_not_created_order_success_transaction_create_order_raises_and_voassert all_payment_transactions[1].kind == TransactionKind.AUTHassert payment_adyen_for_checkout.can_void()- assert void_mock.call_count == 2+ assert void_mock.call_count == 1def test_confirm_payment_and_set_back_to_confirm(diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.pyindex d00c376068..1ca9be7133 100644--- a/saleor/payment/gateways/adyen/webhooks.py+++ b/saleor/payment/gateways/adyen/webhooks.py@@ -24,6 +24,7 @@ from django.http.request import HttpHeadersfrom django.http.response import HttpResponseRedirectfrom graphql_relay import from_global_id+from ....checkout.calculations import calculate_checkout_total_with_gift_cardsfrom ....checkout.complete_checkout import complete_checkoutfrom ....checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom ....checkout.models import Checkout@@ -165,6 +166,20 @@ def create_order(payment, checkout, manager):discounts = fetch_active_discounts()lines = fetch_checkout_lines(checkout)checkout_info = fetch_checkout_info(checkout, lines, discounts, manager)+ checkout_total = calculate_checkout_total_with_gift_cards(+ manager=manager,+ checkout_info=checkout_info,+ lines=lines,+ address=checkout.shipping_address or checkout.billing_address,+ discounts=discounts,+ )+ # when checkout total value is different than total amount from payments+ # it means that some products has been removed during the payment was completed+ if checkout_total.gross.amount != payment.total:+ payment_refund_or_void(payment, manager, checkout_info.channel.slug)+ raise ValidationError(+ "Cannot create order - some products do not exist anymore."+ )order, _, _ = complete_checkout(manager=manager,checkout_info=checkout_info,@@ -175,8 +190,10 @@ def create_order(payment, checkout, manager):user=checkout.user or AnonymousUser(),app=None,)- except ValidationError:- payment_refund_or_void(payment, manager, checkout_info.channel.slug)+ except ValidationError as e:+ logger.info(+ "Failed to create order from checkout %s.", checkout.pk, extra={"error": e}+ )return None# Refresh the payment to assign the newly created orderpayment.refresh_from_db()diff --git a/saleor/payment/gateways/stripe/tests/test_webhooks.py b/saleor/payment/gateways/stripe/tests/test_webhooks.pyindex 5d213f1ddd..4b8fe6ece7 100644--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py@@ -1245,6 +1245,34 @@ def test_finalize_checkout_not_created_order_payment_refund(assert refund_mock.called+@patch("saleor.payment.gateway.refund")+def test_finalize_checkout_not_created_checkout_variant_deleted_order_payment_refund(+ refund_mock,+ stripe_plugin,+ channel_USD,+ payment_stripe_for_checkout,+ stripe_payment_intent,+):+ stripe_plugin()+ checkout = payment_stripe_for_checkout.checkout++ checkout.lines.first().delete()++ _finalize_checkout(+ checkout,+ payment_stripe_for_checkout,+ stripe_payment_intent,+ TransactionKind.CAPTURE,+ payment_stripe_for_checkout.total,+ payment_stripe_for_checkout.currency,+ )++ payment_stripe_for_checkout.refresh_from_db()++ assert not payment_stripe_for_checkout.order+ assert refund_mock.called++@patch("saleor.payment.gateway.void")@patch("saleor.checkout.complete_checkout._get_order_data")def test_finalize_checkout_not_created_order_payment_void(@@ -1274,6 +1302,34 @@ def test_finalize_checkout_not_created_order_payment_void(assert void_mock.called+@patch("saleor.payment.gateway.void")+def test_finalize_checkout_not_created_checkout_variant_deleted_order_payment_void(+ void_mock,+ stripe_plugin,+ channel_USD,+ payment_stripe_for_checkout,+ stripe_payment_intent,+):+ stripe_plugin()+ checkout = payment_stripe_for_checkout.checkout++ checkout.lines.first().delete()++ _finalize_checkout(+ checkout,+ payment_stripe_for_checkout,+ stripe_payment_intent,+ TransactionKind.AUTH,+ payment_stripe_for_checkout.total,+ payment_stripe_for_checkout.currency,+ )++ payment_stripe_for_checkout.refresh_from_db()++ assert not payment_stripe_for_checkout.order+ assert void_mock.called++def test_update_payment_method_details_from_intent_payment_info_does_not_exist(payment_stripe_for_checkout, stripe_payment_intent):diff --git a/saleor/payment/gateways/stripe/webhooks.py b/saleor/payment/gateways/stripe/webhooks.pyindex 61c24c29ad..86cdd5f4d4 100644--- a/saleor/payment/gateways/stripe/webhooks.py+++ b/saleor/payment/gateways/stripe/webhooks.py@@ -9,6 +9,7 @@ from django.http import HttpResponsefrom stripe.error import SignatureVerificationErrorfrom stripe.stripe_object import StripeObject+from ....checkout.calculations import calculate_checkout_total_with_gift_cardsfrom ....checkout.complete_checkout import complete_checkoutfrom ....checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom ....checkout.models import Checkout@@ -19,6 +20,7 @@ from ....order.fetch import fetch_order_infofrom ....order.models import Orderfrom ....plugins.manager import get_plugins_managerfrom ... import ChargeStatus, TransactionKind+from ...gateway import payment_refund_or_voidfrom ...interface import GatewayConfig, GatewayResponsefrom ...models import Paymentfrom ...utils import (@@ -173,8 +175,23 @@ def _finalize_checkout(checkout_info = fetch_checkout_info(checkout, lines, discounts, manager # type: ignore)+ checkout_total = calculate_checkout_total_with_gift_cards(+ manager=manager,+ checkout_info=checkout_info,+ lines=lines,+ address=checkout.shipping_address or checkout.billing_address,+ discounts=discounts,+ )try:+ # when checkout total value is different than total amount from payments+ # it means that some products has been removed during the payment was completed+ if checkout_total.gross.amount != payment.total:+ payment_refund_or_void(payment, manager, checkout_info.channel.slug)+ raise ValidationError(+ "Cannot complete checkout - some products do not exist anymore."+ )+order, _, _ = complete_checkout(manager=manager,checkout_info=checkout_info,