Making IAP work using multiple LUA files?

Hi Guys,

I’m trying to integrate in-app purchases on my app and I’m stuck. I followed the instructions on how to integrate the store module from init to completing a transaction and the following works:

  1. My in-app store can grab the products from iTunes properly.
  2. It can check if in-app purchasing is allowed under device restrictions.
  3. When I click on my BUY button, it asks me for my iTunes username/password.

However, after entering my iTunes details, nothing happens and my transactionCallback function doesn’t seem to work.

I have 2 LUA files–Main.lua and MyStore.lua. Main.lua is where all the “store” functions are placed (initialization, load products, transactionCallback, etc.). MyStore.lua is where the products are displayed and bought. One thing to note is that I’m also using the Director class.

Questions:

  1. Since I’m working with 2 files, should I place transactionCallback inside Main.lua or MyStore.lua? Should the function be global?

  2. How were you able to integrate IAP using multiple LUA files? I’m pretty sure someone has done this already, I just couldn’t find any sample code.

Any help would be greatly appreciated. Thanks in advance! :slight_smile: [import]uid: 44127 topic_id: 21395 reply_id: 321395[/import]

Hi @egarayblas, I’m no expert, but for what it’s worth, here’s what I do:

In main.lua:

loadProductsCallback (global)
savePurchase (local)
transactionCallback (global)
setupMyStore (global)

In external module:

unpackValidProducts (local)
buyNow (local)

About testing, do you have test users set up in iTunes Connect? If I remember correctly, you need to sign out of app store first before testing IAP. Meaning, before you launch your app, sign out of app store, and then after launching your app and when prompted by IAP, enter the test user info. Otherwise, the test user may no longer be treated as test user. (From my experience, once the test user signs in after the IAP prompts it, then this test user can stay signed in without trouble.)

Hope this helps, and good luck.

Naomi
[import]uid: 67217 topic_id: 21395 reply_id: 84713[/import]

Hi Naomi,

Thanks for sharing these info. Follow-up question: From the InAppDemo example, “unpackValidProducts()” is called inside “loadProductsCallback( event )” but in your post above, those 2 functions are in 2 different modules and “unpackValidProducts()” is local. May I know what revisions did you make to achieve that? [import]uid: 44127 topic_id: 21395 reply_id: 84765[/import]

Hi @egarayblas, my IAP has gone through many changes (especially after I added consumable products to the mix), and I honestly don’t remember what I’ve done along the way. One thing I always do when I’m in doubt is to place lots of print statements to see how far the code executes, where it fails, which function does what and what data has been passed/received in each module, etc. This process helped me understand how IAP works just enough to make the necessary changes to the very basic code I started out with. I learn better when I take things apart and put them back together. You might want to try it too.

Naomi

Edit: BTW, just in case this helps, here’s what my unpackValidProducts look like. I don’t remember exactly why I used the delay of 1000 (one second), but here it is:
[lua] local function unpackValidProducts()
print (">>>>>>>>>>>>>>Loading product list")
if not _G.validProducts then
print(">>>>>>>>>>>>>>>>inside if not validProducts")
local alert = native.showAlert( “In-App features not available”, “initStore() failed”, { “OK” } )
else
print(">>>>>>>>>>>>>>>>looping through the invalidProducts")
for numLoop=1, #_G.invalidProducts do
local alert = native.showAlert( “Item " … _G.invalidProducts[numLoop] … " is invalid.”,{ “OK” } )
end
end
if _G.validProducts then
– if we find valid products do this or that
end
end
timerStash.newTimer = timer.performWithDelay (1000, unpackValidProducts);[/lua]
[import]uid: 67217 topic_id: 21395 reply_id: 84798[/import]

Thanks Naomi! That’s exactly what I’m doing (using print statements, alerts, etc.). I even started a new project to simplify everything and to test IAP w/out any complicated code. I’ll experiment more and see if I can figure it out. [import]uid: 44127 topic_id: 21395 reply_id: 84887[/import]

@egarayblas, I hope you’ll get it working very soon.

Naomi [import]uid: 67217 topic_id: 21395 reply_id: 84980[/import]

Thanks Naomi! I decided to work on the other features first (Game Center, Push Notifications, etc.) and leave IAP for now to save time. I’ll post back here if I figure it out. :slight_smile: [import]uid: 44127 topic_id: 21395 reply_id: 85181[/import]

Just wanted everyone to know that I was unable to make IAP work using multiple LUA files. Oh well.

I went with a workaround instead:

  1. I placed all IAP related functions inside my store.lua file including store.init
  2. I added a flag so that I can call store.init only once in my entire app

Its not what I was aiming for but its all working great now! :slight_smile: [import]uid: 44127 topic_id: 21395 reply_id: 86459[/import]