Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | None | 0 0
  1. --Example2.lua
  2. --
  3. --Simple example of using IAP Badger to purchase an IAP for buying coins.
  4.  
  5.  
  6. ---------------------------------
  7. --
  8. -- IAP Badger initialisation
  9. --
  10. ---------------------------------
  11.  
  12. --Load IAP Badger
  13. local iap = require("plugin.iap_badger")
  14.  
  15. --Progress spinner
  16. local spinner=nil
  17. --Text object indicating how many coins the user is currently holding
  18. local coinText=nil
  19.  
  20. --Forward declaration
  21. local buyCoins10=nil
  22.  
  23. --Create the catalogue
  24. local catalogue = {
  25.  
  26.     --Information about the product on the app stores
  27.     products = {   
  28.    
  29.         --buy50coins is the product identifier.
  30.         --Always use this identifier to talk to IAP Badger about the purchase.
  31.         buy50coins = {
  32.                 --A list of product names or identifiers specific to apple's App Store or Google Play.
  33.                 productNames = { apple="buy50coins", google="50_coins", amazon="COINSx50"},
  34.                 --The product type
  35.                 productType = "consumable",
  36.                 --This function is called when a purchase is complete.
  37.                 onPurchase=function() iap.addToInventory("coins", 50) end,
  38.                 --The function is called when a refund is made
  39.                 onRefund=function() iap.removeFromInventory("coins", 50) end,
  40.         },
  41.        
  42.         --buy100coins is the product identifier.
  43.         --Always use this identifier to talk to IAP Badger about the purchase.
  44.         buy100coins = {
  45.                 --A list of product names or identifiers specific to apple's App Store or Google Play.
  46.                 productNames = { apple="buy100coins", google="100_coins", amazon="COINSx100"},
  47.                 --The product type
  48.                 productType = "consumable",
  49.                 --This function is called when a purchase is complete.
  50.                 onPurchase=function() iap.addToInventory("coins", 100) end,
  51.                 --The function is called when a refund is made
  52.                 onRefund=function() iap.removeFromInventory("coins", 100) end,
  53.         },
  54.     },
  55.  
  56.     --Information about how to handle the inventory item
  57.     inventoryItems = {
  58.         coins = { productType="consumable" }
  59.     }
  60. }
  61.  
  62. --Called when a purchase fails
  63. local function failedListener()
  64.    
  65.     --If the spinner is on screen, remove it
  66.     if (spinner) then
  67.         spinner:removeSelf()
  68.         spinner=nil
  69.     end
  70.    
  71. end
  72.  
  73.  
  74. local iapOptions = {
  75.     --The catalogue generated above
  76.     catalogue=catalogue,
  77.     --The filename in which to save the inventory
  78.     filename="example2.txt",
  79.     --Salt for the hashing algorithm
  80.     salt = "something tr1cky to gue55!",
  81.        
  82.     --Listeners for failed and cancelled transactions will just remove the spinner from the screen
  83.     failedListener=failedListener,
  84.     cancelledListener=failedListener,
  85.     --Once the product has been purchased, it will remain in the inventory.  Uncomment the following line
  86.     --to test the purchase functions again in future.
  87.     --doNotLoadInventory=true,
  88.     debugMode=true,
  89. }
  90.  
  91. --Initialise IAP badger
  92. iap.init(iapOptions)
  93.  
  94.  
  95. local function purchaseListener(product)
  96.     --Remove the spinner
  97.     spinner:removeSelf()
  98.     spinner=nil
  99.    
  100.     --Any changes to the value in 'coins' in the inventory has been taken care of.
  101.     --Update the text object to reflect how many coins are now in the user's inventory
  102.     coinText.text = iap.getInventoryValue("coins") .. " coins"
  103.    
  104.     --Save the inventory change
  105.     iap.saveInventory()
  106.    
  107.     --Tell user their purchase was successful
  108.     native.showAlert("Info", "Your purchase was successful", {"Okay"})
  109.    
  110. end
  111.  
  112. --Purchase function
  113. buyCoins=function(event)
  114.    
  115.     --Place a progress spinner on screen and tell the user the app is contating the store
  116.     local spinnerBackground = display.newRect(160,240,360,600)
  117.     spinnerBackground:setFillColor(1,1,1,0.75)
  118.     --Spinner consumes all taps so the user cannot tap the purchase button twice
  119.     spinnerBackground:addEventListener("tap", function() return true end)
  120.     local spinnerText = display.newText("Contacting " .. iap.getStoreName() .. "...", 160,180, native.systemFont, 18)
  121.     spinnerText:setFillColor(0,0,0)
  122.     --Add a little spinning rectangle
  123.     local spinnerRect = display.newRect(160,260,35,35)
  124.     spinnerRect:setFillColor(0.25,0.25,0.25)
  125.     transition.to(spinnerRect, { time=4000, rotation=360, iterations=999999, transition=easing.inOutQuad})
  126.     --Create a group and add all these objects to it
  127.     spinner=display.newGroup()
  128.     spinner:insert(spinnerBackground)
  129.     spinner:insert(spinnerText)
  130.     spinner:insert(spinnerRect)
  131.    
  132.     --Tell IAP to initiate a purchase - the product name will be stored in target.product
  133.     iap.purchase(event.target.product, purchaseListener)
  134.    
  135. end
  136.  
  137. ---------------------------------
  138. --
  139. -- Main game code
  140. --
  141. ---------------------------------
  142.  
  143. --Remove status bar
  144. display.setStatusBar( display.HiddenStatusBar )
  145.  
  146. --Background
  147. local background = display.newRect(160,240,360,600)
  148. background:setFillColor({type="gradient", color1={ 0,0,0 }, color2={ 0,0,0.4 }, direction="down"})
  149.  
  150. --Draw "buy 50 coins" button
  151.     --Create button background
  152.     local buy50Background = display.newRect(240, 400, 150, 50)
  153.     buy50Background.stroke = { 0.5, 0.5, 0.5 }
  154.     buy50Background.strokeWidth = 2
  155.     --Create "buy IAP" text object
  156.     local buy50Text = display.newText("Buy 50 coins", buy50Background.x, buy50Background.y, native.systemFont, 18)
  157.     buy50Text:setFillColor(0,0,0)
  158.    
  159.     --Store the name of the product this button relates to
  160.     buy50Background.product="buy50coins"
  161.     --Create tap listener
  162.     buy50Background:addEventListener("tap", buyCoins)
  163.    
  164. --Draw "buy 100 coins" button
  165.     --Create button background
  166.     local buy100Background = display.newRect(80, 400, 150, 50)
  167.     buy100Background.stroke = { 0.5, 0.5, 0.5 }
  168.     buy100Background.strokeWidth = 2
  169.     --Create "buy IAP" text object
  170.     local buy100Text = display.newText("Buy 100 coins", buy100Background.x, buy100Background.y, native.systemFont, 18)
  171.     buy100Text:setFillColor(0,0,0)
  172.    
  173.     --Store the name of the product this button relates to
  174.     buy100Background.product="buy100coins"
  175.     --Create tap listener
  176.     buy100Background:addEventListener("tap", buyCoins)
  177.    
  178. --Text indicating how many coins the player currently holds
  179.  
  180. --Get how many coins are currently being held by the player
  181. local coinsHeld = iap.getInventoryValue("coins")
  182. --If no coins are held in the inventory, nil will be returned - this equates to no coins
  183. if (not coinsHeld) then coinsHeld=0 end
  184.  
  185. coinText = display.newText(coinsHeld .. " coins", 160, 20, native.systemFont, 18)
  186. coinText:setFillColor(1,1,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement