chirag04 / react-native-in-app-utils

A react-native wrapper for handling in-app payments
MIT License
890 stars 185 forks source link

Restore function works before purchase is made #63

Closed ghost closed 7 years ago

ghost commented 7 years ago

Hi,

I used the exact code in the example for my restore button. Unfortunately, when testing with my sandbox users, I am able to "restore" the purchase successfully before it has ever been bought. Any ideas? My code is posted below.

EDIT: Also, logging response and response.productIdentifier appear to be undefined

onPress() {

  InAppUtils.restorePurchases((error, response)=> {
    if(error) {
        Alert.alert('iTunes Error', 'Could not connect to iTunes store.');
    } else {
        Alert.alert('Restore Successful', 'Successfully restored your purchase.');

        if (global.packs.indexOf('expansion') === -1){
                    global.packs = global.packs.concat('expansion')
        }
        AsyncStorage.setItem("expansion", "unlocked");

        this.setState({bought: true})

    }
})

}

amedeedaboville commented 7 years ago

Yes, I have noticed this issue too, using the the same if(error) { } else { unlock} logic @hpierce93 uses and which is also on the docs on the front page.

This is a major issue for me.

This SO page explains that the restoreCompletedTransactions() StoreKit function can actually return many transactions, and can also return 0 transactions.

Looking at the function in react-native-in-app-utils that handles restores, paymentQueueRestoreCompletedTransactionsFinished, it looks like all it does is compile the transactions returned by StoreKit into an array. As long as that function is called by StoreKit and not restoreCompletedTransactionsFailedWithError, the error field will be empty.

For a clean user who has not purchased anything, it will just return an empty array and not throw an error.

I have a single "App unlock" iap in my app, so I will be checking response to see if it contains anything.

For people with multiple products, they need to check the productIdentifier of each entry in the response array to see which products have been restored.

amedeedaboville commented 7 years ago

Update: PR #42 fixes this.

chirag04 commented 7 years ago

This is expected behavior for restorePurchase. Restore Purchase is transparent in the sense it returns whatever was purchased(which can be nothing) or error if the network request fails, etc.

Aside: this bug is a sign that this library (and the entire RN ecosystem) is highly immature. Is anybody using this for serious work? I got pinged on this by my client a few hours after launching my first RN app yesterday. I would have thought the ~18 months this library has been up would be enough time to iron out bugs in a module with 4 exported functions, given that it's the defacto way of doing IAP on RN with iOS.

Ranting here like that is not going to do any good to this library or the entire RN ecosystem. If you think something is not right, help fix it. Realize that you are getting paid for using somebody else's work(this library). The work that these library authors do may or may not be paid always. Think about how much time it would have taken you if it was not for those library authors.

About that PR: thanks for sharing. I'm not sure how i missed it.

amedeedaboville commented 7 years ago

I agree with you. I have removed my negative remarks, as they are not constructive here. I apologize for flaming you, and the rn community in general. Forgive me. As a developer yourself, I hope you understand what it's like to put a lot at stake on a project and then have an issue at a critical time. In the end it is just my fault for not realizing that I had this issue before I deployed.

Thanks for merging #42, and thanks for the unpaid work. Put up a link and I'll send you $. In any case, codepush helped me send out a fast fix 😊

P.S.: My expected behavior for restorePurchases was that 'unlock store here again' meant, I could unlock my freemium app as there had been no error. There was no mention that response could hold more information.

Readme.MD

Restore payments

...
   } else {
      AlertIOS.alert('Restore Successful', 'Successfully restores all your purchases.');
      //unlock store here again.
   }
});