The basic work flow is:
have a button somewhere in your code that the user will click on to buy the coins. This could be on a menu screen or your own store/shop screen or on a “Loose the level” screen ("Hey, you lost, buying more coins can help, would you like to buy some? type screen). Once the player taps on the button to buy the item you do:
store.purchase( “youritemname”) – Google/Amazon or
store.purchase( { “youritemname” } ) – Apple
This will interact with the store and depending on what happens your transactionCallback function will get triggered. If they bought the item you will get a event.transaction.state = purchased event. If they cancelled the dialog, I think you get a transaction cancelled state.
When you get the transaction purchased state, then you can add the coins to their account. The logic (those if statements) are all determined by your code and number of store products. For instance, if you only have one item they can buy, then it’s the only purchase, so you don’t need to test to see what product they bought.
local function transactionCallback( event ) print("In transactionCallback", event.transaction.state) local transaction = event.transaction local tstate = event.transaction.state if tstate == "purchased" then print("Transaction succuessful!") if transaction.productIdentifier == "com.omnigeekmedia.mygame.unlock" then -- handle unlocking power ups mydata.settings.isPaid = true utility.saveTable(mydata.settings, "settings.json") ads.hide() native.showAlert("Thank you!", "We appreciate your support!", {"Okay"}) end store.finishTransaction( transaction ) elseif tstate == "restored" then print("Transaction restored (from previous session)") if transaction.productIdentifier == "com.omnigeekmedia.mygame.unlock" then mydata.settings.isPaid = true ads.hide() utility.saveTable(mydata.settings, "settings.json") native.showAlert("Thank you!", "Your settings have been restored!", {"Okay"}) end store.finishTransaction( transaction ) elseif tstate == "refunded" then -- Google feature print("User requested a refund") local productId = transaction.productIdentifier if transaction.productIdentifier == "com.omnigeekmedia.mygame.unlock" then mydata.settings.isPaid = false utility.saveTable(mydata.settings, "settings.json") end store.finishTransaction( transaction ) elseif tstate == "revoked" then -- Amazon feature print("productIdentifier", transaction.productIdentifier) print ("The user who has a revoked purchase is", transaction.userId) --Revoke this SKU here: if transaction.productIdentifier == "com.omnigeekmedia.mygame.unlock" then mydata.settings.unlocked = false mydata.saveTable(mydata.settings, "settings.json") end store.finishTransaction( transaction ) elseif tstate == "cancelled" then print("388 User cancelled transaction") store.finishTransaction( transaction ) elseif tstate == "failed" then print("Transaction failed, type:", transaction.errorType, transaction.errorString) store.finishTransaction( transaction ) else print("unknown event") store.finishTransaction( transaction ) end print("done with store business for now") end
In my case, it’s a simple unlock/turn off the ads feature. Inside each possible transaction state, I test to see if it’s the unlock item and if so I do what’s needed. In my case, I set a flag saying .isPaid to true that I use later. In your case, you would just add the number of coins bought to their coin total. Then for Google, you would need to call store.consumePurchase() to allow them to buy the coins again.
Hope this helps
Rob