借助 Google Pay,客户可以用他们在 Google 账户中存储的信用卡或借记卡在您的应用或网站上进行支付,包括 Google Play、YouTube、Chrome 或 Andriod 设备中绑定的银行卡。用 Google Pay API 请求您的客户在 Google 账户中存储的任何信用卡或借记卡。
对于实物商品和服务的销售,您的应用程序可以接受 Google Pay 或任何其他 Stripe 支持的支付方式。这些付款是通过 Stripe 处理的,只需支付 Stripe 的手续费。但是,应用内购买数字产品和内容必须使用 Google Play 计费系统。这些付款由 Google 处理,会收取交易费。
在您的 Android 应用中用 Google Pay 收款
GooglePayLauncher
是 Stripe Android SDK 的一部分,是在 Android 应用中开通 Google Pay 的最快最便捷的方式。
要支持 Android 上的 Google Pay,您需要:
minSdkVersion
:19
或更高 。compileSdkVersion
:28
或更高 。
此外,如果您希望使用自己的设备进行测试,您需要向您的 Google 账户添加支付方式。
要使用 Google Pay,先将以下内容添加到您的 AndroidManifest.xml 的 <application>
标签,来启用 Google Pay API:
<application>
...
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />
</application>
本指南假设您使用最新版的 Stripe Android SDK。
dependencies {
implementation 'com.stripe:stripe-android:20.41.0'
}
更多详情,请参见 Google Pay 针对 Android 设备编写的设置 Google Pay API 指南。
接下来,在您的 Activity
或 Fragment
中创建一个 GooglePayLauncher 的实例。该操作必须要在 Activity#onCreate()
内完成。
GooglePayLauncher.Config
会显示配置 GooglePayLauncher
的必需和可选属性。有关配置选项的更多详情,请查看 GooglePayLauncher.Config
。
class CheckoutActivity : AppCompatActivity() {
private lateinit var clientSecret: String
private lateinit var googlePayButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.checkout_activity)
PaymentConfiguration.init(this, PUBLISHABLE_KEY)
googlePayButton = findViewById<Button>(R.id.google_pay_button)
val googlePayLauncher = GooglePayLauncher(
activity = this,
config = GooglePayLauncher.Config(
environment = GooglePayEnvironment.Test,
merchantCountryCode = "US",
merchantName = "Widget Store"
),
readyCallback = ::onGooglePayReady,
resultCallback = ::onGooglePayResult
)
googlePayButton.setOnClickListener {
googlePayLauncher.presentForPaymentIntent(clientSecret)
}
}
private fun onGooglePayReady(isReady: Boolean) {
}
private fun onGooglePayResult(result: GooglePayLauncher.Result) {
}
}
实例化 GooglePayLauncher
后,通过一个标志调用 GooglePayLauncher.ReadyCallback
实例,该标志表示 Google Pay 是否可以使用。可以用这个标志来更新您的 Ui,通过它告诉客户可以使用 Google Pay。
class CheckoutActivity : AppCompatActivity() {
private lateinit var googlePayButton: Button
private fun onGooglePayReady(isReady: Boolean) {
googlePayButton.isEnabled = isReady
}
}
开通 Google Pay 并且您的应用程序获得了 PaymentIntent
或 SetupIntent
客户端私钥后,用适当的方式启动 GooglePayLauncher
。确认 PaymentIntent
时,使用 GooglePayLauncher#presentForPaymentIntent(clientSecret)
。确认 SetupIntent
时,使用 GooglePayLauncher#presentForSetupIntent(clientSecret)
。
class CheckoutActivity : AppCompatActivity() {
private lateinit var clientSecret: String
private lateinit var googlePayButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
googlePayButton.setOnClickListener {
googlePayLauncher.presentForPaymentIntent(clientSecret)
}
}
}
处理结果
最后,部署 GooglePayLauncher.ResultCallback
,以处理 GooglePayLauncher
操作的结果。
结果可能会是 GooglePayLauncher.Result.Completed
、GooglePayLauncher.Result.Canceled
或 GooglePayLauncher.Result.Failed
。
class CheckoutActivity : AppCompatActivity() {
private fun onGooglePayResult(result: GooglePayLauncher.Result) {
when (result) {
GooglePayLauncher.Result.Completed -> {
}
GooglePayLauncher.Result.Canceled -> {
}
is GooglePayLauncher.Result.Failed -> {
}
}
}
}
按照 Google 的说明,请求访问您的应用程序的生产环境。收到提示时,选择集成类型 Gateway,并提供您的应用的截图供审核。
您的应用程序得到批准后,通过将环境设置为 GooglePayEnvironment.Production
,在生产模式下测试您的集成,然后从一个签署的已发布版本启动 Google Pay。请记得使用您的真实模式下的 API 密钥。您可以用包含 capture_method
= manual
PaymentIntent
来在不捕获付款的情况下处理交易。
创建 PaymentMethod
如果您在您的服务器上确认付款,则可以用 GooglePayPaymentMethodLauncher
仅收集 PaymentMethod
,而非确认付款。
class CheckoutActivity : AppCompatActivity() {
private lateinit var googlePayButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.checkout_activity)
PaymentConfiguration.init(this, PUBLISHABLE_KEY)
googlePayButton = findViewById<Button>(R.id.google_pay_button)
val googlePayLauncher = GooglePayPaymentMethodLauncher(
activity = this,
config = GooglePayPaymentMethodLauncher.Config(
environment = GooglePayEnvironment.Test,
merchantCountryCode = "FR",
merchantName = "Widget Store"
),
readyCallback = ::onGooglePayReady,
resultCallback = ::onGooglePayResult
)
googlePayButton.setOnClickListener {
googlePayLauncher.present(
currencyCode = "EUR",
amount = 2500
)
}
}
private fun onGooglePayReady(isReady: Boolean) {
googlePayButton.isEnabled = isReady
}
private fun onGooglePayResult(
result: GooglePayPaymentMethodLauncher.Result
) {
when (result) {
is GooglePayPaymentMethodLauncher.Result.Completed -> {
val paymentMethodId = result.paymentMethod.id
}
GooglePayPaymentMethodLauncher.Result.Canceled -> {
}
is GooglePayPaymentMethodLauncher.Result.Failed -> {
}
}
}
}