Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass token of a one time purchase product to Google Play Billing Library 5?

How do you get token of PurchaseDetails object in Android? Based on the docs, https://developer.android.com/google/play/billing/integrate#java, to launch a purchase flow we need to do something like below:

// 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);

Note that it shows to get the offer token:

to get an offer token, call ProductDetails.getSubscriptionOfferDetails()

This assumes we are using a subscription, however in my case I'm using an in app purchase which is a one-off purchase. If it is a subscription I believe I could try and use the getOfferToken() method as show here: https://developer.android.com/reference/com/android/billingclient/api/ProductDetails.SubscriptionOfferDetails#getOfferToken()

However, with a one time purchase the object does not contain any methods to deal with tokens, https://developer.android.com/reference/com/android/billingclient/api/ProductDetails.OneTimePurchaseOfferDetails.

So my question is what do we pass into the selectedOfferToken for a one time purchase product?

like image 588
Mark Avatar asked Sep 08 '25 06:09

Mark


2 Answers

For anyone who has been struggling with this, I hope this helps, took days of my time...it seems we don't need to call the .setOfferToken(selectedOfferToken) method.

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
            .build()
    );
like image 155
Mark Avatar answered Sep 11 '25 02:09

Mark


Found a solution with OfferToken:

.setOfferToken(productDetails.getSubscriptionOfferDetails().get(0).getOfferToken())

Simple workaround. :)

like image 26
MoD Avatar answered Sep 11 '25 01:09

MoD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!