B4A Library GooglePlayBilling - In App Purchases

Status
Not open for further replies.
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 (such as B4XPages pages)
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.
- Subscriptions require a different method to start the billing flow: https://www.b4x.com/android/forum/t...-due-to-google-restriction.142524/post-903185

Updates

- v7.01 - Fixed outdated manifest macro.
- v7.00 - Based on Google Play Billing v7.00. Note that the android.test.purchased id no longer works. This partially breaks the example.
- v5.21 - Based on Google Play Billing v5.21.
- v5.00 - Based on Google Play Billing v5.0.0
- v1.11 - Fixed the issue with Google Play warning about AIDL.
- v1.10 - Based on Google Play Billing v3.0.1 - developer payload is no longer supported. The payload parameter (renamed to unused) doesn't do anything.

This is an internal library.
 

Attachments

  • BillingExample.zip
    15 KB · Views: 717
  • GooglePlayBilling.zip
    403.7 KB · Views: 266
Last edited:

Scantech

Well-Known Member
Licensed User
Longtime 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
Longtime 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
Longtime 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
Longtime 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
Longtime 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
Longtime 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
Longtime 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
Longtime 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.
 

diego

Member
Licensed User
Longtime User
Hi, I have a message in the Google Play Console as "Remember that you must implement the suspension and restoration of accounts in your application before November 1, 2020"
I have a non-free app but I don't want to implement any in-app billing product. How to configure it? Just adding the library and the line in the manifest?
thanks
 
Status
Not open for further replies.
Top