App Inventor Extensions


In-App Billing Extension

See the App Inventor Extensions document about how to use an App Inventor Extension.

For questions about this extension or bug reports please start a new thread in the App Inventor community. Thank you.

For feature requests please contact me by email. To be a sponsor of a new method already is possible starting from only 10 USD! With your contribution you will help the complete App Inventor community. Thank you.

Aug 15th, 2016: Version 1: initial version of the Billing Extension released using the In-App Billing library by Anj Lab.

Aug 14th, 2019: Version 2: in app billing library updated to V 1.1.

Aug 29th, 2019: Version 2a: in app billing library updated to V 1.1 and Google Billing API V2.0.3 added. The documentation of version 2 you now can find here.

Apr 19th, 2021: Version 3: using Google Billing API V3.0.3. The extension now uses directly the Google Billing API and has been rebuilt completely. Thank you Hussain for being the sponsor of version 3 of this extension.

Sep 2nd, 2021: Version 3a: bug fix: missing application metadata element added into the manifest. Without that metadata element Google still was assuming, that the extension is using Version 2 of the Google Billing API.

Note: Starting on August 2, 2022, all new apps must use Billing Library version 4 or newer. By November 1, 2022, all updates to existing apps must use Billing Library version 4 or newer.

This billing extension will not be updated anymore. You have the following options to update your app:

Description

This extension enables you to use Google In-App Billing in your App Inventor app for Products (both non-consumable and consumable) and Subscriptions.
Required permission: com.android.vending.BILLING

Some Notes

Properties


Returns whether In-app billing service is ready to purchase.


Returns whether AutoAcknowledge is true or false.


Specifies AutoAcknowledge (true or false), default value is true.

Note: To simplify the billing process, the billing extension provides the AutoAcknowledge property. If AutoAcknowledge has been set to true and the purchased product is in state Purchased then method Acknowledge will be automatically called. Same if the state switches from Pending to Purchased while the app is running. If the state switches from Pending to Purchased while the app is running, then the Acknowledge process will be triggered automatically. As the state also can switch from Pending to Purchased while the app is not running, later in the GotPurchasedProducts event there could be products available, which still have to be acknowledged. The Acknowledge process will be triggered automatically also in this case.

Methods


Start Connection
After the connection successfully started, event SetupFinished will be triggered.


Get Product Details.
Will trigger event GotProductDetails.


Purchase productId.
Will trigger event PurchaseUpdated. If AutoAckmowledge is true, then event Acknowledged will be triggered directly but only in case the state of the product is Purchased. After purchase a product also can be in state Pending or Unspecified state.


Return all purchased products
Will trigger event GotPurchasedProducts.


Get Subscription Details
Will trigger event GotSubscriptionDetails.


Subscribe productId.
Will trigger event PurchaseUpdated. If AutoAckmowledge is true, then event Acknowledged will be triggered directly but only in case the state of the product is Purchased. After purchase a product also can be in state Pending or Unspecified state.


Return all purchased subscriptions
Will trigger event GotPurchasedSubscriptions.


Acknowledges in-app purchases.
Developers are required to acknowledge that they have granted entitlement for all in-app purchases for their application. Warning! All purchases require acknowledgement. Failure to acknowledge a purchase will result in that purchase being refunded. For one-time products ensure you are using the Consume method, which acts as an implicit acknowledgement or you can explicitly acknowledge the purchase via this method.
Will trigger event Acknowledged.


Get purchase token of a purchased product or subscription, jsonString can be a jsonObject returned from event PurchaseUpdated or a jsonArray returned from event GotPurchasedProducts or GotPurchasedSubscriptions.

Note: The use of this method is only required if AutoAcknowledge is false and you like to verify the purchase token on your backend server.

Example use of method PurchaseToken together with the method Acknowledge inside event GotPurchasedProducts


Consumes a given in-app product. Consuming can only be done on an item that's owned, and as a result of consumption, the user will no longer own it.
Will trigger event Consumed.

Events


Event indicating that setup has been finished.


Event indicating that product details have been received.


Event indicating that list of purchased products has been received. The following parameter are available
1) listProductIds (all purchased productIds, can also contain pending products),
2) listAcknowledged (list of product ids, which have been acknowledged),
3) listNotAcknowledged (list of product ids, which are in state Purchased and are ready to be acknowledged) and
4) jsonArray (an array of jsonObjects with details for each product id
If property AutoAcknowledge is true, then for products found in list listNotAcknowledged the Acknowledge process will be triggered automatically.


Event indicating that purchase of productId (inn-app product or subscription) has been updated.
After purchasing a product or subscription, the product id can be in one of the following states

Note: To simplify the billing process, the billing extension provides the AutoAcknowledge property. If AutoAcknowledge has been set to true and the purchased product is in state Purchased then method Acknowledge will be automatically called. Same if the state switches from Pending to Purchased while the app is running. However as the state also can switch from Pending to Purchased while the app is not running, we have to check in the GotPurchasedProducts or GotPurchasedSubscriptions event, if there are products or subscriptions available, which have to be acknowledged. If you set the AutoAcknowledge property to true, the Acknowledge process will be triggered automatically for you.


Event indicating that subscription details have been received.


Event indicating that list of purchased subscriptions has been received. The following parameter are available
1) listProductIds (all purchased productIds, can also contain pending subscriptions),
2) listAcknowledged (list of product ids, which have been acknowledged),
3) listNotAcknowledged (list of product ids, which are in state Purchased and are ready to be acknowledged) and
4) jsonArray (an array of jsonObjects with details for each product id
If property AutoAcknowledge is true, then for products found in list listNotAcknowledged the Acknowledge process will be triggered automatically.


Event indicating that product has been acknowledged.


Event indicating that product has been consumed.


Event indicating that an error occurred. This is a list of possible error messages:

Example App 1: As simple as possible

Note: This example can be tested using the apk file without uploading it to Google Play, because it uses the Android Test product. The simple example uses AutoAcknowledge = true.
Usually after purchase a product will be in state Purchased, so the Acknowledge process will be triggered automatically. However a product also can be in state Pending or Unspecified State. To display a message for the user in case the state is Pending, we can use the PurchaseUpdated event handler, which will be triggered in case the state is not Purchased. If the state switches from Pending to Purchased while the app is running, then the Acknowledge process will be triggered automatically. As the state also can switch from Pending to Purchased while the app is not running, later in the GotPurchasedProducts event there could be products available, which still have to be acknowledged. The Acknowledge process will be triggered automatically also in this case.

Example App 2: Pura Vida Airline

Initialize

Product Details

Purchase

Further blocks used

Example App 3: Simple Subscription

Note: The simple example uses AutoAcknowledge = true.
Usually after purchase a product will be in state Purchased, so the Acknowledge process will be triggered automatically. However a product also can be in state Pending or Unspecified State. To display a message for the user in case the state is Pending, we can use the PurchaseUpdated event handler, which will be triggered in case the state is not Purchased. If the state switches from Pending to Purchased while the app is running, then the Acknowledge process will be triggered automatically. However as the state also can switch from Pending to Purchased while the app is not running, we have to check in the GotPurchasedSubscription event, if there are subscriptions available, which have to be acknowledged.


Setup
protip: use simple subscription ids, in the example I used sub01

Screenshots

Questions and Answers

Q1: How to use and test Google Play's billing system?
A: See this Overview, see especially the chapters Getting ready and Test your Integration. See also Test in-app billing with application licensing. License testers have access to test payment methods that avoid charging the testers real money for purchases. You can also use test payment methods to simulate certain situations, such as when a payment is declined.

Q2: How to cancel a subscription?
A: This feature unfortunately is not available in the BillingClient API, therefore this is also not available in the billing extension. To cancel a subscription see the steps here.
Canceling a subscription never triggers the PurchaseUpdated event! You always have to check, if a subscription still is in listAcknowledged by calling method PurchasedSubscriptions, which triggers the GotPurchasedSubscriptions event. After canceling a subscription Google removes weekly subscriptions from the product list after the subscription week has passed or for monthly subscriptions after the subscription month has been passed...

Q3: What will happen if someone deletes the app then wants to reinstall it - what blocks do I need for a "Restore" button?
A: Google of course still knows, that the user bought the product, which means after calling the method PurchasedProducts or PurchasedSubscriptions you will receive the list of purchased products or subscriptions in the event GotPurchasedProducts or GotPurchsedSubscriptions. Also if a user reinstalls the app and also if the user installs the app on another device using the same Google login.

Q4: How to prevent In App purchase bypass?
A: Version 3 offers additional security. Please read the Fight and abuse chapter and verify purchases on your backend server before granting entitlements.

See also this answer in Stackoverflow for a server example to verify a purchase.

Additionally you still can follow the recommendations by Cian as well as to use the HackProtect Extension by cian to prevent In App Purchase bypass. Thank you Cian!

Q5: How to implement the Account Hold and Restore feature?
A: Just implement deep links, see here. With that you meet the requirements of Google Play. Google wants users to know that they can manage their subscriptions.

Q6: We are a self-service laundry and would like to to implement your solution to charge people by mobile phone for our services.
A: Are you aware, that Google charges a service fee of 30% of the price? You only will receive 70% of the payment, see also here. If the payment is primarily for physical goods or services as in your case, then let me suggest you to NOT use the in app billing extension, but another solution. See also the Google Policy. For questions concerning other payment options please do a search in the App Inventor community or ask there. Thank you.

Q7: How to find out, if a product or subscription has been purchased to disable ads, enable more features, etc.?
A: Use method PurchasedProducts or PurchasedSubscriptions together with the GotPurchasedProducts or GotPurchasedSubscriptions event and check listAcknowledged for the product id you are interested in like this (sub01 and sub02 in the example screenshot are product ids)

My suggestion is to call PurchasedProducts or PurchasedSubscription in the SetupFinished as well as the Acknowledged event, so you always can check in the GotPurchasedProducts or GotPurchasedSubscriptions event, which products have been purchased and enable more features etc.

Q8: I have noticed this when trying to publish an update of my app. Is this normal?


A: If your warning look like this, then it is ok, because your old versions still use the old library, but not the new version anymore


Q9: I get the following error This version of the application is not configured for billing through Google Play. Check the help centre for more information. What can I do?
A: Are you testing using your own Google account? Did you add your Email to license testing? Answer taken from Stackoverflow, see also this Stackoverflow answer. See also Q1 about what needs to be done.

Q10: Problem which I am facing is that the subscription is showing that it will not auto renew unlike the other app. Even the cancel subscription option is not coming.
A: Subscriptions usually are auto renewing. As narender_kumar found out, Auto renewal facility is stopped by Google in India. Every time after finishing the period it will ask for renewal.

Q11:This extension's payment is lifetime? And if you update extension I can get updated extension without payment?
A: No. As it has been announced in 2020, Google plans to release a new version of the billing library every year, so all users are able to benefit from any security, performance, and user experience improvements in new versions of Billing Library, see also the Google blog, especially chapter "Billing Library Version Requirements".

For questions about App Inventor,
please ask in the App Inventor community.Thank you.

Back to top of page ...


Home | Snippets | Tutorials | Extensions | Links | Search | Privacy Policy | Contact