# implementing in-app purchases for android devices

**URL:** <https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779>\
**Category:** Newbie Questions\
**Created:** [April 6, 2016, 10:11am UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779 "2016-04-06T10:11:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 6, 2016, 10:11am UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/1 "2016-04-06T10:11:03Z")

</div>

```
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

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [April 6, 2016, 4:24pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/2 "2016-04-06T16:24:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 6, 2016, 4:37pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/3 "2016-04-06T16:37:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 6, 2016, 4:38pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/4 "2016-04-06T16:38:06Z")

</div>

Sorry about my english

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 7, 2016, 11:07am UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/5 "2016-04-07T11:07:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 7, 2016, 2:36pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/6 "2016-04-07T14:36:46Z")

</div>

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()
```

---

<div class="post-metadata">

**Author:** ![Alex10](https://forums.solar2d.com/user_avatar/forums.solar2d.com/alex10/32/66_2.png) [@Alex10](https://forums.solar2d.com/u/Alex10)\
**Post date:** [April 7, 2016, 2:57pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/7 "2016-04-07T14:57:22Z")

</div>

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/](https://forums.coronalabs.com/topic/59137-iap-badger-a-unified-approach-to-in-app-purchases/)

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 7, 2016, 3:22pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/8 "2016-04-07T15:22:55Z")

</div>

i think i don’t need to change anything in iap\_badger.lua, right?

---

<div class="post-metadata">

**Author:** ![Alex10](https://forums.solar2d.com/user_avatar/forums.solar2d.com/alex10/32/66_2.png) [@Alex10](https://forums.solar2d.com/u/Alex10)\
**Post date:** [April 7, 2016, 3:31pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/9 "2016-04-07T15:31:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 7, 2016, 3:37pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/10 "2016-04-07T15:37:30Z")

</div>

Thank you&nbsp; 🙂 &nbsp;it’s really simple and useful

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 7, 2016, 3:55pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/11 "2016-04-07T15:55:14Z")

</div>

i can’t use this for windows phone games?

---

<div class="post-metadata">

**Author:** ![happymongoose](https://forums.solar2d.com/user_avatar/forums.solar2d.com/happymongoose/32/438_2.png) [@happymongoose](https://forums.solar2d.com/u/happymongoose)\
**Post date:** [April 7, 2016, 11:11pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/12 "2016-04-07T23:11:37Z")

</div>

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](https://docs.coronalabs.com/coronacards/wp8/portapp.html)&nbsp;for more info).&nbsp;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.

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 16, 2016, 10:53am UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/13 "2016-04-16T10:53:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [April 6, 2016, 4:24pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/14 "2016-04-06T16:24:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 6, 2016, 4:37pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/15 "2016-04-06T16:37:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 6, 2016, 4:38pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/16 "2016-04-06T16:38:06Z")

</div>

Sorry about my english

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 7, 2016, 11:07am UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/17 "2016-04-07T11:07:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 7, 2016, 2:36pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/18 "2016-04-07T14:36:46Z")

</div>

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()
```

---

<div class="post-metadata">

**Author:** ![Alex10](https://forums.solar2d.com/user_avatar/forums.solar2d.com/alex10/32/66_2.png) [@Alex10](https://forums.solar2d.com/u/Alex10)\
**Post date:** [April 7, 2016, 2:57pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/19 "2016-04-07T14:57:22Z")

</div>

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/](https://forums.coronalabs.com/topic/59137-iap-badger-a-unified-approach-to-in-app-purchases/)

---

<div class="post-metadata">

**Author:** ![vimaleee1994](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vimaleee1994](https://forums.solar2d.com/u/vimaleee1994)\
**Post date:** [April 7, 2016, 3:22pm UTC](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779/20 "2016-04-07T15:22:55Z")

</div>

i think i don’t need to change anything in iap\_badger.lua, right?

[Next page](https://forums.solar2d.com/t/implementing-in-app-purchases-for-android-devices/338779.md?page=2)
