Reputation: 147
I have implemented non consumable in app purchase in my flutter app. after upgrade in app purchase package purchase dialog not shown and its give me below error:
PlatformException(INVALID_OFFER_TOKEN, Offer token null for product monthly is not valid. Make sure to only pass offer tokens that belong to the product. To obtain offer tokens for a product, fetch the products. An example of how to fetch the products could be found here: https://github.com/flutter/packages/blob/main/packages/in_app_purchase/in_app_purchase/README.md#loading-products-for-sale, null, null)
I don't have any offer with my base plan. Any help is appreciated.
Upvotes: 4
Views: 1659
Reputation: 41
In my case (using version 3.2.0
), the problem is that I was doing a mapper from the result await inAppPurchase.queryProductDetails
to my internal type, so I fixed it by doing the following on the purchase method:
Future<bool> purchase({
required InAppProductDetail productDetail,
required String applicationUserName,
}) async {
final result = (await inAppPurchase.queryProductDetails({productDetail.id}))
.productDetails
.firstOrNull;
if (result == null) {
return false;
}
final purchaseParam = PurchaseParam(
productDetails: result,
applicationUserName: applicationUserName,
);
return inAppPurchase.buyNonConsumable(purchaseParam: purchaseParam);
}
The method queryProductDetails returns runtime cast objects that implement offerToken, in this case, GooglePlayProductDetails
.
Upvotes: 0
Reputation: 86
I would recommend using the product details returned from queryProductDetails
. I had the same issue and that fixed it.
Here is an example, be sure to update it with the correct product id.
Future<void> purchaseSubscription() async {
final productDetailsResponse = await InAppPurchase.instance.queryProductDetails({YOUR_PRODUCT_ID});
final productDetails = productDetailsResponse.productDetails.firstOrNull;
if (productDetails != null) {
final purchaseParam = PurchaseParam(productDetails: productDetails);
await InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam);
}
}
Edit: Background on my issue
I created my own model which copied the properties from the ProductDetails
that was returned, so that I didn't need to call queryProductDetails
again. The issue was that the model returned from queryProductDetails
on Android is GooglePlayProductDetails
. I wasn't checking the type if it was GooglePlayProductDetails
and was only handling it as if it were ProductDetails
. GooglePlayProductDetails
has additional properties, and these were weren't being copied and were therefore lost. To make the purchase I was instantiating ProductDetails
which would then be used to instantiate PurchaseParam
, this meant that the additional fields were not present and hence why the purchase fails with this error.
The reason the solution above works is because it uses the GooglePlayProductDetails
which has all the properties that are required.
In hindsight, my attempt to "optimize" wasn't so well thought through because in the future if more properties were added to GooglePlayProductDetails
then I would have to add them to my own custom model which seems like something that would be very easy to miss. That's why I think it is a better solution.
Upvotes: 5
Reputation: 304
I have found a solution. To resolve the issue related to the in-app purchase library in Flutter, you can use version 3.1.5 of the library.
In the latest version, 3.1.7 of the in-app purchase library, it internally relies on the in_app_purchase_android library with a version of ^0.3.0+8. In this version, the offer token is mandatory, which can cause issues with invalid offer tokens, whether or not you have added a token in the Play Store.
To avoid these issues, you can specifically use version 3.1.5 of the in_app_purchase library. This version should not have the mandatory offer token requirement and can help resolve the problem.
Make sure to update your pubspec.yaml
file with the following line under the dependencies section:
dependencies:
in_app_purchase: 3.1.5
After saving the file, run flutter pub get
to fetch the specified version of the library and resolve the issue.
By using version 3.1.5 of the in_app_purchase library, you should be able to avoid the problems related to the mandatory offer token requirement.
Upvotes: 2