How to make a download function in game-store?


How can I make a function that, when the user clicks (touches) the “Buy” button, it will be asked to pay with money and then if/when the purchase success start downloading the purchased item and register it on the server, in the player’s account…
And also when the same user log in in another device restores its owned items.