Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[in_app_purchase]: duplicate incomplete purchases in purchaseStream #83673

Closed
samarthagarwal opened this issue May 31, 2021 · 4 comments
Closed
Labels
r: duplicate Issue is closed as a duplicate of an existing issue

Comments

@samarthagarwal
Copy link

samarthagarwal commented May 31, 2021

Steps to Reproduce

  1. Create an app, install the in_app_purchase plugin.
  2. Add the following code to the initState() of the HomePage widget.
_processIAP()
  1. Next, add the _processIAP method
StreamSubscription _subscription;

_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);
}
  1. 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.
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.
flutter doctor -v
[✓] Flutter (Channel stable, 2.0.4, on macOS 11.3.1 20E241 darwin-x64, locale en-IN)
    • Flutter version 2.0.4 at /Users/samarthagarwal/flutter
    • Framework revision b1395592de (9 weeks ago), 2021-04-01 14:25:01 -0700
    • Engine revision 2dce47073a
    • Dart version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/samarthagarwal/Library/Android/sdk
    • Platform android-30, build-tools 29.0.2
    • ANDROID_HOME = /Users/samarthagarwal/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/samarthagarwal/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.5, Build version 12E262
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)

[✓] VS Code (version 1.56.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.22.0

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.212

• No issues found!
@TahaTesser TahaTesser added the in triage Presently being triaged by the triage team label Jun 1, 2021
@TahaTesser
Copy link
Member

Hi @samarthagarwal
Is the issue on iOS or Android? Can you please make sure you're using the latest in_app_purchase?
Thank you

@TahaTesser TahaTesser added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jun 1, 2021
@samarthagarwal
Copy link
Author

samarthagarwal commented Jun 1, 2021

Hello @TahaTesser ,
The issue is on iOS. It does not allow to use restorePurchases() before buyNonConsumable(). Also, restorePurchases() returns (n+1) PurchaseDetails objects every time we attempt a purchase, failed or successful (n being the number of PurchaseDetails objects returned in the previous attempt).

Am I not allowed to use restorePurchases() to fetch the previous purchases to find out which subscription the user already has before I allow them to buy a new one?

Yes, I am using the latest version of the plugin.

Edit: I also read on some threads that Apple does not recommend to use restorePurchases() during initialization. In that case, how do we find out the user's current subscription?

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jun 1, 2021
@TahaTesser
Copy link
Member

Hi @samarthagarwal
In that case, this is a documentation issue rather than a bug, it would be addressed in #82631
Closing as duplicate of #82631

@TahaTesser TahaTesser added r: duplicate Issue is closed as a duplicate of an existing issue and removed in triage Presently being triaged by the triage team labels Jun 2, 2021
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
r: duplicate Issue is closed as a duplicate of an existing issue
Projects
None yet
Development

No branches or pull requests

2 participants