implementing in-app purchases for android devices

local store = require( "plugin.google.iap.v3") local storeMod = {} function storeMod.transactionListener( event ) -- body local tstate = event.transaction.state local transaction = event.transaction local tproductId = event.transaction.productIdentifier if tstate == "purchased" then if tproductId=="" then elseif tproductId== "" then end store.finishTransaction( transaction ) elseif tstate == "cancelled" then store.finishTransaction( transaction ) elseif tstate=="failed" then store.finishTransaction( transaction ) else print( "unknown event" ) store.finishTransaction( transaction ) end end function storeMod.startStore( platform ) -- body store.init( platform, storeMod.transactionListener ) end function storeMod.startPurchase( productId ) if store.canMakePurchases then store.purchase( productId ) end end return storeMod

Hi, I’m new to corona’s in-app purchasing. I just want to confirm whether the above mentioned code is enough and efficient for google in-app purchasing or something else i need to include or any corrections in that. Thanks in advance

Its minimalistic and of course your listener function doesn’t do anything with its events, but that seems to have the minimal basics covered. You’re not dealing with restore events, refunds, storing your receipts, unlocking anything, loading products, etc. But for the purpose of flow, its the bare minimum.

Rob

I just made a model for google iap. I’ll write the code for unlocking levels in purchased event later. I want the code for restoring purchases.

Sorry about my english

can you please post the code or link for restoring purchases in android devices?

i found this code in a website. How about this code

local store = nil local platform = system.getInfo("targetAppStore") if platform == "apple" then store = require ("store") elseif platform == "google" then store = require("plugin.google.iap.v3") elseif platform == "amazon" then store = require ("plugin.amazon.iap") end local function StoreListener(event) local transaction = event.transaction if transaction == "purchased" then if isGoogleRestore then -- Restore purchase code goes here else -- successful purchase. Give user what they paid for end elseif transaction = "restored" then -- Apple or Amazon restore code goes here elseif transaction == "cancelled" then -- User decided to cancel purchase elseif transaction == "failed" then -- D'oh, something went wrong. User could not purchase item. end store.finishTransaction(transaction) end if platform == "amazon" then store.init(StoreListener) else store.init(platform, StoreListener) end -- Enter your purchase string here local itemToPurchase = "com.mydomain.item" -- make a purchase if platform == "apple" then store.purchase({itemToPurchase}) else store.purchase(itemToPurchase) end -- define this variable local isGoogleRestore -- place this in your restore button handler if platform == "google then isGoogleRestore = true else isGoogleRestore = false end store.restore()

You might be better off using IAP Badger instead of attempting to code this from scratch:

https://forums.coronalabs.com/topic/59137-iap-badger-a-unified-approach-to-in-app-purchases/

i think i don’t need to change anything in iap_badger.lua, right?

Just follow the IAP badger documentation and you will be all set.

Thank you  :slight_smile:  it’s really simple and useful

i can’t use this for windows phone games?

Hi Vimaleee1994,

No - I’m afraid at the moment IAP Badger does not support IAP on Windows Phone. This is because IAP Badger is just a wrapper class for the various lua store libraries exposed by the Corona SDK (which is different to the libraries offered by Corona Cards).

Windows Phone IAP integration in Corona Cards requires some really specific code and C# project build settings to make IAP work (see here for more info). This would just break something like IAP Badger, which works at a much higher level.

Of course, if the guys at Corona ever expose a lua interface for the Windows store front, I’ll look at updating the libraries.

Simon.

PS - If anyone thinks I’ve misunderstood the Corona Cards Window Phone documentation, I’d be happy to look at this again.

game is crashing while clicking the store button which opens the store scene. Not at the first click but after i played the game for some times or when i enter the store scene multiple times. what is the problem

Its minimalistic and of course your listener function doesn’t do anything with its events, but that seems to have the minimal basics covered. You’re not dealing with restore events, refunds, storing your receipts, unlocking anything, loading products, etc. But for the purpose of flow, its the bare minimum.

Rob

I just made a model for google iap. I’ll write the code for unlocking levels in purchased event later. I want the code for restoring purchases.

Sorry about my english

can you please post the code or link for restoring purchases in android devices?

i found this code in a website. How about this code

local store = nil local platform = system.getInfo("targetAppStore") if platform == "apple" then store = require ("store") elseif platform == "google" then store = require("plugin.google.iap.v3") elseif platform == "amazon" then store = require ("plugin.amazon.iap") end local function StoreListener(event) local transaction = event.transaction if transaction == "purchased" then if isGoogleRestore then -- Restore purchase code goes here else -- successful purchase. Give user what they paid for end elseif transaction = "restored" then -- Apple or Amazon restore code goes here elseif transaction == "cancelled" then -- User decided to cancel purchase elseif transaction == "failed" then -- D'oh, something went wrong. User could not purchase item. end store.finishTransaction(transaction) end if platform == "amazon" then store.init(StoreListener) else store.init(platform, StoreListener) end -- Enter your purchase string here local itemToPurchase = "com.mydomain.item" -- make a purchase if platform == "apple" then store.purchase({itemToPurchase}) else store.purchase(itemToPurchase) end -- define this variable local isGoogleRestore -- place this in your restore button handler if platform == "google then isGoogleRestore = true else isGoogleRestore = false end store.restore()

You might be better off using IAP Badger instead of attempting to code this from scratch:

https://forums.coronalabs.com/topic/59137-iap-badger-a-unified-approach-to-in-app-purchases/

i think i don’t need to change anything in iap_badger.lua, right?