I am looking to add an introductory offer or free-trial to my google play subscription.
I am using the new version of the billing library (5.0), however I cannot see the details of the subscription offer anywhere in the object returned from queryProductDetails
.
val productList = listOf(
QueryProductDetailsParams.Product.newBuilder()
.setProductId(purchase.lowercase())
.setProductType(BillingClient.ProductType.SUBS)
.build()
)
val params = QueryProductDetailsParams.newBuilder().setProductList(productList)
val productDetailsResult = withContext(Dispatchers.IO) {
billingClient.queryProductDetails(params.build())
}
According to https://developer.android.com/google/play/billing/compatibility I should be able to get all base plans and offers from this api, but I only see the base plans.
For a subscription, ProductDetails.getSubscriptionOfferDetails() returns a list of all base plans and offers the user is eligible to purchase. This means that you can access all base plans and offers eligible for the user, regardless of backward-compatibility. getSubscriptionOfferDetails() returns null for non-subscription products. For one-time purchases, you can use getOneTimePurchaseOfferDetails().
Here is the code I have to get subscriptions:
val product = productDetailsResult.productDetailsList?.get(0)!!
var billingPeriod = ""
if (plan == AppConsts.PREMIUM_ID_YEARLY)
billingPeriod = "P1Y"
else if (plan == AppConsts.PREMIUM_ID_MONTHLY)
billingPeriod = "P1M"
else if (plan == AppConsts.PREMIUM_ID_4WEEKLY)
billingPeriod = "P4W"
val offerToken = product.subscriptionOfferDetails?.find {
it.pricingPhases.pricingPhaseList[0].billingPeriod == billingPeriod
}?.offerToken!!
val productDetailsParamsList = listOf(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetailsResult.productDetailsList?.get(0)!!)
.setOfferToken(offerToken)
.build()
)
val flowParams = BillingFlowParams.newBuilder()
.setProductDetailsParamsList(productDetailsParamsList)
.build()
val responseCode = billingClient.launchBillingFlow(activity, flowParams).responseCode