I think you’re really overcomplicating your logic here. Your current logic will have a “canceled” state actually get into your purchased code. It’s best to have a linear if-then-elseif-end block to cover each state.
local function storeTransactionListener( event ) print( json.prettify( event )) local transaction = event.transaction if ( transaction.state == "purchased" ) then store.finishTransaction( event.transaction ) -- do the work you need to do elseif ( transaction.state == "restored" ) then store.finishTransaction( event.transaction ) -- do the work you need to do elseif ( transaction.state == "cancelled" ) then elseif ( transaction.state == "failed" ) then end end
If you get runtime errors in any of the if-then-elseif blocks then the rest of that block will not execute, so it’s safer to finish the transaction as soon as you can. The IAP servers have no clue about what you’re doing in your code, so there isn’t much of a reason to wait around until you complete your unlock code before you let Apple know you’re done.
Also, be careful using concatenation when printing debug messages:
print( "event.transaction: " .. json.prettify( event.transaction ) )
Instead, do
print( "event.transaction: ", json.prettify( event.transaction ) )
If for some reason json.prettify() were to return nil, it would cause a runtime error, the second form will not crash.
Rob