Android 集成google 訂閱商品

一、Google Play Console中創(chuàng)建訂閱商品

1.再自己的項(xiàng)目下,選擇 "產(chǎn)品"->"訂閱",點(diǎn)擊“建立訂閱”,如下圖所示



2.輸入產(chǎn)品id和名字,產(chǎn)品id創(chuàng)建后是唯一的,且不能修改(貌似也不能刪除)



3.新建計(jì)劃,比如公司要求vip有3個(gè)訂閱計(jì)劃(一個(gè)是按1個(gè)月收費(fèi),一個(gè)3個(gè)月收費(fèi),一個(gè)按1年收費(fèi)),這時(shí)候就需要分別創(chuàng)建三個(gè)計(jì)劃


4.創(chuàng)建優(yōu)惠,很多app訂閱商品都有優(yōu)惠,例如:新用戶(hù)訂閱可免費(fèi)使用7天,具體步驟如下圖所示




image.png

二、Android端訂閱google 官網(wǎng)結(jié)算地址

訂閱支付和一次性商品支付流程差不多,具體看文檔,這里說(shuō)下具體區(qū)別
1.查詢(xún)商品類(lèi)型修改setProductType(BillingClient.ProductType.SUBS)

suspend fun processPurchases() {
    val productList = ArrayList<String>()
    productList.add(
              QueryProductDetailsParams.Product.newBuilder()
                  .setProductId("product_id_example")
                    //一次性商品
//                  .setProductType(BillingClient.ProductType.INAPP)
                    //訂閱商品
                  .setProductType(BillingClient.ProductType.SUBS)
                  .build()
           )
    val params = QueryProductDetailsParams.newBuilder()
    params.setProductList(productList)

    // leverage queryProductDetails Kotlin extension function
    val productDetailsResult = withContext(Dispatchers.IO) {
        billingClient.queryProductDetails(params.build())
    }

    // Process the result.
}

2.支付配置

// An activity reference from which the billing flow will be launched.
Activity activity = ...;

ImmutableList productDetailsParamsList =
    ImmutableList.of(
        ProductDetailsParams.newBuilder()
             // retrieve a value for "productDetails" by calling queryProductDetailsAsync()
            .setProductDetails(productDetails)
            // to get an offer token, call ProductDetails.getSubscriptionOfferDetails()
            // for a list of offers that are available to the user
            .setOfferToken(selectedOfferToken)
            .build()
    );

BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
    .setProductDetailsParamsList(productDetailsParamsList)
    .build();

// Launch the billing flow
BillingResult billingResult = billingClient.launchBillingFlow(activity, billingFlowParams);

主要設(shè)置setOfferToken,具體代碼可以下載demo去看代碼下載地址
下面是我自己寫(xiě)的一段代碼,獲取selectedOfferToken

billingClient.queryProductDetailsAsync(queryProductDetailsParams) { billingResult, productDetailsList ->
            if (productDetailsList.isNotEmpty()) {
                val productDetails = productDetailsList[0]
                LogUtils.d("print","hashCode-->${productDetails.hashCode()}")
                //獲取計(jì)劃相關(guān)(獲取指定計(jì)劃)
                val basicOffers =  productDetails.subscriptionOfferDetails?.let { retrieveEligibleOffers(it) }
                val productDetailsParamsList: ArrayList<BillingFlowParams.ProductDetailsParams> = ArrayList()
                val offerToken = basicOffers?.let { leastPricedOfferToken(it) }.toString()

                LogUtils.d("print","offerToken-->${offerToken}")
                productDetailsParamsList.add(
                    BillingFlowParams.ProductDetailsParams.newBuilder()
                        .setProductDetails(productDetails!!)
                        .setOfferToken(offerToken)
                        .build()
                )
                val billingFlowParams = BillingFlowParams.newBuilder()
                    .setProductDetailsParamsList(productDetailsParamsList)
                    .build()
                billingClient.launchBillingFlow(this, billingFlowParams)
            } else {
                Toast.makeText(this, "商品ID無(wú)效", Toast.LENGTH_SHORT).show()
            }
        }
/**
     * 使用ProductDetails中的標(biāo)記檢索所有符合條件的基本計(jì)劃和優(yōu)惠。
     */
    private fun retrieveEligibleOffers(offerDetails: MutableList<ProductDetails.SubscriptionOfferDetails>):
            List<ProductDetails.SubscriptionOfferDetails> {
        val eligibleOffers = emptyList<ProductDetails.SubscriptionOfferDetails>().toMutableList()
        offerDetails.forEach { offerDetail ->
//data[vipAdapter.selectPosition].app_product_id是后臺(tái)接口配置返回的指定計(jì)劃id
            if (offerDetail.basePlanId == data[vipAdapter.selectPosition].app_product_id) {
                eligibleOffers.add(offerDetail)
            }
        }
        return eligibleOffers
    }

3.到這里客戶(hù)端就能調(diào)起訂閱,訂閱后需要確認(rèn)訂單(沒(méi)有確認(rèn),會(huì)自動(dòng)退款),正常是訂閱成功返回的數(shù)據(jù),傳給后臺(tái),讓后臺(tái)去確認(rèn)訂單。一下是Android端確認(rèn)訂單方法

/**
     * 訂閱確認(rèn)
     */
    fun handleSubscribePurchase(purchase: Purchase) {
        if (purchase.purchaseState == PurchaseState.PURCHASED) {
            if (!purchase.isAcknowledged) {
                val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
                    .setPurchaseToken(purchase.purchaseToken)
                    .build()
                billingClient.acknowledgePurchase(acknowledgePurchaseParams){
                    val responseCode = it.responseCode
                    val debugMessage = it.debugMessage
                    LogUtils.d("print", "acknowledgePurchase: $responseCode $debugMessage")
                }
            }
        }
    }

三、相關(guān)功能實(shí)現(xiàn)

Google訂閱推送 >>

調(diào)用Developer API接口配置>>

四、遇到的問(wèn)題

1.調(diào)起了支付界面,但是顯示的是“找不到您要購(gòu)買(mǎi)的項(xiàng)目”。解決方式>>

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

相關(guān)閱讀更多精彩內(nèi)容

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