一、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)目”。解決方式>>