安卓內(nèi)實現(xiàn)應用內(nèi)購買結(jié)算(Google Play 結(jié)算庫)

Google Play 上的應用內(nèi)購買結(jié)算提供了一個直接、簡單的界面,讓您可以使用 Google Play 發(fā)送應用內(nèi)購買結(jié)算請求和管理應用內(nèi)購買結(jié)算交易。

官網(wǎng)文檔 :使用 Google Play 結(jié)算庫

添加權(quán)限

<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.
        }
    })
}

//購買結(jié)果回調(diào)
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
                }
            }
        }
    }
}

發(fā)起購買請求

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}")
                }
            }
        }
    }
}

購買結(jié)果

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) {
        //消耗成功
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容