Iab

From GiderosMobile
Revision as of 16:25, 30 January 2020 by MoKaLux (talk | contribs)

Interface usage is quite simple:

  • IAB.new(iabframework) -- initialize specified IAB framework

Available frameworks: 'amazon', 'google', 'ios'

  • IAB:setUp(value1, value2, ..., valueN) -- provide all the keys/devids/appids/etc needed for the framework, which you will get in same order on Native part in Map object
  • IAB:setProducts({internalId1 = "storeId1", internalId2 = "storeId2", ..., internalIdn = "storeIdn"}) -- provide list of products you will be using (key is your internal products id, value is store specific product id)
  • IAB:setConsumables({"internalId1", "internalId2", ..., "internalIdn"}) -- which of your products are consumables, that people can buy many times (provide internal product ids)
  • IAB:isAvailable() --check if IAB is available, raises event:
    • Event.AVAILABLE --iab is available
  • IAB:requestProducts() --request information for products provided in setProducts method
    • Event.PRODUCTS_COMPLETE
      • event.productId
      • event.title
      • event.description
      • event.price
    • Event.PRODUCTS_ERROR
      • event.error
  • IAB:purchase(prodId) --purchase product by providing your internal product id, raises two events:
    • Event.PURCHASE_COMPLETE
      • event.productId
      • event.receiptId
    • Event.PURCHASE_ERROR
      • event.error
  • iab:restore() --raise purchase events for each previously purchases entitled (not consumed) items, raises two events:
    • Event.RESTORE_COMPLETE
    • Event.RESTORE_ERROR
      • event.error
Code example:
if not tv then
	pcall(function() require "iab" end)
end
if IAB then
	if android then
		if kindle then
			iab=IAB.new("amazon")
			iab:setProducts({credit50="credit_50",credit100="credit_100",credit500="credit_500",credit1000="credit_1000"})
		else
			iab=IAB.new("google")
			iab:setUp(googleLicence)
			iab:setProducts({credit50="credit50",credit100="credit100",credit500="credit500",credit1000="credit1000"})
		end
	elseif ios then
		iab=IAB.new("ios")
		iab:setProducts({credit50="credit50",credit100="credit100",credit500="credit500",credit1000="credit1000"})
	end
 
	if iab then
		iab:setConsumables({"credit50","credit100","credit500","credit1000"})
		iab:addEventListener(Event.AVAILABLE,function(e)
			print("iab is available")
			iabCheck=false
			iab:requestProducts()
		end)
 
		--usually here we would set a flag that it is possible to make purchases
		--basically you can allow doing all the iap stuff after this event is called
 
		--[[ REQUESTING PRODUCTS ]]--
		--if this event is called, we received the list of products and information about them
		iab:addEventListener(Event.PRODUCTS_COMPLETE,function(e)
			print("Product list completed")
			for i = 1, #e.products do
				local p=e.products[i]
				--id, title, description and price
				print(p.productId,p.title,p.description,p.price)
				if p.productId=="fullgame" then
					iabCheck=true
					iabFullGame=false
				end
				for loop=1,#creditText do
					local c=creditText[loop]
					if p.productId==c[1] then
						c[2]=p.price
						c[3]=true
					end
				end
			end
			iab:restore()
		end)
 
		--else we could not retrieve information about products now
		iab:addEventListener(Event.PRODUCTS_ERROR,function(e)
			print(e:getType(),e.error)
		end)
 
		iab:addEventListener(Event.PURCHASE_COMPLETE,function(e)
			--purchases successfully completed 
			--here you need to check if receiptId was not already saved previously
			--as in if purchase was not already made
			--then you can unlock the item here
			--and store receiptId presistently to know 
			--that you have already provided this item to user
			print("Purchase completed")
			if remainingCredits<0 then remainingCredits=0 end
			if e.productId=="credit50" then
				remainingCredits+=50
				saveSettings()
			elseif e.productId=="credit100" then
				remainingCredits+=100
				saveSettings()
			elseif e.productId=="credit500" then
				remainingCredits+=500
				saveSettings()
			elseif e.productId=="credit1000" then
				remainingCredits+=1000
				saveSettings()
			elseif e.productId=="fullgame" then
				iabCheck=true
				iabFullGame=true
			end
			print(e:getType(),e.productId,e.receiptId)
		end)
 
		iab:addEventListener(Event.PURCHASE_ERROR,function(e)
			--it was not possible to complete the purchase, 
			--inform user
			--if e.error=="Unable to buy item (response: 7:Item Already Owned)" then
				iab:isAvailable()
			--end
	--		print(e:getType(),e.error)
		end)
 
		iab:addEventListener(Event.RESTORE_COMPLETE,function(e)
			--restore successfully completed 
			--here you need to check if receiptId was not already saved previously
			--as in if purchase was not already made
			--then you can unlock the item here
			--and store receiptId presistently to know 
			--that you have already provided this item to user
			if iabCheck then
				fullgame=iabFullGame
				saveSettings()
			end
			print("restore complete")
			--iab:purchase("credit500")
		end)
 
		iab:addEventListener(Event.RESTORE_ERROR,function(e)
			--it was not possible to complete the restore, 
			--inform user
		--	print(e:getType(),e.error)
 
		end)
		iab:isAvailable() 
	--		iab:purchase("credit100")
	else	
		print("iab provider not available")
	end
else
	print("iab not available")
end

--To make a purchase I would:
--if iab then	iab:purchase("credit50") end