Hi Naomi: I finally got my next kids app done, but I’m having trouble with my first IAP. I have read ALL the documentation and gone through all the samples and tutorial links you provided, but I think the way I wrote the code for triggering my button is not quite right. I just made one button for users to click on in my “iap.lua” page, and when they click it, it triggers the StoreKit code I copy and pasted from the InAppDemo sample, and my tweaks for my Bundle ID, etc. If you have a minute, can you take a look and see what I’m doing wrong. I would really appreciate it:
– Abstract: InApp sample project
– This project demonstrates Corona In App Purchase support.
local ui = require(“ui”)
local store = require(“store”) – Available in Corona build #261 or later
local isSimulator = “simulator” == system.getInfo(“environment”)
display.setStatusBar( display.HiddenStatusBar )
– Unbuffer console output for debugging
io.output():setvbuf(‘no’) – Remove me for production code
local titlecard
function showTitle()
if isSimulator then
local myAlert = native.showAlert( “iTunes Store not available in Corona Simulator”,
“Offline testing: see console for output.”,
{ “OK” } )
end
titlecard = display.newImage( “titlecard.png”, true )
titlecard.x = display.contentCenterX
titlecard.y = display.contentCenterY
end
local bg
local validProducts, invalidProducts = {}, {}
local descriptionArea
– Product IDs should match the In App Purchase products set up in iTunes Connect.
– We cannot get them from the iTunes store so here they are hard coded;
– your app could obtain them dynamically from your server.
local listOfProducts =
{
– These Product IDs must already be set up in your store
– We’ll use this Product ID to retrieve prices etc. for each item
– The iTunes store will not validate bad Product IDs
“com.jfk4sdk.numbersortshd”,
}
– Process and display product information obtained from store.
– Constructs a button for each item
function unpackValidProducts()
– Utility to build a buy button
– function newBuyButton (index)
– Handler for buy button
– local buttonDefault = “greenBtn.png”
–defaultX=150
–defaultY= 100
Layer1 = display.newImageRect( “greenBtn.png”, 700, 150 );
Layer1.x = 480; Layer1.y = 328; Layer1.alpha = 1
Layer1:addEventListener(“touch”, onbuyButTouch)
local buyThis = function ( product )
print (" Purchasing" …product)
– Purchase the item
if store.canMakePurchases then
store.purchase( {product} )
else
native.showAlert(“Store purchases are not available, please try again later”,
{ “OK” } )
end
end
function buyThis_closure ( index )
– Closure wrapper for buyThis() to remember which button
return function ( event )
buyThis (validProducts[index].productIdentifier)
return true
end
end
end
– Utility to build a restore button
function newRestoreButton ()
local buttonDefault = “buttonRestore.png”
local buttonOver = “buttonRestoreDown.png”
local restore = function ( product )
– Ask the iTunes Store to initiate restore transaction sequence
print ("Restoring " )
store.restore()
end
local hideDescription = function ( )
descriptionArea.text = “Select a product…”
end
local describeThis = function ()
– Display info in description area
print (“Test restore feature”)
descriptionArea.text = “Test restore feature”
timer.performWithDelay( 2000, hideDescription)
end
local label = “Test restore”
local myButton = ui.newButton{
default = buttonDefault, over = buttonOver,
onPress = describeThis, onRelease = restore,
text = “”, textColor = {255, 255, 1, 255}, font=“Marker Felt”, size = 14, emboss = false
}
myButton:setReferencePoint(display.CenterLeftReferencePoint)
myButton:setText(label)
return myButton
end
print (“Loading product list”)
if not validProducts then
native.showAlert( “In App features not available”, “initStore() failed”, { “OK” } )
else
print (“Product list loaded”)
print( "Country: " … system.getPreference( “locale”, “country” ) )
– Show store UI
titlecard:removeSelf()
bg = display.newImage( “storebg.png”, true )
bg.x = display.contentCenterX
bg.y = display.contentCenterY
descriptionArea = native.newTextBox (10, 0.7*display.contentHeight, display.contentCenterX - 20, display.contentCenterY - 10)
descriptionArea.text = “Select a product…”
descriptionArea:setTextColor (2, 0, 127)
descriptionArea.size = 18
descriptionArea.hasBackground = false
local buttonSpacing = 5
print( "Found " … #validProducts … " valid items ")
– display the valid products in buttons
for i=1, #validProducts do
– Debug: print out product info
print ("Item " … i … “: " … validProducts[i].productIdentifier
… " (” … validProducts[i].price … “)”)
print (validProducts[i].title … ", "… validProducts[i].description)
– create and position product button
local myButton = newBuyButton(i)
myButton.x = display.contentWidth - myButton.width - buttonSpacing
myButton.y = i * buttonSpacing + (2 * i - 1) * myButton.height / 2
end
– create and position Restore button
local myButton = newRestoreButton()
myButton.x = display.contentWidth - myButton.width - buttonSpacing
myButton.y = display.contentHeight - myButton.height / 2 - buttonSpacing
for i=1, #invalidProducts do
– Debug: display the product info
native.showAlert( “Item " … invalidProducts[i] … " is invalid.”,
{ “OK” } )
print(“Item " … invalidProducts[i] … " is invalid.”)
end
end
– Handler to receive product information
– This callback is set up by store.loadProducts()
function loadProductsCallback( event )
– Debug info for testing
print(“In loadProductsCallback()”)
print(“event, event.name”, event, event.name)
print(event.products)
print("#event.products", #event.products)
io.flush() – remove for production
– save for later use
validProducts = event.products
invalidProducts = event.invalidProducts
unpackValidProducts ()
end
– Handler for all store transactions
– This callback is set up by store.init()
function transactionCallback( event )
local infoString
print("transactionCallback: Received event ", event.name)
–[[
– Also available for your app to use:
print(“transaction”, event.transaction)
print(“state”, event.transaction.state)
print(“errorType”, event.transaction.errorType)
print(“errorString”, event.transaction.errorString)
–]]
– print(“testing”, store.transactionStatePurchased, store.transactionErrorPaymentCancelled, store.transactionStateFailed )
if event.transaction.state == “purchased” then
infoString = “Transaction successful!”
print (infoString)
descriptionArea.text = infoString
elseif event.transaction.state == “restored” then
– Reminder: your app must store this information somewhere
– Here we just display some of it
infoString = “Restoring transaction:” …
"\n Original ID: " …event.transaction.originalTransactionIdentifier …
"\n Original date: "…event.transaction.originalDate
print (infoString)
descriptionArea.text = infoString
print(“productIdentifier”, event.transaction.productIdentifier)
print(“receipt”, event.transaction.receipt)
print(“transactionIdentifier”, event.transaction.transactionIdentifier)
print(“date”, event.transaction.date)
print(“originalReceipt”, event.transaction.originalReceipt)
elseif event.transaction.state == “cancelled” then
infoString = “Transaction cancelled by user.”
print (infoString)
descriptionArea.text = infoString
elseif event.transaction.state == “failed” then
infoString = "Transaction failed, type: ",
event.transaction.errorType, event.transaction.errorString
print (infoString)
descriptionArea.text = infoString
else
infoString = “Unknown event”
print (infoString)
descriptionArea.text = infoString
end
– Tell the store we are done with the transaction.
– If you are providing downloadable content, do not call this until
– the download has completed.
store.finishTransaction( event.transaction )
end
– Setter upper
function setupMyStore (event)
store.loadProducts( listOfProducts, loadProductsCallback )
print (“After store.loadProducts, waiting for callback”)
if isSimulator then
– No Store, so no callbacks, so exercise our GUI “manually”
validProducts[1] = {}
validProducts[1].title = “Number Sorts HD”
validProducts[1].description = “Numbers 0-20 recogniton game!”
validProducts[1].price = 0.99
validProducts[1].productIdentifier = “com.jfk4sdk.numbersortshd”
unpackValidProducts()
end
end
– Show title card
showTitle ()
– Connect to store at startup
store.init (transactionCallback )
print (“After init”)
– Hide title card, run store
timer.performWithDelay (1000, setupMyStore)
I’m assuming I only need to the one page with the StoreKit code to trigger the events, is that right? Since the sample code was for three products, maybe that’s my problem, but I’m not sure what code to remove. I think I’m OK with all the set ups for Bundle ID, App ID, iTunes Connect set ups, and certificates. I’m hoping to get this uploaded if I can resolve this IAP issue. I would really appreciate any help you can provide. Thanks so much!
John (- [import]uid: 66192 topic_id: 17644 reply_id: 127337[/import]