Android 集成google 订阅商品

一、Google Play Console中创建订阅商品

1.再自己的项目下,选择 "产品"->"订阅",点击“建立订阅”,如下图所示



2.输入产品id和名字,产品id创建后是唯一的,且不能修改(貌似也不能删除)



3.新建计划,比如公司要求vip有3个订阅计划(一个是按1个月收费,一个3个月收费,一个按1年收费),这时候就需要分别创建三个计划


4.创建优惠,很多app订阅商品都有优惠,例如:新用户订阅可免费使用7天,具体步骤如下图所示




image.png

二、Android端订阅google 官网结算地址

订阅支付和一次性商品支付流程差不多,具体看文档,这里说下具体区别
1.查询商品类型修改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);

主要设置setOfferToken,具体代码可以下载demo去看代码下载地址
下面是我自己写的一段代码,获取selectedOfferToken

billingClient.queryProductDetailsAsync(queryProductDetailsParams) { billingResult, productDetailsList ->
            if (productDetailsList.isNotEmpty()) {
                val productDetails = productDetailsList[0]
                LogUtils.d("print","hashCode-->${productDetails.hashCode()}")
                //获取计划相关(获取指定计划)
                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无效", Toast.LENGTH_SHORT).show()
            }
        }
/**
     * 使用ProductDetails中的标记检索所有符合条件的基本计划和优惠。
     */
    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是后台接口配置返回的指定计划id
            if (offerDetail.basePlanId == data[vipAdapter.selectPosition].app_product_id) {
                eligibleOffers.add(offerDetail)
            }
        }
        return eligibleOffers
    }

3.到这里客户端就能调起订阅,订阅后需要确认订单(没有确认,会自动退款),正常是订阅成功返回的数据,传给后台,让后台去确认订单。一下是Android端确认订单方法

/**
     * 订阅确认
     */
    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")
                }
            }
        }
    }

三、相关功能实现

Google订阅推送 >>

调用Developer API接口配置>>

四、遇到的问题

1.调起了支付界面,但是显示的是“找不到您要购买的项目”。解决方式>>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容