hyochan/flutter_inapp_purchase

SubscriptionOfferDetails NullPointerException

Closed this issue · 2 comments

Version of flutter_inapp_purchase 5.4.1

Platforms you faced the error (IOS or Android or both?) Android

if (offerToken == null) {
offerToken = selectedProductDetails.subscriptionOfferDetails!![0].offerToken
}
query BillingClient.ProductType.INAPP product, subscriptionOfferDetails is null

This line throws a NullPointerException

I second that. Set out to find a fix and PR it.

@aniealss Did you by any chance proceed any further?

Before PR-ing it, let's discuss because the following modifications seem to solve it for one-off in-app purchases but subscriptions should also be checked first. In AndroidInappPurchasePlugin.kt:

private fun buyProduct(
...
  // Get the selected offerToken from the product, or first one if this is a migrated from 4.0 product
  // or if the offerTokenIndex was not provided
  var offerToken : String? = null
  if (offerTokenIndex != null) {
      offerToken = selectedProductDetails.subscriptionOfferDetails?.get(offerTokenIndex)?.offerToken
  }
  if (offerToken == null && selectedProductDetails.subscriptionOfferDetails != null) {
      offerToken = selectedProductDetails.subscriptionOfferDetails!![0].offerToken
  }

  val details = ProductDetailsParams.newBuilder().setProductDetails(selectedProductDetails) 
  if (offerToken != null)
    details.setOfferToken(offerToken)
  val productDetailsParamsList = listOf(details.build())
  builder.setProductDetailsParamsList(productDetailsParamsList)
  ...

https://stackoverflow.com/questions/73027160/how-to-pass-token-of-a-one-time-purchase-product-to-google-play-billing-library suggests and this coincides with my experience, too, that there's no need for setOfferToken(offerToken) in the case of non-subscription purchases.