Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Update checkout authorization status if total gross is zero and authorization status is not FULL yet (#18019)
Marcin Wcisło577d31f4dc69
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex eaeb7bc871..ff4cb14f15 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,7 @@ All notable, unreleased changes to this project will be documented in this file.- The following changes were implemented to orders with a zero total amount:- No manual charge (`Transaction` or `Payment`) object will be created.- The `OrderEvents.ORDER_MARKED_AS_PAID` event will no longer be emitted.+- Logic associated with `WebhookEventAsyncType.CHECKOUT_FULLY_PAID` event will no longer be triggered when creating a transaction event from webhook response for checkouts with having total gross being 0. At the point of creating the transaction event checkout is already considered fully paid.### GraphQL API- You can now filter and search orders using the new `where` and `search` fields on the `pages` query.@@ -130,6 +131,8 @@ Like `reference`, the `single-reference` type can target entities defined in the- Fixed bug in user email filtering to make it case-insensitive.+- Checkouts having total gross amount equal to 0 will get their authorization statuses updated to `CheckoutAuthorizeStatus.FULL` upon fetching checkout data.+### DeprecationsFollowing plugins are now marked as deprecated:diff --git a/saleor/checkout/calculations.py b/saleor/checkout/calculations.pyindex b747c226cb..1afb16e0cb 100644--- a/saleor/checkout/calculations.py+++ b/saleor/checkout/calculations.py@@ -8,7 +8,7 @@ from django.db import transactionfrom django.utils import timezonefrom prices import Money, TaxedMoney-from ..checkout import base_calculations+from ..checkout import CheckoutAuthorizeStatus, base_calculationsfrom ..core.db.connection import allow_writerfrom ..core.prices import quantize_pricefrom ..core.taxes import (@@ -798,7 +798,17 @@ def fetch_checkout_data(allow_sync_webhooks=allow_sync_webhooks,)current_total_gross = checkout_info.checkout.total.gross- if current_total_gross != previous_total_gross or force_status_update:+ if (+ current_total_gross != previous_total_gross+ or force_status_update+ or (+ # Checkout with total being zero is fully authorized therefore+ # if authorized status was not yet updated, do it now.+ current_total_gross == zero_money(current_total_gross.currency)+ and checkout_info.checkout.authorize_status != CheckoutAuthorizeStatus.FULL+ and bool(lines)+ )+ ):update_checkout_payment_statuses(checkout=checkout_info.checkout,checkout_total_gross=current_total_gross,diff --git a/saleor/checkout/tests/test_actions.py b/saleor/checkout/tests/test_actions.pyindex 2d4166be49..82e99ce779 100644--- a/saleor/checkout/tests/test_actions.py+++ b/saleor/checkout/tests/test_actions.py@@ -172,43 +172,6 @@ def test_transaction_amounts_for_checkout_updated_with_already_fully_paid(assert not mocked_automatic_checkout_completion_task.called-@patch("saleor.checkout.tasks.automatic_checkout_completion_task.delay")-@patch("saleor.plugins.manager.PluginsManager.checkout_fully_paid")-def test_transaction_amounts_for_checkout_updated_0_checkout_automatic_complete(- mocked_fully_paid,- mocked_automatic_checkout_completion_task,- checkout_with_item_total_0,- transaction_item_generator,- plugins_manager,- app,- django_capture_on_commit_callbacks,-):- # given- checkout = checkout_with_item_total_0- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, plugins_manager)- checkout_info, _ = fetch_checkout_data(checkout_info, plugins_manager, lines)- transaction = transaction_item_generator(checkout_id=checkout.pk, charged_value=0)- channel = checkout_info.channel- channel.automatically_complete_fully_paid_checkouts = True- channel.save(update_fields=["automatically_complete_fully_paid_checkouts"])-- # when- with django_capture_on_commit_callbacks(execute=True):- transaction_amounts_for_checkout_updated(- transaction, manager=plugins_manager, user=None, app=app- )-- # then- checkout.refresh_from_db()- assert checkout.charge_status == CheckoutChargeStatus.FULL- assert checkout.authorize_status == CheckoutAuthorizeStatus.FULL- mocked_fully_paid.assert_called_with(checkout, webhooks=set())- mocked_automatic_checkout_completion_task.assert_called_once_with(- checkout.pk, None, app.id- )--@patch("saleor.checkout.tasks.automatic_checkout_completion_task.delay")@patch("saleor.plugins.manager.PluginsManager.checkout_fully_paid")def test_transaction_amounts_for_checkout_updated_fully_authorized(@@ -336,10 +299,8 @@ def test_transaction_amounts_automatic_checkout_complete_called_once("previous_modified_at",[None, datetime.datetime(2018, 5, 31, 12, 0, 0, tzinfo=datetime.UTC)],)-@patch("saleor.plugins.manager.PluginsManager.checkout_fully_paid")@freeze_time("2023-05-31 12:00:01")def test_transaction_amounts_for_checkout_updated_updates_last_transaction_modified_at(- mocked_fully_paid,previous_modified_at,checkout_with_items,transaction_item_generator,@@ -371,7 +332,6 @@ def test_transaction_amounts_for_checkout_updated_updates_last_transaction_modif# thencheckout.refresh_from_db()assert checkout.last_transaction_modified_at == transaction.modified_at- mocked_fully_paid.assert_called_with(checkout, webhooks=set())def test_get_checkout_refundable_with_transaction_and_last_refund_success(diff --git a/saleor/checkout/tests/test_calculations.py b/saleor/checkout/tests/test_calculations.pyindex 6823a87d89..e25d73bc50 100644--- a/saleor/checkout/tests/test_calculations.py+++ b/saleor/checkout/tests/test_calculations.py@@ -9,6 +9,7 @@ from freezegun import freeze_timefrom graphene import Nodefrom prices import Money, TaxedMoney+from ...checkout import CheckoutAuthorizeStatus, CheckoutChargeStatusfrom ...checkout.utils import (add_promo_code_to_checkout,assign_external_shipping_to_checkout,@@ -19,6 +20,7 @@ from ...core.taxes import (TaxDataError,TaxDataErrorMessage,TaxLineData,+ zero_money,zero_taxed_money,)from ...graphql.core.utils import to_global_id_or_none@@ -1265,3 +1267,36 @@ def test_fetch_checkout_with_prior_price_none(line.refresh_from_db()assert line.prior_unit_price_amount is Noneassert line.currency is not None+++def test_fetch_checkout_data_updates_status_for_zero_amount_checkout_with_lines(+ checkout_with_item_total_0,+):+ # given+ lines, _ = fetch_checkout_lines(checkout_with_item_total_0)+ manager = get_plugins_manager(allow_replica=False)+ checkout_info = fetch_checkout_info(checkout_with_item_total_0, lines, manager)+ address = (+ checkout_with_item_total_0.shipping_address+ or checkout_with_item_total_0.billing_address,+ )++ assert checkout_with_item_total_0.total.gross == zero_money(+ checkout_with_item_total_0.total.currency+ )+ assert checkout_with_item_total_0.authorize_status == CheckoutAuthorizeStatus.NONE+ assert checkout_with_item_total_0.charge_status == CheckoutChargeStatus.NONE+ assert bool(lines) is True++ # when+ fetch_checkout_data(+ checkout_info=checkout_info,+ manager=manager,+ lines=lines,+ address=address,+ )++ # then+ checkout_with_item_total_0.refresh_from_db()+ assert checkout_with_item_total_0.authorize_status == CheckoutAuthorizeStatus.FULL+ assert checkout_with_item_total_0.charge_status == CheckoutChargeStatus.FULL