Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to setup IAB in 2019? — Gideros Forum

How to setup IAB in 2019?

Apollo14Apollo14 Member
edited November 2019 in Code snippets
Hi guys!

I've never tried to setup in-app purchases for GooglePlay.
Some questions raised:
1) Why I can't add events PRODUCT_COMPLETE & PRODUCT_ERROR? (are they deprecated from old googlebilling plugin?)
2) What does 'Iab:confirm()' do? I totally don't get what is that.
3) Is that code enough to integrate in-app purchase in my release version?
Or something essential is missing?
if application:getDeviceInfo() == "Android" then
	require "iab"
 
	IAB_GOOGLE=IAB.new("google")
	IAB_GOOGLE:setUp("VERY_LONG_API_KEY_FROM_GOOGLEPLAY_CONSOLE")
	IAB_GOOGLE:setProducts({remove_ads = "remove_ads"}) 
	IAB_GOOGLE:isAvailable()
 
	IAB_GOOGLE:addEventListener(Event.AVAILABLE, function(event)
		print("Event.AVAILABLE")
		IAB_GOOGLE:requestProducts()
	end)
 
--trying to add these two events result in lua error:
	IAB_GOOGLE:addEventListener(Event.PRODUCT_COMPLETE, function(event)
		print("Event.PRODUCT_COMPLETE:", event.productId, event.title, event.description, event.price)
	end)
	IAB_GOOGLE:addEventListener(Event.PRODUCT_ERROR, function(event)
		print("Event.PRODUCT_ERROR:", event.error)
	end)
--init.lua:213: bad argument #1 to 'addEventListener' (string expected, got nil)
--stack traceback:
--	init.lua:213: in main chunk
 
	IAB_GOOGLE:addEventListener(Event.PURCHASE_COMPLETE, function(event)
		print("Event.PURCHASE_COMPLETE", event.productId, event.receiptId)
 
		--User successfully purchased "no_ads", now I gotta reward him:
		SETTINGS.adsAllowed=false; SETTINGS.interstitialAllowed=false
 
		IAB_GOOGLE:confirm(event.productId) --what does it do?
	end)
	IAB_GOOGLE:addEventListener(Event.PURCHASE_ERROR, function(event)
		print("Event.PURCHASE_ERROR:", event.error)
	end)
end
 
--after "NoAds" button pushed, I'll have to call:
--IAB_GOOGLE:purchase("remove_ads")
Many thanks, guys!
P. S. btw how can we make test transanction without paying money to google?
> Newcomers roadmap: from where to start learning Gideros
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
Tagged:

Comments

  • треба купити плагін у @hgy29
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • Apollo14Apollo14 Member
    edited November 2019
    oleg said:

    треба купити плагін у @hgy29

    What plugin? (i know only about firebase plugin, and my visa card is empty now :( )
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • Apollo14 said:

    oleg said:

    треба купити плагін у @hgy29

    What plugin? (i know only about firebase plugin, and my visa card is empty now :( )
    я можу помилятись та здається в firebase є можливість продаж..
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • SinisterSoftSinisterSoft Maintainer
    Accepted Answer
    This is my iab code from my game - it works fine for me.
    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</pre>
    To make a purchase I would:
    if iab then	iab:purchase("credit50")
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+3 / -0 )Share on Facebook
  • Apollo14Apollo14 Member
    edited November 2019
    Okay I was slightly misguided by old Iab example from wiki. https://wiki.giderosmobile.com/index.php/Iab
    @SinisterSoft I've replaced old example with your code B)

    Thanks a lot, guys! <3

    P. s. For some reason this stuff is not printed:<pre class="CodeBlock">
    iab:addEventListener(Event.PRODUCTS_COMPLETE,function(e)
    			print("Product list completed") --This line gets printed, but everything below does not (though I did setup products earlier:
    --iab:setProducts({remove_ads="remove_ads"})
    			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)
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+3 / -0 )Share on Facebook
  • @Apollo14 Glad to help. btw I can see that code in my browser. I'm using Brave browser (based on Chrome but better).

    Likes: Apollo14

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.