5

I am using in_app_purchase: ^1.0.1 in my Flutter app to implement In-App purchases of subscriptions. I have successfully managed to buy a subscription once but when I try to use restorePurchases() to get the subscriptions already purchased by the user, I get a list that increases in length every time. It looks like there are duplicate purchases made for the same product which is not possible.

Here is my code.

_processIAP() async {
    Stream purchaseUpdated = InAppPurchase.instance.purchaseStream;
    _subscription = purchaseUpdated.listen((purchaseDetailsList) {
        print("purchaseDetailsList.length = " + purchaseDetailsList.length.toString());
        purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
        
        print(purchaseDetails.purchaseID + " " + purchaseDetails.pendingCompletePurchase.toString());

        if (purchaseDetails.status == PurchaseStatus.pending) {
            print("Purchase is still pending!");
        } else {
            if (purchaseDetails.status == PurchaseStatus.error) {
                print("An error has occurred!");
            } else if (purchaseDetails.status == PurchaseStatus.purchased || purchaseDetails.status == PurchaseStatus.restored) {
                print("Purchased or restored successfully!");

                await InAppPurchase.instance.completePurchase(purchaseDetails);
                print("Purchase marked as completed");
          }
          if (purchaseDetails.pendingCompletePurchase) {

                await InAppPurchase.instance.completePurchase(purchaseDetails);
                print("Purchase marked as completed");
          }
        }
      });
    }, onDone: () {
      print("DONE!");
      _subscription.cancel();
    }, onError: (error) {
      print(error);
    });

    const Set<String> _kIds = <String>{'basic_monthly_apple'};
    final ProductDetailsResponse response = await InAppPurchase.instance.queryProductDetails(_kIds);
    if (response.notFoundIDs.isNotEmpty) {
      print("Some product IDs not found!");
    }

    print("Restoring previous purchases!");
    await InAppPurchase.instance.restorePurchases();

    //PurchaseParam purchaseParam = PurchaseParam(productDetails: products[0]);
    // InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam);
}

In the logs, I get that purchaseDetailsList has a length that increases (by 1) every time I try to purchase the subscription after using the restorePurchases() method. Each purchaseDetail has a unique purchaseID. Also, pendingCompletePurchase for all purchaseDetails is always true even though they have been marked as completed.

Logs -

Some product IDs not found!
flutter: Restoring previous purchases!
flutter: purchaseDetailsList.length = 6
flutter: 1000000819394740 true
flutter: Purchased or restored successfully!
flutter: 1000000819394741 true
flutter: Purchased or restored successfully!
flutter: 1000000819394742 true
flutter: Purchased or restored successfully!
flutter: 1000000819394743 true
flutter: Purchased or restored successfully!
flutter: 1000000819394744 true
flutter: Purchased or restored successfully!
flutter: 1000000819394745 true
flutter: Purchased or restored successfully!
flutter: Purchase marked as completed
flutter: Purchase marked as completed
flutter: Purchase marked as completed
flutter: Purchase marked as completed
flutter: Purchase marked as completed
flutter: Purchase marked as completed

This length 6 keeps on increasing every time I restart the app and try again. Also, I am not allowed to execute buyNonConsumable along with restorePurchases. It causes an error that indicates something about a pending transaction that needs to be marked as completed.

Any help is highly appreciated.


Samarth Agarwal
  • 2,044
  • 8
  • 39
  • 79

0 Answers0