ok here is my code
first i created buttons and their event listeners
local inAppHelper = require "inAppPurchase" inAppHelper.initialize() -- METHOD DEFINED IN SECNOD CODE BLOCK local purchasebutton=display.newImage("Purchase.png") purchasebutton.x=200 purchasebutton.y=330 purchasebutton:scale(0.5,0.5) screenGroup:insert(purchasebutton) purchasebutton:addEventListener("tap", onBuyNowButtonClick) local restoreButton=display.newImage("Restore.png") restoreButton.x=200 restoreButton.y=200 restoreButton:scale(0.5,0.5) screenGroup:insert(restoreButton) restoreButton:addEventListener("tap", onRestoreButtonClick) function onBuyNowButtonClick() inAppHelper.loadProducts(transactionCompletionListener) end function onRestoreButtonClick() inAppHelper.restorePurchase() end
I have created a method “log” that simply shows the native alert, because i can not see print statements while running the app on device, so i call this method to debug.
function log(message) native.showAlert("", message) end
then in inAppPurchase.lua,
-- TO INITIALIZE THE STORE function initialize() if ( store.availableStores.apple ) then currentProductList = appleProductList store.init( "apple", storeTransaction ) elseif ( store.availableStores.google ) then currentProductList = googleProductList store.init( "google", storeTransaction ) else log( "In-app purchases are not supported on this system/device." ) end end -- TO LOAD PRODUCTS function loadProducts(completionListener) listener = completionListener alert.showLoader("Retrieving Products...") if (store.canLoadProducts) then if ( store.availableStores.apple ) then log("going to load apple products list") store.loadProducts( appleProductList, loadProductsCallback ) end else if ( store.availableStores.google ) then alert.showLoader("Loading...") store.purchase( googleProductList ) else log( "this store does not support an app fetching products" ) listener(true) end end end --- AND THE CALL BACK IS local function loadProductsCallback( event ) alert.hideLoader() local validProducts = event.products local invalidProducts = event.invalidProducts for i = 1,#validProducts do local currentItem = validProducts[i] -- log("Title = ".. currentItem.title.."Description = "..currentItem.description.."Price = "..currentItem.price.."Product Identifier = "..currentItem.productIdentifier ) alert.showLoader("Loading...") store.purchase( validProducts ) -- SINCE I HAVE ONLY ONE PRODUCT.. DIRECTLY CALLING PURCHASE end end -- AND FINALLY THE TRANSACTION CALLBACK IS function storeTransaction( event ) alert.hideLoader() local transaction = event.transaction if ( transaction.state == "purchased" ) then log( " Transaction successfully completed" ) elseif ( transaction.state == "cancelled" ) then log( " Transaction was cancelled" ) elseif ( transaction.state == "restored" ) then log( " Transaction RESTORED SUCCESSFULLY" ) elseif ( transaction.state == "failed" ) then log( " Transaction was failed because: "..transaction.errorString ) elseif ( transaction.state == "refunded" ) then log( " Transaction REFUNDED" ) log( "productIdentifier", transaction.productIdentifier ) else log( "transaction state is: "..transaction.state ) end store.finishTransaction( event.transaction ) end -- RESTORE METHOD function restorePurchase() alert.showLoader("Restoring Products...") store.restore() end
Working on iOS for now:
As mentioned in the code, First I call initialize store, then I click buy, it first loads products, and after loading calls store.purchase, after giving my account credentials, i get the callback called and it displays native alert saying " Transaction successfully completed".
Then I click on restore button, which calls the method inAppHelper.restorePurchase(), it asks for account credentials, i enter them, but i get no native alert.
may be store.finishTransaction is not working properly. I’m using the daily build 2013.1193
Please let me know if i’m doing anything wrong in the code.
also can you please share with me the sample IAP code, i couldn’t find it yet.
Thanks,