Easy In App Purchases
The Starters Core
kit comes with the Easy IAP
module. It is a layer on top of IAP Badger to make it even easier to set up In App Purchases.
Project Settings
Please see the Corona docs for setting up the config.lua and build.settings files.
Basic Usage
Step 1 - Require Once As Global
To simplify using the module, just require it once as a global.
_G.easyIAP = require "easy.iap.easyIAP"
Now, it is available for use everywhere you might want it.
Step 2 - Get IAP IDs
Acquire IDs for all IAP items you intend to sell from the Apple and/or Google Play console.
How to do this is outside the scope of these docs and you are assumed to know how to do this already.
Step 3 - Add Item(s)
Example 1 - No Ads Non-Consumable
local ids = { android = {}, ios = {} }
ids.android.noads = "com.yourcompany.appname.noads"
ids.ios.noads = "com.yourcompany.appname.noads"
local os = utils.os()
if( os == "android" or os == "ios" ) then
easyIAP.addItem( "noads", ids[os].noads, "non-consumable" )
end
Tip: Use all lower case for ids when you make them and use all lowercase for item names when you add them. This ensures you don't make errors later.
Once this step is complete and once you have initilized the module, two new functions will automatically be available for use:
easyIAP.buy_noads() - You can call this to buy the 'noads' IAP.
easyIAP.owns_noads() - You can call thos to see if you own the 'noads' IAP.
Example 2 - Consumable Coin Packs
local ids = { android = {}, ios = {} }
ids.android.coins100 = "com.yourcompany.appname.coins100"
ids.android.coins1000 = "com.yourcompany.appname.coins1000"
ids.android.coins5000 = "com.yourcompany.appname.coins5000"
ids.ios.coins100 = "com.yourcompany.appname.coins100"
ids.ios.coins1000 = "com.yourcompany.appname.coins1000"
ids.ios.coins5000 = "com.yourcompany.appname.coins5000"
local os = utils.os()
if( os == "android" or os == "ios" ) then
easyIAP.addItem( "coins100", ids[os].coins100, "consumable" )
easyIAP.addItem( "coins1000", ids[os].coins1000, "consumable" )
easyIAP.addItem( "coins5000", ids[os].coins5000, "consumable" )
end
Tip: Use all lower case for ids when you make them and use all lowercase for item names when you add them. This ensures you don't make errors later.
Once this step is complete and once you have initilized the module, six new functions will automatically be available for use:
-- Buy the items.
easyIAP.buy_coins100()
easyIAP.buy_coins1000()
easyIAP.buy_coins5000()
--
easyIAP.owns_coins100()
easyIAP.owns_coins1000()
easyIAP.owns_coins5000()
Step 4 - Initialize Easy Ads
easyIAP.init(
{
salt = "TypeSomeRandomStringHere",
testMode = true,
doNotLoadInventory = false,
} )
In this example, we passed three arguments:
salt
- Used by IAP badger to encrypt your IAP.json settomgs storage file.testMode
- A boolean that determines if we are running in test mode or production mode.doNotLoadInventory
- If you set this totrue
, the catalog is not reloaded next time the apps restarts. i.e. it is as if you did not purchase any non-consumable items.
Additional arguments you may want to use/pass are:
iapFilename
(iap.json
) - You can provide an alternate name for the settings file.onFail
- A reference to a common 'failed' listener. This is not required but may be convenient if you like to have all your code in one place.onCancel
- A reference to a common 'cancelled' listener. This is not required but may be convenient if you like to have all your code in one place.
Lastly, the init()
function takes a second argument: onCatalog
which references an optional listener. If you provide this argument, IAP badger will query the store and some time later return a table of all known products, names, their IDs, and their localized prices. This information will be passed into the listener as a table.
local function onCatalog( catalog )
utils.print_r( catalog )
end
easyIAP.init(
{
salt = "TypeSomeRandomStringHere",
testMode = true,
doNotLoadInventory = false,
}, onCatalog )
Automatically Generated Functions
As was mentioned above when describing 'adding items', adding a named item automatically creates two functions on the easyAds
module.
easyAds.addItem( <name>, storeIAPID )
generates,
easyAds.buy_<name>( onSuccess )
- Calling this will attempt to buy the product.onSuccess
is a function that will be called when the purchase attempt completes successfully.
easyAds.owns_<name>()
- When called, this will returntrue
if the item is a non-consumable and the user has purchased it.
Restoring
Finally, any time after initializing, you can attempto to restore all non-consumable items by calling:
easyAds.restore( [ onSuccess [ , onFailure ] ] )
onSuccess
is an optional function that will be called when the restore attempt completes successfully.onFailure
is an optional function that will be called when the restore attempt completes unsuccessfully.
Copyright © Roaming Gamer, LLC. 2008-2018; All Rights Reserved