Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Ensure order status progress with confirmation and transactions API (#16254)
Tomasz Szymański877b515573d2
diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_create.py b/saleor/graphql/payment/tests/mutations/test_transaction_create.pyindex 4b4f7fe758..a3dcdbea4e 100644--- a/saleor/graphql/payment/tests/mutations/test_transaction_create.py+++ b/saleor/graphql/payment/tests/mutations/test_transaction_create.py@@ -1781,6 +1781,13 @@ def test_transaction_create_creates_calculation_events(assert cancel_event.amount.amount == canceled_value+ ("auto_order_confirmation", "excpected_order_status"),+ [+ (True, OrderStatus.UNFULFILLED),+ (False, OrderStatus.UNCONFIRMED),+ ],+)@patch("saleor.plugins.manager.PluginsManager.order_paid")@patch("saleor.plugins.manager.PluginsManager.order_updated")@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")@@ -1788,15 +1795,20 @@ def test_transaction_create_for_order_triggers_webhooks_when_fully_paid(mock_order_fully_paid,mock_order_updated,mock_order_paid,- order_with_lines,+ auto_order_confirmation,+ excpected_order_status,+ unconfirmed_order_with_lines,permission_manage_payments,staff_api_client,):# given- charged_value = order_with_lines.total.gross.amount+ order = unconfirmed_order_with_lines+ order.channel.automatically_confirm_all_new_orders = auto_order_confirmation+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])+ charged_value = order.total.gross.amountvariables = {- "id": graphene.Node.to_global_id("Order", order_with_lines.pk),+ "id": graphene.Node.to_global_id("Order", order.pk),"transaction": {"name": "Credit Card","pspReference": "PSP reference - 123",@@ -1814,13 +1826,14 @@ def test_transaction_create_for_order_triggers_webhooks_when_fully_paid()# then- order_with_lines.refresh_from_db()+ order.refresh_from_db()get_graphql_content(response)- assert order_with_lines.charge_status == OrderChargeStatus.FULL- mock_order_fully_paid.assert_called_once_with(order_with_lines)- mock_order_updated.assert_called_once_with(order_with_lines)- mock_order_paid.assert_called_once_with(order_with_lines)+ assert order.status == excpected_order_status+ assert order.charge_status == OrderChargeStatus.FULL+ mock_order_fully_paid.assert_called_once_with(order)+ mock_order_updated.assert_called_once_with(order)+ mock_order_paid.assert_called_once_with(order)@patch("saleor.plugins.manager.PluginsManager.order_paid")diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py b/saleor/graphql/payment/tests/mutations/test_transaction_event_report.pyindex 5b614a395a..ae464b5ef6 100644--- a/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py+++ b/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py@@ -12,7 +12,7 @@ from freezegun import freeze_timefrom .....checkout import CheckoutAuthorizeStatus, CheckoutChargeStatusfrom .....checkout.calculations import fetch_checkout_datafrom .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines-from .....order import OrderGrantedRefundStatus+from .....order import OrderGrantedRefundStatus, OrderStatusfrom .....payment import TransactionEventTypefrom .....payment.models import TransactionEventfrom .....payment.transaction_item_calculations import recalculate_transaction_amounts@@ -1619,6 +1619,13 @@ def test_transaction_event_report_doesnt_accept_old_id_for_new_transaction(assert error["field"] == "id"+ ("auto_order_confirmation", "excpected_order_status"),+ [+ (True, OrderStatus.UNFULFILLED),+ (False, OrderStatus.UNCONFIRMED),+ ],+)@patch("saleor.plugins.manager.PluginsManager.order_paid")@patch("saleor.plugins.manager.PluginsManager.order_updated")@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")@@ -1626,13 +1633,17 @@ def test_transaction_event_report_for_order_triggers_webhooks_when_fully_paid(mock_order_fully_paid,mock_order_updated,mock_order_paid,+ auto_order_confirmation,+ excpected_order_status,transaction_item_generator,app_api_client,permission_manage_payments,- order_with_lines,+ unconfirmed_order_with_lines,):# given- order = order_with_lines+ order = unconfirmed_order_with_lines+ order.channel.automatically_confirm_all_new_orders = auto_order_confirmation+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])psp_reference = "111-abc"transaction = transaction_item_generator(app=app_api_client.app, order_id=order.pk)transaction_id = graphene.Node.to_global_id("TransactionItem", transaction.token)@@ -1671,6 +1682,7 @@ def test_transaction_event_report_for_order_triggers_webhooks_when_fully_paid(get_graphql_content(response)order.refresh_from_db()+ assert order.status == excpected_order_statusassert order.charge_status == OrderChargeStatusEnum.FULL.valuemock_order_fully_paid.assert_called_once_with(order)mock_order_updated.assert_called_once_with(order)diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py b/saleor/graphql/payment/tests/mutations/test_transaction_initialize.pyindex e9bb13f1ff..32da1b1be1 100644--- a/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py+++ b/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py@@ -13,6 +13,7 @@ from .....checkout import CheckoutAuthorizeStatus, CheckoutChargeStatusfrom .....checkout.calculations import fetch_checkout_datafrom .....checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom .....core.prices import quantize_price+from .....order import OrderChargeStatus, OrderStatusfrom .....payment import TransactionEventTypefrom .....payment.interface import (PaymentGatewayData,@@ -958,6 +959,73 @@ def test_order_when_amount_is_not_provided(assert order.total_charged_amount == order.total_gross_amount+ ("auto_order_confirmation", "excpected_order_status"),+ [+ (True, OrderStatus.UNFULFILLED),+ (False, OrderStatus.UNCONFIRMED),+ ],+)[email protected]("saleor.plugins.manager.PluginsManager.transaction_initialize_session")+def test_order_status_with_order_confirmation(+ mocked_initialize,+ auto_order_confirmation,+ excpected_order_status,+ user_api_client,+ unconfirmed_order_with_lines,+ webhook_app,+ transaction_session_response,+):+ # given+ order = unconfirmed_order_with_lines+ order.channel.automatically_confirm_all_new_orders = auto_order_confirmation+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])+ expected_app_identifier = "webhook.app.identifier"+ webhook_app.identifier = expected_app_identifier+ webhook_app.save()++ expected_psp_reference = "ppp-123"+ expected_response = transaction_session_response.copy()+ expected_response["result"] = TransactionEventType.CHARGE_SUCCESS.upper()+ expected_response["amount"] = str(order.total_gross_amount)+ expected_response["pspReference"] = expected_psp_reference+ mocked_initialize.return_value = TransactionSessionResult(+ app_identifier=expected_app_identifier, response=expected_response+ )++ variables = {+ "amount": order.total_gross_amount,+ "id": to_global_id_or_none(order),+ "paymentGateway": {"id": expected_app_identifier, "data": None},+ }++ # when+ response = user_api_client.post_graphql(TRANSACTION_INITIALIZE, variables)++ # then+ content = get_graphql_content(response)+ _assert_fields(+ content=content,+ source_object=order,+ expected_amount=order.total_gross_amount,+ expected_psp_reference=expected_psp_reference,+ response_event_type=TransactionEventType.CHARGE_SUCCESS,+ app_identifier=webhook_app.identifier,+ mocked_initialize=mocked_initialize,+ charged_value=order.total_gross_amount,+ returned_data=expected_response["data"],+ )++ assert order.status == OrderStatus.UNCONFIRMED+ assert not order.is_fully_paid()+ order.refresh_from_db()+ assert order.is_fully_paid()+ assert order.total_authorized_amount == Decimal(0)+ assert order.total_charged_amount == order.total_gross_amount+ assert order.status == excpected_order_status+ assert order.charge_status == OrderChargeStatus.FULL++@mock.patch("saleor.plugins.manager.PluginsManager.transaction_initialize_session")def test_order_with_transaction_when_amount_is_not_provided(mocked_initialize,diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_process.py b/saleor/graphql/payment/tests/mutations/test_transaction_process.pyindex 1b2048bf73..bebc9d9281 100644--- a/saleor/graphql/payment/tests/mutations/test_transaction_process.py+++ b/saleor/graphql/payment/tests/mutations/test_transaction_process.py@@ -13,6 +13,7 @@ from .....checkout import CheckoutAuthorizeStatus, CheckoutChargeStatusfrom .....checkout.calculations import fetch_checkout_datafrom .....checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom .....core.prices import quantize_price+from .....order import OrderChargeStatus, OrderStatusfrom .....payment import TransactionEventTypefrom .....payment.interface import (PaymentGatewayData,@@ -357,6 +358,77 @@ def test_for_order_without_data()+ ("auto_order_confirmation", "excpected_order_status"),+ [+ (True, OrderStatus.UNFULFILLED),+ (False, OrderStatus.UNCONFIRMED),+ ],+)[email protected]("saleor.plugins.manager.PluginsManager.transaction_process_session")+def test_order_status_with_order_confirmation(+ mocked_process,+ auto_order_confirmation,+ excpected_order_status,+ user_api_client,+ unconfirmed_order_with_lines,+ webhook_app,+ transaction_session_response,+ transaction_item_generator,+):+ # given+ order = unconfirmed_order_with_lines+ order.channel.automatically_confirm_all_new_orders = auto_order_confirmation+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])+ expected_app_identifier = "webhook.app.identifier"+ webhook_app.identifier = expected_app_identifier+ webhook_app.save()++ transaction_item = transaction_item_generator(order_id=order.pk, app=webhook_app)+ TransactionEvent.objects.create(+ transaction=transaction_item,+ amount_value=order.total.gross.amount,+ currency=transaction_item.currency,+ type=TransactionEventType.CHARGE_REQUEST,+ )++ expected_psp_reference = "ppp-123"+ expected_response = transaction_session_response.copy()+ expected_response["amount"] = order.total.gross.amount+ expected_response["result"] = TransactionEventType.CHARGE_SUCCESS.upper()+ expected_response["pspReference"] = expected_psp_reference+ del expected_response["data"]+ mocked_process.return_value = TransactionSessionResult(+ app_identifier=expected_app_identifier, response=expected_response+ )++ variables = {+ "id": graphene.Node.to_global_id("TransactionItem", transaction_item.token),+ "data": None,+ }++ # when+ response = user_api_client.post_graphql(TRANSACTION_PROCESS, variables)++ # then+ content = get_graphql_content(response)+ _assert_fields(+ content=content,+ source_object=order,+ expected_amount=order.total.gross.amount,+ expected_psp_reference=expected_psp_reference,+ response_event_type=TransactionEventType.CHARGE_SUCCESS,+ app_identifier=webhook_app.identifier,+ mocked_process=mocked_process,+ charged_value=order.total.gross.amount,+ returned_data=None,+ )++ order.refresh_from_db()+ assert order.status == excpected_order_status+ assert order.charge_status == OrderChargeStatus.FULL++@mock.patch("saleor.plugins.manager.PluginsManager.transaction_process_session")def test_for_checkout_with_data(mocked_process,diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_update.py b/saleor/graphql/payment/tests/mutations/test_transaction_update.pyindex c0d79c74f8..0dbfb06642 100644--- a/saleor/graphql/payment/tests/mutations/test_transaction_update.py+++ b/saleor/graphql/payment/tests/mutations/test_transaction_update.py@@ -8,7 +8,7 @@ from freezegun import freeze_timefrom .....checkout import CheckoutAuthorizeStatus, CheckoutChargeStatusfrom .....checkout.calculations import fetch_checkout_datafrom .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines-from .....order import OrderAuthorizeStatus, OrderChargeStatus, OrderEvents+from .....order import OrderAuthorizeStatus, OrderChargeStatus, OrderEvents, OrderStatusfrom .....payment import TransactionEventTypefrom .....payment.error_codes import TransactionUpdateErrorCodefrom .....payment.models import TransactionEvent, TransactionItem@@ -2688,6 +2688,13 @@ def test_transaction_update_doesnt_accept_old_id_for_new_transactions(assert error["field"] == "id"+ ("auto_order_confirmation", "excpected_order_status"),+ [+ (True, OrderStatus.UNFULFILLED),+ (False, OrderStatus.UNCONFIRMED),+ ],+)@patch("saleor.plugins.manager.PluginsManager.order_paid")@patch("saleor.plugins.manager.PluginsManager.order_updated")@patch("saleor.plugins.manager.PluginsManager.order_fully_paid")@@ -2695,17 +2702,22 @@ def test_transaction_update_for_order_triggers_webhooks_when_fully_paid(mock_order_fully_paid,mock_order_updated,mock_order_paid,- order_with_lines,+ auto_order_confirmation,+ excpected_order_status,+ unconfirmed_order_with_lines,permission_manage_payments,app_api_client,app,transaction_item_generator,):# given+ order = unconfirmed_order_with_lines+ order.channel.automatically_confirm_all_new_orders = auto_order_confirmation+ order.channel.save(update_fields=["automatically_confirm_all_new_orders"])current_authorized_value = Decimal("1")current_charged_value = Decimal("2")transaction = transaction_item_generator(- order_id=order_with_lines.pk,+ order_id=order.pk,app=app,authorized_value=current_authorized_value,charged_value=current_charged_value,@@ -2715,7 +2727,7 @@ def test_transaction_update_for_order_triggers_webhooks_when_fully_paid("id": graphene.Node.to_global_id("TransactionItem", transaction.token),"transaction": {"amountCharged": {- "amount": order_with_lines.total.gross.amount,+ "amount": order.total.gross.amount,"currency": "USD",},},@@ -2727,14 +2739,14 @@ def test_transaction_update_for_order_triggers_webhooks_when_fully_paid()# then- order_with_lines.refresh_from_db()-+ order.refresh_from_db()get_graphql_content(response)- assert order_with_lines.charge_status == OrderChargeStatus.FULL- mock_order_fully_paid.assert_called_once_with(order_with_lines)- mock_order_updated.assert_called_once_with(order_with_lines)- mock_order_paid.assert_called_once_with(order_with_lines)+ assert order.status == excpected_order_status+ assert order.charge_status == OrderChargeStatus.FULL+ mock_order_fully_paid.assert_called_once_with(order)+ mock_order_updated.assert_called_once_with(order)+ mock_order_paid.assert_called_once_with(order)@patch("saleor.plugins.manager.PluginsManager.order_paid")diff --git a/saleor/order/actions.py b/saleor/order/actions.pyindex a5aacd35cd..82f3071d50 100644--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@ -169,6 +169,9 @@ def handle_fully_paid_order()call_event(manager.order_fully_paid, order)+ if order.channel.automatically_confirm_all_new_orders:+ update_order_status(order)+call_event(manager.order_updated, order)