Page Lock&Unlock with In App Purchase
In App Purchase
Preposition: Before you start this tutorial, create an IAP setting in iTune Connect/Apple Developer service or Google Play Developer console or Amazon developer service.
how to setup an IAP configuration
-
https://docs.coronalabs.com/guide/monetization/IAP/index.html
Kwik4 uses iap_badger from Happy Mongoose Games. It is a good IAP library for Solar2D. Please read this page as well
Please read Montization page and iap tools section too
In App Purchase - Adding Products

From Monetization tab, choose Simple Unlock as In-APP type,
- Simple Unlock
then please click plus(+) icon to add a product

Register a product name and ID by clicking pluse icon. Dummy IDs are OK. Later you set official IDs from apple, google or amazon in components/store/model.lua and build the app for device testing. Device testing allows you to connect to App store APIs with the sandbox environment.
Latest Kwik4 has each ID field of apple, google and amazon

- product01
- appleID: you can input dummy
- googleID:
- amazonID:
For example, an officail product ID should be like this
- com.kwiksher.app_name.product_id
Page Properites
You can specify the page to be locked from page properites

go down to In-App Unlock section

Set the product name and redirect page

Page structures
- page 1 normal
- page 2 buy/restore button
- page 3 locked page
- page 4 locked page
when user tries to go to page 3 from page 2 by swipping the screen, Kwik shows the page 3 for a second and instantly rewinds back to page2.
So page 4 is completely hidden but page 3, user can see the content for a moment.
Unlock & Restore Button
You need these two buttons. Restore button works if user renew his/her device or uninstall and re-install the app, and the purchased items are retrived from the database of Apple store.

Montization > Buy Product

-
Choose the product to be purchased. The button will be hidden when the transaction is completed.
Montization > Restore Product

Build for device
- components/store/model.lua
- components/store/IAP.lua
setting official product IDs
In components/store/model.lua, please set the product ID from Apple, Google and Amazon in model.lua.
iap tools section describes how to configure IAP products in their developer sites.
For example, “com.kwiksher.bookshelf.book01” is the prodcut id of iTune store.
M.catalogue = {
products = {
test = {
productNames = { apple="com.kwiksher.bookshelf.book01", google="test_googgle", amazon="test_amazon"},
productType = "non-consumable",
onPurchase = function() IAP.setInventoryValue("unlock_test") end,
onRefund = function() IAP.removeFromInventory("unlock_test") end,
},
},
inventoryItems = {
unlock_test = { productType="non-consumable" },
}
}
you may set “consumable” to productType if you desire. Please read the iap badger document.
http://happymongoosegames.co.uk/iapbadger.php
debug mode false

- IAP Debug mode should be unchecked for device testing
In components/store/model.lua, please change debug to false
local M = {}
--
local IAP = require("components.store.IAP")
--
M.bookShelfType = 0 --{none = -1, pages = 0, embedded = 1, tmplt=2}
--
M.debug = true
In components/store/IAP.lua, please change the text in salt field.
function M:init(catalogue, restoreAlert, purchaseAlert)
...
local iapOptions = {
catalogue = catalogue,
filename = "epsodes_inventory.txt",
--Salt for the hashing algorithm
salt = "something tr1cky to gue55!",
failedListener = failedListener,
cancelledListener = failedListener,
--Once the product has been purchased, it will remain in the inventory. Uncomment the following line
--to test the purchase functions again in future. It's also useful for testing restore purchases.
--doNotLoadInventory=true,
debugMode = debug,
}
Now Build for Device in Corona Simulator. Choose Code Signing Signature(iOS) or keystore(Android) for your IAP app. Don’t use normal debug signature or key store.
If user reaches the locked page, the dialog pops up. This comes from system, you don’t need to create it.

The purchase button will brign up the confirmation dialog too.

- In order to test restore, uninstall the app after purchased. Re-install again and select restore button.