Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Android IAP Problem — Gideros Forum

Android IAP Problem

edited August 2014 in General questions
Hello all,

i have following code in my game but it is not working when i test this on my device with "android.test.purchased" i successfully get AlertDialog.new("title","successsfully purchased","ok"):show() as notice but code is never entering into onPurchaseStateChange(event) what i am doing wrong here please advice
require "googlebilling"
 
 
 function onRequestPurchaseComplete(event)
    if (event.responseCode == GoogleBilling.OK) then
	AlertDialog.new("title","successsfully purchased","ok"):show()
		-- transaction has been sent to google (thanks Atilim)
		-- don't unlock items here
	else
		local msg = "purchase failed"
		if (event.responseCode == GoogleBilling.USER_CANCELED) then
			msg = "GoogleBilling.USER_CANCELED"
		end
		if (event.responseCode == GoogleBilling.SERVICE_UNAVAILABLE) then
			msg = "GoogleBilling.SERVICE_UNAVAILABLE"
		end
		if (event.responseCode == GoogleBilling.BILLING_UNAVAILABLE) then
			msg = "GoogleBilling.BILLING_UNAVAILABLE"
		end
		if (event.responseCode == GoogleBilling.ITEM_UNAVAILABLE) then
			msg = "GoogleBilling.ITEM_UNAVAILABLE"
		end
		if (event.responseCode == GoogleBilling.DEVELOPER_ERROR) then
			msg = "GoogleBilling.DEVELOPER_ERROR"
		end
		if (event.responseCode == GoogleBilling.ERROR) then
			msg = "GoogleBilling.ERROR"
		end
 
		print(msg)
    end
end
 
--------------------------------------------------
-- GOOGLE Billing
-- <a href="http://forum.gideros.rocks/profile/param" rel="nofollow">@param</a> event
-- <a href="http://forum.gideros.rocks/profile/return" rel="nofollow">@return</a>
--------------------------------------------------
 function onPurchaseStateChange(event)
	AlertDialog.new(event.purchaseState,event.productId,"ok"):show()
 
	if (event.purchaseState == GoogleBilling.CANCELED) then
            if (event.productId == "your_product_001") then
			-- lock or don't unlock
		elseif (event.productId == "your_product_002") then
			-- lock or don't unlock
		elseif (event.productId == "android.test.purchased") then
			AlertDialog.new("cancelled","android.test.purchased","ok"):show()
		end
	elseif (event.purchaseState == GoogleBilling.PURCHASED) then
            if (event.productId == "your_product_001") then
			-- unlock 
			-- show Message, item bought
		elseif (event.productId == "your_product_002") then
			-- unlock 
			-- show Message, item bought
		elseif (event.productId == "android.test.purchased") then
				AlertDialog.new("purchased","android.test.purchased","ok"):show()
			-- unlock 
			-- show Message, item bought
		end
	elseif (event.purchaseState == GoogleBilling.REFUNDED) then
            if (event.productId == "your_product_001") then
			-- lock or don't unlock
		elseif (event.productId == "your_product_002") then
			-- lock or don't unlock
		elseif (event.productId == "android.test.purchased") then
 
				AlertDialog.new("refunded","android.test.purchased","ok"):show()
			-- lock or don't unlock
		end
	elseif (event.purchaseState == GoogleBilling.EXPIRED) then
		-- for subscriptions
	else
		-- unknown state
	end
 
	googlebilling:confirmNotification(event.notificationId)
end
 
googlebilling:setPublicKey("longlongkey")
googlebilling:addEventListener(Event.REQUEST_PURCHASE_COMPLETE, onRequestPurchaseComplete)
googlebilling:addEventListener(Event.PURCHASE_STATE_CHANGE, onPurchaseStateChange)

Comments

  • i am calling request purchase on button click in the same file as below
    local function onPurchseTommy(self,event)
    					if self:hitTestPoint(event.x,event.y) then
     
    						  googlebilling:requestPurchase("android.test.purchased")
     
    					end
    				end
    				myImg:addEventListener(Event.MOUSE_DOWN,onPurchseTommy,myImg)
  • I had also uploaded apk to playstore it is in draft mode currently and it has been almost 24 hours till i had uploaded. i am also using signed apk to test in my device
  • The code seems to be correct

    where is the whole require "googlebilling" etc code is palced?

    It is better to place it in main.lua for loading and accessibility purpose

    also do you use the same apk file that you uploaded to Google Play?
    (the package name and certificate signature must match)/

  • The same thing happened to me. I never got onPurchaseStateChange triggered. What I did, despite the code comment saying not to do so, is to test for the purchase in the onRequestPurchaseComplete function. If you have more than one item you can reference it by using event.productId as you do in the other event. I only have one, so I didn't have a problem with that.

    What I inferred (as obvious as it looks) from these two functions is that onRequestPurchaseComplete is called when you purchase the item, and onPurchaseStateChange is called when the user changes the purchase by cancelling, refunding or some of the other states you can find there. Therefore you won't have that in your tests because you are testing if the item was purchased, not if the user cancelled afterwards, etc. You may do so, but as for testing the purchase itself I think it should be in the other function. However, I've only been able to test it using the "test" item so I might be completely wrong if this changes when using real items.
Sign In or Register to comment.