訂單履行 Saga

PR Ready

具持久性的訂單處理流程,失敗時執行補償操作。

7 個節點 · 8 條連接pr ready
temporalsagae-commerceworkflow-engine
視覺化
驗證訂單cli

檢查商品庫存、收貨地址與訂單限額。

sequential預留庫存
預留庫存db

在倉儲管理系統中對商品進行鎖定。

sequential扣款
error執行補償操作
扣款api

透過 Stripe 進行付款授權與請款。

sequential建立出貨單
error執行補償操作
建立出貨單api

產生運送標籤並安排物流取件。

sequential寄送確認通知
error執行補償操作
寄送確認通知api

將含追蹤號碼的訂單確認信寄送給客戶。

event處理退貨申請
處理退貨申請human

客戶申請退貨,觸發逆向物流流程。

執行補償操作system

失敗時依反向順序執行補償交易。

ex-temporal-order-fulfillment.osop.yaml
# Temporal Order Fulfillment — OSOP Portable Workflow
#
# Durable order processing: validate the order, reserve inventory, charge
# payment, ship the order, and send confirmation. Each step is idempotent
# with compensating actions for rollback on failure.
#
# Run with Temporal or validate: osop validate temporal-order-fulfillment.osop.yaml

osop_version: "1.0"
id: "temporal-order-fulfillment"
name:"訂單履行 Saga"
description:"具持久性的訂單處理流程,失敗時執行補償操作。"
version: "1.0.0"
tags: [temporal, saga, e-commerce, workflow-engine]

nodes:
  - id: "validate_order"
    type: "cli"
    subtype: "script"
    name: "驗證訂單"
    description: "檢查商品庫存、收貨地址與訂單限額。"

  - id: "reserve_inventory"
    type: "db"
    name: "預留庫存"
    description: "在倉儲管理系統中對商品進行鎖定。"
    config:
      compensate: "release_inventory"
      timeout_minutes: 30

  - id: "charge_payment"
    type: "api"
    subtype: "rest"
    name: "扣款"
    description: "透過 Stripe 進行付款授權與請款。"
    config:
      url: "https://api.stripe.com/v1/charges"
      compensate: "refund_payment"
      retry_policy: { max_attempts: 3, backoff: "exponential" }

  - id: "ship_order"
    type: "api"
    subtype: "rest"
    name: "建立出貨單"
    description: "產生運送標籤並安排物流取件。"
    config:
      compensate: "cancel_shipment"

  - id: "send_confirmation"
    type: "api"
    subtype: "rest"
    name: "寄送確認通知"
    description: "將含追蹤號碼的訂單確認信寄送給客戶。"

  - id: "handle_return"
    type: "human"
    name: "處理退貨申請"
    description: "客戶申請退貨,觸發逆向物流流程。"

  - id: "compensation_handler"
    type: "system"
    name: "執行補償操作"
    description: "失敗時依反向順序執行補償交易。"

edges:
  - from: "validate_order"
    to: "reserve_inventory"
    mode: "sequential"
  - from: "reserve_inventory"
    to: "charge_payment"
    mode: "sequential"
  - from: "charge_payment"
    to: "ship_order"
    mode: "sequential"
  - from: "ship_order"
    to: "send_confirmation"
    mode: "sequential"
  - from: "send_confirmation"
    to: "handle_return"
    mode: "event"
    when: "return_requested"
    label: "Customer initiates return"
  - from: "reserve_inventory"
    to: "compensation_handler"
    mode: "error"
    label: "Any downstream step fails"
  - from: "charge_payment"
    to: "compensation_handler"
    mode: "error"
  - from: "ship_order"
    to: "compensation_handler"
    mode: "error"