'Flutter: Test purchases do not show after Restore on Android Play Store
Using the same Flutter code on both Android and iOS, restores work on iOS, but not on Android. I’m able to buy non-consumable products on both platforms, but when I try to restore them on Android, I always get zero as a return value.
This is how I initialize the IAP:
@override
initState() {
final Stream<List<PurchaseDetails>> purchaseUpdated = InAppPurchase.instance.purchaseStream;
_subscription = purchaseUpdated.listen((purchaseDetailsList) {
_listenToPurchaseUpdatedBooks(purchaseDetailsList);
}, onDone: () {
_subscription.cancel();
}, onError: (error) {
// handle error here.
});
super.initState();
}
For purchasing, I used the following code:
late PurchaseParam purchaseParam;
List<PurchaseDetails> _purchases = <PurchaseDetails>[];
InAppPurchase _inAppPurchase = InAppPurchase.instance;
bool isIOS = Theme
.of(context)
.platform == TargetPlatform.iOS;
if (isIOS == false) {
final Map<String, PurchaseDetails> purchasesMap =
Map<String, PurchaseDetails>.fromEntries(
_purchases.map((PurchaseDetails purchase) {
if (purchase.pendingCompletePurchase) {
_inAppPurchase.completePurchase(purchase);
}
return MapEntry<String, PurchaseDetails>(
purchase.productID, purchase);
}));
final GooglePlayPurchaseDetails? oldSubscription = _getOldSubscription(
productL, purchasesMap);
purchaseParam = GooglePlayPurchaseParam(
productDetails: productL,
applicationUserName: null,
changeSubscriptionParam: (oldSubscription != null)
? ChangeSubscriptionParam(
oldPurchaseDetails: oldSubscription,
prorationMode: ProrationMode
.immediateWithTimeProration,
)
: null);
} else {
purchaseParam = PurchaseParam(
productDetails: productL,
applicationUserName: null,
);
}
await InAppPurchase.instance.buyConsumable(purchaseParam: purchaseParam)
.catchError((e) {
print('Got error: $e'); // Finally, callback fires.
return false; // Future completes with 42.
}).then((value) {
if (value == true) {
print('The value is $value');
}
});
I expected to get a value different from zero for restored purchases, but on Android, I always get zero as a return value.
On Android, I am also checking for old subscriptions, as I am required to do.
In order for in-app purchases to register on Android, does my code need to be further adapted for Android? We are running the app under "Closed testing", all of our test users are configured under "License testing" in the Play Console, and we are using version 3.0.2 of the in_app_purchase library. Many thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
