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..6f9d095 100644 --- a/rest/python/server/integration_test.py +++ b/rest/python/server/integration_test.py @@ -2136,6 +2136,94 @@ 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: + """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" + ), + 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,