Android Question Occassionally in-app purchases not working (?)

mc73

Well-Known Member
Licensed User
Longtime User
Unfortunately, I don't have any log files or whatever, but here's what some clients complain about:
After successfully purchasing an in-app product, and everything works as expected, this in-app purchase is not recognized from time to time.

Here's the code I use:

B4X:
In activity_create

if firstTime then
   manager.Initialize("manager", keyInApp)
end if

Then:

Sub Manager_BillingSupported (Supported As Boolean, Message As String)
   If Supported Then
       manager.GetOwnedProducts
   End If
End Sub

Sub manager_OwnedProducts (Success As Boolean, purchases As Map)
   If Success Then
      For Each p As Purchase In purchases.Values
           If p.ProductId="myProductID" And p.PurchaseState=p.STATE_PURCHASED Then
               someFlag=true
           End If      
      Next

       continueWithLoadingScreen
End Sub

I really don't know the cause of the problem. Does it, for example, need internet connectivity at the time of loading? Perhaps, I should store locally the product?
Any help is appreciated, thank you!
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I store a boolean "purchased" value locally, then use that when the app starts. I also call BillingManager3.GetOwnedProducts from my Starter service & if the call is successful, I update my local "purchased" value.

That way, if the local Play Store cache isn't up to date & there's no internet connection, or the GetOwnedProducts call is slow to return, the app is always aware of the correct purchased state of in-app products.

- Colin.
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I store a boolean "purchased" value locally, then use that when the app starts. I also call BillingManager3.GetOwnedProducts from my Starter service & if the call is successful, I update my local "purchased" value.
Can you put a code with an example?
I think will be very usefull.
Thanks.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Can you put a code with an example?
I think will be very usefull.
Thanks.
It's hard to post the exact code I use, because the functionality is spread across multiple different classes - but basically...

I have an Options class that I use to load/save the various app settings. In a game with an IAP to turn off ads I might have a "noads" option, which is set to False by default. When a user purchases the no ads IAP, I set the "noads" option to True & save it.

When the app starts, I initialize BillingManager3 in my Starter service Service_Create, which returns to the BillingManager3_BillingSupported callback. If billing is supported, I call BillingManager3.GetOwnedProducts. I also look at the value stored in my "noads" option in my Activity_Create & use that until GetOwnedProducts returns. Here's the main part of my code that sets the "noads" option based on what GetOwnedProducts returns:

B4X:
Private Sub manager_BillingSupported (Supported As Boolean, Message As String)
    Log(Supported & ", " & Message)
    Log("Subscriptions supported: " & manager.SubscriptionsSupported)

    billingSupported = Supported 
    If Supported Then manager.GetOwnedProducts 
End Sub

Private Sub manager_OwnedProducts (Success As Boolean, purchases As Map)       
      
    If Success Then
        Log("PURCHASES: " & purchases
        For Each p As Purchase In purchases.Values
            If p.ProductId = "no_ads" Then
                If p.PurchaseState = p.STATE_PURCHASED Then
                    cOpts.NoAds = True 
                    cOpts.Write_Options
                    Return
                End If
            End If
        Next
    End If
       
    Log("No Upgrade Purchased")
    cOpts.NoAds = False
    cOpts.Write_Options
       
End Sub

- Colin.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I store a boolean "purchased" value locally, then use that when the app starts. I also call BillingManager3.GetOwnedProducts from my Starter service & if the call is successful, I update my local "purchased" value.

That way, if the local Play Store cache isn't up to date & there's no internet connection, or the GetOwnedProducts call is slow to return, the app is always aware of the correct purchased state of in-app products.

- Colin.

Thank you, Colin, I think this is the best approach for now at least.
 
Upvote 0
Top