In-app purchase: Cannot connect to iTunes Store

Hi, so I’ve searched the entire net to no avail. I’ve read through every Corona thread and tried everything and I just can’t seem to figure this out…

So I’ve tried implementing IAP into my app using this code from the code exchange: (http://web-c1.anscamobile.com/code/super-simple-iap-1-line). And here are the things I’ve done so far:

* I’ve set up my billing information in ITC
* Created the iap products in ITC and specified a product ID like so: com.domainName.appName.iapProduct
* I have added the iap code into my project, modifying it to suit my needs:

module(..., package.seeall)  
  
local store = require("store")  
local callback = nil  
  
--To get a price from any screen, just call iap.localizedPrices[XX] and replace the XX with the item number that you'd like  
localizedPrices = {};  
  
--[[  
  
Fill out this section with the price that you want (otherwise it will load the prices dynamically)  
\*\*\*\*\*\*Uncomment this section if you want to override the prices that it fetches\*\*\*\*\*\*  
  
]]--  
localizedPrices[1] = "$0.99"  
--Fill out this section with all your product IDs. Add or remove them as necessary  
local productID = {}  
productID[1] = "com.domainName.appName.iapProduct" --buyItem1  
  
--If you specifed a callback, then your callback function (in another lua file) would be something like this:  
  
--[[  
  
local function callback(state)  
  
 if (state == "purchased") then  
  
 elseif (state == "cancelled") then  
  
 elseif (state == "failed") then  
  
 end  
  
end  
  
iap.buyItem1(callback)  
  
]]--  
  
function transactionCallback(e)  
 local trans = e.transaction  
  
 if (trans.state == "purchased") then  
 if (trans.productIdentifier == productID[1]) then --item1  
  
 print ("success buying product 1! Give the user the item here, or in the callback (see comments above)");  
  
 end   
  
  
 elseif (trans.state == "cancelled") then  
  
 print("player cancelled; you may want to show an alert here")  
  
  
 elseif (trans.state == "failed") then  
  
 print("transaction failed")  
 print("event.transaction.errorType = " .. tostring(trans.errorType))  
 print("event.transaction.errorString = " .. tostring(trans.errorString))  
 native.showAlert("Oops!" , "Please check your internet connection and try again." , {"Dismiss"}, none)  
  
 end  
  
 if (callback ~=nil) then callback(trans.state); end  
  
 native.setActivityIndicator(false)  
 store.finishTransaction(trans)  
  
end  
  
store.init(transactionCallback)  
local function loadedCallback(e)  
  
 for i=1, #e.products do  
 table.insert(localizedPrices, i, tostring(e.products[i].price))  
 end  
  
end  
  
store.loadProducts(productID, loadedCallback)  
  
--These are the functions that you call from another lua file.  
-- Example: you'd call something like: iap.buyItem3(callback)  
function buyItem1 (callbackFunction)  
 store.purchase({productID[1]})  
 native.setActivityIndicator(true)  
 callback = callbackFunction  
end  
-----------  
-- Using this line underneath here to call this module from another lua file:  
iap.buyItem1(callbackFunction)  
-----------  

Now after calling the function and trying to perform the purchase, I keep getting the “failed” transaction state, and the errorType “Unknown” and errorString “Cannot connect to iTunes Store”. I have no clue what can be causing this… I am on a jailbroken device but when sending the app in for review Apple also rejected the app for this very reason, you cannot complete the purchase…

I have tried deleting the app, reinstalling, recreating the iap product, changing the product id multiple times. It feels like I’ve tried just about anything and that there is something I’m missing…

Has anyone had the same issue and can offer some tips and advice? I’m just mainly out of ideas and can’t think of anything else to try anymore, been struggling with this all day now…
Thanks in advance, [import]uid: 14018 topic_id: 29098 reply_id: 329098[/import]

Oh and I forgot to mention I have logged out of my iTunes account from the settings and I have setup created a test account. Can’t reach the point where I have to use it though [import]uid: 14018 topic_id: 29098 reply_id: 117046[/import]

I think there are many different reasons why this happens. In my case, I needed to wipe my device to get this fixed. Have you looked at these threads? Maybe you’ll find some clues for your fix there.

http://developer.coronalabs.com/forum/2012/03/18/iap-cannot-connect-itunes

http://developer.coronalabs.com/forum/2012/04/20/cannot-connect-itunes-store-help

http://developer.coronalabs.com/forum/2012/01/13/iap-cannot-connect-itunes-store

Naomi [import]uid: 67217 topic_id: 29098 reply_id: 117056[/import]

Hey Naomi, thank you for ( once again :slight_smile: ) helping out someone with iap!

I did look through those threads and I tried everything suggested in them. I’m now using my distribution profile and I’ve double checked my paid apps contracts.

I’ve considered hard reseting my device but since I have so much stuff on it, I’m seeing that as the very last resort. But since I did send my app in for review using this code before and had it rejected for this same reason, I’m guessing there’s a bug in the code and not the device - since Apple experienced the same thing…

And thanks again for your help!

// [import]uid: 14018 topic_id: 29098 reply_id: 117067[/import]

@Mitaten, my IAP looks a bit different from yours. Maybe someone else might be able to help with your code, but just in case this helps, mine is structured something like this:

Edit: Missing function added to the code structure (this is what happens when rushing through some long lines of code and trying to simplify the whole thing too fast.)

-- main.lua  
store = require("store")  
productsForSale = {  
-- list of available products  
}  
function loadProductsCallback(event)  
-- check if there are valid products  
end  
local function savePurchase(product)  
-- perform this function when purchase is successful  
end  
  
function transactionCallback(event)  
 local transaction = event.transaction  
 if transaction.state == "purchased" then  
 elseif transaction.state == "restored" then  
 elseif transaction.state == "cancelled" then  
 elseif transaction.state == "failed" then  
 else  
 end  
 store.finishTransaction( transaction )  
end  
function setupMyStore(event)  
 store.loadProducts( productsForSale, loadProductsCallback )  
end  
-- Connect to store at startup  
if store.availableStores.apple then  
 store.init("apple", transactionCallback)  
end   
timer.performWithDelay (1000, setupMyStore);  

I call store.purchase function in a module where users actually make purchase.

Naomi [import]uid: 67217 topic_id: 29098 reply_id: 117078[/import]

Make sure that your test account is an us-american account. Otherwise you will get problem!

Joakim [import]uid: 81188 topic_id: 29098 reply_id: 117088[/import]

Thank you for all your tips! I’ve found what the problem was, (finally)… The problem was my old habit of installing distribution builds on JB devices… When I finally gave testing it on a non jailbroken device a shot I couldn’t install the build. I tried solving that problem instead and found out I’d have to use the Ad Hoc build, tried that and it worked :slight_smile: Jailbreak can cause so many unnecessary issues… [import]uid: 14018 topic_id: 29098 reply_id: 117314[/import]

@Mitaten, glad to hear you got this sorted. It was really frustrating when I first tackled IAP too – I always used AdHoc to test my game, but somehow, dunno how, I got it in my head that I need to use developer cert to generate build to test IAP, and that never worked for me, and I wasted something like 2 weeks, literally doing everything I thought was possible cause of all my problem, including wiping out my keychains, uninstalling and reinstalling Xcode, to redoing the provisioning profiles what-felt-like thousand times. It was maddening.

Naomi [import]uid: 67217 topic_id: 29098 reply_id: 117330[/import]

Haha yes! I almost abandoned the app because of this. Even though the solution is right before your eyes you just can’t see it, drives one crazy. It pays off to be stubborn :slight_smile:

Thanks again!
// [import]uid: 14018 topic_id: 29098 reply_id: 117335[/import]