BASE="https://mystore.com/ucp"
TOKEN="Bearer ucp_pk_test_xxxx"
# 步骤1: 创建结账会话
SESSION=$(curl -s -X POST "$BASE/checkout/sessions" \
-H "Content-Type: application/json" \
-H "Authorization: $TOKEN" \
-d '{
"line_items": [{"product_id": "prod_001", "quantity": 1}],
"buyer": {"email": "test@example.com"}
}')
echo "Create: $(echo $SESSION | jq -r '.checkout_session.status')"
# 期望: incomplete
SESSION_ID=$(echo $SESSION | jq -r '.checkout_session.id')
# 步骤2: 更新买家信息和配送
curl -s -X PATCH "$BASE/checkout/sessions/$SESSION_ID" \
-H "Content-Type: application/json" \
-H "Authorization: $TOKEN" \
-d '{
"buyer": {
"name": "Test User",
"shipping_address": {
"line1": "123 Test St",
"city": "Beijing",
"state": "Beijing",
"postal_code": "100000",
"country_code": "CN"
}
},
"fulfillment": {"method": "standard_shipping"}
}' | jq '.checkout_session.status'
# 步骤3: 查询会话状态
STATUS=$(curl -s "$BASE/checkout/sessions/$SESSION_ID" \
-H "Authorization: $TOKEN" | jq -r '.checkout_session.status')
echo "After update: $STATUS"
# 期望: incomplete 或 ready_for_complete
# 步骤4: 取消会话(测试环境建议取消而非完成)
curl -s -X POST "$BASE/checkout/sessions/$SESSION_ID/cancel" \
-H "Content-Type: application/json" \
-H "Authorization: $TOKEN" \
-d '{"reason": "test_cancellation"}' | jq '.checkout_session.status'
# 期望: canceled