I try to test In App Purchase with google but it never called the “purchase” state.
When I call the purchase function the state is always “verificationFailed” but not “purchase” ?
What to do to solve the problem ?
Thank you.
local function storeListener(event)
if event.name == 'init' then
if not ( event.transaction.isError ) then
else -- Unsuccessful initialization; output error details
print( event.transaction.errorType )
print( event.transaction.errorString )
end
elseif ( event.name == "storeTransaction" ) then
if not ( event.transaction.state == "failed" ) then -- Successful transaction
local productID = transaction.productIdentifier
else
print( event.transaction.errorType )
print( event.transaction.errorString )
end
end
store.finishTransaction(event.transaction)
end
local productLists = {
['100diamonds'] = "com.google.bhb.iap.100diamonds",
['800diamonds'] = "com.google.bhb.iap.800diamonds",
['1500diamonds'] = "com.google.bhb.iap.1500diamonds",
}
local function purchase(product)
store.purchase( productLists[product.name] )
end
purchase("100diamonds")
considering that you initiated the store and loaded the products then you should make the purchase by passing productIdentifier with the purchase method
local IAPList={}
local realIAPList={}
local IAPListT={}
local function onLoadProducts( event )
for i = 1,#event.products do
realIAPList[i]=event.products[i].productIdentifier
IAPListT[i]=event.products[i].title.." "..event.products[i].localizedPrice
end
-- print( "Valid products:", #event.products )
-- print( "Invalid products:", #event.invalidProducts )
end
local function transactionListenerAndroid( event )
if ( event.name == "init" ) then
if not ( event.transaction.isError ) then
if (platformName=="android" or platformName=="iphone os" or platformName=="mac os x" or platformName=="tvos") and store.isActive then
if store.canLoadProducts then
store.loadProducts( IAPList, onLoadProducts )
end
end
else -- Unsuccessful initialization; output error details
native.showAlert( "Info", tostring(event.transaction.errorType) .. "\n"..event.transaction.errorString ,{"Ok"} )
end
-- Store transaction event
elseif ( event.name == "storeTransaction" ) then
if event.transaction.state == "purchased" then -- Successful transaction
store.consumePurchase( currItem)
elseif event.transaction.state == "restored" then -- Successful transaction
store.consumePurchase( currItem)
else -- Unsuccessful transaction; output error details
native.showAlert( "Info", "Unable to complete purchase" ,{"Ok"} )
end
end
end
local function transactionListeneriOS( event )
if event.transaction.state == "purchased" then -- Successful transaction
-- print( json.prettify( event ) )
-- print( "event.transaction: " .. json.prettify( event.transaction ) )
elseif event.transaction.state == "restored" then -- Successful transaction
store.consumePurchase( currItem)
else -- Unsuccessful transaction; output error details
native.showAlert( "Info", "Unable to complete purchase" ,{"Ok"} )
end
-- Tell the store that the transaction is finished
store.finishTransaction( event.transaction )
end
local function loadiOSProducts()
if store.canLoadProducts then
store.loadProducts( IAPList, onLoadProducts )
end
end
-- Initialize IAP
if platformName=="android" then
store.init( transactionListenerAndroid )
elseif platformName=="iphone os" or platformName=="mac os x" or platformName=="tvos" then
store.init( transactionListeneriOS )
timer.performWithDelay( 3000, loadiOSProducts )
end
--somewhere in your code
store.purchase( realIAPList[ID_Of_Product] )
Hello thanks for the response but I have try it with google iap billing but it doesn’t work.
Is the function store.loadProducts mandatory to call on the init event ?
Thank you.
Hello
loadProduct is only usefull to display the list of products.
I have test with and without loadProduct and on a smartphone where the main google account of the device and the google account of the play store are the same.
The state after clicking on buying test iap is always “verificationFailed”.
Ok I find the solution : The app needed the licence key inside the config file for the verification of the purchases (otherwise it is “verificationFailed”).
The licence key can be founded inside the google game account on the section “Monetize > Configuration of monetization > Licence”