Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter in_app_purchase queryPastPurchases() returns empty list on iOS

Tags:

flutter

Why does queryPastPurchases() return a populated list with my subscription on android, but returns an empty list on iOS?

I have a physical iOS SE phone, my apple user is added as an tester in testflight, I've added a subscription in App Store Connect, and I am able to buy the subscription(in testflight). However queryPastPurchases() returns an empty list...

  // Gets past purchases
  Future<void> _getPastPurchases() async {
    QueryPurchaseDetailsResponse response =
        await _iap.queryPastPurchases();

    for (PurchaseDetails purchase in response.pastPurchases) {
      if (Platform.isIOS) {
        InAppPurchaseConnection.instance.completePurchase(purchase);
      }
    }

    setState(() {
      _purchases = response.pastPurchases;
    });
  }
like image 415
Christer Avatar asked Aug 05 '19 07:08

Christer


Video Answer


2 Answers

Turns out there was nothing wrong with the code.

In order to make it work I had to create a sandbox user. Then I had to log out from my current logged in AppID on the phone. Then I had to "Edit Scheme" in Xcode from Release to Debug. Then I ran "flutter run" in terminal. Then I purchased my NonConsumable subscription, and in the dialog I had to enter the sandbox user email and pwd.

Updated: Also I had to create a completely new subscription in app store connect, because the original subscription got trapped in a state were it was not bought nor purchasable. This was due to the fact that the completePurchase was done at the wrong place in my code. It should not be done in _getPastPurchases(), but rather in the actual purchase logic.

So put completePurchase here:

_subscription = _iap.purchaseUpdatedStream.listen((data) => setState(() {
    _purchases.addAll(data);
    for(PurchaseDetails purchase in _purchases){
        if(purchase.productID == 'subName'){
            //SOLUTION
            if (Platform.isIOS) {
                InAppPurchaseConnection.instance.completePurchase(purchase);
            }
        }
    }   
}));

PS: I don't remember if I had the completePurchse in the for loop or outside...

like image 163
Christer Avatar answered Oct 18 '22 17:10

Christer


Unfortunately, this is currently a known bug of the plugin and as of today there is no other solution than using the unofficial plugin that works better....

You can find the current status of the bug here and the unofficial plugin here

like image 24
schankam Avatar answered Oct 18 '22 17:10

schankam