Android Question Google billing problem

Jerryk

Member
Licensed User
Longtime User
I have two functions that read the available products to buy, one reads inapp, the other reads subs. Each can be called separately or consecutively.

B4X:
Public Sub GetInAppProducts(lSKUs As List) As ResumableSub
  'make sure that the store service is connected
  Wait For (billing.ConnectIfNeeded) billing_Connected (Result As BillingResult)
  'get the sku details
  If Result.IsSuccess Then
  ....

Public Sub GetSubscriptions(lSKUs As List) As ResumableSub
    'make sure that the store service is connected
    Wait For (billing.ConnectIfNeeded) billing_Connected (Result As BillingResult)
    'get the sku details
    If Result.IsSuccess Then
    ....

The problem is that if I call the function one after the other, then the second function returns an error:
BillingResult IsSuccess = False, ResponseCode = DEVELOPER_ERROR
Debug string: Client is already in the process of connecting to billing service.


It seems to me that the asynchronous connect did not end when the first function was called. How to solve it?
I could combine both functions into one with one connect, but here we have duplication in the code. Or read BillingClient.ConnectionState and react accordingly? How to get BillingClient.ConnectionState :
 

asales

Expert
Licensed User
Longtime User
Get the subscriptions and inapp purchases in the same sub:

B4X:
Private Sub RestorePurchases
    Wait For (billing.ConnectIfNeeded) Billing_Connected (Result As BillingResult)
    If Result.IsSuccess Then
        'Subscriptions
        Wait For (billing.QueryPurchases("subs")) Billing_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
        If Result.IsSuccess Then
            For Each p As Purchase In Purchases
                If p.Sku = SKU_ID2 Then Handle_Subscription_Purchase(p)
            Next
        End If
    
        'In-app purchases    
        Wait For (billing.QueryPurchases("inapp")) Billing_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
        If Result.IsSuccess Then
            For Each p As Purchase In Purchases
                If p.Sku = SKU_ID1 Then Handle_Inapp_Purchase(p)
            Next
        End If       
    End If
End Sub
 
Upvote 0
Top