B4A Library GooglePlayBilling - In App Purchases

Erel

Administrator
Staff member
Licensed User
The previous service which was used by InAppBilling3 library is being deprecated and will soon won't be available.

GooglePlayBilling is based on the new in app purchases service: https://developer.android.com/google/play/billing/billing_library_overview

Usage instructions:
1. Add to the manifest editor:
B4X:
CreateResourceFromFile(Macro, GooglePlayBilling.GooglePlayBilling)
2. Create a BillingClient object in the starter service and initialize it.
The PurchasesUpdated is the only event that needs to be handled in its own sub and that sub must be in the same module where BillingClient was initialized.
The other events should be handled with Wait For.

3. You shouldn't assume that the connection to the store service is always valid. You should instead call Billing.ConnectIfNeeded before making other requests:
B4X:
Wait For (billing.ConnectIfNeeded) Billing_Connected (Result As BillingResult)
If Result.IsSuccess Then
4. Call QueryPurchases to get the currently owned purchases. Note that it might include incomplete (pending) purchases. You should check the purchase state.

5. Making an order is done in several steps:
a. Get the SKU details with QuerySkuDetails.
b. Call LaunchBillingFlow to start the order process. This is the only method that must be called from an Activity or a class initialized with an activity context.
c. The PurchasesUpdated event will be raised with the result, unless there was an error which prevented the order from starting. You should also check the returned BillingResult object.
d. Verify the order signature. This can be done locally or remotely. It is safer to do it with with a remote server.
Verifying it locally is done by calling VerifyPurchase with the base64 key you got from Google Play Console.

6. All orders must be acknowledged or consumed in three days. Consuming a purchase removes it from the "owned" products.

Acknowledging:
B4X:
If p.IsAcknowledged = False Then
   Wait For (billing.AcknowledgePurchase(p.PurchaseToken, "")) Billing_AcknowledgeCompleted (Result As BillingResult)
   Log("Acknowledged: " & Result.IsSuccess)
End If
Consuming:
B4X:
Wait For (billing.Consume(p.PurchaseToken, "")) Billing_ConsumeCompleted (Result As BillingResult)
Tips:

- Calling BillingResult.IsSuccess will log the error message in debug mode when there is an error.
 

Attachments

Last edited:

Scantech

Well-Known Member
Licensed User
I had it working first few times but now i get empty list?
purchase size = 0?
B4X:
Private Sub RestorePurchases
    Dim blnPurchaseisValid As Boolean
 
    Wait For (billing.ConnectIfNeeded) Billing_Connected (Result As BillingResult)
    If Result.IsSuccess Then
        Wait For (billing.QueryPurchases("inapp")) Billing_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
        Log("Query completed: " & Result.IsSuccess)
        If Result.IsSuccess Then
            Log(Purchases.Size)
            For Each p As Purchase In Purchases
                If p.Sku = ADS_SDK_ID Then
                    blnPurchaseisValid = True
                    HandleAdsPurchase(p)
                End If
            Next
        End If
    End If
 
    If blnPurchaseisValid = False Then
        AppisNotPurchased
    End If
End Sub
In main i call resetAds
purchase.size = 0
B4X:
Private Sub ConsumeAdsProduct(Purchases As List)
    Log(Purchases.Size)
    For Each p As Purchase In Purchases
        If p.Sku = ADS_SDK_ID Then
            Wait For (billing.Consume(p.PurchaseToken, "")) Billing_ConsumeCompleted (Result As BillingResult)
            If Result.IsSuccess Then
                Log("consumed")
  
            End If
        End If
    Next
 
 

End Sub
Update: I had to repurchase everytime. i think im not suppose to call ResetAds. This was probably causing it to reset. I did not understand what ResetAds was for.

It seems to be working good now. Thanks Erel
 
Last edited:

rboeck

Well-Known Member
Licensed User
I got this message in google play store:

"We’ve detected that your app is using an old version of the Google Play developer API. From 1 December 2019, versions 1 and 2 of this API will no longer be available. Update to version 3 before this date"

I hope, this date is only for new apps...
 

DonManfred

Expert
Licensed User
"We’ve detected that your app is using an old version of the Google Play developer API. From 1 December 2019, versions 1 and 2 of this API will no longer be available. Update to version 3 before this date"

I hope, this date is only for new apps...
Does not look like it is related to installed app or not.
Google has discontinued the two versions. they get discontinued in December.

A new Library is available which matches the new requirements by google; using the v3 of the Api.

I guess everyone has to update.

Just my 2 cent but i do not use Inapp billing at all so i can´t really speak about experience.
 

Robert Valentino

Well-Known Member
Licensed User
"The library is currently in beta stage."

Can/should we release APPs using this library to the Play Store? Or should we wait until it is out of beta?
 

diego

Member
Licensed User
Hi Erel,

Is it possible to create a simple subscription that allows or not the user to update the App in Play Store?
I mean, for example a yearly subscription, if you don't pay you can't get new updates.
 

andyr00d

Member
Licensed User
I'm not understanding how this relates to the inapp managed products. Is QueryPurchases a replacement of OwnedProducts (from InAppBilling3)? If I have a managed product for example called "example_123", is that now the ADS_SDK_ID? For now I just want to query which actual managed products I have available... any help?
 

andyr00d

Member
Licensed User
Thanks for your reply. So in order to get all currently owned (purchased) products, what is the equivalent of this sub from the previous lib?

B4X:
Sub manager_OwnedProducts (Success As Boolean, purchases As Map)

   If Success Then
      Log(purchases)
      For Each p As Purchase In purchases.Values
         Log(p.ProductId & ", Purchased? " & (p.PurchaseState = p.STATE_PURCHASED))
      Next
I see in RestorePurchases there is a similar looking method, but it's not returning any of my purchased items.. is there any other "link" between the play store and my app apart from the BILLING_KEY?
 

rboeck

Well-Known Member
Licensed User
In the sample app maybe you have to change from "inapp" to "subs", if you are using them. I get my current subscription, with changing the BILLING_KEY.
 
Top