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

- 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: 436
  • GooglePlayBilling.zip
    340.1 KB · Views: 160
Last edited:

diego

Member
Licensed User
Longtime User
Thank you, as they show it as an alert and in red I thought it was mandatory.
 

rboeck

Well-Known Member
Licensed User
Longtime User
This message is only relevant if you are using in app purchases.
But if we have in app purchases, what to we have to check?
I have found in google docu: your app should query the Google Play Developer API to get the latest subscription state.
Does it mean, that we have to use the play developer API parallel to the library to get full information?
 

prbmjr

Active Member
Licensed User
This message is only relevant if you are using in app purchases.

Hi Erel, based in your answer, if I have a app in the google play and the app do not have any direct purchase inside , but the user need a user name and password to use the app, that must be created in a external webapp first... It´s true that the user have 30 days to use the app for free, but after the free time, the user must purchase a license using the webapp... Obs.: in the google console the app is registered as a free app...
In this case, I need to implement any change inside my app to stay updated with the new google policy?
 

rboeck

Well-Known Member
Licensed User
Longtime User
Today i could see what happen if a user is pausing his subscription: the list of purchaches is empty now, so i can't get any information about state. The user could stopped or paused his subscription, my app has no information about. Because i dont know about pausing his subscription, i let the user buy a new subscription and things are out of control. Do i make some wrong assumptions?
 

tufanv

Expert
Licensed User
Longtime User
Example was updated and is based on B4XPages. This makes things simpler as there is no need to use the starter service and communicate between the two modules.
Can you also post the old tutorial with starter service in case we dont want to use b4xpages.?
 

tufanv

Expert
Licensed User
Longtime User
I have tested with the sdk_id Erel posted and di not change the key also and I get an example purchase flow for 0.99, when I change it to my product id and the key in developer console / app page / monetization setup that starts with MII, I always get : Error Starting billing process. What may be the mistake here?
 

Tempomaster

Active Member
Licensed User
Longtime User
I uploaded this example (Post #1) to the Play Store today as a replacement for one of my apps. I changed the package name accordingly. To avoid damage, I have saved the app as a closed test. After checking the uploaded APK, I still got a warning:

We noticed that this app is using an old version of the Google Play Billing Library. Starting November 1, 2021, all app updates must use Billing Library 3 or higher. Until then, please update to Billing Library 3.


There is still some time until November. I just have to change something until then. I just don't know what right now.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Version 5.00 is released, it is based on Google Play Billing v5.0.0
The previous version was based on v3.0.1 which is no longer accepted by Google Play (for new apps, and soon for all apps).

The changes in the SDK are mostly related to more sophisticated subscriptions. These features are not exposed in the library but can be accessed with some Java or JavaObject based code.
The updated Java code: https://github.com/AnywhereSoftware...oftware/b4a/objects/BillingClientWrapper.java
 
Status
Not open for further replies.
Top