如何将一次性购买产品的令牌传递到 Google Play Billing Library 5?

Mar*_*ark 25 google-play play-billing-library

Android中如何获取PurchaseDetails对象的token?根据文档https://developer.android.com/google/play/billing/integrate#java,要启动购买流程,我们需要执行如下操作:

// 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);
Run Code Online (Sandbox Code Playgroud)

请注意,它显示获取优惠令牌:

to get an offer token, call ProductDetails.getSubscriptionOfferDetails()
Run Code Online (Sandbox Code Playgroud)

这假设我们使用的是订阅,但就我而言,我使用的是应用程序内购买,这是一次性购买。如果是订阅,我相信我可以尝试使用getOfferToken()此处显示的方法: https: //developer.android.com/reference/com/android/billingclient/api/ProductDetails.SubscriptionOfferDetails#getOfferToken()

但是,对于一次性购买,该对象不包含任何处理令牌的方法,https://developer.android.com/reference/com/android/billingclient/api/ProductDetails.OneTimePurchaseOfferDetails

所以我的问题是我们将什么传递给selectedOfferToken一次性购买产品?

Mar*_*ark 31

对于任何一直为此苦苦挣扎的人,我希望这会有所帮助,花了我几天的时间......看来我们不需要调用该.setOfferToken(selectedOfferToken)方法。

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()
    );
Run Code Online (Sandbox Code Playgroud)

  • 是的,我不需要 setOfferToken 就可以成功购买。setOfferToken 似乎仅用于订阅使用 (5认同)
  • 需要检查productDetails的类型,如果是Subscription则需要设置offer token,否则你会得到`java.lang.NullPointerException: OfferToken is required for Construction ProductDetailsParams.` 对于InApp类型不执行任何操作 (4认同)

MoD*_*MoD 9

找到了一个解决方案OfferToken

.setOfferToken(productDetails.getSubscriptionOfferDetails().get(0).getOfferToken())
Run Code Online (Sandbox Code Playgroud)

简单的解决方法。:)