T
traeai
登录
返回首页
Google Developers Blog

Enhancing Android Checkout with Dynamic Callbacks in Google Pay

7.5Score

TL;DR · AI 摘要

Google 发布了在 Android 应用中使用动态回调增强 Google Pay 支付体验的功能,支持在支付流程中动态更新地址、运费和总价,并处理授权反馈。

核心要点

  • Android 应用现在可以使用动态回调功能来增强 Google Pay 支付体验。
  • 开发者可以在支付流程中动态更新地址、运费和总价。
  • 动态回调允许在不关闭支付界面的情况下处理授权反馈。

结构提纲

按章节快速跳转。

  1. Google 发布了在 Android 应用中使用动态回调增强 Google Pay 支付体验的功能。

  2. 动态回调使支付流程更加流畅,用户可以在支付过程中动态更新地址、运费和总价,并处理授权反馈。

  3. 开发者需要更新 Google Pay 依赖并在应用中实现特定的回调逻辑。

  4. 通过继承 `BasePaymentDataCallbacks` 类并重写相关方法来实现动态回调逻辑。

思维导图

用一张图看清主题之间的关系。

查看大纲文本(无障碍 / 无 JS 友好)
  • 动态回调增强 Google Pay 支付体验
    • 为什么使用动态回调?
      • 流畅的支付流程
      • 动态更新地址、运费和总价
      • 处理授权反馈
    • 如何开始使用动态回调?
      • 更新 Google Pay 依赖
      • 实现特定的回调逻辑
    • 实现回调逻辑
      • 继承 `BasePaymentDataCallbacks` 类
      • 重写相关方法

金句 / Highlights

值得收藏与分享的关键句。

  • 动态回调使支付流程更加流畅,用户可以在支付过程中动态更新地址、运费和总价,并处理授权反馈。

    第二段

    ⬇︎ 下载 PNG𝕏 分享到 X
  • 开发者需要更新 Google Pay 依赖并在应用中实现特定的回调逻辑。

    第三段

    ⬇︎ 下载 PNG𝕏 分享到 X
  • 通过继承 `BasePaymentDataCallbacks` 类并重写相关方法来实现动态回调逻辑。

    第四段

    ⬇︎ 下载 PNG𝕏 分享到 X
#Google Pay#Android#支付体验#动态回调
打开原文

MAY 26, 2026

We are excited to bring Express checkout with Google Pay for Android native apps enabling developers to leverage users stored credentials (payment and address) from Google Wallet to streamline their checkout journeys. You will be able to implement familiar callbacks onPaymentDataChanged and onPaymentAuthorized that are already supported on Web now in your Android applications and streamline the checkout funnels. With these callbacks, you can update shipping options, taxes, and total prices dynamically as users interact with the Google Pay sheet, and handle authorization feedback without ever closing the sheet. This is available with play-services-wallet:20.0.0 and onwards.

Why use Dynamic Callbacks?

Dynamic callbacks enable a true "Express Checkout" experience. By moving the Google Pay button upstream to your Product Detail or Cart pages, you can provide the user's shipping address, payment credentials, and contact details all within the Pay sheet.

  • Dynamic Shipping & Taxes: Update shipping methods and calculate tax on-the-fly based on the user's selected address.
  • Upstream Positioning: Place the Pay button earlier in the funnel to drive higher conversion rates.
  • Inline Authorization: Handle transaction authorization and retries directly within the Google Pay interface.

Getting Started

To get started, update your Google Pay dependency in your build.gradle file:

com.google.android.gms:play-services-wallet:20.0.0 Kotlin

Copied

1. Implement the Callback Logic

Extend BasePaymentDataCallbacks to handle the specific events triggered during the checkout process.

code
import com.google.android.gms.wallet.PaymentData
import com.google.android.gms.wallet.callback.BasePaymentDataCallbacks
import com.google.android.gms.wallet.callback.IntermediatePaymentData
import com.google.android.gms.wallet.callback.OnCompleteListener
import com.google.android.gms.wallet.callback.PaymentAuthorizationResult
import com.google.android.gms.wallet.callback.PaymentDataRequestUpdate
import org.json.JSONObject

class MerchantPaymentDataCallbacks : BasePaymentDataCallbacks() {

    /**
     * Handles payment data changes in the payment sheet such as shipping address and shipping options.
     */
    override fun onPaymentDataChanged(
        request: IntermediatePaymentData,
        onCompleteListener: OnCompleteListener<PaymentDataRequestUpdate>
    ) {

        // Example: Process the request and return updates
        // In a real application, you would likely make a network request to your server
        // to get updated shipping options and cart details based on the new address.

        val shippingAddress = request.shippingAddress
        val shippingOptionData = request.shippingOptionData

        // Construct the response object
        val responseJson = JSONObject().apply {
            // Example: Add new shipping options based on the address
            put("newShippingOptionParameters", JSONObject().apply {
                put("defaultSelectedOptionId", "shipping-001")
                put("shippingOptions", JSONObject().apply {
                    put("id", "shipping-001")
                    put("label", "$0.00: Free shipping")
                    put("description", "Free shipping on all orders")
                })
            })

            // Example: Update transaction info
            put("newTransactionInfo", JSONObject().apply {
                put("totalPriceStatus", "FINAL")
                put("totalPrice", "12.34")
                put("currencyCode", "USD")
            })
        }

        val response = PaymentDataRequestUpdate.fromJson(responseJson.toString())
        onCompleteListener.complete(response)
    }

    /**
     * Called when a payment is authorized in the payment sheet.
     */
    override fun onPaymentAuthorized(
        request: PaymentData,
        onCompleteListener: OnCompleteListener<PaymentAuthorizationResult>
    ) {
        // Log the payment data for debugging
        println("onPaymentAuthorized called with PaymentData: ${request.toJson()}")

        // Example: Process the payment authorization
        // In a real application, you would send the payment token and other data
        // to your server to be processed by your payment service provider.

        // Construct the response object
        val responseJson = JSONObject().apply {
            put("transactionState", "SUCCESS") // Or "ERROR"
            // Optionally include an error message
            // put("error", JSONObject().apply {
            //     put("reason", "PAYMENT_DATA_INVALID")
            //     put("intent", "PAYMENT_AUTHORIZATION")
            //     put("message", "Invalid payment method.")
            // })
        }

        val response = PaymentAuthorizationResult.fromJson(responseJson.toString())
        onCompleteListener.complete(response)
    }
}

Kotlin

Copied

2. Host the Callback Service

Implement a service that extends BasePaymentDataCallbacksService to provide an instance of your callbacks.

code
import androidx.annotation.NonNull
import com.google.android.gms.wallet.callback.BasePaymentDataCallbacks
import com.google.android.gms.wallet.callback.BasePaymentDataCallbacksService

/**
 * Service class which hosts the payment data callbacks.
 */
class MerchantPaymentDataCallbacksService : BasePaymentDataCallbacksService() {

    @NonNull
    override fun createPaymentDataCallbacks(): BasePaymentDataCallbacks {
        return MerchantPaymentDataCallbacks()
    }
}

Kotlin

Copied

3. Update the Android Manifest

You must declare your service and protect it with the BIND_PAYMENTS_CALLBACK_SERVICE permission.

code
<service
    android:name=".service.MerchantPaymentDataCallbacksService"
    android:permission="com.google.android.gms.permission.BIND_PAYMENTS_CALLBACK_SERVICE"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.android.gms.wallet.callback.PAYMENT_DATA_CALLBACKS" />
    </intent-filter>
</service>

XML

Copied

4. Configure the Payment Request

Finally, include the callbackIntents in your PaymentDataRequest JSON object to tell Google Pay which events you want to listen for.

code
{
  "apiVersion": 2,
  "apiVersionMinor": 0,
  ...
  "callbackIntents": [
    "PAYMENT_AUTHORIZATION",
    "SHIPPING_ADDRESS",
    "SHIPPING_OPTION"
  ]
}

JSON

Copied

**Takeaways**

Implementing dynamic callbacks on Android allows you to:

  • Reduce Friction: Enable a 1-click experience by moving checkout upstream.
  • Increase Accuracy: Provide real-time shipping and tax pricing.
  • Improve Authorization: Handle success or failure feedback within the Pay sheet to increase conversion.

Dynamic callbacks bring the Google Pay developer platform on Android to parity with its capabilities on the web. For a full implementation guide, see the updated developer documentation: goo.gle/pay-android-dpu

[](https://developers.googleblog.com/enhancing-android-checkout-with-dynamic-callbacks-in-google-pay/) Previous

Next

[](https://developers.googleblog.com/all-the-news-from-the-google-io-2026-developer-keynote/)

AI 可能会生成不准确的信息,请核实重要内容

Enhancing Android Checkout with Dynamic Callbacks in Google Pay | Google Developers Blog | traeai