本指南將逐步引導您開發 Actions 專案,其中包含實體商品交易,以及使用 Google Pay 支付款項。
交易流程
Actions 專案使用 Google Pay 處理實體交易時,會使用下列流程:
- 收集資訊 (選填):視交易性質而定,您可能想要在對話開始時向使用者收集下列資訊。
- 驗證交易需求 - 在開始對話時使用交易需求輔助工具,以確保在使用者建立購物車前,使用者的付款資訊設定正確且可供使用。
- 要求寄送地址:如果您的交易需要寄送地址,請要求寄送地址輔助程式意圖,以便向使用者收集地址。
- 建立訂單:引導使用者完成「購物車組裝」流程,從中挑選要購買的商品。
- 建議訂單 - 購物車完成後,請向使用者顯示訂單,以便確認訂單正確。如果訂單已確認,您會收到回應,內含訂單詳細資料和付款權杖。
- 完成訂單並傳送收據:在訂單確認後,更新庫存追蹤或其他出貨服務,然後傳送收據給使用者。
- 傳送訂單更新 - 在訂單執行期限的效期內,傳送 PATCH 要求至 Orders API,掌握使用者訂單更新情形。
限制和查看規範
請注意,「用於交易的動作」須遵守額外政策。我們最多需要六週的處理時間來處理含有交易的動作,因此在規劃發布時間表時,請將這段時間納入考量。為簡化審查程序,在將動作送交審查之前,請務必遵守交易政策與規範。
您只能在下列國家/地區部署販售實體商品的動作:
澳洲 巴西 加拿大 印尼 |
日本 墨西哥 卡達 俄羅斯 |
新加坡 瑞士 泰國 土耳其 英國 美國 |
建構您的專案
如需完整的交易對話範例,請參閱 Node.js 和 Java 中的交易範例。
專案設定
建立動作時,您必須在動作控制台中指定要執行交易。此外,如果您使用的是 Node.JS 用戶端程式庫,請設定執行要求以使用最新版的 Orders API。
如要設定專案和執行要求,請按照以下步驟操作:
- 建立新專案或匯入現有專案。
- 依序前往「Deploy」(部署) >「Directory information」(目錄資訊)。
在「其他資訊」>「交易」下方,勾選「您的動作是否使用 Transactions API 執行實體商品交易?」方塊。
如果您是使用 Node.JS 用戶端程式庫建構動作的執行要求,請開啟執行要求程式碼並更新應用程式差異化功能,將
ordersv3
標記設為true
。下列程式碼片段是訂單第 3 版的應用程式宣告範例。
Node.js
const {dialogflow} = require('actions-on-google'); let app = dialogflow({ clientId, // If using account linking debug: true, ordersv3: true, });
Node.js
const {actionssdk} = require('actions-on-google'); let app = actionssdk({ clientId, // If using account linking debug: true, ordersv3: true, });
1. 收集資訊 (選填)
1a. 驗證交易規定 (選用)
使用者體驗
使用者表示要購買後,建議您立即觸發 actions.intent.TRANSACTION_REQUIREMENTS_CHECK
意圖,確保他們能夠快速完成交易。例如,叫用時,您的動作可能會詢問:「要在購買鞋款或查看帳戶餘額嗎?」如果使用者說出「訂購鞋子」,您應立即要求這個意圖。這樣就能確保他們可以繼續操作,並讓他們有機會修正所有設定,防止他們繼續交易。
要求交易要求檢查意圖會產生下列其中一個結果:
- 如果符合要求,意圖就會傳回執行要求並傳回成功,然後您就可以繼續建立使用者的訂單。
- 如果不符合一或多項要求,意圖會回傳失敗的條件則傳回執行要求。在這種情況下,您應該停止對話體驗,或是結束對話。
- 如果使用者可以修正任何導致失敗狀態的錯誤,系統會提示使用者在裝置上解決這些問題。如果對話在僅支援語音的途徑上,系統會轉接至使用者的手機。
出貨
為確保使用者符合交易需求,請使用 TransactionRequirementsCheckSpec 物件要求執行 actions.intent.TRANSACTION_REQUIREMENTS_CHECK
意圖。
確認規定
您可以使用用戶端程式庫,查看使用者是否符合交易需求:
Node.js
conv.ask(new TransactionRequirements());
Node.js
conv.ask(new TransactionRequirements());
Java
return getResponseBuilder(request) .add(new TransactionRequirements()) .build();
Java
return getResponseBuilder(request) .add(new TransactionRequirements()) .build();
JSON
{ "payload": { "google": { "expectUserResponse": true, "richResponse": { "items": [ { "simpleResponse": { "textToSpeech": "Looks like you're good to go! Next I'll need your delivery address.Try saying \"get delivery address\"." } } ], "suggestions": [ { "title": "get delivery address" } ] } } } }
JSON
{ "expectUserResponse": true, "expectedInputs": [ { "possibleIntents": [ { "intent": "actions.intent.TEXT" } ], "inputPrompt": { "richInitialPrompt": { "items": [ { "simpleResponse": { "textToSpeech": "Looks like you're good to go! Next I'll need your delivery address.Try saying \"get delivery address\"." } } ], "suggestions": [ { "title": "get delivery address" } ] } } } ], "conversationToken": "{\"data\":{\"paymentType\":\"google_payment\"}}" }
接收需求檢查結果
Google 助理執行意圖後,會傳送具有 actions.intent.TRANSACTION_REQUIREMENTS_CHECK
意圖的要求和檢查結果。
為了正確處理這項要求,請宣告由 actions_intent_TRANSACTION_REQUIREMENTS_CHECK
事件觸發的 Dialogflow 意圖。觸發後,請使用用戶端程式庫在執行要求中處理。
Node.js
app.intent('Transaction Check Complete', (conv) => { const arg = conv.arguments.get('TRANSACTION_REQUIREMENTS_CHECK_RESULT'); if (arg && arg.resultType === 'CAN_TRANSACT') { // Normally take the user through cart building flow conv.ask(`Looks like you're good to go! ` + `Next I'll need your delivery address.` + `Try saying "get delivery address".`); conv.ask(new Suggestions('get delivery address')); } else { // Exit conversation conv.close('Transaction failed.'); } });
Node.js
app.intent('actions.intent.TRANSACTION_REQUIREMENTS_CHECK', (conv) => { const arg = conv.arguments.get('TRANSACTION_REQUIREMENTS_CHECK_RESULT'); if (arg && arg.resultType === 'CAN_TRANSACT') { // Normally take the user through cart building flow conv.ask(`Looks like you're good to go! ` + `Next I'll need your delivery address.` + `Try saying "get delivery address".`); conv.ask(new Suggestions('get delivery address')); } else { // Exit conversation conv.close('Transaction failed.'); } });
Java
@ForIntent("Transaction Check Complete") public ActionResponse transactionCheckComplete(ActionRequest request) { LOGGER.info("Checking Transaction Requirements Result."); // Check result of transaction requirements check Argument transactionCheckResult = request .getArgument("TRANSACTION_REQUIREMENTS_CHECK_RESULT"); boolean result = false; if (transactionCheckResult != null) { Map<String, Object> map = transactionCheckResult.getExtension(); if (map != null) { String resultType = (String) map.get("resultType"); result = resultType != null && resultType.equals("CAN_TRANSACT"); } } ResponseBuilder responseBuilder = getResponseBuilder(request); if (result) { // Normally take the user through cart building flow responseBuilder .add("Looks like you're good to go! Next " + "I'll need your delivery address. Try saying " + "\"get delivery address\".") .addSuggestions(new String[]{"get delivery address"}); } else { // Exit conversation responseBuilder.add("Transaction failed."); } return responseBuilder.build(); }
Java
@ForIntent("actions.intent.TRANSACTION_REQUIREMENTS_CHECK") public ActionResponse transactionCheckComplete(ActionRequest request) { LOGGER.info("Checking Transaction Requirements Result."); // Check result of transaction requirements check Argument transactionCheckResult = request .getArgument("TRANSACTION_REQUIREMENTS_CHECK_RESULT"); boolean result = false; if (transactionCheckResult != null) { Map<String, Object> map = transactionCheckResult.getExtension(); if (map != null) { String resultType = (String) map.get("resultType"); result = resultType != null && resultType.equals("CAN_TRANSACT"); } } ResponseBuilder responseBuilder = getResponseBuilder(request); if (result) { // Normally take the user through cart building flow responseBuilder .add("Looks like you're good to go! Next " + "I'll need your delivery address. Try saying " + "\"get delivery address\".") .addSuggestions(new String[]{"get delivery address"}); } else { // Exit conversation responseBuilder.add("Transaction failed."); } return responseBuilder.build(); }
JSON
{ "responseId": "db1a333c-2781-41e3-84b1-cc0cc37643d7-594de0a7", "queryResult": { "queryText": "actions_intent_TRANSACTION_REQUIREMENTS_CHECK", "action": "transaction.check.complete", "parameters": {}, "allRequiredParamsPresent": true, "fulfillmentText": "Failed to get transaction check results", "fulfillmentMessages": [ { "text": { "text": [ "Failed to get transaction check results" ] } } ], "outputContexts": [ { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_media_response_audio" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_audio_output" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_account_linking" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_screen_output" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_web_browser" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/google_assistant_input_type_keyboard" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/merchant_payment", "lifespanCount": 1 }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_intent_transaction_requirements_check", "parameters": { "TRANSACTION_REQUIREMENTS_CHECK_RESULT": { "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionRequirementsCheckResult", "resultType": "CAN_TRANSACT" }, "text": "" } } ], "intent": { "name": "projects/df-transactions/agent/intents/fd16d86b-60db-4d19-a683-5b52a22f4795", "displayName": "Transaction Check Complete" }, "intentDetectionConfidence": 1, "languageCode": "en" }, "originalDetectIntentRequest": { "source": "google", "version": "2", "payload": { "user": { "locale": "en-US", "lastSeen": "2019-09-23T19:49:32Z", "userVerificationStatus": "VERIFIED" }, "conversation": { "conversationId": "ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy", "type": "ACTIVE", "conversationToken": "[\"merchant_payment\"]" }, "inputs": [ { "intent": "actions.intent.TRANSACTION_REQUIREMENTS_CHECK", "rawInputs": [ { "inputType": "KEYBOARD" } ], "arguments": [ { "name": "TRANSACTION_REQUIREMENTS_CHECK_RESULT", "extension": { "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionRequirementsCheckResult", "resultType": "CAN_TRANSACT" } }, { "name": "text" } ] } ], "surface": { "capabilities": [ { "name": "actions.capability.MEDIA_RESPONSE_AUDIO" }, { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.ACCOUNT_LINKING" }, { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.WEB_BROWSER" } ] }, "availableSurfaces": [ { "capabilities": [ { "name": "actions.capability.WEB_BROWSER" }, { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.SCREEN_OUTPUT" } ] } ] } }, "session": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy" }
JSON
{ "user": { "locale": "en-US", "lastSeen": "2019-11-11T23:56:03Z", "userVerificationStatus": "VERIFIED" }, "conversation": { "conversationId": "ABwppHGFwStZlYaQ9YT8rg9t_idVsxZrku1pUDrEbGSJmSUMatVdPwPEEQSCe1IwIBoN4sS4Weyn9pmgetEgbsWgw3JSvQmw", "type": "ACTIVE", "conversationToken": "{\"data\":{\"paymentType\":\"merchant_payment\"}}" }, "inputs": [ { "intent": "actions.intent.TRANSACTION_REQUIREMENTS_CHECK", "rawInputs": [ {} ], "arguments": [ { "name": "TRANSACTION_REQUIREMENTS_CHECK_RESULT", "extension": { "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionRequirementsCheckResult", "resultType": "CAN_TRANSACT" } }, { "name": "text" } ] } ], "surface": { "capabilities": [ { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.ACCOUNT_LINKING" }, { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.MEDIA_RESPONSE_AUDIO" } ] }, "availableSurfaces": [ { "capabilities": [ { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.WEB_BROWSER" } ] } ] }
1b. 索取寄送地址 (選填)
如果交易需要使用者的寄送地址,您可以要求執行 actions.intent.DELIVERY_ADDRESS
意圖。如要確保使用者位於您的服務地區內,便可參考這項資訊來確認總價、寄送地點/取貨地點。
要求執行這項意圖時,您必須傳入 reason
選項,讓您預先向 Google 助理的要求,取得含有字串的地址。舉例來說,如果您指定「知道訂單的傳送地點」,Google 助理可能會詢問使用者:
「為了知道訂單的交貨地點,我我需要取得你的寄送地址」
使用者體驗
在顯示螢幕的介面上,使用者可以選擇要用於交易的位址。如果他們之前沒有提供地址,可以輸入新地址。
在純語音介面上,Google 助理會要求使用者授予交易預設地址的權限。如果他們之前尚未提供地址,則對話會傳送至手機進行輸入。
要求地址
Node.js
app.intent('Delivery Address', (conv) => { conv.ask(new DeliveryAddress({ addressOptions: { reason: 'To know where to send the order', }, })); });
Node.js
conv.ask(new DeliveryAddress({ addressOptions: { reason: 'To know where to send the order', }, }));
Java
@ForIntent("Delivery Address") public ActionResponse deliveryAddress(ActionRequest request) { DeliveryAddressValueSpecAddressOptions addressOptions = new DeliveryAddressValueSpecAddressOptions() .setReason("To know where to send the order"); return getResponseBuilder(request) .add(new DeliveryAddress() .setAddressOptions(addressOptions)) .build(); }
Java
DeliveryAddressValueSpecAddressOptions addressOptions = new DeliveryAddressValueSpecAddressOptions() .setReason("To know where to send the order"); return getResponseBuilder(request) .add(new DeliveryAddress() .setAddressOptions(addressOptions)) .build();
JSON
{ "payload": { "google": { "expectUserResponse": true, "systemIntent": { "intent": "actions.intent.DELIVERY_ADDRESS", "data": { "@type": "type.googleapis.com/google.actions.v2.DeliveryAddressValueSpec", "addressOptions": { "reason": "To know where to send the order" } } } } } }
JSON
{ "expectUserResponse": true, "expectedInputs": [ { "possibleIntents": [ { "intent": "actions.intent.DELIVERY_ADDRESS", "inputValueData": { "@type": "type.googleapis.com/google.actions.v2.DeliveryAddressValueSpec", "addressOptions": { "reason": "To know where to send the order" } } } ] } ], "conversationToken": "{\"data\":{\"paymentType\":\"merchant_payment\"}}" }
接收位址
Google 助理執行意圖後,會使用 actions.intent.DELIVERY_ADDRESS
意圖傳送執行要求。
為了正確處理這項要求,請宣告由 actions_intent_DELIVERY_ADDRESS
事件觸發的 Dialogflow 意圖。觸發後,請使用用戶端程式庫在執行要求中處理。
Node.js
app.intent('Delivery Address Complete', (conv) => { const arg = conv.arguments.get('DELIVERY_ADDRESS_VALUE'); if (arg && arg.userDecision ==='ACCEPTED') { conv.data.location = arg.location; conv.ask('Great, got your address! Now say "confirm transaction".'); conv.ask(new Suggestions('confirm transaction')); } else { conv.close('Transaction failed.'); } });
Node.js
app.intent('actions.intent.DELIVERY_ADDRESS', (conv) => { const arg = conv.arguments.get('DELIVERY_ADDRESS_VALUE'); if (arg && arg.userDecision ==='ACCEPTED') { conv.data.location = arg.location; conv.ask('Great, got your address! Now say "confirm transaction".'); conv.ask(new Suggestions('confirm transaction')); } else { conv.close('Transaction failed.'); } });
Java
@ForIntent("Delivery Address Complete") public ActionResponse deliveryAddressComplete(ActionRequest request) { Argument deliveryAddressValue = request.getArgument("DELIVERY_ADDRESS_VALUE"); Location deliveryAddress = null; if (deliveryAddressValue != null) { Map<String, Object> map = deliveryAddressValue.getExtension(); if (map != null) { String userDecision = (String) map.get("userDecision"); Location location = (Location) map.get("location"); deliveryAddress = userDecision != null && userDecision.equals("ACCEPTED") ? location : null; } } ResponseBuilder responseBuilder = getResponseBuilder(request); if (deliveryAddress != null) { // Cache delivery address in conversation data for later use Map<String, Object> conversationData = request.getConversationData(); conversationData.put("location", GSON_BUILDER.create().toJson(deliveryAddress, Location.class)); responseBuilder .add("Great, got your address! Now say \"confirm transaction\".") .addSuggestions(new String[] { "confirm transaction" }); } else { responseBuilder.add("Transaction failed.").endConversation(); } return responseBuilder.build(); }
Java
@ForIntent("actions.intent.DELIVERY_ADDRESS") public ActionResponse deliveryAddressComplete(ActionRequest request) { Argument deliveryAddressValue = request.getArgument("DELIVERY_ADDRESS_VALUE"); Location deliveryAddress = null; if (deliveryAddressValue != null) { Map<String, Object> map = deliveryAddressValue.getExtension(); if (map != null) { String userDecision = (String) map.get("userDecision"); Location location = (Location) map.get("location"); deliveryAddress = userDecision != null && userDecision.equals("ACCEPTED") ? location : null; } } ResponseBuilder responseBuilder = getResponseBuilder(request); if (deliveryAddress != null) { // Cache delivery address in conversation data for later use Map<String, Object> conversationData = request.getConversationData(); conversationData.put("location", GSON_BUILDER.create().toJson(deliveryAddress, Location.class)); responseBuilder .add("Great, got your address! Now say \"confirm transaction\".") .addSuggestions(new String[] { "confirm transaction" }); } else { responseBuilder.add("Transaction failed.").endConversation(); } return responseBuilder.build(); }
JSON
{ "responseId": "58b0c305-b437-47ac-8593-4fb0122a19e6-594de0a7", "queryResult": { "queryText": "actions_intent_DELIVERY_ADDRESS", "parameters": {}, "allRequiredParamsPresent": true, "fulfillmentMessages": [ { "text": { "text": [ "" ] } } ], "outputContexts": [ { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_media_response_audio" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_audio_output" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_account_linking" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_screen_output" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_web_browser" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_intent_delivery_address", "parameters": { "DELIVERY_ADDRESS_VALUE": { "@type": "type.googleapis.com/google.actions.v2.DeliveryAddressValue", "userDecision": "ACCEPTED", "location": { "coordinates": { "latitude": 37.432524, "longitude": -122.098545 }, "zipCode": "94043-1351", "city": "MOUNTAIN VIEW", "postalAddress": { "regionCode": "US", "postalCode": "94043-1351", "administrativeArea": "CA", "locality": "MOUNTAIN VIEW", "addressLines": [ "1600 AMPHITHEATRE PKWY" ], "recipients": [ "John Doe" ] }, "phoneNumber": "+1 123-456-7890" } }, "text": "1600 AMPHITHEATRE PKWY" } } ], "intent": { "name": "projects/df-transactions/agent/intents/0be5d130-1760-4355-85e9-4dc01da8bf3c", "displayName": "Delivery Address Complete" }, "intentDetectionConfidence": 1, "languageCode": "en" }, "originalDetectIntentRequest": { "source": "google", "version": "2", "payload": { "user": { "locale": "en-US", "lastSeen": "2019-09-23T19:49:55Z", "userVerificationStatus": "VERIFIED" }, "conversation": { "conversationId": "ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy", "type": "ACTIVE", "conversationToken": "[]" }, "inputs": [ { "intent": "actions.intent.DELIVERY_ADDRESS", "rawInputs": [ { "query": "1600 AMPHITHEATRE PKWY" } ], "arguments": [ { "name": "DELIVERY_ADDRESS_VALUE", "extension": { "@type": "type.googleapis.com/google.actions.v2.DeliveryAddressValue", "userDecision": "ACCEPTED", "location": { "coordinates": { "latitude": 37.432524, "longitude": -122.098545 }, "zipCode": "94043-1351", "city": "MOUNTAIN VIEW", "postalAddress": { "regionCode": "US", "postalCode": "94043-1351", "administrativeArea": "CA", "locality": "MOUNTAIN VIEW", "addressLines": [ "1600 AMPHITHEATRE PKWY" ], "recipients": [ "John Doe" ] }, "phoneNumber": "+1 123-456-7890" } } } ] } ], "surface": { "capabilities": [ { "name": "actions.capability.WEB_BROWSER" }, { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.ACCOUNT_LINKING" }, { "name": "actions.capability.MEDIA_RESPONSE_AUDIO" }, { "name": "actions.capability.AUDIO_OUTPUT" } ] }, "availableSurfaces": [ { "capabilities": [ { "name": "actions.capability.WEB_BROWSER" }, { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.SCREEN_OUTPUT" } ] } ] } }, "session": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy" }
JSON
{ "user": { "locale": "en-US", "lastSeen": "2019-11-11T23:57:20Z", "userVerificationStatus": "VERIFIED" }, "conversation": { "conversationId": "ABwppHGFwStZlYaQ9YT8rg9t_idVsxZrku1pUDrEbGSJmSUMatVdPwPEEQSCe1IwIBoN4sS4Weyn9pmgetEgbsWgw3JSvQmw", "type": "ACTIVE", "conversationToken": "{\"data\":{\"paymentType\":\"merchant_payment\"}}" }, "inputs": [ { "intent": "actions.intent.DELIVERY_ADDRESS", "rawInputs": [ { "inputType": "VOICE", "query": "1600 AMPHITHEATRE PKWY" } ], "arguments": [ { "name": "DELIVERY_ADDRESS_VALUE", "extension": { "@type": "type.googleapis.com/google.actions.v2.DeliveryAddressValue", "userDecision": "ACCEPTED", "location": { "coordinates": { "latitude": 37.421578499999995, "longitude": -122.0837816 }, "zipCode": "94043-1351", "city": "MOUNTAIN VIEW", "postalAddress": { "regionCode": "US", "postalCode": "94043-1351", "administrativeArea": "CA", "locality": "MOUNTAIN VIEW", "addressLines": [ "1600 AMPHITHEATRE PKWY" ], "recipients": [ "John Doe" ] }, "phoneNumber": "+1 123-456-7890" } } }, { "name": "text", "rawText": "1600 AMPHITHEATRE PKWY", "textValue": "1600 AMPHITHEATRE PKWY" } ] } ], "surface": { "capabilities": [ { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.MEDIA_RESPONSE_AUDIO" }, { "name": "actions.capability.ACCOUNT_LINKING" } ] }, "availableSurfaces": [ { "capabilities": [ { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.WEB_BROWSER" }, { "name": "actions.capability.SCREEN_OUTPUT" } ] } ] }
2. 建立訂單
使用者體驗
取得所需的使用者資訊後,您就可以建構「購物車組合」體驗,引導使用者建立訂單。每項動作的購物車組件流程,會因其產品或服務而異。
最基本的購物車組合體驗可讓使用者從清單中挑選要加入順序的商品,但您可以設計對話來簡化使用者體驗。您可以打造購物車組合體驗,讓使用者透過簡單的是非題,重新訂購最近的購買交易。您也可以向使用者展示熱門「精選」或「推薦」項目的輪轉介面或清單資訊卡。
我們建議使用豐富的回應以視覺化方式呈現使用者選項,同時設計對話,讓使用者僅使用語音建構購物車。如需高品質購物車組合體驗的最佳做法和範例,請參閱「交易設計指南」。
出貨
在對話過程中,您需要收集使用者想購買的項目,然後建構 Order
物件。
您的 Order
至少必須包含下列項目:
buyerInfo
- 購買使用者的相關資訊。transactionMerchant
- 處理訂單的商家相關資訊。contents
- 訂單的實際內容 (顯示為lineItems
)。priceAttributes
:訂單的價格詳細資料,包括訂單總費用 (含折扣和稅金)。
請參閱 Order
回應說明文件,瞭解如何建構購物車。請注意,您可能需要根據順序加入不同的欄位。
以下程式碼範例顯示完整訂單,包括選填欄位:
Node.js
const order = { createTime: '2019-09-24T18:00:00.877Z', lastUpdateTime: '2019-09-24T18:00:00.877Z', merchantOrderId: orderId, // A unique ID String for the order userVisibleOrderId: orderId, transactionMerchant: { id: 'http://www.example.com', name: 'Example Merchant', }, contents: { lineItems: [ { id: 'LINE_ITEM_ID', name: 'Pizza', description: 'A four cheese pizza.', priceAttributes: [ { type: 'REGULAR', name: 'Item Price', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 8990000, }, taxIncluded: true, }, { type: 'TOTAL', name: 'Total Price', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 9990000, }, taxIncluded: true, }, ], notes: [ 'Extra cheese.', ], purchase: { quantity: 1, unitMeasure: { measure: 1, unit: 'POUND', }, itemOptions: [ { id: 'ITEM_OPTION_ID', name: 'Pepperoni', prices: [ { type: 'REGULAR', state: 'ACTUAL', name: 'Item Price', amount: { currencyCode: 'USD', amountInMicros: 1000000, }, taxIncluded: true, }, { type: 'TOTAL', name: 'Total Price', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 1000000, }, taxIncluded: true, }, ], note: 'Extra pepperoni', quantity: 1, subOptions: [], }, ], }, }, ], }, buyerInfo: { email: 'janedoe@gmail.com', firstName: 'Jane', lastName: 'Doe', displayName: 'Jane Doe', }, priceAttributes: [ { type: 'SUBTOTAL', name: 'Subtotal', state: 'ESTIMATE', amount: { currencyCode: 'USD', amountInMicros: 9990000, }, taxIncluded: true, }, { type: 'DELIVERY', name: 'Delivery', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 2000000, }, taxIncluded: true, }, { type: 'TAX', name: 'Tax', state: 'ESTIMATE', amount: { currencyCode: 'USD', amountInMicros: 3780000, }, taxIncluded: true, }, { type: 'TOTAL', name: 'Total Price', state: 'ESTIMATE', amount: { currencyCode: 'USD', amountInMicros: 15770000, }, taxIncluded: true, }, ], followUpActions: [ { type: 'VIEW_DETAILS', title: 'View details', openUrlAction: { url: 'http://example.com', }, }, { type: 'CALL', title: 'Call us', openUrlAction: { url: 'tel:+16501112222', }, }, { type: 'EMAIL', title: 'Email us', openUrlAction: { url: 'mailto:person@example.com', }, }, ], termsOfServiceUrl: 'http://www.example.com', note: 'Sale event', promotions: [ { coupon: 'COUPON_CODE', }, ], purchase: { status: 'CREATED', userVisibleStatusLabel: 'CREATED', type: 'FOOD', returnsInfo: { isReturnable: false, daysToReturn: 1, policyUrl: 'http://www.example.com', }, fulfillmentInfo: { id: 'FULFILLMENT_SERVICE_ID', fulfillmentType: 'DELIVERY', expectedFulfillmentTime: { timeIso8601: '2019-09-25T18:00:00.877Z', }, location: location, price: { type: 'REGULAR', name: 'Delivery Price', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 2000000, }, taxIncluded: true, }, fulfillmentContact: { email: 'johnjohnson@gmail.com', firstName: 'John', lastName: 'Johnson', displayName: 'John Johnson', }, }, purchaseLocationType: 'ONLINE_PURCHASE', }, };
Node.js
const order = { createTime: '2019-09-24T18:00:00.877Z', lastUpdateTime: '2019-09-24T18:00:00.877Z', merchantOrderId: orderId, // A unique ID String for the order userVisibleOrderId: orderId, transactionMerchant: { id: 'http://www.example.com', name: 'Example Merchant', }, contents: { lineItems: [ { id: 'LINE_ITEM_ID', name: 'Pizza', description: 'A four cheese pizza.', priceAttributes: [ { type: 'REGULAR', name: 'Item Price', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 8990000, }, taxIncluded: true, }, { type: 'TOTAL', name: 'Total Price', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 9990000, }, taxIncluded: true, }, ], notes: [ 'Extra cheese.', ], purchase: { quantity: 1, unitMeasure: { measure: 1, unit: 'POUND', }, itemOptions: [ { id: 'ITEM_OPTION_ID', name: 'Pepperoni', prices: [ { type: 'REGULAR', state: 'ACTUAL', name: 'Item Price', amount: { currencyCode: 'USD', amountInMicros: 1000000, }, taxIncluded: true, }, { type: 'TOTAL', name: 'Total Price', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 1000000, }, taxIncluded: true, }, ], note: 'Extra pepperoni', quantity: 1, subOptions: [], }, ], }, }, ], }, buyerInfo: { email: 'janedoe@gmail.com', firstName: 'Jane', lastName: 'Doe', displayName: 'Jane Doe', }, priceAttributes: [ { type: 'SUBTOTAL', name: 'Subtotal', state: 'ESTIMATE', amount: { currencyCode: 'USD', amountInMicros: 9990000, }, taxIncluded: true, }, { type: 'DELIVERY', name: 'Delivery', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 2000000, }, taxIncluded: true, }, { type: 'TAX', name: 'Tax', state: 'ESTIMATE', amount: { currencyCode: 'USD', amountInMicros: 3780000, }, taxIncluded: true, }, { type: 'TOTAL', name: 'Total Price', state: 'ESTIMATE', amount: { currencyCode: 'USD', amountInMicros: 15770000, }, taxIncluded: true, }, ], followUpActions: [ { type: 'VIEW_DETAILS', title: 'View details', openUrlAction: { url: 'http://example.com', }, }, { type: 'CALL', title: 'Call us', openUrlAction: { url: 'tel:+16501112222', }, }, { type: 'EMAIL', title: 'Email us', openUrlAction: { url: 'mailto:person@example.com', }, }, ], termsOfServiceUrl: 'http://www.example.com', note: 'Sale event', promotions: [ { coupon: 'COUPON_CODE', }, ], purchase: { status: 'CREATED', userVisibleStatusLabel: 'CREATED', type: 'FOOD', returnsInfo: { isReturnable: false, daysToReturn: 1, policyUrl: 'http://www.example.com', }, fulfillmentInfo: { id: 'FULFILLMENT_SERVICE_ID', fulfillmentType: 'DELIVERY', expectedFulfillmentTime: { timeIso8601: '2019-09-25T18:00:00.877Z', }, location: location, price: { type: 'REGULAR', name: 'Delivery Price', state: 'ACTUAL', amount: { currencyCode: 'USD', amountInMicros: 2000000, }, taxIncluded: true, }, fulfillmentContact: { email: 'johnjohnson@gmail.com', firstName: 'John', lastName: 'Johnson', displayName: 'John Johnson', }, }, purchaseLocationType: 'ONLINE_PURCHASE', }, };
Java
// Transaction Merchant MerchantV3 transactionMerchant = new MerchantV3() .setId("http://www.example.com") .setName("Example Merchant"); // Line Item PriceAttribute itemPrice = new PriceAttribute() .setType("REGULAR") .setName("Item Price") .setState("ACTUAL") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(8990000L) ) .setTaxIncluded(true); PriceAttribute totalItemPrice = new PriceAttribute() .setType("TOTAL") .setName("Total Price") .setState("ACTUAL") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(9990000L) ) .setTaxIncluded(true); // Purchase Item Extension PurchaseItemExtension purchaseItemExtension = new PurchaseItemExtension() .setQuantity(1) .setUnitMeasure(new MerchantUnitMeasure() .setMeasure(1.0) .setUnit("POUND")) .setItemOptions(Arrays.asList(new PurchaseItemExtensionItemOption() .setId("ITEM_OPTION_ID") .setName("Pepperoni") .setPrices(Arrays.asList( new PriceAttribute() .setType("REGULAR") .setState("ACTUAL") .setName("Item Price") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(1000000L)) .setTaxIncluded(true), new PriceAttribute() .setType("TOTAL") .setState("ACTUAL") .setName("Total Price") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(1000000L)) .setTaxIncluded(true) )) .setNote("Extra pepperoni") .setQuantity(1))); LineItemV3 lineItem = new LineItemV3() .setId("LINE_ITEM_ID") .setName("Pizza") .setDescription("A four cheese pizza.") .setPriceAttributes(Arrays.asList(itemPrice, totalItemPrice)) .setNotes(Collections.singletonList("Extra cheese.")) .setPurchase(purchaseItemExtension); // Order Contents OrderContents contents = new OrderContents() .setLineItems(Collections.singletonList(lineItem)); // User Info UserInfo buyerInfo = new UserInfo() .setEmail("janedoe@gmail.com") .setFirstName("Jane") .setLastName("Doe") .setDisplayName("Jane Doe"); // Price Attributes PriceAttribute subTotal = new PriceAttribute() .setType("SUBTOTAL") .setName("Subtotal") .setState("ESTIMATE") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(9990000L) ) .setTaxIncluded(true); PriceAttribute deliveryFee = new PriceAttribute() .setType("DELIVERY") .setName("Delivery") .setState("ACTUAL") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(2000000L) ) .setTaxIncluded(true); PriceAttribute tax = new PriceAttribute() .setType("TAX") .setName("Tax") .setState("ESTIMATE") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(3780000L) ) .setTaxIncluded(true); PriceAttribute totalPrice = new PriceAttribute() .setType("TOTAL") .setName("Total Price") .setState("ESTIMATE") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(15770000L) ) .setTaxIncluded(true); // Follow up actions Action viewDetails = new Action() .setType("VIEW_DETAILS") .setTitle("View details") .setOpenUrlAction(new OpenUrlAction() .setUrl("https://example.com")); Action call = new Action() .setType("CALL") .setTitle("Call us") .setOpenUrlAction(new OpenUrlAction() .setUrl("tel:+16501112222")); Action email = new Action() .setType("EMAIL") .setTitle("Email us") .setOpenUrlAction(new OpenUrlAction() .setUrl("mailto:person@example.com")); // Terms of service and order note String termsOfServiceUrl = "http://example.com"; String orderNote = "Sale event"; // Promotions PromotionV3 promotion = new PromotionV3() .setCoupon("COUPON_CODE"); // Purchase Order Extension Location location = GSON_BUILDER.create().fromJson( (String) conversationData.get("location"), Location.class); PurchaseOrderExtension purchaseOrderExtension = new PurchaseOrderExtension() .setStatus("CREATED") .setUserVisibleStatusLabel("CREATED") .setType("FOOD") .setReturnsInfo(new PurchaseReturnsInfo() .setIsReturnable(false) .setDaysToReturn(1) .setPolicyUrl("https://example.com")) .setFulfillmentInfo(new PurchaseFulfillmentInfo() .setId("FULFILLMENT_SERVICE_ID") .setFulfillmentType("DELIVERY") .setExpectedFulfillmentTime(new TimeV3() .setTimeIso8601("2019-09-25T18:00:00.877Z")) .setLocation(location) .setPrice(new PriceAttribute() .setType("REGULAR") .setName("Delivery price") .setState("ACTUAL") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(2000000L)) .setTaxIncluded(true)) .setFulfillmentContact(new UserInfo() .setEmail("johnjohnson@gmail.com") .setFirstName("John") .setLastName("Johnson") .setDisplayName("John Johnson"))) .setPurchaseLocationType("ONLINE_PURCHASE"); OrderV3 order = new OrderV3() .setCreateTime("2019-09-24T18:00:00.877Z") .setLastUpdateTime("2019-09-24T18:00:00.877Z") .setMerchantOrderId(orderId) .setUserVisibleOrderId(orderId) .setTransactionMerchant(transactionMerchant) .setContents(contents) .setBuyerInfo(buyerInfo) .setPriceAttributes(Arrays.asList( subTotal, deliveryFee, tax, totalPrice )) .setFollowUpActions(Arrays.asList( viewDetails, call, email )) .setTermsOfServiceUrl(termsOfServiceUrl) .setNote(orderNote) .setPromotions(Collections.singletonList(promotion)) .setPurchase(purchaseOrderExtension);
Java
// Transaction Merchant MerchantV3 transactionMerchant = new MerchantV3() .setId("http://www.example.com") .setName("Example Merchant"); // Line Item PriceAttribute itemPrice = new PriceAttribute() .setType("REGULAR") .setName("Item Price") .setState("ACTUAL") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(8990000L) ) .setTaxIncluded(true); PriceAttribute totalItemPrice = new PriceAttribute() .setType("TOTAL") .setName("Total Price") .setState("ACTUAL") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(9990000L) ) .setTaxIncluded(true); // Purchase Item Extension PurchaseItemExtension purchaseItemExtension = new PurchaseItemExtension() .setUnitMeasure(new MerchantUnitMeasure() .setMeasure(1.0) .setUnit("POUND")) .setItemOptions(Arrays.asList(new PurchaseItemExtensionItemOption() .setId("ITEM_OPTION_ID") .setName("Pepperoni") .setPrices(Arrays.asList( new PriceAttribute() .setType("REGULAR") .setState("ACTUAL") .setName("Item Price") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(1000000L)) .setTaxIncluded(true), new PriceAttribute() .setType("TOTAL") .setState("ACTUAL") .setName("Total Price") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(1000000L)) .setTaxIncluded(true) )) .setNote("Extra pepperoni"))); LineItemV3 lineItem = new LineItemV3() .setId("LINE_ITEM_ID") .setName("Pizza") .setDescription("A four cheese pizza.") .setPriceAttributes(Arrays.asList(itemPrice, totalItemPrice)) .setNotes(Collections.singletonList("Extra cheese.")) .setPurchase(purchaseItemExtension); // Order Contents OrderContents contents = new OrderContents() .setLineItems(Collections.singletonList(lineItem)); // User Info UserInfo buyerInfo = new UserInfo() .setEmail("janedoe@gmail.com") .setFirstName("Jane") .setLastName("Doe") .setDisplayName("Jane Doe"); // Price Attributes PriceAttribute subTotal = new PriceAttribute() .setType("SUBTOTAL") .setName("Subtotal") .setState("ESTIMATE") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(9990000L) ) .setTaxIncluded(true); PriceAttribute deliveryFee = new PriceAttribute() .setType("DELIVERY") .setName("Delivery") .setState("ACTUAL") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(2000000L) ) .setTaxIncluded(true); PriceAttribute tax = new PriceAttribute() .setType("TAX") .setName("Tax") .setState("ESTIMATE") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(3780000L) ) .setTaxIncluded(true); PriceAttribute totalPrice = new PriceAttribute() .setType("TOTAL") .setName("Total Price") .setState("ESTIMATE") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(15770000L) ) .setTaxIncluded(true); // Follow up actions Action viewDetails = new Action() .setType("VIEW_DETAILS") .setTitle("View details") .setOpenUrlAction(new OpenUrlAction() .setUrl("https://example.com")); Action call = new Action() .setType("CALL") .setTitle("Call us") .setOpenUrlAction(new OpenUrlAction() .setUrl("tel:+16501112222")); Action email = new Action() .setType("EMAIL") .setTitle("Email us") .setOpenUrlAction(new OpenUrlAction() .setUrl("mailto:person@example.com")); // Terms of service and order note String termsOfServiceUrl = "http://example.com"; String orderNote = "Sale event"; // Promotions PromotionV3 promotion = new PromotionV3() .setCoupon("COUPON_CODE"); // Purchase Order Extension Location location = GSON_BUILDER.create().fromJson( (String) conversationData.get("location"), Location.class); PurchaseOrderExtension purchaseOrderExtension = new PurchaseOrderExtension() .setStatus("CREATED") .setUserVisibleStatusLabel("CREATED") .setType("FOOD") .setReturnsInfo(new PurchaseReturnsInfo() .setIsReturnable(false) .setDaysToReturn(1) .setPolicyUrl("https://example.com")) .setFulfillmentInfo(new PurchaseFulfillmentInfo() .setId("FULFILLMENT_SERVICE_ID") .setFulfillmentType("DELIVERY") .setExpectedFulfillmentTime(new TimeV3() .setTimeIso8601("2019-09-25T18:00:00.877Z")) .setLocation(location) .setPrice(new PriceAttribute() .setType("REGULAR") .setName("Delivery price") .setState("ACTUAL") .setAmount(new MoneyV3() .setCurrencyCode("USD") .setAmountInMicros(2000000L)) .setTaxIncluded(true)) .setFulfillmentContact(new UserInfo() .setEmail("johnjohnson@gmail.com") .setFirstName("John") .setLastName("Johnson") .setDisplayName("John Johnson"))) .setPurchaseLocationType("ONLINE_PURCHASE"); OrderV3 order = new OrderV3() .setCreateTime("2019-09-24T18:00:00.877Z") .setLastUpdateTime("2019-09-24T18:00:00.877Z") .setMerchantOrderId(orderId) .setUserVisibleOrderId(orderId) .setTransactionMerchant(transactionMerchant) .setContents(contents) .setBuyerInfo(buyerInfo) .setPriceAttributes(Arrays.asList( subTotal, deliveryFee, tax, totalPrice )) .setFollowUpActions(Arrays.asList( viewDetails, call, email )) .setTermsOfServiceUrl(termsOfServiceUrl) .setNote(orderNote) .setPromotions(Collections.singletonList(promotion)) .setPurchase(purchaseOrderExtension);
3. 提出訂單
建立訂單後,您必須向使用者顯示訂單,以確認或拒絕。要求 actions.intent.TRANSACTION_DECISION
意圖,並提供您建立的訂單。
使用者體驗
當您要求 actions.intent.TRANSACTION_DECISION
意圖時,Google 助理會啟動內建體驗,而您傳遞的 Order
會直接轉譯至「購物車預覽資訊卡」。使用者可以說出「下單」、拒絕交易、變更信用卡或地址等付款方式,或是要求變更訂單內容。
使用者在這個時候也可以要求變更訂單。在這種情況下,您應確保完成購物車組件體驗後,您的執行要求可以處理訂單變更要求。
出貨
要求 actions.intent.TRANSACTION_DECISION
意圖時,您會建立包含 Order
、orderOptions
和 paymentParameters
的 TransactionDecision
。
paymentParameters
物件會包含根據您打算使用的 Google Pay 處理方 (例如 Stripe、Braintree、ACI 等),而變更的權杖化參數。
以下程式碼顯示訂單的 TransactionsDecision
範例:
Node.js
conv.ask(new TransactionDecision({ orderOptions: { userInfoOptions: { userInfoProperties: [ 'EMAIL', ], }, }, paymentParameters: { googlePaymentOption: { // facilitationSpec is expected to be a serialized JSON string facilitationSpec: JSON.stringify({ apiVersion: 2, apiVersionMinor: 0, merchantInfo: { merchantName: 'Example Merchant', }, allowedPaymentMethods: [ { type: 'CARD', parameters: { allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'], allowedCardNetworks: [ 'AMEX', 'DISCOVER', 'JCB', 'MASTERCARD', 'VISA'], }, tokenizationSpecification: { type: 'PAYMENT_GATEWAY', parameters: { gateway: 'example', gatewayMerchantId: 'exampleGatewayMerchantId', }, }, }, ], transactionInfo: { totalPriceStatus: 'FINAL', totalPrice: '15.77', currencyCode: 'USD', }, }), }, }, presentationOptions: { actionDisplayName: 'PLACE_ORDER', }, order: order, }));
Node.js
conv.ask(new TransactionDecision({ orderOptions: { userInfoOptions: { userInfoProperties: [ 'EMAIL', ], }, }, paymentParameters: { googlePaymentOption: { // facilitationSpec is expected to be a serialized JSON string facilitationSpec: JSON.stringify({ apiVersion: 2, apiVersionMinor: 0, merchantInfo: { merchantName: 'Example Merchant', }, allowedPaymentMethods: [ { type: 'CARD', parameters: { allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'], allowedCardNetworks: [ 'AMEX', 'DISCOVER', 'JCB', 'MASTERCARD', 'VISA'], }, tokenizationSpecification: { type: 'PAYMENT_GATEWAY', parameters: { gateway: 'example', gatewayMerchantId: 'exampleGatewayMerchantId', }, }, }, ], transactionInfo: { totalPriceStatus: 'FINAL', totalPrice: '15.77', currencyCode: 'USD', }, }), }, }, presentationOptions: { actionDisplayName: 'PLACE_ORDER', }, order: order, }));
Java
// Create order options OrderOptionsV3 orderOptions = new OrderOptionsV3() .setUserInfoOptions(new UserInfoOptions() .setUserInfoProperties(Collections.singletonList("EMAIL"))); // Create presentation options PresentationOptionsV3 presentationOptions = new PresentationOptionsV3() .setActionDisplayName("PLACE_ORDER"); // Create payment parameters JSONObject merchantInfo = new JSONObject(); merchantInfo.put("merchantName", "Example Merchant"); JSONObject facilitationSpec = new JSONObject(); facilitationSpec.put("apiVersion", 2); facilitationSpec.put("apiVersionMinor", 0); facilitationSpec.put("merchantInfo", merchantInfo); JSONObject allowedPaymentMethod = new JSONObject(); allowedPaymentMethod.put("type", "CARD"); JSONArray allowedAuthMethods = new JSONArray(); allowedAuthMethods.addAll(Arrays.asList("PAN_ONLY", "CRYPTOGRAM_3DS")); JSONArray allowedCardNetworks = new JSONArray(); allowedCardNetworks.addAll(Arrays.asList("AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA")); JSONObject allowedPaymentMethodParameters = new JSONObject(); allowedPaymentMethodParameters.put("allowedAuthMethods", allowedAuthMethods); allowedPaymentMethodParameters.put("allowedCardNetworks", allowedCardNetworks); allowedPaymentMethod.put("parameters", allowedPaymentMethodParameters); JSONObject tokenizationSpecificationParameters = new JSONObject(); tokenizationSpecificationParameters.put("gateway", "example"); tokenizationSpecificationParameters.put("gatewayMerchantId", "exampleGatewayMerchantId"); JSONObject tokenizationSpecification = new JSONObject(); tokenizationSpecification.put("type", "PAYMENT_GATEWAY"); tokenizationSpecification.put("parameters", tokenizationSpecificationParameters); allowedPaymentMethod.put("tokenizationSpecification", tokenizationSpecification); JSONArray allowedPaymentMethods = new JSONArray(); allowedPaymentMethods.add(allowedPaymentMethod); facilitationSpec.put("allowedPaymentMethods", allowedPaymentMethods); JSONObject transactionInfo = new JSONObject(); transactionInfo.put("totalPriceStatus", "FINAL"); transactionInfo.put("totalPrice", "15.77"); transactionInfo.put("currencyCode", "USD"); facilitationSpec.put("transactionInfo", transactionInfo); GooglePaymentOption googlePaymentOption = new GooglePaymentOption() .setFacilitationSpec(facilitationSpec.toJSONString()); paymentParameters.setGooglePaymentOption(googlePaymentOption); return getResponseBuilder(request) .add(new TransactionDecision() .setOrder(order) .setOrderOptions(orderOptions) .setPresentationOptions(presentationOptions) .setPaymentParameters(paymentParameters) ) .build();
Java
// Create order options OrderOptionsV3 orderOptions = new OrderOptionsV3() .setUserInfoOptions(new UserInfoOptions() .setUserInfoProperties(Collections.singletonList("EMAIL"))); // Create presentation options PresentationOptionsV3 presentationOptions = new PresentationOptionsV3() .setActionDisplayName("PLACE_ORDER"); // Create payment parameters JSONObject merchantInfo = new JSONObject(); merchantInfo.put("merchantName", "Example Merchant"); JSONObject facilitationSpec = new JSONObject(); facilitationSpec.put("apiVersion", 2); facilitationSpec.put("apiVersionMinor", 0); facilitationSpec.put("merchantInfo", merchantInfo); JSONObject allowedPaymentMethod = new JSONObject(); allowedPaymentMethod.put("type", "CARD"); JSONArray allowedAuthMethods = new JSONArray(); allowedAuthMethods.addAll(Arrays.asList("PAN_ONLY", "CRYPTOGRAM_3DS")); JSONArray allowedCardNetworks = new JSONArray(); allowedCardNetworks.addAll(Arrays.asList("AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA")); JSONObject allowedPaymentMethodParameters = new JSONObject(); allowedPaymentMethodParameters.put("allowedAuthMethods", allowedAuthMethods); allowedPaymentMethodParameters.put("allowedCardNetworks", allowedCardNetworks); allowedPaymentMethod.put("parameters", allowedPaymentMethodParameters); JSONObject tokenizationSpecificationParameters = new JSONObject(); tokenizationSpecificationParameters.put("gateway", "example"); tokenizationSpecificationParameters.put("gatewayMerchantId", "exampleGatewayMerchantId"); JSONObject tokenizationSpecification = new JSONObject(); tokenizationSpecification.put("type", "PAYMENT_GATEWAY"); tokenizationSpecification.put("parameters", tokenizationSpecificationParameters); allowedPaymentMethod.put("tokenizationSpecification", tokenizationSpecification); JSONArray allowedPaymentMethods = new JSONArray(); allowedPaymentMethods.add(allowedPaymentMethod); facilitationSpec.put("allowedPaymentMethods", allowedPaymentMethods); JSONObject transactionInfo = new JSONObject(); transactionInfo.put("totalPriceStatus", "FINAL"); transactionInfo.put("totalPrice", "15.77"); transactionInfo.put("currencyCode", "USD"); facilitationSpec.put("transactionInfo", transactionInfo); GooglePaymentOption googlePaymentOption = new GooglePaymentOption() .setFacilitationSpec(facilitationSpec.toJSONString()); paymentParameters.setGooglePaymentOption(googlePaymentOption); return getResponseBuilder(request) .add(new TransactionDecision() .setOrder(order) .setOrderOptions(orderOptions) .setPresentationOptions(presentationOptions) .setPaymentParameters(paymentParameters) ) .build();
JSON
{ "payload": { "google": { "expectUserResponse": true, "richResponse": { "items": [ { "simpleResponse": { "textToSpeech": "Transaction Decision Placeholder." } } ] }, "systemIntent": { "intent": "actions.intent.TRANSACTION_DECISION", "data": { "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionDecisionValueSpec", "orderOptions": { "userInfoOptions": { "userInfoProperties": [ "EMAIL" ] } }, "paymentParameters": { "merchantPaymentOption": { "defaultMerchantPaymentMethodId": "12345678", "managePaymentMethodUrl": "https://example.com/managePayment", "merchantPaymentMethod": [ { "paymentMethodDisplayInfo": { "paymentMethodDisplayName": "VISA **** 1234", "paymentType": "PAYMENT_CARD" }, "paymentMethodGroup": "Payment method group", "paymentMethodId": "12345678", "paymentMethodStatus": { "status": "STATUS_OK", "statusMessage": "Status message" } } ] } }, "presentationOptions": { "actionDisplayName": "PLACE_ORDER" }, "order": { "createTime": "2019-09-24T18:00:00.877Z", "lastUpdateTime": "2019-09-24T18:00:00.877Z", "merchantOrderId": "ORDER_ID", "userVisibleOrderId": "ORDER_ID", "transactionMerchant": { "id": "http://www.example.com", "name": "Example Merchant" }, "contents": { "lineItems": [ { "id": "LINE_ITEM_ID", "name": "Pizza", "description": "A four cheese pizza.", "priceAttributes": [ { "type": "REGULAR", "name": "Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 8990000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 9990000 }, "taxIncluded": true } ], "notes": [ "Extra cheese." ], "purchase": { "quantity": 1, "unitMeasure": { "measure": 1, "unit": "POUND" }, "itemOptions": [ { "id": "ITEM_OPTION_ID", "name": "Pepperoni", "prices": [ { "type": "REGULAR", "state": "ACTUAL", "name": "Item Price", "amount": { "currencyCode": "USD", "amountInMicros": 1000000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 1000000 }, "taxIncluded": true } ], "note": "Extra pepperoni", "quantity": 1, "subOptions": [] } ] } } ] }, "buyerInfo": { "email": "janedoe@gmail.com", "firstName": "Jane", "lastName": "Doe", "displayName": "Jane Doe" }, "priceAttributes": [ { "type": "SUBTOTAL", "name": "Subtotal", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 9990000 }, "taxIncluded": true }, { "type": "DELIVERY", "name": "Delivery", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 2000000 }, "taxIncluded": true }, { "type": "TAX", "name": "Tax", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 3780000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 15770000 }, "taxIncluded": true } ], "followUpActions": [ { "type": "VIEW_DETAILS", "title": "View details", "openUrlAction": { "url": "http://example.com" } }, { "type": "CALL", "title": "Call us", "openUrlAction": { "url": "tel:+16501112222" } }, { "type": "EMAIL", "title": "Email us", "openUrlAction": { "url": "mailto:person@example.com" } } ], "termsOfServiceUrl": "http://www.example.com", "note": "Sale event", "promotions": [ { "coupon": "COUPON_CODE" } ], "purchase": { "status": "CREATED", "userVisibleStatusLabel": "CREATED", "type": "FOOD", "returnsInfo": { "isReturnable": false, "daysToReturn": 1, "policyUrl": "http://www.example.com" }, "fulfillmentInfo": { "id": "FULFILLMENT_SERVICE_ID", "fulfillmentType": "DELIVERY", "expectedFulfillmentTime": { "timeIso8601": "2019-09-25T18:00:00.877Z" }, "location": { "city": "MOUNTAIN VIEW", "coordinates": { "latitude": 37.432524, "longitude": -122.098545 }, "phoneNumber": "+1 123-456-7890", "postalAddress": { "addressLines": [ "1600 AMPHITHEATRE PKWY" ], "administrativeArea": "CA", "locality": "MOUNTAIN VIEW", "postalCode": "94043-1351", "recipients": [ "John Doe" ], "regionCode": "US" }, "zipCode": "94043-1351" }, "price": { "type": "REGULAR", "name": "Delivery Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 2000000 }, "taxIncluded": true }, "fulfillmentContact": { "email": "johnjohnson@gmail.com", "firstName": "John", "lastName": "Johnson", "displayName": "John Johnson" } }, "purchaseLocationType": "ONLINE_PURCHASE" } } } } } }, "outputContexts": [ { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/_actions_on_google", "lifespanCount": 99, "parameters": { "data": "{\"location\":{\"coordinates\":{\"latitude\":37.432524,\"longitude\":-122.098545},\"zipCode\":\"94043-1351\",\"city\":\"MOUNTAIN VIEW\",\"postalAddress\":{\"regionCode\":\"US\",\"postalCode\":\"94043-1351\",\"administrativeArea\":\"CA\",\"locality\":\"MOUNTAIN VIEW\",\"addressLines\":[\"1600 AMPHITHEATRE PKWY\"],\"recipients\":[\"John Doe\"]},\"phoneNumber\":\"+1 123-456-7890\"},\"latestOrderId\":\"ORDER_ID\"}" } } ] }
JSON
{ "expectUserResponse": true, "expectedInputs": [ { "possibleIntents": [ { "intent": "actions.intent.TRANSACTION_DECISION", "inputValueData": { "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionDecisionValueSpec", "orderOptions": { "userInfoOptions": { "userInfoProperties": [ "EMAIL" ] } }, "paymentParameters": { "merchantPaymentOption": { "defaultMerchantPaymentMethodId": "12345678", "managePaymentMethodUrl": "https://example.com/managePayment", "merchantPaymentMethod": [ { "paymentMethodDisplayInfo": { "paymentMethodDisplayName": "VISA **** 1234", "paymentType": "PAYMENT_CARD" }, "paymentMethodGroup": "Payment method group", "paymentMethodId": "12345678", "paymentMethodStatus": { "status": "STATUS_OK", "statusMessage": "Status message" } } ] } }, "presentationOptions": { "actionDisplayName": "PLACE_ORDER" }, "order": { "createTime": "2019-09-24T18:00:00.877Z", "lastUpdateTime": "2019-09-24T18:00:00.877Z", "merchantOrderId": "ORDER_ID", "userVisibleOrderId": "ORDER_ID", "transactionMerchant": { "id": "http://www.example.com", "name": "Example Merchant" }, "contents": { "lineItems": [ { "id": "LINE_ITEM_ID", "name": "Pizza", "description": "A four cheese pizza.", "priceAttributes": [ { "type": "REGULAR", "name": "Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 8990000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 9990000 }, "taxIncluded": true } ], "notes": [ "Extra cheese." ], "purchase": { "quantity": 1, "unitMeasure": { "measure": 1, "unit": "POUND" }, "itemOptions": [ { "id": "ITEM_OPTION_ID", "name": "Pepperoni", "prices": [ { "type": "REGULAR", "state": "ACTUAL", "name": "Item Price", "amount": { "currencyCode": "USD", "amountInMicros": 1000000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 1000000 }, "taxIncluded": true } ], "note": "Extra pepperoni", "quantity": 1, "subOptions": [] } ] } } ] }, "buyerInfo": { "email": "janedoe@gmail.com", "firstName": "Jane", "lastName": "Doe", "displayName": "Jane Doe" }, "priceAttributes": [ { "type": "SUBTOTAL", "name": "Subtotal", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 9990000 }, "taxIncluded": true }, { "type": "DELIVERY", "name": "Delivery", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 2000000 }, "taxIncluded": true }, { "type": "TAX", "name": "Tax", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 3780000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 15770000 }, "taxIncluded": true } ], "followUpActions": [ { "type": "VIEW_DETAILS", "title": "View details", "openUrlAction": { "url": "http://example.com" } }, { "type": "CALL", "title": "Call us", "openUrlAction": { "url": "tel:+16501112222" } }, { "type": "EMAIL", "title": "Email us", "openUrlAction": { "url": "mailto:person@example.com" } } ], "termsOfServiceUrl": "http://www.example.com", "note": "Sale event", "promotions": [ { "coupon": "COUPON_CODE" } ], "purchase": { "status": "CREATED", "userVisibleStatusLabel": "CREATED", "type": "FOOD", "returnsInfo": { "isReturnable": false, "daysToReturn": 1, "policyUrl": "http://www.example.com" }, "fulfillmentInfo": { "id": "FULFILLMENT_SERVICE_ID", "fulfillmentType": "DELIVERY", "expectedFulfillmentTime": { "timeIso8601": "2019-09-25T18:00:00.877Z" }, "location": { "coordinates": { "latitude": 37.421578499999995, "longitude": -122.0837816 }, "zipCode": "94043-1351", "city": "MOUNTAIN VIEW", "postalAddress": { "regionCode": "US", "postalCode": "94043-1351", "administrativeArea": "CA", "locality": "MOUNTAIN VIEW", "addressLines": [ "1600 AMPHITHEATRE PKWY" ], "recipients": [ "John Doe" ] }, "phoneNumber": "+1 123-456-7890" }, "price": { "type": "REGULAR", "name": "Delivery Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 2000000 }, "taxIncluded": true }, "fulfillmentContact": { "email": "johnjohnson@gmail.com", "firstName": "John", "lastName": "Johnson", "displayName": "John Johnson" } }, "purchaseLocationType": "ONLINE_PURCHASE" } } } } ], "inputPrompt": { "richInitialPrompt": { "items": [ { "simpleResponse": { "textToSpeech": "Transaction Decision Placeholder." } } ] } } } ], "conversationToken": "{\"data\":{\"paymentType\":\"merchant_payment\",\"location\":{\"coordinates\":{\"latitude\":37.421578499999995,\"longitude\":-122.0837816},\"zipCode\":\"94043-1351\",\"city\":\"MOUNTAIN VIEW\",\"postalAddress\":{\"regionCode\":\"US\",\"postalCode\":\"94043-1351\",\"administrativeArea\":\"CA\",\"locality\":\"MOUNTAIN VIEW\",\"addressLines\":[\"1600 AMPHITHEATRE PKWY\"],\"recipients\":[\"John Doe\"]},\"phoneNumber\":\"+1 123-456-7890\"},\"latestOrderId\":\"ORDER_ID\"}}" }
每個付款閘道的 tokenizationSpecification
物件內容都不同。下表列出每個閘道使用的參數:
"parameters": { "gateway": "example", "gatewayMerchantId": "exampleGatewayMerchantId" }
"parameters": { "gateway": "aciworldwide", "gatewayMerchantId": "YOUR_ENTITY_ID" }
"parameters": { "gateway": "adyen", "gatewayMerchantId": "YOUR_MERCHANT_ACCOUNT_NAME" }
"parameters": { "gateway": "alfabank", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "bluemedia", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "bluesnap", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "braintree", "braintree:apiVersion": "v1", "braintree:sdkVersion": braintree.client.VERSION, "braintree:merchantId": "YOUR_BRAINTREE_MERCHANT_ID", "braintree:clientKey": "YOUR_BRAINTREE_TOKENIZATION_KEY" }
"parameters": { "gateway": "chase", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ACCOUNT_NUMBER" }
"parameters": { "gateway": "checkoutltd", "gatewayMerchantId": "YOUR_PUBLIC_KEY" }
"parameters": { "gateway": "cloudpayments", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "cybersource", "gatewayMerchantId": "YOUR_MERCHANT_ID" }
"parameters": { "gateway": "datatrans", "gatewayMerchantId": "YOUR_MERCHANT_ID" }
"parameters": { "gateway": "ebanx", "gatewayMerchantId": "YOUR_PUBLIC_INTEGRATION_KEY" }
"parameters": { "gateway": "firstdata", "gatewayMerchantId": "YOUR_MERCHANT_ID" }
"parameters": { "gateway": "globalpayments", "gatewayMerchantId": "YOUR_MERCHANT_ID" }
"parameters": { "gateway": "gopay", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "hitrustpay", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "imsolutions", "gatewayMerchantId": "YOUR_MERCHANT_ID" }
"parameters": { "gateway": "lyra", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "mpgs", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "moneymailru", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "newebpay", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "nexi", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "creditcall", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "paysafe", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "payture", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "payu", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "przelewy24", "gatewayMerchantId": "YOUR_MERCHANT_ID" }
"parameters": { "gateway": "rbkmoney", "gatewayMerchantId": "YOUR_MERCHANT_ID" }
"parameters": { "gateway": "sberbank", "gatewayMerchantId": "YOUR_ORGANIZATION_NAME" }
"parameters": { "gateway": "square", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "stripe", "stripe:version": "2018-10-31", "stripe:publishableKey": "YOUR_PUBLIC_STRIPE_KEY" }
"parameters": { "gateway": "tappay", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "tinkoff", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "uniteller", "gatewayMerchantId": "YOUR_GATEWAY_MERCHANT_ID" }
"parameters": { "gateway": "vantiv", "vantiv:merchantPayPageId": "YOUR_PAY_PAGE_ID", "vantiv:merchantOrderId": "YOUR_ORDER_ID", "vantiv:merchantTransactionId": "YOUR_TRANSACTION_ID", "vantiv:merchantReportGroup": "*web" }
"parameters": { "gateway": "worldpay", "gatewayMerchantId": "YOUR_WORLDPAY_MERCHANT_ID" }
"parameters": { "gateway": "yandexcheckout", "gatewayMerchantId": "YOUR_SHOP_ID" }
處理使用者的決定
Google 助理完成意圖後,會傳送含有 actions_intent_TRANSACTION_DECISION
意圖的要求以及使用者的交易決策答案。您會收到含有 TransactionDecisionValue 的 Argument
。這個值會包含以下內容:
transactionDecision
:使用者對建議訂單的決定。可能的值為ORDER_ACCEPTED
、ORDER_REJECTED
、DELIVERY_ADDRESS_UPDATED
、CART_CHANGE_REQUESTED
和USER_CANNOT_TRANSACT
。deliveryAddress
- 更新後的寄送地址,以在使用者變更寄送地址時觸發。在這種情況下,transactionDecision
的值為DELIVERY_ADDRESS_UPDATED
。
為了正確處理這項要求,請宣告由 actions_intent_TRANSACTION_DECISION
事件觸發的 Dialogflow 意圖。觸發後,請在執行要求中處理:
Node.js
const arg = conv.arguments.get('TRANSACTION_DECISION_VALUE'); if (arg && arg.transactionDecision === 'ORDER_ACCEPTED') { console.log('Order accepted.'); const order = arg.order; }
Node.js
const arg = conv.arguments.get('TRANSACTION_DECISION_VALUE'); if (arg && arg.transactionDecision === 'ORDER_ACCEPTED') { console.log('Order accepted.'); const order = arg.order; }
Java
// Check transaction decision value Argument transactionDecisionValue = request .getArgument("TRANSACTION_DECISION_VALUE"); Map<String, Object> extension = null; if (transactionDecisionValue != null) { extension = transactionDecisionValue.getExtension(); } String transactionDecision = null; if (extension != null) { transactionDecision = (String) extension.get("transactionDecision"); } ResponseBuilder responseBuilder = getResponseBuilder(request); if ((transactionDecision != null && transactionDecision.equals("ORDER_ACCEPTED"))) { OrderV3 order = ((OrderV3) extension.get("order")); }
Java
// Check transaction decision value Argument transactionDecisionValue = request .getArgument("TRANSACTION_DECISION_VALUE"); Map<String, Object> extension = null; if (transactionDecisionValue != null) { extension = transactionDecisionValue.getExtension(); } String transactionDecision = null; if (extension != null) { transactionDecision = (String) extension.get("transactionDecision"); } ResponseBuilder responseBuilder = getResponseBuilder(request); if ((transactionDecision != null && transactionDecision.equals("ORDER_ACCEPTED"))) { OrderV3 order = ((OrderV3) extension.get("order")); }
JSON
{ "responseId": "aba44717-4236-4602-af55-e5ae1fc2d97a-594de0a7", "queryResult": { "queryText": "actions_intent_TRANSACTION_DECISION", "action": "transaction.decision.complete", "parameters": {}, "allRequiredParamsPresent": true, "fulfillmentText": "Failed to get transaction decision", "fulfillmentMessages": [ { "text": { "text": [ "Failed to get transaction decision" ] } } ], "outputContexts": [ { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_media_response_audio" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_audio_output" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_account_linking" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_screen_output" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_capability_web_browser" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/google_assistant_input_type_voice" }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/_actions_on_google", "lifespanCount": 97, "parameters": { "data": "{\"location\":{\"coordinates\":{\"latitude\":37.432524,\"longitude\":-122.098545},\"zipCode\":\"94043-1351\",\"city\":\"MOUNTAIN VIEW\",\"postalAddress\":{\"regionCode\":\"US\",\"postalCode\":\"94043-1351\",\"administrativeArea\":\"CA\",\"locality\":\"MOUNTAIN VIEW\",\"addressLines\":[\"1600 AMPHITHEATRE PKWY\"],\"recipients\":[\"John Doe\"]},\"phoneNumber\":\"+1 123-456-7890\"}}" } }, { "name": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy/contexts/actions_intent_transaction_decision", "parameters": { "TRANSACTION_DECISION_VALUE": { "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionDecisionValue", "transactionDecision": "ORDER_ACCEPTED", "order": { "createTime": "2019-09-24T18:00:00.877Z", "lastUpdateTime": "2019-09-24T18:00:00.877Z", "merchantOrderId": "ORDER_ID", "userVisibleOrderId": "ORDER_ID", "transactionMerchant": { "id": "http://www.example.com", "name": "Example Merchant" }, "contents": { "lineItems": [ { "id": "LINE_ITEM_ID", "name": "Pizza", "description": "A four cheese pizza.", "priceAttributes": [ { "type": "REGULAR", "name": "Line Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 8990000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 9990000 }, "taxIncluded": true } ], "notes": [ "Extra cheese." ], "purchase": { "quantity": 1, "unitMeasure": { "measure": 1, "unit": "POUND" }, "itemOptions": [ { "id": "ITEM_OPTION_ID", "name": "Pepperoni", "prices": [ { "type": "REGULAR", "state": "ACTUAL", "name": "Item Price", "amount": { "currencyCode": "USD", "amountInMicros": 1000000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 1000000 }, "taxIncluded": true } ], "note": "Extra pepperoni", "quantity": 1, "subOptions": [] } ] } } ] }, "buyerInfo": { "email": "janedoe@gmail.com", "firstName": "Jane", "lastName": "Doe", "displayName": "Jane Doe" }, "priceAttributes": [ { "type": "SUBTOTAL", "name": "Subtotal", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 9990000 }, "taxIncluded": true }, { "type": "DELIVERY", "name": "Delivery", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 2000000 }, "taxIncluded": true }, { "type": "TAX", "name": "Tax", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 3780000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 15770000 }, "taxIncluded": true } ], "followUpActions": [ { "type": "VIEW_DETAILS", "title": "View details", "openUrlAction": { "url": "http://example.com" } }, { "type": "CALL", "title": "Call us", "openUrlAction": { "url": "tel:+16501112222" } }, { "type": "EMAIL", "title": "Email us", "openUrlAction": { "url": "mailto:person@example.com" } } ], "termsOfServiceUrl": "www.example.com", "note": "Sale event", "promotions": [ { "coupon": "COUPON_CODE" } ], "purchase": { "status": "CREATED", "userVisibleStatusLabel": "CREATED", "type": "FOOD", "returnsInfo": { "isReturnable": false, "daysToReturn": 1, "policyUrl": "http://www.example.com" }, "fulfillmentInfo": { "id": "FULFILLMENT_SERVICE_ID", "fulfillmentType": "DELIVERY", "expectedFulfillmentTime": { "timeIso8601": "2019-09-25T18:00:00.877Z" }, "location": {}, "price": { "type": "REGULAR", "name": "Delivery Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 2000000 }, "taxIncluded": true }, "fulfillmentContact": { "email": "johnjohnson@gmail.com", "firstName": "John", "lastName": "Johnson", "displayName": "John Johnson" } }, "purchaseLocationType": "ONLINE_PURCHASE" } } }, "text": "" } } ], "intent": { "name": "projects/df-transactions/agent/intents/fd16d86b-60db-4d19-a683-5b52a22f4795", "displayName": "Transaction Decision Complete" }, "intentDetectionConfidence": 1, "languageCode": "en" }, "originalDetectIntentRequest": { "source": "google", "version": "2", "payload": { "user": { "locale": "en-US", "lastSeen": "2019-09-23T19:49:32Z", "userVerificationStatus": "VERIFIED" }, "conversation": { "conversationId": "ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy", "type": "ACTIVE", "conversationToken": "[\"merchant_payment\"]" }, "inputs": [ { "intent": "actions.intent.TRANSACTION_DECISION", "rawInputs": [ { "inputType": "KEYBOARD" } ], "arguments": [ { "name": "TRANSACTION_DECISION_VALUE", "extension": { "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionDecisionValue", "transactionDecision": "ORDER_ACCEPTED", "order": { "createTime": "2019-09-24T18:00:00.877Z", "lastUpdateTime": "2019-09-24T19:00:00.877Z", "merchantOrderId": "ORDER_ID", "userVisibleOrderId": "ORDER_ID", "transactionMerchant": { "id": "http://www.example.com", "name": "Example Merchant" }, "contents": { "lineItems": [ { "id": "LINE_ITEM_ID", "name": "Pizza", "description": "A four cheese pizza.", "priceAttributes": [ { "type": "REGULAR", "name": "Line Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 8990000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 9990000 }, "taxIncluded": true } ], "notes": [ "Extra cheese." ], "purchase": { "quantity": 1, "unitMeasure": { "measure": 1, "unit": "POUND" }, "itemOptions": [ { "id": "ITEM_OPTION_ID", "name": "Pepperoni", "prices": [ { "type": "REGULAR", "state": "ACTUAL", "name": "Item Price", "amount": { "currencyCode": "USD", "amountInMicros": 1000000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 1000000 }, "taxIncluded": true } ], "note": "Extra pepperoni", "quantity": 1, "subOptions": [] } ] } } ] }, "buyerInfo": { "email": "janedoe@gmail.com", "firstName": "Jane", "lastName": "Doe", "displayName": "Jane Doe" }, "priceAttributes": [ { "type": "SUBTOTAL", "name": "Subtotal", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 9990000 }, "taxIncluded": true }, { "type": "DELIVERY", "name": "Delivery", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 2000000 }, "taxIncluded": true }, { "type": "TAX", "name": "Tax", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 3780000 }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": 15770000 }, "taxIncluded": true } ], "followUpActions": [ { "type": "VIEW_DETAILS", "title": "View details", "openUrlAction": { "url": "http://example.com" } }, { "type": "CALL", "title": "Call us", "openUrlAction": { "url": "tel:+16501112222" } }, { "type": "EMAIL", "title": "Email us", "openUrlAction": { "url": "mailto:person@example.com" } } ], "termsOfServiceUrl": "www.example.com", "note": "Sale event", "promotions": [ { "coupon": "COUPON_CODE" } ], "purchase": { "status": "CREATED", "userVisibleStatusLabel": "CREATED", "type": "FOOD", "returnsInfo": { "isReturnable": false, "daysToReturn": 1, "policyUrl": "http://www.example.com" }, "fulfillmentInfo": { "id": "FULFILLMENT_SERVICE_ID", "fulfillmentType": "DELIVERY", "expectedFulfillmentTime": { "timeIso8601": "2019-09-25T18:00:00.877Z" }, "location": {}, "price": { "type": "REGULAR", "name": "Delivery Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": 2000000 }, "taxIncluded": true }, "fulfillmentContact": { "email": "johnjohnson@gmail.com", "firstName": "John", "lastName": "Johnson", "displayName": "John Johnson" } }, "purchaseLocationType": "ONLINE_PURCHASE" } } } }, { "name": "text" } ] } ], "surface": { "capabilities": [ { "name": "actions.capability.MEDIA_RESPONSE_AUDIO" }, { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.ACCOUNT_LINKING" }, { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.WEB_BROWSER" } ] }, "availableSurfaces": [ { "capabilities": [ { "name": "actions.capability.WEB_BROWSER" }, { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.SCREEN_OUTPUT" } ] } ] } }, "session": "projects/df-transactions/agent/sessions/ABwppHGYEP2Fj7tJBxoaKMevL6lZ2rs063lOEWhSW5etZWVOoJe7Dzm_bLejRTYIYXL3D78ER7YvA5aN9Wpy" }
JSON
{ "user": { "locale": "en-US", "lastSeen": "2019-11-11T23:57:31Z", "userVerificationStatus": "VERIFIED" }, "conversation": { "conversationId": "ABwppHGFwStZlYaQ9YT8rg9t_idVsxZrku1pUDrEbGSJmSUMatVdPwPEEQSCe1IwIBoN4sS4Weyn9pmgetEgbsWgw3JSvQmw", "type": "ACTIVE", "conversationToken": "{\"data\":{\"paymentType\":\"merchant_payment\",\"location\":{\"coordinates\":{\"latitude\":37.421578499999995,\"longitude\":-122.0837816},\"zipCode\":\"94043-1351\",\"city\":\"MOUNTAIN VIEW\",\"postalAddress\":{\"regionCode\":\"US\",\"postalCode\":\"94043-1351\",\"administrativeArea\":\"CA\",\"locality\":\"MOUNTAIN VIEW\",\"addressLines\":[\"1600 AMPHITHEATRE PKWY\"],\"recipients\":[\"John Doe\"]},\"phoneNumber\":\"+1 123-456-7890\"},\"latestOrderId\":\"ORDER_ID\"}}" }, "inputs": [ { "intent": "actions.intent.TRANSACTION_DECISION", "rawInputs": [ {} ], "arguments": [ { "name": "TRANSACTION_DECISION_VALUE", "extension": { "@type": "type.googleapis.com/google.actions.transactions.v3.TransactionDecisionValue", "transactionDecision": "ORDER_ACCEPTED", "order": { "googleOrderId": "05528125187071048269", "merchantOrderId": "ORDER_ID", "userVisibleOrderId": "ORDER_ID", "buyerInfo": { "email": "janedoe@example.com", "firstName": "Jane", "lastName": "Doe", "displayName": "Jane Doe" }, "createTime": "2019-09-24T18:00:00.877Z", "lastUpdateTime": "2019-09-24T18:00:00.877Z", "transactionMerchant": { "id": "http://www.example.com", "name": "Example Merchant" }, "contents": { "lineItems": [ { "id": "LINE_ITEM_ID", "name": "Pizza", "priceAttributes": [ { "type": "REGULAR", "name": "Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "8990000" }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "9990000" }, "taxIncluded": true } ], "description": "A four cheese pizza.", "notes": [ "Extra cheese." ], "purchase": { "quantity": 1, "itemOptions": [ { "id": "ITEM_OPTION_ID", "name": "Pepperoni", "prices": [ { "type": "REGULAR", "name": "Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "1000000" }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "1000000" }, "taxIncluded": true } ], "note": "Extra pepperoni", "quantity": 1 } ], "unitMeasure": { "measure": 1, "unit": "POUND" } }, "vertical": { "@type": "type.googleapis.com/google.actions.orders.v3.verticals.purchase.PurchaseItemExtension", "quantity": 1, "itemOptions": [ { "id": "ITEM_OPTION_ID", "name": "Pepperoni", "prices": [ { "type": "REGULAR", "name": "Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "1000000" }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "1000000" }, "taxIncluded": true } ], "note": "Extra pepperoni", "quantity": 1 } ], "unitMeasure": { "measure": 1, "unit": "POUND" } } } ] }, "priceAttributes": [ { "type": "SUBTOTAL", "name": "Subtotal", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": "9990000" }, "taxIncluded": true }, { "type": "DELIVERY", "name": "Delivery", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "2000000" }, "taxIncluded": true }, { "type": "TAX", "name": "Tax", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": "3780000" }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": "15770000" }, "taxIncluded": true } ], "followUpActions": [ { "type": "VIEW_DETAILS", "title": "View details", "openUrlAction": { "url": "http://example.com" } }, { "type": "CALL", "title": "Call us", "openUrlAction": { "url": "tel:+16501112222" } }, { "type": "EMAIL", "title": "Email us", "openUrlAction": { "url": "mailto:person@example.com" } } ], "termsOfServiceUrl": "http://www.example.com", "note": "Sale event", "paymentData": { "paymentResult": { "merchantPaymentMethodId": "12345678" }, "paymentInfo": { "paymentMethodDisplayInfo": { "paymentType": "PAYMENT_CARD", "paymentMethodDisplayName": "VISA **** 1234" }, "paymentMethodProvenance": "PAYMENT_METHOD_PROVENANCE_MERCHANT" } }, "promotions": [ { "coupon": "COUPON_CODE" } ], "purchase": { "status": "CREATED", "type": "FOOD", "returnsInfo": { "daysToReturn": 1, "policyUrl": "http://www.example.com" }, "fulfillmentInfo": { "id": "FULFILLMENT_SERVICE_ID", "fulfillmentType": "DELIVERY", "expectedFulfillmentTime": { "timeIso8601": "2019-09-25T18:00:00.877Z" }, "location": { "coordinates": { "latitude": 37.421578499999995, "longitude": -122.0837816 }, "zipCode": "94043-1351", "city": "MOUNTAIN VIEW", "postalAddress": { "regionCode": "US", "postalCode": "94043-1351", "administrativeArea": "CA", "locality": "MOUNTAIN VIEW", "addressLines": [ "1600 AMPHITHEATRE PKWY" ], "recipients": [ "John Doe" ] }, "phoneNumber": "+1 123-456-7890" }, "price": { "type": "REGULAR", "name": "Delivery Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "2000000" }, "taxIncluded": true }, "fulfillmentContact": { "email": "johnjohnson@gmail.com", "firstName": "John", "lastName": "Johnson", "displayName": "John Johnson" } }, "purchaseLocationType": "ONLINE_PURCHASE", "userVisibleStatusLabel": "CREATED" }, "vertical": { "@type": "type.googleapis.com/google.actions.orders.v3.verticals.purchase.PurchaseOrderExtension", "status": "CREATED", "type": "FOOD", "returnsInfo": { "daysToReturn": 1, "policyUrl": "http://www.example.com" }, "fulfillmentInfo": { "id": "FULFILLMENT_SERVICE_ID", "fulfillmentType": "DELIVERY", "expectedFulfillmentTime": { "timeIso8601": "2019-09-25T18:00:00.877Z" }, "location": { "coordinates": { "latitude": 37.421578499999995, "longitude": -122.0837816 }, "zipCode": "94043-1351", "city": "MOUNTAIN VIEW", "postalAddress": { "regionCode": "US", "postalCode": "94043-1351", "administrativeArea": "CA", "locality": "MOUNTAIN VIEW", "addressLines": [ "1600 AMPHITHEATRE PKWY" ], "recipients": [ "John Doe" ] }, "phoneNumber": "+1 123-456-7890" }, "price": { "type": "REGULAR", "name": "Delivery Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "2000000" }, "taxIncluded": true }, "fulfillmentContact": { "email": "johnjohnson@gmail.com", "firstName": "John", "lastName": "Johnson", "displayName": "John Johnson" } }, "purchaseLocationType": "ONLINE_PURCHASE", "userVisibleStatusLabel": "CREATED" } } } }, { "name": "text" } ] } ], "surface": { "capabilities": [ { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.MEDIA_RESPONSE_AUDIO" }, { "name": "actions.capability.ACCOUNT_LINKING" }, { "name": "actions.capability.SCREEN_OUTPUT" } ] }, "availableSurfaces": [ { "capabilities": [ { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.WEB_BROWSER" }, { "name": "actions.capability.AUDIO_OUTPUT" } ] } ] }
4. 完成訂單並傳送收據
當 actions.intent.TRANSACTION_DECISION
意圖傳回 ORDER_ACCEPTED
的 transactionDecision
時,您必須立即執行必要的任何處理來「確認」訂單 (例如將訂單保存在自己的資料庫中,並向使用者收費)。
您可以透過這個回應結束對話,但您必須加入簡易回應,才能讓對話持續進行。當您提供此初始 orderUpdate
時,使用者會看到「收合的收據卡」以及您的其餘回應。這張卡片會反映使用者在「訂單記錄」中找到的收據。
在訂單確認期間,訂單物件可包含 userVisibleOrderId
,這是使用者看到的訂單 ID。您可以將 merchantOrderId
重複用於這個欄位。
OrderUpdate 物件的部分需要包含後續追蹤動作物件,該物件會在訂單詳細資料底部以網址按鈕的形式列出,供使用者在 Google 助理訂單記錄中找到。
出貨
Node.js
// Set lastUpdateTime and update status of order const order = arg.order; order.lastUpdateTime = '2019-09-24T19:00:00.877Z'; order.purchase.status = 'CONFIRMED'; order.purchase.userVisibleStatusLabel = 'Order confirmed'; // Send synchronous order update conv.ask(`Transaction completed! Your order` + ` ${conv.data.latestOrderId} is all set!`); conv.ask(new Suggestions('send order update')); conv.ask(new OrderUpdate({ type: 'SNAPSHOT', reason: 'Reason string', order: order, }));
Node.js
// Set lastUpdateTime and update status of order const order = arg.order; order.lastUpdateTime = '2019-09-24T19:00:00.877Z'; order.purchase.status = 'CONFIRMED'; order.purchase.userVisibleStatusLabel = 'Order confirmed'; // Send synchronous order update conv.ask(`Transaction completed! Your order ` + `${conv.data.latestOrderId} is all set!`); conv.ask(new Suggestions('send order update')); conv.ask(new OrderUpdate({ type: 'SNAPSHOT', reason: 'Reason string', order: order, }));
Java
OrderV3 order = ((OrderV3) extension.get("order")); order.setLastUpdateTime("2019-09-24T19:00:00.877Z"); // Update order status PurchaseOrderExtension purchaseOrderExtension = order.getPurchase(); purchaseOrderExtension.setStatus("CONFIRMED"); purchaseOrderExtension.setUserVisibleStatusLabel("Order confirmed"); order.setPurchase(purchaseOrderExtension); // Order update OrderUpdateV3 orderUpdate = new OrderUpdateV3() .setType("SNAPSHOT") .setReason("Reason string") .setOrder(order); Map<String, Object> conversationData = request.getConversationData(); String orderId = (String) conversationData.get("latestOrderId"); responseBuilder .add("Transaction completed! Your order " + orderId + " is all set!") .addSuggestions(new String[] {"send order update"}) .add(new StructuredResponse().setOrderUpdateV3(orderUpdate));
Java
OrderV3 order = ((OrderV3) extension.get("order")); order.setLastUpdateTime("2019-09-24T19:00:00.877Z"); // Update order status PurchaseOrderExtension purchaseOrderExtension = order.getPurchase(); purchaseOrderExtension.setStatus("CONFIRMED"); purchaseOrderExtension.setUserVisibleStatusLabel("Order confirmed"); order.setPurchase(purchaseOrderExtension); // Order update OrderUpdateV3 orderUpdate = new OrderUpdateV3() .setType("SNAPSHOT") .setReason("Reason string") .setOrder(order); Map<String, Object> conversationData = request.getConversationData(); String orderId = (String) conversationData.get("latestOrderId"); responseBuilder .add("Transaction completed! Your order " + orderId + " is all set!") .addSuggestions(new String[] {"send order update"}) .add(new StructuredResponse().setOrderUpdateV3(orderUpdate));
JSON
{ "payload": { "google": { "expectUserResponse": true, "richResponse": { "items": [ { "simpleResponse": { "textToSpeech": "Transaction completed! Your order undefined is all set!" } }, { "structuredResponse": { "orderUpdateV3": { "order": { "buyerInfo": { "displayName": "Jane Doe", "email": "janedoe@gmail.com", "firstName": "Jane", "lastName": "Doe" }, "contents": { "lineItems": [ { "description": "A four cheese pizza.", "id": "LINE_ITEM_ID", "name": "Pizza", "notes": [ "Extra cheese." ], "priceAttributes": [ { "amount": { "amountInMicros": 8990000, "currencyCode": "USD" }, "name": "Line Item Price", "state": "ACTUAL", "taxIncluded": true, "type": "REGULAR" }, { "amount": { "amountInMicros": 9990000, "currencyCode": "USD" }, "name": "Total Price", "state": "ACTUAL", "taxIncluded": true, "type": "TOTAL" } ], "purchase": { "itemOptions": [ { "id": "ITEM_OPTION_ID", "name": "Pepperoni", "note": "Extra pepperoni", "prices": [ { "amount": { "amountInMicros": 1000000, "currencyCode": "USD" }, "name": "Item Price", "state": "ACTUAL", "taxIncluded": true, "type": "REGULAR" }, { "amount": { "amountInMicros": 1000000, "currencyCode": "USD" }, "name": "Total Price", "state": "ACTUAL", "taxIncluded": true, "type": "TOTAL" } ], "quantity": 1, "subOptions": [] } ], "quantity": 1, "unitMeasure": { "measure": 1, "unit": "POUND" } } } ] }, "createTime": "2019-09-24T18:00:00.877Z", "followUpActions": [ { "openUrlAction": { "url": "http://example.com" }, "title": "View details", "type": "VIEW_DETAILS" }, { "openUrlAction": { "url": "tel:+16501112222" }, "title": "Call us", "type": "CALL" }, { "openUrlAction": { "url": "mailto:person@example.com" }, "title": "Email us", "type": "EMAIL" } ], "lastUpdateTime": "2019-09-24T19:00:00.877Z", "merchantOrderId": "ORDER_ID", "note": "Sale event", "priceAttributes": [ { "amount": { "amountInMicros": 9990000, "currencyCode": "USD" }, "name": "Subtotal", "state": "ESTIMATE", "taxIncluded": true, "type": "SUBTOTAL" }, { "amount": { "amountInMicros": 2000000, "currencyCode": "USD" }, "name": "Delivery", "state": "ACTUAL", "taxIncluded": true, "type": "DELIVERY" }, { "amount": { "amountInMicros": 3780000, "currencyCode": "USD" }, "name": "Tax", "state": "ESTIMATE", "taxIncluded": true, "type": "TAX" }, { "amount": { "amountInMicros": 15770000, "currencyCode": "USD" }, "name": "Total Price", "state": "ESTIMATE", "taxIncluded": true, "type": "TOTAL" } ], "promotions": [ { "coupon": "COUPON_CODE" } ], "purchase": { "fulfillmentInfo": { "expectedFulfillmentTime": { "timeIso8601": "2019-09-25T18:00:00.877Z" }, "fulfillmentContact": { "displayName": "John Johnson", "email": "johnjohnson@gmail.com", "firstName": "John", "lastName": "Johnson" }, "fulfillmentType": "DELIVERY", "id": "FULFILLMENT_SERVICE_ID", "location": {}, "price": { "amount": { "amountInMicros": 2000000, "currencyCode": "USD" }, "name": "Delivery Price", "state": "ACTUAL", "taxIncluded": true, "type": "REGULAR" } }, "purchaseLocationType": "ONLINE_PURCHASE", "returnsInfo": { "daysToReturn": 1, "isReturnable": false, "policyUrl": "http://www.example.com" }, "status": "CONFIRMED", "type": "FOOD", "userVisibleStatusLabel": "Order confirmed" }, "termsOfServiceUrl": "www.example.com", "transactionMerchant": { "id": "http://www.example.com", "name": "Example Merchant" }, "userVisibleOrderId": "ORDER_ID" }, "reason": "Reason string", "type": "SNAPSHOT" } } } ], "suggestions": [ { "title": "send order update" } ] } } } }
JSON
{ "expectUserResponse": true, "expectedInputs": [ { "possibleIntents": [ { "intent": "actions.intent.TEXT" } ], "inputPrompt": { "richInitialPrompt": { "items": [ { "simpleResponse": { "textToSpeech": "Transaction completed! Your order ORDER_ID is all set!" } }, { "structuredResponse": { "orderUpdateV3": { "type": "SNAPSHOT", "reason": "Reason string", "order": { "googleOrderId": "05528125187071048269", "merchantOrderId": "ORDER_ID", "userVisibleOrderId": "ORDER_ID", "buyerInfo": { "email": "janedoe@example.com", "firstName": "Jane", "lastName": "Doe", "displayName": "Jane Doe" }, "createTime": "2019-09-24T18:00:00.877Z", "lastUpdateTime": "2019-09-24T19:00:00.877Z", "transactionMerchant": { "id": "http://www.example.com", "name": "Example Merchant" }, "contents": { "lineItems": [ { "id": "LINE_ITEM_ID", "name": "Pizza", "priceAttributes": [ { "type": "REGULAR", "name": "Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "8990000" }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "9990000" }, "taxIncluded": true } ], "description": "A four cheese pizza.", "notes": [ "Extra cheese." ], "purchase": { "quantity": 1, "itemOptions": [ { "id": "ITEM_OPTION_ID", "name": "Pepperoni", "prices": [ { "type": "REGULAR", "name": "Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "1000000" }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "1000000" }, "taxIncluded": true } ], "note": "Extra pepperoni", "quantity": 1 } ], "unitMeasure": { "measure": 1, "unit": "POUND" } }, "vertical": { "@type": "type.googleapis.com/google.actions.orders.v3.verticals.purchase.PurchaseItemExtension", "quantity": 1, "itemOptions": [ { "id": "ITEM_OPTION_ID", "name": "Pepperoni", "prices": [ { "type": "REGULAR", "name": "Item Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "1000000" }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "1000000" }, "taxIncluded": true } ], "note": "Extra pepperoni", "quantity": 1 } ], "unitMeasure": { "measure": 1, "unit": "POUND" } } } ] }, "priceAttributes": [ { "type": "SUBTOTAL", "name": "Subtotal", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": "9990000" }, "taxIncluded": true }, { "type": "DELIVERY", "name": "Delivery", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "2000000" }, "taxIncluded": true }, { "type": "TAX", "name": "Tax", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": "3780000" }, "taxIncluded": true }, { "type": "TOTAL", "name": "Total Price", "state": "ESTIMATE", "amount": { "currencyCode": "USD", "amountInMicros": "15770000" }, "taxIncluded": true } ], "followUpActions": [ { "type": "VIEW_DETAILS", "title": "View details", "openUrlAction": { "url": "http://example.com" } }, { "type": "CALL", "title": "Call us", "openUrlAction": { "url": "tel:+16501112222" } }, { "type": "EMAIL", "title": "Email us", "openUrlAction": { "url": "mailto:person@example.com" } } ], "termsOfServiceUrl": "http://www.example.com", "note": "Sale event", "paymentData": { "paymentResult": { "merchantPaymentMethodId": "12345678" }, "paymentInfo": { "paymentMethodDisplayInfo": { "paymentType": "PAYMENT_CARD", "paymentMethodDisplayName": "VISA **** 1234" }, "paymentMethodProvenance": "PAYMENT_METHOD_PROVENANCE_MERCHANT" } }, "promotions": [ { "coupon": "COUPON_CODE" } ], "purchase": { "status": "CONFIRMED", "type": "FOOD", "returnsInfo": { "daysToReturn": 1, "policyUrl": "http://www.example.com" }, "fulfillmentInfo": { "id": "FULFILLMENT_SERVICE_ID", "fulfillmentType": "DELIVERY", "expectedFulfillmentTime": { "timeIso8601": "2019-09-25T18:00:00.877Z" }, "location": { "coordinates": { "latitude": 37.421578499999995, "longitude": -122.0837816 }, "zipCode": "94043-1351", "city": "MOUNTAIN VIEW", "postalAddress": { "regionCode": "US", "postalCode": "94043-1351", "administrativeArea": "CA", "locality": "MOUNTAIN VIEW", "addressLines": [ "1600 AMPHITHEATRE PKWY" ], "recipients": [ "John Doe" ] }, "phoneNumber": "+1 123-456-7890" }, "price": { "type": "REGULAR", "name": "Delivery Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "2000000" }, "taxIncluded": true }, "fulfillmentContact": { "email": "johnjohnson@gmail.com", "firstName": "John", "lastName": "Johnson", "displayName": "John Johnson" } }, "purchaseLocationType": "ONLINE_PURCHASE", "userVisibleStatusLabel": "Order confirmed" }, "vertical": { "@type": "type.googleapis.com/google.actions.orders.v3.verticals.purchase.PurchaseOrderExtension", "status": "CREATED", "type": "FOOD", "returnsInfo": { "daysToReturn": 1, "policyUrl": "http://www.example.com" }, "fulfillmentInfo": { "id": "FULFILLMENT_SERVICE_ID", "fulfillmentType": "DELIVERY", "expectedFulfillmentTime": { "timeIso8601": "2019-09-25T18:00:00.877Z" }, "location": { "coordinates": { "latitude": 37.421578499999995, "longitude": -122.0837816 }, "zipCode": "94043-1351", "city": "MOUNTAIN VIEW", "postalAddress": { "regionCode": "US", "postalCode": "94043-1351", "administrativeArea": "CA", "locality": "MOUNTAIN VIEW", "addressLines": [ "1600 AMPHITHEATRE PKWY" ], "recipients": [ "John Doe" ] }, "phoneNumber": "+1 123-456-7890" }, "price": { "type": "REGULAR", "name": "Delivery Price", "state": "ACTUAL", "amount": { "currencyCode": "USD", "amountInMicros": "2000000" }, "taxIncluded": true }, "fulfillmentContact": { "email": "johnjohnson@gmail.com", "firstName": "John", "lastName": "Johnson", "displayName": "John Johnson" } }, "purchaseLocationType": "ONLINE_PURCHASE", "userVisibleStatusLabel": "CREATED" } } } } } ], "suggestions": [ { "title": "send order update" } ] } } } ], "conversationToken": "{\"data\":{\"paymentType\":\"merchant_payment\",\"location\":{\"coordinates\":{\"latitude\":37.421578499999995,\"longitude\":-122.0837816},\"zipCode\":\"94043-1351\",\"city\":\"MOUNTAIN VIEW\",\"postalAddress\":{\"regionCode\":\"US\",\"postalCode\":\"94043-1351\",\"administrativeArea\":\"CA\",\"locality\":\"MOUNTAIN VIEW\",\"addressLines\":[\"1600 AMPHITHEATRE PKWY\"],\"recipients\":[\"John Doe\"]},\"phoneNumber\":\"+1 123-456-7890\"},\"latestOrderId\":\"ORDER_ID\"}}" }
5. 傳送訂單最新狀態
您必須在生命週期內讓使用者瞭解訂單狀態。傳送 HTTP PATCH 要求給 Orders API,其中包含訂單狀態和詳細資料,即可傳送使用者訂單更新內容。
設定對 Orders API 的非同步要求
系統會透過存取權杖授權向 Orders API 發出訂單更新要求。如要對 Orders API 更新訂單,請下載與 Actions Console 專案相關聯的 JSON 服務帳戶金鑰,然後交換服務帳戶金鑰,取得可傳遞至 HTTP 要求 Authorization
標頭的不記名憑證。
如要擷取服務帳戶金鑰,請執行下列步驟:
- 在 Google Cloud 控制台中,依序前往「選單」圖示 ⋮ >「API 和服務」>「憑證」>「建立憑證」>「服務帳戶金鑰」。
- 在「服務帳戶」下方,選取「新增服務帳戶」。
- 將服務帳戶設為
service-account
。 - 將「Role」(角色) 設為 [Project] (專案) > [Owner] (擁有者)。
- 將金鑰類型設為「JSON」。
- 選取「Create」(建立)。
- 系統會將私人 JSON 服務帳戶金鑰下載到本機電腦。
在訂單更新程式碼中,您可以使用 Google API 用戶端程式庫和 "https://www.googleapis.com/auth/actions.order.developer" 範圍,交換服務金鑰並取得不記名憑證。如需安裝步驟和範例,請前往 API 用戶端程式庫 GitHub 頁面。
您也可以參考 Node.js 和 Java 範例的 order-update.js
,做為金鑰交換範例。
傳送訂單最新狀態
將服務帳戶金鑰交換為 OAuth 不記名權杖後,您就能以經過授權的 PATCH 要求來將訂單更新傳送至 Orders API。
Orders API 網址:
PATCH https://actions.googleapis.com/v3/orders/${orderId}
在要求中提供下列標頭:
"Authorization: Bearer token"
改成您交換服務帳戶金鑰的 OAuth 不記名憑證。"Content-Type: application/json"
.
PATCH 要求應採用下列格式的 JSON 主體:
{ "orderUpdate": OrderUpdate }
OrderUpdate
物件包含下列頂層欄位:
updateMask
- 要更新的訂單欄位。如要更新訂單狀態,請將值設為purchase.status, purchase.userVisibleStatusLabel
。order
:更新的內容。如果您要更新訂單內容,請將值設為更新後的Order
物件。如果您要更新訂單狀態 (例如從"CONFIRMED"
到"SHIPPED"
),物件包含下列欄位:merchantOrderId
:您在Order
物件中設定的 ID。lastUpdateTime
:本次更新的時間戳記。purchase
- 包含下列項目的物件:status
:訂單狀態,為PurchaseStatus
,例如「SHIPPED
」或「DELIVERED
」。userVisibleStatusLabel
- 面向使用者的標籤,提供訂單狀態詳細資料,例如「您的訂單已出貨,且正在路上」。
userNotification
物件。請注意,加入這個物件並不保證會在使用者的裝置上收到通知。
以下程式碼範例顯示 OrderUpdate
範例,可將訂單狀態更新為 DELIVERED
:
Node.js
// Import the 'googleapis' module for authorizing the request. const {google} = require('googleapis'); // Import the 'request-promise' module for sending an HTTP POST request. const request = require('request-promise'); // Import the OrderUpdate class from the Actions on Google client library. const {OrderUpdate} = require('actions-on-google'); // Import the service account key used to authorize the request. // Replacing the string path with a path to your service account key. // i.e. const serviceAccountKey = require('./service-account.json') // Create a new JWT client for the Actions API using credentials // from the service account key. let jwtClient = new google.auth.JWT( serviceAccountKey.client_email, null, serviceAccountKey.private_key, ['https://www.googleapis.com/auth/actions.order.developer'], null, ); // Authorize the client let tokens = await jwtClient.authorize(); // Declare order update const orderUpdate = new OrderUpdate({ updateMask: [ 'lastUpdateTime', 'purchase.status', 'purchase.userVisibleStatusLabel', ].join(','), order: { merchantOrderId: orderId, // Specify the ID of the order to update lastUpdateTime: new Date().toISOString(), purchase: { status: 'DELIVERED', userVisibleStatusLabel: 'Order delivered', }, }, reason: 'Order status updated to delivered.', }); // Set up the PATCH request header and body, // including the authorized token and order update. let options = { method: 'PATCH', uri: `https://actions.googleapis.com/v3/orders/${orderId}`, auth: { bearer: tokens.access_token, }, body: { header: { isInSandbox: true, }, orderUpdate, }, json: true, }; // Send the PATCH request to the Orders API. try { await request(options); conv.close(`The order has been updated.`); } catch (e) { console.log(`Error: ${e}`); conv.close(`There was an error sending an order update.`); }
Node.js
// Import the 'googleapis' module for authorizing the request. const {google} = require('googleapis'); // Import the 'request-promise' module for sending an HTTP POST request. const request = require('request-promise'); // Import the OrderUpdate class from the Actions on Google client library. const {OrderUpdate} = require('actions-on-google'); // Import the service account key used to authorize the request. // Replacing the string path with a path to your service account key. // i.e. const serviceAccountKey = require('./service-account.json') // Create a new JWT client for the Actions API using credentials // from the service account key. let jwtClient = new google.auth.JWT( serviceAccountKey.client_email, null, serviceAccountKey.private_key, ['https://www.googleapis.com/auth/actions.order.developer'], null, ); // Authorize the client let tokens = await jwtClient.authorize(); // Declare order update const orderUpdate = new OrderUpdate({ updateMask: [ 'lastUpdateTime', 'purchase.status', 'purchase.userVisibleStatusLabel', ].join(','), order: { merchantOrderId: orderId, // Specify the ID of the order to update lastUpdateTime: new Date().toISOString(), purchase: { status: 'DELIVERED', userVisibleStatusLabel: 'Order delivered', }, }, reason: 'Order status updated to delivered.', }); // Set up the PATCH request header and body, // including the authorized token and order update. let options = { method: 'PATCH', uri: `https://actions.googleapis.com/v3/orders/${orderId}`, auth: { bearer: tokens.access_token, }, body: { header: { isInSandbox: true, }, orderUpdate, }, json: true, }; // Send the PATCH request to the Orders API. try { await request(options); conv.close(`The order has been updated.`); } catch (e) { console.log(`Error: ${e}`); conv.close(`There was an error sending an order update.`); }
Java
// Setup service account credentials String serviceAccountFile = MyActionsApp.class.getClassLoader() .getResource(SERVICE_ACCOUNT_KEY_FILE_NAME) .getFile(); InputStream actionsApiServiceAccount = new FileInputStream( serviceAccountFile); ServiceAccountCredentials serviceAccountCredentials = (ServiceAccountCredentials) ServiceAccountCredentials.fromStream(actionsApiServiceAccount) .createScoped(Collections.singleton( "https://www.googleapis.com/auth/actions.order.developer")); AccessToken token = serviceAccountCredentials.refreshAccessToken(); // Setup request with headers HttpPatch patchRequest = new HttpPatch( "https://actions.googleapis.com/v3/orders/" + orderId); patchRequest.setHeader("Content-type", "application/json"); patchRequest.setHeader("Authorization", "Bearer " + token.getTokenValue()); // Create order update FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList( "lastUpdateTime", "purchase.status", "purchase.userVisibleStatusLabel")) .build(); OrderUpdateV3 orderUpdate = new OrderUpdateV3() .setOrder(new OrderV3() .setMerchantOrderId(orderId) .setLastUpdateTime(Instant.now().toString()) .setPurchase(new PurchaseOrderExtension() .setStatus("DELIVERED") .setUserVisibleStatusLabel("Order delivered."))) .setUpdateMask(FieldMaskUtil.toString(fieldMask)) .setReason("Order status was updated to delivered."); // Setup JSON body containing order update JsonParser parser = new JsonParser(); JsonObject orderUpdateJson = parser.parse(new Gson().toJson(orderUpdate)).getAsJsonObject(); JsonObject body = new JsonObject(); body.add("orderUpdate", orderUpdateJson); JsonObject header = new JsonObject(); header.addProperty("isInSandbox", true); body.add("header", header); StringEntity entity = new StringEntity(body.toString()); entity.setContentType(ContentType.APPLICATION_JSON.getMimeType()); patchRequest.setEntity(entity); // Make request HttpClient httpClient = HttpClientBuilder.create().build(); HttpResponse response = httpClient.execute(patchRequest); LOGGER.info(response.getStatusLine().getStatusCode() + " " + response .getStatusLine().getReasonPhrase()); return getResponseBuilder(request) .add("The order has been updated.") .build();
Java
// Setup service account credentials String serviceAccountFile = MyActionsApp.class.getClassLoader() .getResource(SERVICE_ACCOUNT_KEY_FILE_NAME) .getFile(); InputStream actionsApiServiceAccount = new FileInputStream( serviceAccountFile); ServiceAccountCredentials serviceAccountCredentials = (ServiceAccountCredentials) ServiceAccountCredentials.fromStream(actionsApiServiceAccount) .createScoped(Collections.singleton( "https://www.googleapis.com/auth/actions.order.developer")); AccessToken token = serviceAccountCredentials.refreshAccessToken(); // Setup request with headers HttpPatch patchRequest = new HttpPatch( "https://actions.googleapis.com/v3/orders/" + orderId); patchRequest.setHeader("Content-type", "application/json"); patchRequest.setHeader("Authorization", "Bearer " + token.getTokenValue()); // Create order update FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList( "lastUpdateTime", "purchase.status", "purchase.userVisibleStatusLabel")) .build(); OrderUpdateV3 orderUpdate = new OrderUpdateV3() .setOrder(new OrderV3() .setMerchantOrderId(orderId) .setLastUpdateTime(Instant.now().toString()) .setPurchase(new PurchaseOrderExtension() .setStatus("DELIVERED") .setUserVisibleStatusLabel("Order delivered."))) .setUpdateMask(FieldMaskUtil.toString(fieldMask)) .setReason("Order status was updated to delivered."); // Setup JSON body containing order update JsonParser parser = new JsonParser(); JsonObject orderUpdateJson = parser.parse(new Gson().toJson(orderUpdate)).getAsJsonObject(); JsonObject body = new JsonObject(); body.add("orderUpdate", orderUpdateJson); JsonObject header = new JsonObject(); header.addProperty("isInSandbox", true); body.add("header", header); StringEntity entity = new StringEntity(body.toString()); entity.setContentType(ContentType.APPLICATION_JSON.getMimeType()); patchRequest.setEntity(entity); // Make request HttpClient httpClient = HttpClientBuilder.create().build(); HttpResponse response = httpClient.execute(patchRequest); LOGGER.info(response.getStatusLine().getStatusCode() + " " + response .getStatusLine().getReasonPhrase()); return getResponseBuilder(request) .add("The order has been updated.") .build();
設定購買狀態
訂單更新的 status
必須描述訂單的目前狀態。在更新的 order.purchase.status
欄位中,使用下列其中一個值:
CREATED
- 使用者接受訂單,並且從動作的角度「建立」訂單,但需要您後端手動處理。CONFIRMED
- 訂單有效且正在處理履行要求。IN_PREPARATION
- 目前正在準備運送/配送訂單,例如正在烹調的食物或包裝的商品。READY_FOR_PICKUP
- 訂單可取貨,DELIVERED
- 訂購商品已送達接收者OUT_OF_STOCK
- 訂單中的一或多項商品缺貨中。CHANGE_REQUESTED
- 使用者要求變更訂單,系統正在處理變更。RETURNED
- 訂單商品已在送達後由使用者退回。REJECTED
- 如果您無法處理、收費或以其他方式「啟用」訂單。CANCELLED
- 使用者已取消訂單。
您應針對與交易相關的每個狀態傳送訂單更新。舉例來說,如果您的交易需要以手動處理的方式在下單後記錄訂單,請傳送 CREATED
訂單更新,直到該額外處理作業完成為止。並非所有訂單都需要所有狀態值。
疑難排解
如果您在測試期間遇到任何問題,請參閱交易的疑難排解步驟。