There are many important details that need to be set-up correctly to use the Corona store.* API for IAP (in-app purchases) on the App Store, so when my implementation was not working, I thought I’d gotten one of these details wrong. I spent a few hours going over everything with a fine-toothed comb and rereading the guides (staring with Corona’s IAP guide). But there was something fundamental that I misunderstood about store.init and I want to share my realization here.
When you call
store.init( transactionListener )
the listener function is not called once the store is initialized. I thought the listener was called when initialization completed, but that’s incorrect. You call the store.init() method and then you can call the other store.* methods right away, or so it seems. To be on the safe side, you could make the call to store.init() and wrap the first call to another store.* method in a function that’s called in timer.performWithDelay(). In my tests, however, the code below works without introducing any delay.
local store = require 'store' local function transactionListener( event ) -- handle outcome of transaction (like store.purchase() ) store.finishTransaction( event.transaction ) end store.init( transactionListener ) if store.isActive and store.canLoadProducts then local function loadListener( event ) if event.products then for i=1,#event.products do print('Valid ID: ', event.products[i].productIdentifier ) end end end local productIdentifiers = { 'com.newb.insights' } store.loadProducts( productIdentifiers, loadListener ) end
I’ll try to suggest that this be clarified in the API documentation, but in the meantime, I hope this post can help others who, like me, assumed that you had to wait for store.init() to do its thing and let you know when it when store.* was ready.