# plugin.google.iap.billing TransactionListener Sample?

**URL:** <https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404>\
**Category:** Monetization (in-app purchases, ads, etc.)\
**Created:** [April 17, 2021, 5:14am UTC](https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404 "2021-04-17T05:14:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![d.mach](https://forums.solar2d.com/user_avatar/forums.solar2d.com/d.mach/32/58_2.png) [@d.mach](https://forums.solar2d.com/u/d.mach)\
**Post date:** [April 17, 2021, 5:14am UTC](https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404/1 "2021-04-17T05:14:19Z")

</div>

I wonder if there is a new sample code available somewhere for the new version of the TransactionListener with all the changes to the new plugin?

Is anybody having a sample for a correct new TransactionListener and some information on how a purchase now has to be made to really make it work?

Any help welcome!

---

<div class="post-metadata">

**Author:** ![kbradford](https://forums.solar2d.com/user_avatar/forums.solar2d.com/kbradford/32/2146_2.png) [@kbradford](https://forums.solar2d.com/u/kbradford)\
**Post date:** [April 19, 2021, 6:02pm UTC](https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404/2 "2021-04-19T18:02:55Z")

</div>

Is this not what you’re looking for: [https://docs.coronalabs.com/plugin/google-iap-billing/init.html](https://docs.coronalabs.com/plugin/google-iap-billing/init.html)

Really not much changed in the plug-in (thankfully!), just what’s noted under the “Compatiblity with IAPv3” here: [https://docs.coronalabs.com/plugin/google-iap-billing/index.html](https://docs.coronalabs.com/plugin/google-iap-billing/index.html)

Is there a specific value/case you’re looking to handle?

---

<div class="post-metadata">

**Author:** ![chodongwon82](https://forums.solar2d.com/user_avatar/forums.solar2d.com/chodongwon82/32/3928_2.png) [@chodongwon82](https://forums.solar2d.com/u/chodongwon82)\
**Post date:** [April 19, 2021, 6:11pm UTC](https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404/3 "2021-04-19T18:11:00Z")

</div>

I just changed the two lines below from the old sample.

build.settings

```
-- ["plugin.google.iap.v3"] = {
["plugin.google.iap.billing"] = {

```

main.lua

```
-- store = require( "plugin.google.iap.v3" )
store = require( "plugin.google.iap.billing" )
```

---

<div class="post-metadata">

**Author:** ![d.mach](https://forums.solar2d.com/user_avatar/forums.solar2d.com/d.mach/32/58_2.png) [@d.mach](https://forums.solar2d.com/u/d.mach)\
**Post date:** [April 20, 2021, 7:40am UTC](https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404/4 "2021-04-20T07:40:00Z")

</div>

Thanks for your fast feedback. I knew the links but missing some explicit samples for those parts:

- [storeTransaction.transaction](https://docs.coronalabs.com/plugin/google-iap-billing/event/storeTransaction/transaction.html) event have additional values of `state` field.

What are the additional values of the state field and are there code samples on how to use them?  
For example: “pending”… isn’t this messing with old code? And what is the new “restoreCompleted” for exactly when a restore is also handled in the purchase state?

---

<div class="post-metadata">

**Author:** ![kbradford](https://forums.solar2d.com/user_avatar/forums.solar2d.com/kbradford/32/2146_2.png) [@kbradford](https://forums.solar2d.com/u/kbradford)\
**Post date:** [April 20, 2021, 7:33pm UTC](https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404/5 "2021-04-20T19:33:54Z")

</div>

As for “restoreCompleted”, if you call restore() you get flooded with every purchase transaction. For most apps that may be 1 or 2. But for something like a monthly subscription, you’ll get EVERY transaction which could be dozens and dozens (one for every month). So the restoreCompleted transaction comes in at the end, to essentially tell you that it’s sent everything it can find.

Not sure on “pending”. Our subscription code only looks for “purchased”, “cancelled”, or “failed” and anything else it just ignores.

---

<div class="post-metadata">

**Author:** ![d.mach](https://forums.solar2d.com/user_avatar/forums.solar2d.com/d.mach/32/58_2.png) [@d.mach](https://forums.solar2d.com/u/d.mach)\
**Post date:** [June 10, 2021, 12:11pm UTC](https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404/6 "2021-06-10T12:11:23Z")

</div>

Thanks for your feedback.

I have a strange problem with my code: I am using the same purchase code from another game where it is working perfectly. I have looked through the code 10 times and it really is identical in usage but somehow I now do not get into the part of the transaction listener which is handling the purchased state.  
I first thought it would be the new plugin for billing but it also happened with the old one.

Now I wonder if the “new” states like for example “pending” were sent and catched by the transactionListener with the old plugin also?

I am finishing the Transaction Listener for all states… so does this mean with “pending” I am not allowed to close the Listener, right? Maybe this is the reason I don’t get to the “purchased” state?

What is the best practice for handling the new states and when do you close the listener with **store.finishTransaction( event.transaction )**?

Here is my example for the NOT working listener (Products are tested by getting the correct price for each… so the store also is working correctly)… is there something I am missing?

```
_G.transactionListener = function( event )
   
  local productID

  if ( event.name == "init" ) then
 
    if not ( event.transaction.isError ) then

        _G.storeinitsuccesfull=1
        productID= event.transaction.productIdentifier
 
    else -- Unsuccessful initialization; output error details

        local doitnow=performWithDelay(3000,function() removeShopPopup();initstore() end,1)

    end
 

  else
      productID= event.transaction.productIdentifier;

      if event.transaction.state == "purchased" then
           
           -- THIS PART OF CODE IS NOT GETTING ACCESSED IT SEEMS! 

      elseif event.transaction.state == "refunded" then
        -- handle refunds here

      elseif event.transaction.state == "cancelled" then

          removeShopPopup()

      elseif event.transaction.state == "failed" then        

          removeShopPopup()
      else

          removeShopPopup()
      end

      local doitnow=performWithDelay(2200,function() _G.store.finishTransaction( event.transaction );_G.storeinitsuccesfull=0;removeShopPopup();initstore() end,1)

  end    
  end
```

---

<div class="post-metadata">

**Author:** ![kbradford](https://forums.solar2d.com/user_avatar/forums.solar2d.com/kbradford/32/2146_2.png) [@kbradford](https://forums.solar2d.com/u/kbradford)\
**Post date:** [June 16, 2021, 3:17pm UTC](https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404/7 "2021-06-16T15:17:39Z")

</div>

Is there a reason you have finishTransaction on a delay? That could be causing some issues for sure.

And I don’t do any special handling for “pending”. I actually don’t do anything with that type of transaction, other than calling finishTransaction on it.

---

<div class="post-metadata">

**Author:** ![troylyndon](https://forums.solar2d.com/user_avatar/forums.solar2d.com/troylyndon/32/313_2.png) [@troylyndon](https://forums.solar2d.com/u/troylyndon)\
**Post date:** [June 22, 2022, 8:36am UTC](https://forums.solar2d.com/t/plugin-google-iap-billing-transactionlistener-sample/353404/8 "2022-06-22T08:36:55Z")

</div>

RE: plugin.google.iap.billing, check this issue out: [Google Play's IAP Auto-Refunds Big Problem](https://forums.solar2d.com/t/google-plays-iap-auto-refunds-big-problem/354819)
