---
id: user-checkout
name: User Checkout
version: 1.0.0
summary: >-
  An authenticated customer checks out a persisted basket through Ordering API;
  Finance coordinates asynchronous basket cleanup and Notification submits the
  order email over SMTP with independent outcomes and bounded retries.
owners:
  - commerce-fulfillment-team
diagrams:
  - id: checkout-interaction
    version: 1.0.0
badges:
  - content: Business Process
    backgroundColor: purple
    textColor: purple
steps:
  - id: customer
    title: Customer starts checkout
    actor:
      name: Customer
    next_step:
      id: authenticate
      label: Sign in to Storefront
  - id: authenticate
    title: OAuth API - Entra ID sign-in
    summary: >-
      Storefront authenticates the customer via Microsoft Entra ID with PKCE and
      customer scopes. Protected APIs derive buyer identity, full name, and
      email from verified token claims.
    service:
      id: oauth-api
      version: 1.0.0
    next_step:
      id: review-basket
      label: Authenticated customer reviews basket
  - id: review-basket
    title: Basket API - load persisted basket
    summary: >-
      Commerce loads the customer-scoped basket and displays items enriched
      with Catalog details. Edits must be saved to affect checkout.
    service:
      id: basket-api
      version: 1.0.0
    next_step:
      id: submit-checkout
      label: Customer confirms checkout
  - id: submit-checkout
    title: Create order
    summary: >-
      Commerce submits the checkout command through the gateway with a unique
      request key for idempotency. Ordering reads the customer's persisted
      basket rather than trusting client-supplied totals.
    message:
      id: CreateOrder
      version: 1.0.0
    next_step: create-order
  - id: create-order
    title: Ordering API - validate and build order
    summary: >-
      Enforces customer authorization, per-user rate limiting, and request
      idempotency before reading the persisted basket and current prices.
    service:
      id: order-api
      version: 1.0.0
    next_step: read-checkout-basket
  - id: read-checkout-basket
    title: Get current basket
    message:
      id: GetBasketGrpc
      version: 1.0.0
    next_step: basket-checkout-read
  - id: basket-checkout-read
    title: Basket API - return persisted quantities
    summary: >-
      Returns the authoritative item quantities stored in the basket cache.
      Unsaved browser edits are not included.
    service:
      id: basket-api
      version: 1.0.0
    next_step: read-checkout-books
  - id: read-checkout-books
    title: Resolve current catalog books
    message:
      id: GetBooksGrpc
      version: 1.0.0
    next_step: catalog-pricing
  - id: catalog-pricing
    title: Catalog API - provide authoritative prices
    summary: >-
      Resolves each book ID and supplies the sale price when active, otherwise
      the regular price. A missing book aborts order creation.
    service:
      id: catalog-api
      version: 1.0.0
    next_step: persist-order
  - id: persist-order
    title: Order Database - save New order snapshot
    summary: >-
      Under a per-buyer distributed lock, Ordering persists the New order and
      item-price snapshot and atomically queues the checkout integration event
      in the transactional outbox.
    container:
      id: order-database
      version: 1.0.0
    next_steps:
      - id: checkout-confirmation
        label: Confirm order acceptance immediately
      - id: checked-out
        label: Publish checkout event via outbox
  - id: checkout-confirmation
    title: Customer receives checkout confirmation
    summary: >-
      Commerce displays the confirmation page with the Order ID. This confirms
      order acceptance only—not basket deletion, payment settlement, or email
      delivery.
    actor:
      name: Customer
  - id: checked-out
    message:
      id: UserCheckedOutIntegrationEvent
      version: 1.0.0
    next_step:
      id: start-finance-saga
      label: Deliver via ordering.user-checked-out
  - id: start-finance-saga
    title: Finance - start tracked orchestration
    summary: >-
      Starts a durable orchestration correlated by Order ID, publishes the
      placement command, and schedules a 30-minute timeout. Finance coordinates
      messages and does not process card payments or own order status.
    service:
      id: order-orchestration
      version: 1.0.0
    next_steps:
      - id: place-order
        label: Request basket cleanup and order email
      - id: saga-timeout
        label: Schedule 30-minute orchestration timeout
  - id: place-order
    message:
      id: PlaceOrderCommand
      version: 1.0.0
    next_step: placement-channel
  - id: placement-channel
    title: Finance placement channel
    summary: >-
      Basket API and Notification consume the same command independently. Email
      submission does not wait for basket cleanup, and retries can repeat both
      consumers' work.
    channel:
      id: bookworm.{env}.finance.place-order
      version: 1.0.0
    next_steps:
      - id: delete-basket
        label: Basket cleanup consumer
      - id: prepare-email
        label: Order email consumer
  - id: delete-basket
    title: Basket API - delete submitted basket
    summary: >-
      Deletes the customer's basket from Redis. Returns a completion event on
      success or a failure event if deletion returns false.
    service:
      id: basket-api
      version: 1.0.0
    next_steps:
      - id: basket-deleted
        label: Basket deleted
      - id: basket-deletion-failed
        label: Deletion returned false
  - id: basket-deleted
    message:
      id: BasketDeletedCompleteIntegrationEvent
      version: 1.0.0
    next_step:
      id: finance-cleanup-success
      label: Deliver via basket.delete-complete
  - id: finance-cleanup-success
    title: Finance - forward cleanup completion
    summary: >-
      Records cleanup progress and notifies Ordering. Orchestration remains
      active for explicit order completion, cancellation, or timeout.
    service:
      id: order-orchestration
      version: 1.0.0
    next_step: report-cleanup-success
  - id: report-cleanup-success
    message:
      id: DeleteBasketCompleteCommand
      version: 1.0.0
    next_step:
      id: ordering-cleanup-success
      label: Deliver via finance.delete-basket-complete
  - id: ordering-cleanup-success
    title: Ordering API - log basket cleanup
    summary: >-
      Logs the cleanup completion for observability without changing the
      order's New business status.
    service:
      id: order-api
      version: 1.0.0
  - id: basket-deletion-failed
    message:
      id: BasketDeletedFailedIntegrationEvent
      version: 1.0.0
    next_step:
      id: finance-cleanup-failure
      label: Deliver via basket.delete-failed
  - id: finance-cleanup-failure
    title: Finance - finalize failed cleanup
    summary: >-
      Marks the orchestration as failed, ends coordination, and instructs
      Ordering to soft-delete the order. Does not request a cancellation email.
    service:
      id: order-orchestration
      version: 1.0.0
    next_step: report-cleanup-failure
  - id: report-cleanup-failure
    message:
      id: DeleteBasketFailedCommand
      version: 1.0.0
    next_step:
      id: ordering-cleanup-failure
      label: Deliver via finance.delete-basket-failed
  - id: ordering-cleanup-failure
    title: Ordering API - soft-delete order
    summary: >-
      Marks the order as soft-deleted while preserving its New business status
      and without emitting an order-cancellation event.
    service:
      id: order-api
      version: 1.0.0
  - id: prepare-email
    title: Notification - check idempotency and render email
    summary: >-
      Applies a 24-hour Redis idempotency guard keyed by message ID, skips
      blank recipient addresses, and renders the order email template.
    service:
      id: notification-api
      version: 1.0.0
    next_steps:
      - id: submit-email
        label: Valid recipient and template rendered
      - id: skip-email
        label: Missing recipient email
  - id: skip-email
    title: Skip email delivery
    summary: >-
      Returns without rendering or sending email. Does not affect the accepted
      order or basket cleanup.
    custom:
      title: Email skipped
      type: Outcome
      icon: CheckCircleIcon
      color: gray
  - id: submit-email
    title: SendGrid - SMTP submission
    summary: >-
      Submits the MIME message over SMTP with MailKit resilience (Mailpit in
      local development, SendGrid in Azure). SMTP acceptance confirms provider
      handoff, not inbox delivery.
    service:
      id: sendgrid-api
      version: 1.0.0
    next_steps:
      - id: email-submitted
        label: Provider accepted message
      - id: email-failed
        label: Submission failed after retries
  - id: email-submitted
    title: Email submitted to provider
    summary: >-
      Notification emits no delivery-result callback and never mutates order
      status or Finance orchestration state.
    custom:
      title: Email submitted
      type: Outcome
      icon: CheckCircleIcon
      color: green
  - id: email-failed
    title: Notification dead-letter queue
    summary: >-
      Transient errors exhaust Wolverine retries and route the command to the
      notification dead-letter queue for operator inspection.
    channel:
      id: bookworm.{env}.notification.dead-letter-queue
      version: 1.0.0
  - id: saga-timeout
    title: Finance - evaluate 30-minute timeout
    summary: >-
      Increments the retry counter while orchestration remains active. Up to
      two reissues are allowed (three total attempts) before exhaustion.
    service:
      id: order-orchestration
      version: 1.0.0
    next_steps:
      - id: retry-placement
        label: Retry count below maximum attempts
      - id: timeout-exhausted
        label: Retry limit reached
  - id: retry-placement
    title: Finance - reissue placement command
    summary: >-
      Reissues the shared placement command with a new message ID and schedules
      the next 30-minute timeout.
    service:
      id: order-orchestration
      version: 1.0.0
    next_steps:
      - id: place-order
        label: Reissue PlaceOrderCommand
      - id: saga-timeout
        label: Schedule next timeout
  - id: timeout-exhausted
    title: Finance - cancel orchestration only
    summary: >-
      Marks the orchestration cancelled when retries exhaust. Emits a
      cancellation email command if contact details are present, without
      cancelling the order in Ordering.
    service:
      id: order-orchestration
      version: 1.0.0
    next_steps:
      - id: request-cancellation-email
        label: Name and email present
      - id: saga-ended-without-email
        label: Contact details absent
  - id: request-cancellation-email
    message:
      id: CancelOrderCommand
      version: 1.0.0
    next_step:
      id: prepare-email
      label: Deliver via finance.cancel-order
  - id: saga-ended-without-email
    title: Orchestration ended without email
    summary: >-
      Retry exhaustion ends Finance coordination without changing the order's
      business status.
    custom:
      title: Orchestration ended
      type: Outcome
      icon: ExclamationTriangleIcon
      color: orange
---

## Trigger & Business Outcome

This flow is triggered when an authenticated customer confirms checkout in the Commerce storefront to convert their persisted shopping basket into an immutable purchase snapshot. Success is confirmed when [[service|order-api]] persists a **New** [[entity|order]] in [[container|order-database]] and returns immediate confirmation, while [[service|order-orchestration]] asynchronously coordinates basket deletion in [[service|basket-api]] and order email submission via [[service|notification-api]].

## Interactive Flow Visualization

<NodeGraph />

## Step-by-Step Walkthrough

<Steps title="Checkout Acceptance and Post-Checkout Coordination">
	<Step title="Authenticate and review persisted basket">
		The customer signs in with [[system|identity-provider]] ([[service|oauth-api]]) and reviews
		their basket in Commerce, which loads customer-scoped items from [[service|basket-api]]. Only
		saved basket quantities participate in checkout.
	</Step>
	<Step title="Accept order synchronously in Ordering">
		[[service|order-api]] handles [[command|CreateOrder]] with request-level idempotency and
		per-user rate limiting. It fetches persisted quantities via [[query|GetBasketGrpc]] from
		[[service|basket-api]] and authoritative prices via [[query|GetBooksGrpc]] from
		[[service|catalog-api]] (preferring sale price when active). Under a per-buyer distributed lock,
		it persists the **New** [[entity|order]] in [[container|order-database]], queues
		[[event|UserCheckedOutIntegrationEvent]] in its transactional outbox, and confirms acceptance
		immediately.
	</Step>
	<Step title="Start asynchronous Finance orchestration">
		[[service|order-orchestration]] consumes [[event|UserCheckedOutIntegrationEvent]] from{" "}
		<ResourceLink id="bookworm.{env}.ordering.user-checked-out" type="channel">
			`bookworm.{env}.ordering.user-checked-out`
		</ResourceLink>
		, starts a tracked orchestration correlated by Order ID, publishes [[command|PlaceOrderCommand]]
		to{" "}
		<ResourceLink id="bookworm.{env}.finance.place-order" type="channel">
			`bookworm.{env}.finance.place-order`
		</ResourceLink>
		, and schedules a 30-minute timeout.
	</Step>
	<Step title="Fan out independently to Basket and Notification">
		[[service|basket-api]] and [[service|notification-api]] consume [[command|PlaceOrderCommand]] in
		parallel. [[service|basket-api]] deletes the basket from [[container|basket-database]] and
		reports either [[event|BasketDeletedCompleteIntegrationEvent]] or
		[[event|BasketDeletedFailedIntegrationEvent]]. Meanwhile, [[service|notification-api]] applies a
		24-hour Redis idempotency guard, renders the order email template, and submits it over SMTP to
		[[service|sendgrid-api]] (or Mailpit locally).
	</Step>
	<Step title="Enforce bounded timeout retries">
		While orchestration remains active, each 30-minute timeout reissues
		[[command|PlaceOrderCommand]] up to two times (three total attempts). When the retry limit is
		reached, [[service|order-orchestration]] cancels its own orchestration and—if contact details
		are present—publishes [[command|CancelOrderCommand]] to [[service|notification-api]] without
		cancelling the [[entity|order]] in [[service|order-api]].
	</Step>
</Steps>

## Failure, Retry & Compensation Behavior

| Failure Point                            | System Behavior                                                                                                                                                                                     | Compensation / Recovery                                                                                                                                                                                                                                                                                                                                  |
| :--------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Missing catalog book or gRPC timeout** | <ResourceLink id="order-api" type="service">order-api</ResourceLink> aborts order creation before committing                                                                                        | Customer remains on checkout; no <ResourceLink id="order" type="entity">order</ResourceLink> or outbox event is created                                                                                                                                                                                                                                  |
| **Basket deletion returns false**        | <ResourceLink id="basket-api" type="service">basket-api</ResourceLink> emits <ResourceLink id="BasketDeletedFailedIntegrationEvent" type="event">BasketDeletedFailedIntegrationEvent</ResourceLink> | <ResourceLink id="order-orchestration" type="service">order-orchestration</ResourceLink> finalizes as failed and sends <ResourceLink id="DeleteBasketFailedCommand" type="command">DeleteBasketFailedCommand</ResourceLink>; <ResourceLink id="order-api" type="service">order-api</ResourceLink> soft-deletes the order while preserving **New** status |
| **Basket repository exception**          | Exception propagates to message-broker retries                                                                                                                                                      | Retried automatically; dead-lettered to <ResourceLink id="bookworm.{env}.basket.dead-letter-queue" type="channel">`bookworm.{env}.basket.dead-letter-queue`</ResourceLink> if exhausted                                                                                                                                                                  |
| **SMTP submission failure**              | MailKit resilience and worker retries execute with backoff                                                                                                                                          | Exhausted failures route to <ResourceLink id="bookworm.{env}.notification.dead-letter-queue" type="channel">`bookworm.{env}.notification.dead-letter-queue`</ResourceLink>; order status is unaffected                                                                                                                                                   |
| **Orchestration timeout exhaustion**     | <ResourceLink id="order-orchestration" type="service">order-orchestration</ResourceLink> reaches its three-attempt cap                                                                              | Cancels orchestration only and requests a cancellation email via <ResourceLink id="CancelOrderCommand" type="command">CancelOrderCommand</ResourceLink> if contact details exist                                                                                                                                                                         |

:::warning
**Confirmation means order acceptance—not basket deletion, payment settlement, or email delivery.**

- Because [[service|basket-api]] and [[service|notification-api]] consume [[command|PlaceOrderCommand]] independently, a placement email may be submitted even if basket cleanup later fails.
- Reissued timeout commands carry new message IDs, which bypass per-message-ID deduplication in [[service|notification-api]] and can result in a duplicate email.
- Always inspect both order business status and soft-deletion state in [[container|order-database]] before replaying failed checkout messages.
  :::

## Participating Resources

- **Domains & Systems:** [[domain|shopping]], [[domain|ordering]], [[domain|catalog]], [[domain|platform]], [[system|identity-provider]], [[system|sendgrid]]
- **Services:** [[service|oauth-api]], [[service|basket-api]], [[service|catalog-api]], [[service|order-api]], [[service|order-orchestration]], [[service|notification-api]], [[service|sendgrid-api]]
- **Messages:** [[command|CreateOrder]], [[query|GetBasketGrpc]], [[query|GetBooksGrpc]], [[event|UserCheckedOutIntegrationEvent]], [[command|PlaceOrderCommand]], [[event|BasketDeletedCompleteIntegrationEvent]], [[command|DeleteBasketCompleteCommand]], [[event|BasketDeletedFailedIntegrationEvent]], [[command|DeleteBasketFailedCommand]], [[command|CancelOrderCommand]]
- **Channels & Stores:** <ResourceLink id="bookworm.{env}.ordering.user-checked-out" type="channel">`bookworm.{env}.ordering.user-checked-out`</ResourceLink>, <ResourceLink id="bookworm.{env}.finance.place-order" type="channel">`bookworm.{env}.finance.place-order`</ResourceLink>, <ResourceLink id="bookworm.{env}.notification.dead-letter-queue" type="channel">`bookworm.{env}.notification.dead-letter-queue`</ResourceLink>, [[container|basket-database]], [[container|order-database]]
- **Related Views:** [[diagram|checkout-interaction]], [[flow|order-lifecycle]], [[flow|notification-delivery]]
