IN APP question: where to add my code?

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.

Lets start with buying one gem.  Normally you are going to put the transaction call back function and the init code in your main.lua.  Your buy button goes in your app where ever makes the most sense.

The request to buy the item happens and if it was successful, your transaction call back is called with a transaction.state value of “purchased”.  In side this “if” statement where you test to see if that is the state, you would as you said increment your gem count like you are.  The trick is to try and avoid globals while making the gem variable available to other modules.  If you gem is set up as a consumable, every transaction.state of “purchased” will be a new gem and you do add it.  Now with Google, if a gem is a non-consumable, you only want to grant it once and Google doesn’t do restores, they do purchases, so in that case you would have gems = true, but it sounds more like you want consumables.

Now what about multiple products?  In that case transaction.state’s of “purchased” may  not mean a gem, it could be something else.  AT that point you have to put in if statements that test the transaction.productIdentifier to see which product was purchased.  If it’s your gem, increment your gem counter.  If it’s something else, handle the logic for that.

Rob

Thanks Rob for the answer, very appreciated. 

I can’t test it right now im my app because i have an IAP error with google and my version of the game (probably need to wait some more time to load current version that i’ve uploaded to google play console), but basically if i call for example:

elseif transaction.state == "purchased" then   print("Transaction succuessful!") if transaction.productIdentifier == "com.mycompany.mygame.1gem" then gems = gems + 1 elseif transaction.productIdentifier == "com.mycompany.mygame.10gem" then gems = gems + 10 end

Inside the transactionCallback function it should work?

Something like that should work.

Rob

Thanks a lot

Lets start with buying one gem.  Normally you are going to put the transaction call back function and the init code in your main.lua.  Your buy button goes in your app where ever makes the most sense.

The request to buy the item happens and if it was successful, your transaction call back is called with a transaction.state value of “purchased”.  In side this “if” statement where you test to see if that is the state, you would as you said increment your gem count like you are.  The trick is to try and avoid globals while making the gem variable available to other modules.  If you gem is set up as a consumable, every transaction.state of “purchased” will be a new gem and you do add it.  Now with Google, if a gem is a non-consumable, you only want to grant it once and Google doesn’t do restores, they do purchases, so in that case you would have gems = true, but it sounds more like you want consumables.

Now what about multiple products?  In that case transaction.state’s of “purchased” may  not mean a gem, it could be something else.  AT that point you have to put in if statements that test the transaction.productIdentifier to see which product was purchased.  If it’s your gem, increment your gem counter.  If it’s something else, handle the logic for that.

Rob

Thanks Rob for the answer, very appreciated. 

I can’t test it right now im my app because i have an IAP error with google and my version of the game (probably need to wait some more time to load current version that i’ve uploaded to google play console), but basically if i call for example:

elseif transaction.state == "purchased" then   print("Transaction succuessful!") if transaction.productIdentifier == "com.mycompany.mygame.1gem" then gems = gems + 1 elseif transaction.productIdentifier == "com.mycompany.mygame.10gem" then gems = gems + 10 end

Inside the transactionCallback function it should work?

Something like that should work.

Rob

Thanks a lot