iaphub / react-native-iaphub

The easiest way to implement IAP (In-app purchase) in your React Native app.
https://www.iaphub.com/
MIT License
319 stars 22 forks source link

Error: User id required #100

Closed DevrealmEOOD closed 2 years ago

DevrealmEOOD commented 2 years ago

Hello,

I have caught an error case when using the library with the following scenario:

  1. My app has a login, so IAPHub.setId() is called on successful login
  2. I init the library BEFORE the login as part of my initial loading process
  3. Any user makes a pruchase while logged in - all works great
  4. The user then uninstalls the app or logs out
  5. If the app is restarted at that stage the "User id required" error is thrown

I have investigated what happens and it turnes out in the initialization of the library there's this piece of code:

// Init purchase updated listener
    if (!this.purchaseUpdatedListener) {
      this.purchaseUpdatedListener = RNIap.purchaseUpdatedListener((purchase) => {
        // Add receipt to the queue
        this.receiptQueue.add({date: new Date(), purchase: purchase})
        // Resume queues in case it was paused
        this.resumeQueues();
      });
    }

As the device account actually has an active purchase - the receiptQueue method gets called which then calls processReceipt, which then calls fetchUser which throws the error. But at this point we don't have a valid user logged in, so the error is thrown which seems wrong. The restore should be triggered manually after the user logs in I think.

DevrealmEOOD commented 2 years ago

It turned out there was another trigger for checking the active subscriptions in parallel with the init - that was the reason for the error