本指南將說明如何透過 Google Wallet API 使用回呼。建立或刪除有價值時,Google 可以對您選擇的 HTTPS 端點執行回呼。這個回呼因類別而異,包含事件相關資料,例如類別、物件和事件類型。這項功能可以用來與使用者的新增和刪除次數保持同步。舉例來說,您可以設定回呼將事件傳送至數據分析應用程式,藉此追蹤促銷活動期間的客戶參與度。
必要條件
開始之前,請詳閱下列必備條件:
- 設定處理 POST 要求的 HTTPS 端點。這個端點必須設為公開。
-
透過程式輔助方式更新每個類別的回呼端點。請參閱 REST API 中不同類別的
callbackOptions
屬性。 - 建議做法:使用 Tink 程式庫驗證簽名。
實作回呼
每當使用者對物件執行新增或刪除操作時,Google 都會向商家發出回呼,並提供各類別網址新增或刪除作業的詳細資料。商家必須先使用公開金鑰來驗證訊息的真實性。回呼驗證訊息後,回呼可用於下游作業。
驗證簽名
實作 HTTPS 端點時,建議您選用 Tink 程式庫驗證訊息簽名。Tink 程式庫提供 PaymentMethodTokenRecipient
。這項公用程式可自動驗證簽名,並在驗證成功時傳回訊息的實際內容。
以下範例說明如何使用 Tink 程式庫實作 PaymentMethodTokenRecipient
:
import java.io.IOException; import javax.servlet.http.*; import com.google.common.io.CharStreams; import com.google.crypto.tink.apps.paymentmethodtoken.*; // Replace ISSUER_ID with your issuer id private static final String RECIPIENT_ID = "ISSUER_ID"; private static final String PUBLIC_KEY_URL = "https://pay.google.com/gp/m/issuer/keys"; private static final String SENDER_ID = "GooglePayPasses"; private static final String PROTOCOL = "ECv2SigningOnly"; private static final GooglePaymentsPublicKeysManager keysManager = new GooglePaymentsPublicKeysManager.Builder() .setKeysUrl(PUBLIC_KEY_URL) .build(); public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { try { // Extract signed message with signature from POST request body. String signedMessage = CharStreams.toString(request.getReader()); PaymentMethodTokenRecipient recipient = new PaymentMethodTokenRecipient.Builder() .protocolVersion(PROTOCOL) .fetchSenderVerifyingKeysWith(keysManager) .senderId(SENDER_ID) .recipientId(RECIPIENT_ID) .build(); String serializedJsonMessage = recipient.unseal(signedMessage); // Use serializedJsonMessage to extract the details } catch (Exception e) { // Handle the error } }
Expected message format
The message format is JSON that's serialized into a string with the following properties:
Identifier | Description |
---|---|
classId |
Fully qualified class ID. Uses the following format: <issuer_id.class_id> objectId
完整物件 ID。格式如下: <issuer_id.object_id> expTimeMillis
自 EPOCH 起算的到期時間 (以毫秒為單位)。到期時間過後,訊息必須視為無效。eventType 可為 DELETE 和 SAVE 的 del 或 save 。nonce
Nonce 可追蹤任何重複的提交項目。
處理 Google 伺服器發出的要求傳送至回呼端點的要求標頭包含下列金鑰欄位清單:
請設定您的伺服器,以免伺服器拒絕要求。如要這麼做,您可以在 User-agent: Googlebot Disallow: 重試次數回呼會盡可能履行。Google 會嘗試兩次嘗試兩次暫時性的錯誤。嘗試兩次後,Google 會刪除該訊息,且不會嘗試重新傳送。 重複提交
在某些情況下,提交項目可能會重複。建議您使用 除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。 上次更新時間:2024-03-26 (世界標準時間)。
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"缺少我需要的資訊"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"過於複雜/步驟過多"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"過時"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"翻譯問題"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"示例/程式碼問題"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"其他"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"容易理解"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"確實解決了我的問題"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"其他"
}]
|