Hi, i’ve been trying for some days to code in-apps for android. I’ve read many tutorials and all the documentation about it, but it seems like there isn’t an actual example of how to do it, or maybe im missing it.
Either way, i’m having problems setting it up, i know that i have to code a transaction callback function like this:
function transactionCallback( event ) local transaction = event.transaction if transaction.state == "purchased" then print("Transaction succuessful!") print("productIdentifier", transaction.productIdentifier) print("receipt", transaction.receipt) print("transactionIdentifier", transaction.identifier) print("date", transaction.date) elseif transaction.state == "restored" then print("Transaction restored (from previous session)") print("productIdentifier", transaction.productIdentifier) print("receipt", transaction.receipt) print("transactionIdentifier", transaction.identifier) print("date", transaction.date) print("originalReceipt", transaction.originalReceipt) print("originalTransactionIdentifier", transaction.originalIdentifier) print("originalDate", transaction.originalDate) elseif transaction.state == "cancelled" then print("User cancelled transaction") elseif transaction.state == "failed" then print("Transaction failed, type:", transaction.errorType, transaction.errorString) else print("unknown event") end -- Once we are done with a transaction, call this to tell the store -- we are done with the transaction. -- If you are providing downloadable content, wait to call this until -- after the download completes. store.finishTransaction( transaction ) end
and then initialize the store like
store.init( "google", transactionCallback )
And to buy something you need to have something like:
local function buygem(event) if event.phase == "began" then store.purchase({"com.mycompany.mygame.1gem"}) gem = gem+1 end end button:addEventListener("touch",buygem)
Where the product ID is valid.
Well, i tested all that and it works. But here is where i get confused and i couldn’t get enough info from the tutorials: when i purchase an item i dont know how to make it like if the transaction was complete, user gets 1 gem. If i use that code, the user gets 1 gem every time he pressed the button, no matter what happens, so thats incorrect. I tried using if transaction.complete == true inside the buygem function but it looks like store events dont work outside the transaction callback function.
The only thing that worked was adding (for this example) a “gem = gem+1” inside one of the transactions events, inside the callback function like this:
function transactionCallback( event ) local transaction = event.transaction if transaction.state == "purchased" then print("Transaction succuessful!") gem = gem +1 elseif transaction.state == "restored" then print("Transaction restored (from previous session)") elseif transaction.state == "cancelled" then print("User cancelled transaction") elseif transaction.state == "failed" then print("Transaction failed, type:", transaction.errorType, transaction.errorString) else print("unknown event") end store.finishTransaction( transaction ) end
But then… what if i want to make 2 in-app products? or even more? Where i put the “gem = gem+1” statement when someone purchases the product succesfully?
I hope the explanation wasn’t too long, i added all the logic functions that people usually use so my question may be pretty simple but it takes a lot of code.
Can anyone help me here please? I’m obviously missing some basic information…
Thanks a lot in advance.