Could anybody help me to understand the following schema? I have an iOS app and an app server. A connection between app and server (1A, 1B) is already working.
Let's say a user of the app has 10 gems, which controlled by the server due to security reasons. I have a consumable IAP type in the AppStore: 100 gems pack and the user wants to buy it. Of course, I want to make a record in my Mongo database to increase the number of gems for the user.
How does this flow work? Should I use StoreKit in the app first and then communicate to the server? Or can make an endpoint on the server like "/buyPack/100gems" and do everything on the server side? As far as I understand I need to validate a receipt from the AppStore, but where to get it and what does it contain?
If you could use my arrows (1A, 2A, 3B and so on) I will really appreciate this.
The purchase must happen on the iOS device using StoreKit.
When you get a transaction with a .purchased
state you should retrieve the receipt and send it to your server for validation. Once you your server has validated the receipt with Apple and updated the user's balance in MongoDB it should return a success status to the app. At that point the app should complete the transaction with StoreKit.
Be aware that it is conceivable that the app is terminated by the user or loses network connectivity after you have updated your database but before it received the response and completed the transaction. In this case the app will receive the transaction again when it is next launched. Your server should be prepared for this by checking the transaction identifier. If you have previously processed the transaction, return success to your app so that it can complete the transaction but don't increase the user's balance again.
See this answer for some more advice on receipt validation.
The flow is something like: