Guest User

Untitled

a guest
Jan 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. local store = require("store")
  2.     local listOfProducts = {
  3.        "com.yourcompany.app.productID",  
  4.     }
  5.     ----------------------------------
  6.     -- In-App area
  7.     ----------------------------------
  8.     local validProducts, invalidProducts = {}, {}
  9.    
  10.     function unpackValidProducts()
  11.         print ("Loading product list")
  12.         if not validProducts then
  13.             native.showAlert( "In-App features not available", "initStore() failed", { "OK" } )        
  14.         else
  15.             for i=1, #invalidProducts do
  16.                 native.showAlert( "Item " .. invalidProducts[i] .. " is invalid.",{ "OK" } )
  17.             end
  18.  
  19.         end
  20.     end
  21.    
  22.     function loadProductsCallback( event )
  23.         validProducts = event.products
  24.         invalidProducts = event.invalidProducts    
  25.         unpackValidProducts ()
  26.    
  27.         end
  28.        
  29.         function savePurchase(product)
  30.                 --function on what should be save when user buy a product
  31.         end
  32.  
  33.         function transactionCallback( event )
  34.         if event.transaction.state == "purchased" then
  35.            savePurchase("product") --you should enter here the product being purchased
  36.         elseif event.transcation.state == "restored" then  
  37.            savePurchase("product") --you should enter here the product being purchased
  38.         elseif event.transaction.state == "cancelled" then
  39.         elseif event.transaction.state == "failed" then        
  40.             infoString = "Transaction failed, type: ", event.transaction.errorType, event.transaction.errorString
  41.             local alert = native.showAlert("Failed ", infoString,{ "OK" })
  42.         else
  43.             infoString = "Unknown event"
  44.             local alert = native.showAlert("Unknown ", infoString,{ "OK" })
  45.         end
  46.         store.finishTransaction( event.transaction )
  47.         end
  48.  
  49.         function setupMyStore (event)
  50.                 store.loadProducts( listOfProducts, loadProductsCallback )
  51.         end
  52.  
  53.  
  54.   local onbuyButTouch = function(event)
  55.           if event.phase=="ended" then  
  56.             local buyThis = function ( product )
  57.                if store.canMakePurchases then
  58.                   store.purchase( {product} )
  59.                else  
  60.                   native.showAlert("Store purchases are not available, please try again later",  { "OK" } )
  61.                end  
  62.             end
  63.             -- Enter your product id here
  64.  
  65. store.init(transactionCallback)
  66.    timer.performWithDelay (1000, setupMyStore)
  67.             buyThis ("com.youcompany.app.productID")  
  68.           end
  69.        end
  70.        buyBut = ui.newButton{
  71.            defaultSrc="p1_buyBut.png",
  72.            defaultX = 272,
  73.            defaultY = 68,
  74.            overSrc="p1_buyBut.png",
  75.            overX = 272,
  76.            overY = 68,
  77.            onRelease=onbuyButTouch,
  78.            id="buyButButton"
  79.        }
Add Comment
Please, Sign In to add comment