Google Play 上的应用内购买结算提供了一个直接、简单的界面,让您可以使用 Google Play 发送应用内购买结算请求和管理应用内购买结算交易。
官网文档 :使用 Google Play 结算库
添加权限
<uses-permission android:name="com.android.vending.BILLING" />
添加依赖库
implementation 'com.android.billingclient:billing:2.0.1'
初始化操作
private lateinit var billingClient: BillingClient
private val skuId = Constants.skuId
/**
* 初始化
*/
private fun initBilling() {
billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
// The BillingClient is ready. You can query purchases here.
querySkuDetail()
}
}
override fun onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
})
}
//购买结果回调
override fun onPurchasesUpdated(billingResult: BillingResult, purchases: MutableList<Purchase>?) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
for (purchase in purchases) {
Log.d(TAG, "购买成功")
Log.d(TAG, "purchase:$purchase")
}
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
Log.d(TAG, "用户取消购买")
} else {
// Handle any other error codes.
Log.d(TAG, "onPurchasesUpdated :${billingResult.responseCode}")
Log.d(TAG, "onPurchasesUpdated :${billingResult.debugMessage}")
}
}
查询库存信息
private fun querySkuDetail() {
val skuList = ArrayList<String>()
skuList.add(skuId)
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP)
billingClient.querySkuDetailsAsync(params.build()) { billingResult, skuDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
for (skuDetails in skuDetailsList) {
if (skuId == skuDetails.sku) {
Log.d(TAG, "商品信息:$skuDetails")
tv_price.text = skuDetails.originalPrice
}
}
}
}
}
发起购买请求
private fun launchBilling() {
val skuList = ArrayList<String>()
skuList.add(skuId)
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP)
billingClient.querySkuDetailsAsync(params.build()) { billingResult, skuDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
for (skuDetails in skuDetailsList) {
if (skuId == skuDetails.sku) {
val flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build()
val responseCode = billingClient.launchBillingFlow(this@ChargeActivity, flowParams)
Log.d(TAG, "launchBilling:${responseCode.responseCode}")
Log.d(TAG, "launchBilling:${responseCode.debugMessage}")
}
}
}
}
}
购买结果
override fun onPurchasesUpdated(billingResult: BillingResult, purchases: MutableList<Purchase>?) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
for (purchase in purchases) {
Log.d(TAG, "购买成功")
Log.d(TAG, "purchase:$purchase")
//购买成功后直接消耗掉,方便下载直接购买。
val consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
billingClient.consumeAsync(consumeParams) { result, outToken ->
Log.d(TAG, "consumeAsync:${result.debugMessage}")
Log.d(TAG, "consumeAsync:${result.responseCode}")
Log.d(TAG, "outToken:$outToken")
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
//消耗成功后,下载音乐
StoreContentDetailDialog.getInstance(bean, true).show(supportFragmentManager, "detail")
}
}
}
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
Log.d(TAG, "用户取消购买")
} else {
// Handle any other error codes.
Log.d(TAG, "onPurchasesUpdated :${billingResult.responseCode}")
Log.d(TAG, "onPurchasesUpdated :${billingResult.debugMessage}")
}
}
消耗购买的商品
//购买成功后直接消耗掉,方便下载直接购买。
val consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
billingClient.consumeAsync(consumeParams) { result, outToken ->
Log.d(TAG, "consumeAsync:${result.debugMessage}")
Log.d(TAG, "consumeAsync:${result.responseCode}")
Log.d(TAG, "outToken:$outToken")
if (result.responseCode == BillingClient.BillingResponseCode.OK) {
//消耗成功
}
}
网友评论