From c4e1fbad03a4a556aed1efe7d016d95b8b7b1d52 Mon Sep 17 00:00:00 2001 From: Akshar Prabhudesai <262747075+aksharprabhudesai@users.noreply.github.com> Date: Wed, 14 Oct 2026 01:18:12 -0700 Subject: [PATCH 1/2] fix(rest/python): return ready_for_complete only when fulfillment is selected --- rest/python/server/README.md | 4 +- rest/python/server/integration_test.py | 78 +++++++++++++++++++ .../server/services/checkout_service.py | 37 +++++---- 3 files changed, 102 insertions(+), 17 deletions(-) diff --git a/rest/python/server/README.md b/rest/python/server/README.md index 34daa38..11bded0 100644 --- a/rest/python/server/README.md +++ b/rest/python/server/README.md @@ -493,7 +493,7 @@ Full request "phone_number": null, "consent": null }, - "status": "ready_for_complete", + "status": "incomplete", "currency": "USD", "totals": [ { @@ -625,7 +625,7 @@ curl -X PUT http://localhost:8182/checkout-sessions/$CHECKOUT_ID \ "phone_number": null, "consent": null }, - "status": "ready_for_complete", + "status": "incomplete", "currency": "USD", "totals": [ { diff --git a/rest/python/server/integration_test.py b/rest/python/server/integration_test.py index 765606d..f5e5181 100644 --- a/rest/python/server/integration_test.py +++ b/rest/python/server/integration_test.py @@ -2136,6 +2136,84 @@ def test_create_applies_payment_instruments(self) -> None: self.assertEqual(returned[0].get("handler_id"), "mock_payment_handler") self.assertEqual(returned[0].get("type"), "card") + def test_checkout_readiness_lifecycle(self) -> None: + """A checkout without fulfillment is incomplete and becomes ready once selected.""" + with self.client: + # 1. Create checkout without fulfillment -> status must be 'incomplete' + create_res = self.client.post( + "/checkout-sessions", + headers=self._get_headers(idempotency_key="ready_1", request_id="ready_1"), + json={ + "line_items": [{"item": {"id": "rose"}, "quantity": 1}], + }, + ) + self.assertEqual(create_res.status_code, 201, create_res.text) + checkout_data = create_res.json() + self.assertEqual( + checkout_data["status"], + "incomplete", + "checkout without fulfillment selection must be incomplete", + ) + checkout_id = self.get_resource_id(checkout_data["id"]) + + # 2. Attempt complete -> must fail with 400 + payment_payload = self._create_payment_payload() + complete_res = self.client.post( + f"/checkout-sessions/{checkout_id}/complete", + headers=self._get_headers(idempotency_key="ready_2", request_id="ready_2"), + json=payment_payload, + ) + self.assertEqual(complete_res.status_code, 400) + + # 3. Update checkout to select destination and option -> status becomes 'ready_for_complete' + line_item_id = checkout_data["line_items"][0]["id"] + update_res = self.client.put( + f"/checkout-sessions/{checkout_id}", + headers=self._get_headers(idempotency_key="ready_3", request_id="ready_3"), + json={ + "line_items": [ + { + "id": line_item_id, + "item": {"id": "rose"}, + "quantity": 1, + } + ], + "fulfillment": { + "methods": [ + { + "id": "method_1", + "type": "shipping", + "line_item_ids": [line_item_id], + "destinations": [{"id": "dest_1", "address_country": "US"}], + "selected_destination_id": "dest_1", + "groups": [ + { + "id": "group_1", + "line_item_ids": [line_item_id], + "selected_option_id": "std-ship", + } + ], + } + ] + }, + }, + ) + self.assertEqual(update_res.status_code, 200, update_res.text) + self.assertEqual( + update_res.json()["status"], + "ready_for_complete", + "checkout with destination and option selected must be ready_for_complete", + ) + + # 4. Now complete succeeds + complete_res = self.client.post( + f"/checkout-sessions/{checkout_id}/complete", + headers=self._get_headers(idempotency_key="ready_4", request_id="ready_4"), + json=payment_payload, + ) + self.assertEqual(complete_res.status_code, 200, complete_res.text) + self.assertEqual(complete_res.json()["status"], "completed") + if __name__ == "__main__": absltest.main() diff --git a/rest/python/server/services/checkout_service.py b/rest/python/server/services/checkout_service.py index 63905d5..f6c1669 100644 --- a/rest/python/server/services/checkout_service.py +++ b/rest/python/server/services/checkout_service.py @@ -426,7 +426,7 @@ async def create_checkout( await self._enrich_and_recalculate(checkout) await self._validate_inventory(checkout) - checkout.status = CheckoutStatus.READY_FOR_COMPLETE + checkout.status = self._readiness_status(checkout) response_body = checkout.model_dump( mode="json", by_alias=True, exclude_none=True @@ -687,6 +687,8 @@ async def update_checkout( await self._enrich_and_recalculate(existing) await self._validate_inventory(existing) + existing.status = self._readiness_status(existing) + response_body = existing.model_dump( mode="json", by_alias=True, exclude_none=True ) @@ -760,20 +762,7 @@ async def complete_checkout( await self._process_payment(payment) # Validate Fulfillment (Required for completion in this implementation) - fulfillment_valid = False - if checkout.fulfillment and checkout.fulfillment.methods: - for method in checkout.fulfillment.methods: - if method.type == "shipping" and not method.selected_destination_id: - continue - if method.groups: - for group in method.groups: - if group.selected_option_id: - fulfillment_valid = True - break - if fulfillment_valid: - break - - if not fulfillment_valid: + if not self._has_fulfillment_selection(checkout): raise InvalidRequestError( "Fulfillment address and option must be selected before completion." ) @@ -1202,6 +1191,24 @@ def _ensure_modifiable(self, checkout: Checkout, action: str) -> None: f"Cannot {action} checkout in state '{checkout.status}'" ) + @staticmethod + def _has_fulfillment_selection(checkout: Checkout) -> bool: + """Return True if a fulfillment destination and option are selected.""" + if not checkout.fulfillment or not checkout.fulfillment.methods: + return False + for method in checkout.fulfillment.methods: + if method.type == "shipping" and not method.selected_destination_id: + continue + if method.groups and any(g.selected_option_id for g in method.groups): + return True + return False + + def _readiness_status(self, checkout: Checkout) -> CheckoutStatus: + """Status to report: ready only if Complete Checkout would accept it.""" + if self._has_fulfillment_selection(checkout): + return CheckoutStatus.READY_FOR_COMPLETE + return CheckoutStatus.IN_PROGRESS + async def _validate_inventory( self, checkout: Checkout, From f8487b51875a9bd8a8f3fb31f997754e9bc1cd66 Mon Sep 17 00:00:00 2001 From: damaz91 Date: Thu, 15 Oct 2026 10:49:20 +0000 Subject: [PATCH 2/2] style(rest/python): wrap long lines in test_checkout_readiness_lifecycle --- rest/python/server/integration_test.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/rest/python/server/integration_test.py b/rest/python/server/integration_test.py index f5e5181..6f9d095 100644 --- a/rest/python/server/integration_test.py +++ b/rest/python/server/integration_test.py @@ -2137,12 +2137,14 @@ def test_create_applies_payment_instruments(self) -> None: self.assertEqual(returned[0].get("type"), "card") def test_checkout_readiness_lifecycle(self) -> None: - """A checkout without fulfillment is incomplete and becomes ready once selected.""" + """Checkout without fulfillment is incomplete until selected.""" with self.client: # 1. Create checkout without fulfillment -> status must be 'incomplete' create_res = self.client.post( "/checkout-sessions", - headers=self._get_headers(idempotency_key="ready_1", request_id="ready_1"), + headers=self._get_headers( + idempotency_key="ready_1", request_id="ready_1" + ), json={ "line_items": [{"item": {"id": "rose"}, "quantity": 1}], }, @@ -2160,16 +2162,21 @@ def test_checkout_readiness_lifecycle(self) -> None: payment_payload = self._create_payment_payload() complete_res = self.client.post( f"/checkout-sessions/{checkout_id}/complete", - headers=self._get_headers(idempotency_key="ready_2", request_id="ready_2"), + headers=self._get_headers( + idempotency_key="ready_2", request_id="ready_2" + ), json=payment_payload, ) self.assertEqual(complete_res.status_code, 400) - # 3. Update checkout to select destination and option -> status becomes 'ready_for_complete' + # 3. Update checkout to select destination and option -> + # status becomes 'ready_for_complete' line_item_id = checkout_data["line_items"][0]["id"] update_res = self.client.put( f"/checkout-sessions/{checkout_id}", - headers=self._get_headers(idempotency_key="ready_3", request_id="ready_3"), + headers=self._get_headers( + idempotency_key="ready_3", request_id="ready_3" + ), json={ "line_items": [ { @@ -2202,13 +2209,16 @@ def test_checkout_readiness_lifecycle(self) -> None: self.assertEqual( update_res.json()["status"], "ready_for_complete", - "checkout with destination and option selected must be ready_for_complete", + "checkout with destination and option selected must be " + "ready_for_complete", ) # 4. Now complete succeeds complete_res = self.client.post( f"/checkout-sessions/{checkout_id}/complete", - headers=self._get_headers(idempotency_key="ready_4", request_id="ready_4"), + headers=self._get_headers( + idempotency_key="ready_4", request_id="ready_4" + ), json=payment_payload, ) self.assertEqual(complete_res.status_code, 200, complete_res.text)