Transaction Callback For More In App Products

Hello,

I just followed the tutorial about Understanding In App Purchases

There was used In App Product to remove ads (banners) from game. I have also implemented the same product, tested it and it works fine. When transaction was successful I saved that info in the table mySettings which is later saved in json file (same as in the above tutorial). 

But, I will need one more product to unlock more characters in the game and in the tutorial while saving purchase info author used one variable:

mySettings.isPaid = true

I will save info for every purchased product so I will need 2 variables in table mySettings:

mySettings.removeAds = true mySettings.moreCharacters = true

The problem is the following:

In the transactionCallback method it checks the state and saves variable in the table. For eg. if it’s purchased it will save mySettings.isPaid as true.

 

This whole code is intended to use for only one product and I need it for more products (removeAds and moreCharacters). What do I need to change to make it work? Do I need to add some other booleans and then check them (before checking tstate) to know which product is purchased.

I need this information because I am storing it in json table so I’ll know what one has purchased.

 

PS, take a look on the transactionCallback method in the above tutorial .

Thanks and kindly Regards!

It’s easy to do.  Each in-app item has its own product identifier.  So you will have two purchase buttons: one to remove ads and one to add more characters.  Each purchase button will purchase a different product identifier.

In the transactionCallback function, add this line after the local tstate line:

local tproductid = event.transaction.productIdentifier

Then, in the if/elseif/else block of the transactionCallback function, update the code to first determine which product identifier was purchased.

For example, the purchased code block would change from this:

 if tstate == "purchased" then print("Transaction succuessful!") mySettings.isPaid = true utility.saveTable(mySettings, "settings.json") native.showAlert("Thank you!", "Your support is greatly appreciated!", {"Okay"}) store.finishTransaction( transaction )

to this:

 if tstate == "purchased" then print("Transaction successful!") if tproductid == "com.mycompany.myapp.removeads" then mySettings.removeAds = true elseif tproductid == "com.mycompany.myapp.morecharacters" then mySettings.moreCharacters = true end utility.saveTable(mySettings, "settings.json") native.showAlert("Thank you!", "Your support is greatly appreciated!", {"Okay"}) store.finishTransaction( transaction )

Thanks! This is exactly what I needed! 

Glad that worked for you. 

You should also do the same thing for the restored block for all stores.  And if you are doing the Google Play and Amazon stores, then you should use the same logic for the refunded and revoked blocks as well.

I have one more question regarding this topic, but now about loadProductsListener() method. Do I need to modify some parts of current code (from the tutorial) to this:

store.loadProducts({"removebanners", "unlockcharacter"}, loadProductsListener)

if store.availableStores.apple and (mySettings.removeAds or mySettings.unlockCharacters) then

Thanks.

In the store.loadProducts function, you need to load the different products that you have available as an in-app purchase.  You can see this more clearly in the product docs:

http://docs.coronalabs.com/api/library/store/loadProducts.html

As for the if block, in Rob’s example, he doesn’t kick off the store functionality for the iTunes store if the product is already marked as purchased.  In your case, you have two different products, so the only time you would likely not want to init the store is if both are marked as purchased, so:

if store.availableStores.apple and not (mySettings.removeAds and mySettings.unlockCharacters) then

That code didn’t worked because there has to be or in the brackets:

if store.availableStores.apple and not (mySettings.removeAds or mySettings.unlockCharacters) then

Thanks!

The whole not thing always gets me lost when there are multiple things.   :slight_smile:

It’s easy to do.  Each in-app item has its own product identifier.  So you will have two purchase buttons: one to remove ads and one to add more characters.  Each purchase button will purchase a different product identifier.

In the transactionCallback function, add this line after the local tstate line:

local tproductid = event.transaction.productIdentifier

Then, in the if/elseif/else block of the transactionCallback function, update the code to first determine which product identifier was purchased.

For example, the purchased code block would change from this:

 if tstate == "purchased" then print("Transaction succuessful!") mySettings.isPaid = true utility.saveTable(mySettings, "settings.json") native.showAlert("Thank you!", "Your support is greatly appreciated!", {"Okay"}) store.finishTransaction( transaction )

to this:

 if tstate == "purchased" then print("Transaction successful!") if tproductid == "com.mycompany.myapp.removeads" then mySettings.removeAds = true elseif tproductid == "com.mycompany.myapp.morecharacters" then mySettings.moreCharacters = true end utility.saveTable(mySettings, "settings.json") native.showAlert("Thank you!", "Your support is greatly appreciated!", {"Okay"}) store.finishTransaction( transaction )

Thanks! This is exactly what I needed! 

Glad that worked for you. 

You should also do the same thing for the restored block for all stores.  And if you are doing the Google Play and Amazon stores, then you should use the same logic for the refunded and revoked blocks as well.

I have one more question regarding this topic, but now about loadProductsListener() method. Do I need to modify some parts of current code (from the tutorial) to this:

store.loadProducts({"removebanners", "unlockcharacter"}, loadProductsListener)

if store.availableStores.apple and (mySettings.removeAds or mySettings.unlockCharacters) then

Thanks.

In the store.loadProducts function, you need to load the different products that you have available as an in-app purchase.  You can see this more clearly in the product docs:

http://docs.coronalabs.com/api/library/store/loadProducts.html

As for the if block, in Rob’s example, he doesn’t kick off the store functionality for the iTunes store if the product is already marked as purchased.  In your case, you have two different products, so the only time you would likely not want to init the store is if both are marked as purchased, so:

if store.availableStores.apple and not (mySettings.removeAds and mySettings.unlockCharacters) then

That code didn’t worked because there has to be or in the brackets:

if store.availableStores.apple and not (mySettings.removeAds or mySettings.unlockCharacters) then

Thanks!

The whole not thing always gets me lost when there are multiple things.   :slight_smile: