Android Google Pay 集成(1)

==============================================================================

官方文档:https://developer.android.com/google/play/billing/billing_library_overview

Google Play支持商品内购和订阅

这里主要记录了内购的集成过程,语言:Kotlin

集成

===========================================================

1. 添加依赖


dependencies {

implementation ‘com.android.billingclient:billing:1.2’

}

2. 添加权限


需要使用Google Play 内购功能,必须添加权限,否则无法支付。

网络及其他必要权限自行添加就好了。

3. 初始化


private var billingClient: BillingClient = BillingClient.newBuilder(context).setListener { responseCode: Int, purchases: MutableList? ->

if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {

// TODO 支付完成

} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {

// Handle an error caused by a user cancelling the purchase flow.

// TODO 用户取消了支付

} else if (responseCode == BillingClient.BillingResponse.ITEM_ALREADY_OWNED) {

// Handle an error caused by a user cancelling the purchase flow.

// TODO 商品已经购买过(重复购买了此商品,如果需要支持重复购买,需要将商品购买成功后消费掉)

} else {

// Handle any other error codes.

}

}.build()

4. 连接 Google Play Service


在使用支付功能之前,首先要连接Google Play Service,确保当前状态支付是可用的,此操作对于国内用户来说是需要梯子的,但是对于国外的用户,就不用操心了,毕竟我们开发Google Play主要还是给国外用户使用。

billingClient.startConnection(object : BillingClientStateListener {

override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) {

// 连接成功

if (billingResponseCode == BillingClient.BillingResponse.OK) {

// The billing client is ready. You can query purchases here.

// 5. Query for in-app product details.

// 5. 查询商品详情

// 6. 支付商品

} else {

// TODO 连接失败

}

}

// 连接断开

override fun onBillingServiceDisconnected() {

// Try to restart the connection on the next request to

// Google Play by calling the startConnection() method.

}

})

5. 获取商品信息


商品信息需要将带有内购权限的apk上传到GooglePlayConsole后,添加内购商品,设置商品ID,待商品生效后,移动端通过商品ID来查询商品的详细信息。

val params = SkuDetailsParams.newBuilder().apply {

setSkusList(ArrayList().apply {

add(“要查询的商品ID”) // 可以单个查询也可以多个查询

}).setType(BillingClient.SkuType.INAPP)

}

billingClient.querySkuDetailsAsync(params.build()) { responseCode, skuDetailsList ->

Logger.d(“responseCode = $responseCode skuDetailsList = ${skuDetailsList?.size}”)

// responseCode 为响应码

// skuDetailsList 为查询的商品信息列表

}

6. 支付商品


skuDetails为查询到的商品信息

// 唤起GooglePay支付

val flowParams = BillingFlowParams.newBuilder()

.setSkuDetails(skuDetails)

.build()

billingClient.launchBillingFlow(activity, flowParams)

支付成功后,会在初始化的监听接口回调支付结果,包含必要的支付结果信息。

7. 消费商品(非必须)


Google的商品默认是单次消费的,即只能购买一次,如果需求是可以多次购买的,比如充值等需求,那么就需要在支付成功后将购买的商品消费掉。

billingClient.consumeAsync(purchaseToken){ responseCode: Int, purchaseToken: String ->

}

  • purchaseToken : 支付成功后返回的支付令牌

如果支付成功后没有立即消费,需要后续手动消费掉,则先要查询未消费的商品,有同步方法和异步方法:

// 查询历史购买 同步

val purchasesResult: Purchase.PurchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP)

if (purchasesResult.responseCode == BillingClient.BillingResponse.OK) {

purchasesResult.purchasesList?.forEach { purchase ->

purchase.purchaseToken?.apply {

// TODO 消费掉商品

}

}

}

// 查询历史购买 异步

billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP) { responseCode, purchasesList ->

if (responseCode == BillingClient.BillingResponse.OK) {

purchasesList?.forEach { purchase ->

purchase.purchaseToken?.apply {

// TODO 消费掉商品

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值