如要進行原生整合,您必須建構 RESTful API,供 Google 呼叫以建立及管理結帳工作階段。
整體流程如下:
- 建立結帳工作階段:使用者和 (選用) 服務專員會循環將項目新增至工作階段。
- 移交給 Google UI:使用者決定結帳後,代理程式 (如果已參與) 會將控制權移交給 Google UI (並傳遞結帳工作階段資料)。
- 手動結帳:使用者現在只會與 Google UI 互動,填寫敏感的訂單履行和付款詳細資料,並提交訂單。代理程式不會參與這部分,確保確定性。
- 完成及退貨:Google UI 會顯示「感謝你」頁面,確認訂單。使用者也可以選擇重新導向至服務專員,服務專員可能已收到購買完成的通知。
以下步驟說明您需要實作的 API 端點及其行為。
1. 建立結帳工作階段
這個端點可建立結帳工作階段,內含使用者感興趣的產品。
- 端點:
POST /checkout-sessions - 觸發條件:使用者點按產品的「購買」按鈕。
要求:Google 會傳送委刊項和幣別。
{
"line_items": [
{
"item": {
"id": "product_12345", // Must match Product Feed ID
"title": "Running Shoes"
},
"quantity": 1
}
],
"currency": "USD"
}
回應:您會傳回已初始化的工作階段,其中包含總計金額、稅金 (一開始為預估值) 和付款功能。您必須加入服務條款和隱私權政策的連結,這些連結會顯示在付款按鈕下方。
{
"ucp": {
"version": "2026-01-11",
"capabilities": [
{
"name": "dev.ucp.shopping.checkout",
"version": "2026-01-11"
},
{
"name": "dev.ucp.shopping.fulfillment",
"version": "2026-01-11"
}
]
},
"id": "gid://merchant.example.com/Checkout/session_abc123",
"status": "incomplete",
"line_items": [
{
"id": "line_1",
"item": {
"id": "product_12345",
"title": "Running Shoes",
"price": 10000
},
"quantity": 1,
"base_amount": 10000,
"subtotal": 10000,
"total": 10000
}
],
"totals": [
{ "type": "subtotal", "amount": 10000 }, // in cents
{ "type": "tax", "amount": 0 },
{ "type": "total", "amount": 10000 }
],
"payment": {
"handlers": [
{
"id": "gpay",
"name": "com.google.pay",
"config": {
"api_version": 2,
"api_version_minor": 0,
"merchant_info": {
"merchant_id": "12345678901234567890",
"merchant_name": "Example Merchant"
},
"allowed_payment_methods": [
{
"type": "CARD",
"parameters": {
"allowed_auth_methods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],
"allowed_card_networks": ["VISA", "MASTERCARD"]
},
"tokenization_specification": {
"type": "PAYMENT_GATEWAY",
"parameters": {
"gateway": "stripe",
"gateway_merchant_id": "exampleGatewayMerchantId"
}
}
}
]
}
}
]
},
"links": [
{ "type": "privacy_policy", "url": "https://m.com/privacy" },
{ "type": "terms_of_service", "url": "https://m.com/terms" }
]
}
2. 取得結帳工作階段
這個端點可擷取結帳工作階段。
- 端點:
GET /checkout-sessions/{id}
要求:Google 會傳送結帳工作階段的 ID。如果您使用全域 ID
(例如 gid://merchant.example.com/Checkout/session_abc123),請注意,要求路徑中的 ID 只會是這個 ID 的最後一個元件 (例如session_abc123)。
回應:傳回完整的結帳物件。
3. 更新結帳工作階段
這個端點可更新結帳工作階段。更新運送地址時,必須重新計算並傳回稅金和運送選項。
- 端點:
PUT /checkout-sessions/{id} - 觸發條件:使用者選取或變更運送地址、更新運送選項,或更新付款資訊。
要求:Google 會傳送包含更新資訊的完整結帳物件。
{
"id": "gid://merchant.example.com/Checkout/session_abc123",
"buyer": {
"first_name": "John",
"last_name": "Doe",
"email": "john@example.com"
},
"fulfillment": {
"methods": [
{
"type": "shipping",
"destinations": [
{
"id": "dest_1",
"postal_code": "94043",
"country": "US",
"address_locality": "Mountain View",
"address_region": "CA"
}
],
"selected_destination_id": "dest_1"
}
]
},
"payment": {
"selected_instrument_id": "pi_gpay_5678",
"instruments": [
{
"id": "pi_gpay_5678",
"handler_id": "gpay",
"type": "card",
"brand": "mastercard",
"last_digits": "5678",
"rich_text_description": "Google Pay •••• 5678"
}
]
}
// ... other fields (line_items, currency, etc.)
}
回應:視需要重新計算稅金和運送選項,並傳回完整的結帳物件。
{
"id": "gid://merchant.example.com/Checkout/session_abc123",
"totals": [
{ "type": "subtotal", "amount": 10000 },
{ "type": "shipping", "display_text": "Ground Shipping", "amount": 500 },
{ "type": "tax", "amount": 850 },
{ "type": "total", "amount": 11350 }
],
// ... other fields (line_items, currency, etc.)
"fulfillment": {
"methods": [
{
"id": "method_shipping",
"type": "shipping",
"line_item_ids": ["line_1"],
"selected_destination_id": "dest_1",
"destinations": [
{
"id": "dest_1",
"postal_code": "94043",
"country": "US",
"address_locality": "Mountain View",
"address_region": "CA"
}
],
"groups": [
{
"id": "group_1",
"line_item_ids": ["line_1"],
"selected_option_id": "ship_ground",
"options": [
{
"id": "ship_ground",
"title": "Ground (3-5 days)",
"total": 500
},
{
"id": "ship_express",
"title": "Express (1-2 days)",
"total": 1500
}
]
}
]
}
]
}
}
4. 完成結帳工作階段
這個端點可完成結帳程序並下單。系統應傳回已完成的結帳工作階段,並包含訂單資訊。收到此呼叫後,就應開始處理付款事宜。
- 端點:
POST /checkout-sessions/{id}/complete - 觸發條件:使用者點選「下單」。
要求:Google 會傳送供應商的付款權杖 (加密 Blob),Google Pay),以及買家的風險信號,供您自行偵測詐欺行為。
{
"payment_data": {
"id": "pi_gpay_5678",
"handler_id": "gpay",
"type": "card",
"brand": "mastercard",
"last_digits": "5678",
"billing_address": {
"postal_code": "94043",
"address_country": "US"
},
"credential": {
"type": "PAYMENT_GATEWAY",
"token": "{\"signature\":\"...\",\"protocolVersion\":\"ECv2\"...}"
}
}
}
回應:您會傳回完整的結帳物件,指出訂單已完成,包括訂單 ID 和訂單的永久連結網址。
{
"ucp": {
"version": "2026-01-11",
"capabilities": [...]
},
"id": "gid://merchant.example.com/Checkout/session_abc123",
"status": "completed",
// ... other fields (line_items, currency, etc.)
"order_id": "gid://merchant.example.com/Order/789",
"order_permalink_url": "https://merchant.example.com/orders/789"
}
5. 取消結帳工作階段
這個端點會取消結帳工作階段。
- 端點:
POST /checkout-sessions/{id}/cancel
要求:Google 會傳送結帳工作階段的 ID。
回應:您會傳回完整的結帳物件,並將狀態更新為 canceled。