Flutter: get past purchases on iOS

4.1k Views Asked by At

Everything is working on Android with the new implementation of in_app_purchase (https://pub.dev/packages/in_app_purchase) but on iOS I don't get past purchases. Following the documentation I don't see anything special.

My code is:

final Stream<List<PurchaseDetails>> purchaseUpdated =
          inAppPurchase.purchaseStream;

      _subscription = purchaseUpdated.listen((purchaseDetailsList) {
        if (purchaseDetailsList.isEmpty) {
          Provider.of<AdState>(context, listen: false).toggleAds(context, true);
        } else {
          Provider.of<AdState>(context, listen: false)
              .toggleAds(context, false);
          this.purchases.addAll(purchaseDetailsList);
          listenToPurchaseUpdated(purchaseDetailsList);
        }
      }, onDone: () {
        _subscription.cancel();
      }, onError: (error) {
        // handle error here.
      });

      inAppPurchase.restorePurchases();

It just doesn't go in and if I try to buy same product I get this error:

There is a pending transaction for the same product identifier. Please either wait for it to be finished or finish it manually using completePurchase to avoid edge cases

2

There are 2 best solutions below

0
On BEST ANSWER

I opened a ticket on the repo and it was fixed on version 1.0.9: https://github.com/flutter/flutter/issues/89950

7
On

You need to call _inAppPurchase.completePurchase(purchase); to complete it. Code here:

if (purchase.pendingCompletePurchase) {
  _inAppPurchase.completePurchase(purchase);
}

You also need to call it every time your app is opened to ensure that all payments have been completed, it's very important, remember to check purchase.pendingCompletePurchase before calling it.

Btw, I prefer flutter_inapp_purchase over in_app_purchase because I also had some unexpected issues with it in iOS.