Reminder: By Aug 31, 2025, all new apps and updates to existing apps must use Billing Library version 7 or newer. If you need more time to update your app, you can request an extension until Nov 1, 2025. Learn about Play Billing Library version deprecation.
Google Play 提供即時開發人員通知 (RTDN),可監控購買交易生命週期事件,而適用於訂閱項目和應用程式內購的 Play Developer API,則可用來根據這些事件採取必要動作。只要使用這些工具,並建構完善的購買交易生命週期管理系統,就能提供順暢的使用者體驗,以及有效管理購買交易和授權。
建構即時開發人員通知的用戶端
透過 Google Play 結帳系統完成的購買交易,可能會在生命週期中歷經多次授權異動。可能觸發這些異動的各種動作包括:
使用者在應用程式中啟動的動作。
使用者透過 Play 商店應用程式啟動的動作。
直接透過後端系統啟動的動作。
您透過 Google Play 管理中心啟動的動作。
例如:
使用者透過 Play 商店訂閱中心取消訂閱項目。
開發人員使用 Google Play Developer API 延遲訂閱項目收費。
開發人員透過 Google Play 管理中心核發購買交易的退款並撤銷授權。
後端必須知道購買交易可能經歷的不同狀態,據此及時採取所有必要措施來調整授權狀態。
雖然可以使用 Google Play Developer API 手動檢查購買狀態,但仰賴定期檢查來追蹤異動,不僅效率不彰,還容易發生錯誤及延遲。只要使用 RTDN,您就能立即回應異動情形,無須為 Google Play 購買交易建構生命週期追蹤邏輯。
Google Play 的後端可做為 RTDN 發布端使用。如要為應用程式設定 RTDN,請按照設定指南中的操作說明進行。完成這些步驟後,Google Play 結帳系統就能做為應用程式的 RTDN 發布端使用。為完成這項設定,您應熟悉 Google Cloud Platform Console,才能調整 Pub/Sub 基本設定。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Purchase lifecycle and RTDNs\n\nWhen you sell digital products through your app, you must consider the entire\nuser experience. In-app integration lets you launch purchase flows and\nmanage the user experience, but it's crucial to keep your backend up-to-date on\nthe entitlements that users are purchasing. This is important for tracking\npurchases and managing other aspects of the user experience, such as\ncross-platform entitlements.\n\nTo monitor purchase lifecycle events and respond quickly to changes in user\nentitlements, you should build a purchase status management system in your\nbackend for both subscriptions and one-time purchases. This system ensures quick\nand secure purchase processing regardless of device status, maintains consistent\nuser entitlements across all platforms, and provides the ability to consult\npurchase history and entitlement data in your backend.\n\nGoogle Play offers [real-time developer notifications\n(RTDN)](/google/play/billing/rtdn-reference) to monitor purchase lifecycle\nevents, and the Play Developer APIs for [Subscriptions and In-App\nPurchases](https://developers.google.com/android-publisher#subscriptions) can be\nused to take necessary actions based on these events. By using these tools and\nbuilding a robust purchase lifecycle management system, you can provide a\nseamless user experience and manage purchases and entitlements efficiently.\n\nBuild a real-time developer notification client\n-----------------------------------------------\n\nPurchases made on Google Play's billing system can go through several entitlement\nchanges throughout their lifecycle. Various actions can trigger these changes,\nincluding the following:\n\n- Actions initiated by users in your app.\n- Actions initiated by users through the Play Store app.\n- Actions initiated directly from your backend systems.\n- Actions that you initiate through the Google Play Console.\n\nFor example:\n\n- A user canceling a subscription through the Play Store subscription center.\n- A developer deferring subscription billing using the Google Play Developer API.\n- A developer issuing a refund and revoking entitlement for a purchase through the Google Play Console.\n\nIt is crucial that your backend is aware of the different states a purchase can\ngo through and that it takes all necessary measures to adjust the entitlement\naccordingly in a timely manner.\n\nWhile it is possible to use the Google Play Developer API to check a purchase\nstatus manually, relying on periodic checks is a very inefficient way to track\nchanges and it's prone to errors and delays. RTDNs can help you respond to\nchanges immediately without having to build lifecycle tracking logic for your\nGoogle Play purchases.\n\nThis section discusses how to build a client for RTDNs. RTDN is a feature built\nusing Google Cloud Pub/Sub, which sends your backend an instant notification\nwhen a user's entitlement state changes. The Pub/Sub system consists of a\npublisher that sends notifications and a client that subscribes to those\nnotifications. By implementing RTDN, you can track all changes to the user's\nentitlement state in real time and respond to them promptly.\n\n### RTDN publisher\n\nGoogle Play's backend acts as the publisher for RTDNs. To set up RTDN for you\napp, follow the instructions in the [Setup](/google/play/billing/getting-ready)\nguide. These steps allow Google Play's billing system to act as the publisher for\nyour app's RTDNs. To complete this setup, you should familiarize yourself with\nthe Google Cloud Platform Console to set up a basic Pub/Sub configuration.\n\n### RTDN subscriber\n\nAfter setting up the publisher, you should prepare your backend to consume\nRTDNs. To do this, you need to build a client to receive Google Cloud Pub/Sub\nmessages. Your RTDN client's basic function consists of receiving instances of\n[`PubSubMessage`](https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage),\neither through HTTPS requests in a registered endpoint or by using the [Cloud\nPub/Sub client\nlibraries](https://cloud.google.com/pubsub/docs/reference/libraries). See the\nPub/Sub documentation to learn about using a\n[push](https://cloud.google.com/pubsub/docs/push) or a\n[pull](https://cloud.google.com/pubsub/docs/pull) strategy, or the [RTDN setup\ndocumentation](/google/play/billing/getting-ready#create-sub) for guidelines on\nchoosing the strategy that works best for your needs.\n\nFor each message you receive, your backend should do the following:\n\n- Unpack the base-64-encoded `data` field, which contains the [RTDN\n object](/google/play/billing/rtdn-reference#encoding).\n- Trigger any required backend processes related to the entitlement change notified by the RTDN event.\n\nHandle purchase state transitions\n---------------------------------\n\nOne-time purchases and subscription purchases have different lifecycles based on\nthe different states and events that can affect them. Thanks to RTDN, you don't\nneed to build logic to confirm state transitions. All you need to do is define\nwhat happens when your backend receives each type of notification.\n| **Note:** For [pre-orders](/google/play/billing/multi-offer-one-time-product#pre-order), RTDN follows the same lifecycle as the one-time purchase.\n\nSee the following guides to learn more about these scenarios:\n\n- [One-time purchase lifecycle](/google/play/billing/lifecycle/one-time)\n- [Subscription purchase lifecycle](/google/play/billing/lifecycle/subscriptions)"]]