androidsubscriptionplay-billing-library

How to get freeTrialPeriod of subscribtion from ProductDetails?


In Google Play Billing Library 5 there are ProductDetais instead of deprecated SkuDetails. SkuDetails had freeTrialPeriod field which returned free trial of subscribtion. In ProductDetails I can't find any similar field, is there a way to get free trial period from ProductDetails?


Solution

  • Yes, there is a way. First check that this is a subscription (and not a one-time purchase). Then get the pricing plan that you need. The free trial period is always the first pricing phase of the pricing plan and will have priceAmountMicros = 0 and FormattedPrice="free". If the first pricing phase in your pricing plan matches the criteria, you can use its billing period as the trial period.

    int trialDays = -1;
    if(BillingClient.ProductType.SUBS.equals(productDetails.getProductType()))
    {
        List<ProductDetails.SubscriptionOfferDetails> subscriptionPlans = productDetails.getSubscriptionOfferDetails();
        ProductDetails.SubscriptionOfferDetails pricingPlan = subscriptionOffers.get(planIndex);
        ProductDetails.PricingPhase firstPricingPhase = offer.getPricingPhases().getPricingPhaseList().get(0);
        if(firstPricingPhase.getPriceAmountMicros() == 0)
        {
            trialDays = BillingFlavor.parseDuration(firstPricingPhase.getBillingPeriod());
        }
    }
    return trialDays;