Android Question BillingManager3 in a Class

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi everyone,

I am trying to implement BillingManager3 in a Class to make it portable between my apps. The methods of the class are called from an activity. So far all the events of the BillingManager3 included in the class are firing except of the PurchaseCompleted. Has anyone faced this problem? Any suggestions?

Thanks in advance

P.S. All the methods are not called in the Activitity Main but in another activity. Could this be a problem?
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
It will work in an activity other than Main. I got it to work. Here is my class.

B4X:
Sub Class_Globals
    Dim manager As BillingManager3
    Public products,idlist,prices As List
    Public googleplayid As String
    Public title As String
    Public purchasedids As List
    Public callingmod As Object
    Public storeready As Boolean
    
End Sub
Public Sub Initialize(callingmodule As Object,googleid As String)
    callingmod=callingmodule
    googleplayid=googleid
    misc.LogM("initializing key="&googleplayid)
    manager.Initialize("manager", googleplayid)
    manager.DebugLogging = True   
End Sub

Sub Purchase_Item(id As String, payload As String)
    Try
        manager.RequestPayment(id,"subs",payload)
    Catch
        misc.ProApp("Billing Crash")
    End Try
End Sub

Sub Simulate_Purchase_Item(id As String, payload As String)
    Dim p As Purchase
    manager_PurchaseCompleted (True, p)
End Sub

Sub Start_Store(storetitle As String, image As String)
    title=storetitle
    CallSubDelayed3(StoreActivity,"Start_Store", title,image)
End Sub

#Region Billing
Sub Manager_BillingSupported (Supported As Boolean, Message As String)
       misc.LogM("in store class")
    misc.LogM(Supported & ", " & Message)
'       misc.LogM("Subscriptions supported: " & manager.SubscriptionsSupported)
    storeready=Supported
    Try
        If Supported Then
            manager.GetOwnedProducts
        Else
            CallSubDelayed(callingmod,"No_Purchased_Features")
        End If
    Catch
        CallSubDelayed(callingmod,"No_Purchased_Features")
        Log(LastException.Message)
    End Try
End Sub
Sub manager_OwnedProducts (Success As Boolean, purchases As Map)
   misc.LogM(Success)
   storeready=Success
   Dim purchasesAvailable As Boolean
    purchasedids.Initialize
    If Success Then
        misc.LogM(purchases)
        Try
            For Each p As Purchase In purchases.Values
                misc.LogM(p.productid & ", Purchased? " & (p.PurchaseState = p.STATE_PURCHASED))
                purchasedids.Add(p.DeveloperPayload)
                'payload=p.DeveloperPayload
                'productid=p.productid
                If p.PurchaseState = p.STATE_PURCHASED Then purchasesAvailable=True
        
            Next
        Catch
            purchasesAvailable=False
            Log(LastException.Message)
        End Try
    End If
       If purchasesAvailable Then
           CallSubDelayed2(callingmod,"Enable_Purchased_Features",purchasedids)
    Else
        CallSubDelayed(callingmod,"No_Purchased_Features")
    End If
End Sub
Sub manager_PurchaseCompleted (Success As Boolean, Product As Purchase)
'    LogColor("manager_PurchaseCompleted storeclass "&Success,Colors.Yellow)
    If Not(purchasedids.IsInitialized) Then purchasedids.Initialize
    If Success Then
        purchasedids.Add(Product)
        Main.adsenabled=False
        CallSubDelayed(StoreActivity,"btnBack_Click")
        
        CallSubDelayed2(callingmod,"Enable_Purchased_Features",purchasedids)
    End If
End Sub
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
Actually, I declare it in Process_Globals of Main, and initialize it in the Main activity. Then in the second StoreActivity, I reference it like Main.store. That could be the key difference.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi @Jack Cole , I tried what you suggested (transferring the declaration of the class in Main) and the result is the same. No PurchaseCompleted event is firing. Any suggestions on this?
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
I also tried implementing the BillingManager3 in an Activity different than Main and creating the purchase completed event for this BillingManager3 object in this Activity. The result is that no PurchaseCompleted event is raised (I use Android 9). Here is what I have got. @Erel I could really use your opinion here.

requestCode = 1
** Activity (checkandupdateiapdata) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
Arrived: 1, 1
** Activity (checkandupdateiapdata) Resume **
 
Last edited:
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
You might look at the unfiltered logs.

Also, make sure you've set your email address that the device is registered with under license testing in the Play Console (see below). Make sure your IAP is set to active. Please also see this link on testing IAP if you haven't already seen it.

Is the BillingSupported event firing?

upload_2019-9-20_5-50-52.png
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi @Jack Cole, I will check it immediately but searching in the mean time the net I found this:
https://developer.android.com/google/play/licensing/setting-up

This page if you scroll down in the section "Setting up tests accounts" you can see a table which clearly indicates that the publisher account can receive the response. My phone's account is the publisher account so unless Google is getting "dizzy" updating the documentation, I do not expect something different. Anyway, I suppose I have activated also the IAP but I am not sure about that (all I did is uploading a draft app in "Internal test track" releases with the BillingManager3 library and having added the proper permission in the manifest) as the tutorial with the "Draft app" is not updated with the latest Google changes in Release Management. I suppose I have done the right thing since all the other events are firing but If you see that I have done something wrong please feel free to "slap" me because it's bugging me for three days.

@Erel please feel free to jump in the discussion any time you like...
 
Last edited:
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Reading the link that @Jack Cole provided I saw that it is needed a closed or open track trial release. I have uploaded to internal track. I do not know if this is the problem since I uploaded the new release to a closed track and I have to wait several hours as it is described before the application is available. Out of curiosity. Is this the answer? Has anyone faced it already and came up with the same solution?

P.S. Reading again my previous post (#7) looks kind of rude to my eyes but this was not my intention so I apologize if it was indeed taken as rude.
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
That may be the culprit. I ran into that in the past. It may take a few hours. I also had to add my email address into the licensing testers list even though it said you shouldn't have to. It wouldn't work for me without it.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi Jack, did you receive an e-mail and then started testing? It's been almost 17 hours now since the full roll out and no e-mail received with link for the testers. I do not want to waste more time by making a test myself since the way the test app is built I will be forced to change it to consume the product (No consume is supposed to happen during normal execution).
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Thanks, I didn't know that.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
It did not work. I moved the entire process to Activity Main and also initialized the class in Main. The result is negative again...

Here is what I get:
requestCode = 1
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
Arrived: 1, 1
Yet no PurchaseCompleted event is raised.

@Erel Do you want to help me?
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
You are absolutely right on what you are suggesting. I do not want to post the full code so I will send a private message in a few minutes to you and Erel. Can you please both check my code and see if it works for you or what am I doing wrong?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The service behind BillingManager3 is now deprecated. I'm working on a new library based on the new Google Play Billing service. Beta version of that library will be released tomorrow.

I'm not 100% sure yet but it seems like the old library will stop functioning on December 1, 2019 so the focus should be shifted to the new library.
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
hatzin,

I downloaded your code and was able to get it to work. What I did was set my APIKey in starter, changed the package name to one of my apps, and changed the info for your button3_click.

B4X:
Sub Button3_Click
'    iap.Purchase("android.test.purchased", iap.ProductTypeManaged, "trialpayload")
    iap.Purchase("pmmpro", iap.ProductTypeManaged, "trialpayload")
End Sub

From what I can tell online, the android.test.purchased often doesn't work correctly (it didn't for me). I had to use an actual product.

You code looks good.

I have a general recommendation though. When you are trying to solve a problem like this, don't try to write all the functionality. Reduce it down to the bare essentials (like just the code used in Erel's original example). Get that working first, and then make it more advanced. You have a large amount of code to try to debug.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
@Jack Cole: Hi Jack, out of curiosity did the class call the ProductPurchased sub in Main which is the direct call from b4amng_ProductPurchased in the class?

@Erel: I will be waiting for this library Erel.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Yes DonManfred, I just saw that... Thanks.
 
Upvote 0
Top