This is store scene file
local composer = require( "composer" ) local iap = require("iap\_badger") local myData = require("myData") local analytics = require("analytics") local scene = composer.newScene() local widget = require("widget") local storeBg local centerX = display.contentCenterX local centerY = display.contentCenterY local width = display.contentWidth local height = display.contentHeight local spinner=nil local catalogue = { --Information about the product on the app stores products = { buy20Tokens = { productNames = {apple = "buy20tokens",google = "buy20tokens" }, productType = "consumable", onPurchase = function ()iap.addToInventory("buy20token",20) end, onRefund = function() iap.removeFromInventory("buy20token",20) end, }, buy50Tokens = { productNames = {apple = "buy50tokens",google = "buy50tokens" }, productType = "consumable", onPurchase = function ()iap.addToInventory("buy50token",50) end, onRefund = function() iap.removeFromInventory("buy50token",50) end, }, levelUnlock = { productNames = {apple="MixlevelUnlock",google = "mixlevelunlock"}, productType = "non-consumable", onPurchase = function() iap.setInventoryValue("unlocklevel",true) end, onRefund = function() iap.removeFromInventory("unlocklevel",true) end, } }, --Information about how to handle the inventory item inventoryItems = { buy20token = {productType = "consumable", reportMissingAsZero=true}, buy50token = {productType = "consumable", reportMissingAsZero=true}, unlocklevel = {productType = "non-consumable"}, } } local function failedListener() if (spinner) then spinner:removeSelf() spinner=nil end native.showAlert( "Info", "Sorry, Transaction failed." , {"Okay"} ) end --This table contains all of the options we need to specify in this example program. local iapOptions = { --The catalogue generated above catalogue=catalogue, --The filename in which to save the inventory filename="inventory.txt", --Salt for the hashing algorithm salt = "something tr1cky to gue55!", --Listeners for failed and cancelled transactions will just remove the spinner from the screen failedListener=failedListener, cancelledListener=failedListener, --Once the product has been purchased, it will remain in the inventory. Uncomment the following line --to test the purchase functions again in future. It's also useful for testing restore purchases. --doNotLoadInventory=true, --debugMode=true, } --Initialise IAP badger iap.init(iapOptions) iap.loadProducts() local productsDetail = iap.getLoadProductsCatalogue() local price1,price2 if(productsDetail)then price1 = productsDetail.buy20Tokens.localizedPrice price2 = productsDetail.buy50Tokens.localizedPrice end --Called when the relevant app store has completed the purchase local function purchaseListener(product ) spinner:removeSelf() spinner=nil myData.saveFile("levelstatus.txt",tostring(iap.getInventoryValue("unlocklevel"))) --Save the inventory change iap.saveInventory() --Give the user a message saying the purchase was successful native.showAlert("Info", "Your purchase was successful", {"Okay"}) end local function afterPurchase(product ) if(product=="buy20Tokens")then myData.saveFile("skipTokens.txt", tonumber(myData.loadFile("skipTokens.txt"))+20) elseif(product=="buy50Tokens")then myData.saveFile("skipTokens.txt", tonumber(myData.loadFile("skipTokens.txt"))+50) end iap.saveInventory() --Give the user a message saying the purchase was successful native.showAlert("Info", "Token purchase was successful", {"Okay"}) end buyUnlock=function(event) if (event.phase=="ended") then if (event.target.id == "levelunlock") then print("levelunlock pressed") analytics.logEvent("LevelUnlockEvent") local spinnerBackground = display.newRect(display.contentCenterX,240,360,600) spinnerBackground:setFillColor(0,157/255,207/255,0.75) spinnerBackground.alpha = 50 --Spinner consumes all taps so the user cannot tap the purchase button twice spinnerBackground:addEventListener("touch", function() return true end) local spinnerText = display.newText("Contacting " .. iap.getStoreName() .. "...", display.contentCenterX,180, native.systemFont, 18) spinnerText:setFillColor(0,0,0) --Add a little spinning rectangle local spinnerRect = display.newRect(display.contentCenterX,display.contentCenterY,35,35) spinnerRect:setFillColor(0.25,0.25,0.25) transition.to(spinnerRect, { time=4000, rotation=360, iterations=999999, transition=easing.inOutQuad}) --Create a group and add all these objects to it spinner=display.newGroup() spinner:insert(spinnerBackground) spinner:insert(spinnerText) spinner:insert(spinnerRect) iap.purchase("levelUnlock", purchaseListener) end end end consumeToken = function(event) if ( event.phase =="ended")then if (event.target.id == "buy20") then analytics.logEvent("20 skipTokens") iap.purchase("buy20Tokens",afterPurchase) elseif(event.target.id =="buy50")then analytics.logEvent("50 skipTokens") iap.purchase("buy50Tokens", afterPurchase) end end end local function restoreListener(productName, event) --If this is the first product to be restored, remove the spinner --(Not really necessary in a one-product app, but I'll leave this as template --code for those of you writing apps with multi-products). if (event.firstRestoreCallback) then --myData.saveFile("levelstatus.txt",tostring(iap.getInventoryValue("unlocklevel"))) --Save the inventory change spinner:removeSelf() spinner=nil native.showAlert("Restore", "Your items are being restored", {"Okay"}) restoreBtn:setEnabled( false ) restoreBtn.alpha = 0.5 levelunlockbtn:setEnabled( false ) levelunlockbtn.alpha = 0.5 end --Save any inventory changes iap.saveInventory() end local function restorePurchases(event) --Tell IAP to initiate a purchase --Use the failedListener from onPurchase, which just clears away the spinner from the screen. --You could have a separate function that tells the user "Unable to contact the app store" or --similar on a timeout. --On the simulator, or in debug mode, this function attempts to restore all of the non-consumable --items in the catalogue. if (event.phase=="ended")then analytics.logEvent("RestoredEvent") local spinnerBackground = display.newRect(display.contentCenterX,240,360,600) spinnerBackground:setFillColor(1,1,1,0.75) --Spinner consumes all taps so the user cannot tap the purchase button twice spinnerBackground:addEventListener("tap", function() return true end) local spinnerText = display.newText("Contacting " .. iap.getStoreName() .. "...", display.contentCenterX,180, native.systemFont, 18) spinnerText:setFillColor(0,0,0) --Add a little spinning rectangle local spinnerRect = display.newRect(display.contentCenterX,display.contentCenterY,35,35) spinnerRect:setFillColor(0.25,0.25,0.25) transition.to(spinnerRect, { time=4000, rotation=360, iterations=999999, transition=easing.inOutQuad}) --Create a group and add all these objects to it spinner=display.newGroup() spinner:insert(spinnerBackground) spinner:insert(spinnerText) spinner:insert(spinnerRect) iap.restore(false, restoreListener, failedListener) end end function scene:create( event ) local sceneGroup = self.view print( "store create scene" ) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase print( "store show scene" ) if ( phase == "will" ) then storeBg = display.newImage( sceneGroup, "storeBg.png") storeBg.x = centerX storeBg.y = centerY storeBg.width = width storeBg.height = height buy20 = widget.newButton{ width = display.contentWidth\*0.20, height = display.contentHeight\*0.06, defaultFile = "buybtn.png", overFile = "buybtnover.png", label = "0.45$", id = "buy20", onEvent = consumeToken } buy20.x = display.contentWidth\*0.58 buy20.y = display.contentHeight\* 0.50 sceneGroup:insert( buy20 ) buy50 = widget.newButton{ width = display.contentWidth\*0.20, height = display.contentHeight\*0.06, defaultFile = "buybtn.png", overFile = "buybtnover.png", label = "0.75$", id = "buy50", onEvent = consumeToken } buy50.x = display.contentWidth\*0.82 buy50.y = display.contentHeight\* 0.50 sceneGroup:insert( buy50 ) if (iap.getInventoryValue("unlock")==true) then buy20:setEnabled( false ) buy20.alpha = 0 print( "Game unlocked" ) end leveltext = display.newImage( sceneGroup, "leveltext.png" ) leveltext.x = display.contentCenterX leveltext.y = display.contentHeight\*0.65 leveltext.width = display.contentWidth\*0.60 leveltext.height = display.contentHeight\*0.06 levelunlockbtn = widget.newButton{ defaultFile = "levelbtnover.png", overFile = "levelbtn.png", id = "levelunlock", onEvent = buyUnlock, } levelunlockbtn.x= display.contentWidth\*0.30 levelunlockbtn.y= display.contentHeight\*0.75 sceneGroup:insert( levelunlockbtn ) restoreBtn = widget.newButton{ width = display.contentWidth\*0.40, defaultFile = "restorebtnover.png", overFile = "restorebtn.png", onEvent = restorePurchases, } restoreBtn.x = display.contentWidth\*0.70 restoreBtn.y = display.contentHeight\*0.75 sceneGroup:insert( restoreBtn ) if (iap.getInventoryValue("unlocklevel") == true) then levelunlockbtn:setEnabled( false ) levelunlockbtn.alpha = 0.5 restoreBtn:setEnabled( false ) restoreBtn.alpha = 0.5 end elseif ( phase == "did" ) then end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene