1

I'm having trouble implementing in-app purchases in my mobile app. I want to implement in-app purchase in my application.can any one tell step by step procedure to implement in-app purchase in android application. I have googled and found many tutorials but they all are using old billing library version(1.2).I want to use latest version(2.2.0). Any sample project, tutorial...

Bad Boy
  • 381
  • 2
  • 14

1 Answers1

2

These steps are based on my experience with version: 2.0.2. Since there are not any breaking changes in version: 2.2.0, the same applies to the maximum extent.

To start with BillingClient for in-app purchases:

  1. A billing client has to be created using BillingClient.Builder.
billingClient = BillingClient.newBuilder(applicationContext)
                .enablePendingPurchases()
                .setListener(/* a PurchasesUpdatedListener object */)
                .build()

enablePendingPurchase method has to be called before build, as Google supports cash payments in future, otherwise billingClient creation fails. A callback will be triggered after creation to PurchaseUpdateListener.onPurchasesUpdated method to handle pending purchases.

  1. After creating a billingClient start billingClient connection.
billingClient.startConnection(/* a BillingClientStateListener object */)

BillingClientStateListener has two methods; one triggers when the connection is successfully established and the other triggers when connection is failure or disconnected. billingClient connection should always be maintained and retry mechanism should be handled in onBillingServiceDisconnected method.

  1. After establishing a successful connection, make a call with billingClient's querySkuDetailsAsync method to fetch 'sku' details asyncronously.
billingClient.querySkuDetailsAsync(skuDetailsParams, /* lambda or SkuDetailsResponseListener object*/)

This method fetches us the In-app purchasable products created by us in play store to display it in the UI or do whatever we want with it.

  1. (Optional) Make a call with billingClient's queryPurchases method to details for all items purchased within the app.
vaultBillingClient.queryPurchases(/* BillingClient.SkuType.SUBS or BillingClient.SkuType.INAPP */)

'SUBS' for subscription purchase details and 'INAPP' for one time in app purchases.

  1. When user tries to purchase a in-app or subscription product check if the product is supported using billngClient's isFeatureSupported(BillingClient.FeatureType./* SUBSCRIPTIONS or other */) method and make a call to billingClient's launchBillingFlow method.
billingClient.launchBillingFlow(activity, billingFlowParams)

billingFlowParams can be built using BillingFlowParams builder method by passing 'sku' details. System UI i.e., purchase bottomSheet will be shown. After the purchase is finished a call will be triggered to PurchasesUpdatedListener.onPurchasesUpdated method given for billingClient in the step 1.

  1. After a successful purchase by the user, it has to be acknowledged immediately using either of consumeAsync or acknowledgePurchase methods of billingClient based on purchase type.
billingClient.acknowledgePurchase(acknowledgePurchaseParam)

acknowledgePurchaseParams is built with AcknowledgePurchaseParams builder method by passing 'purchaseToken'. Acknowledgment should be done after verifying purchase.

  1. Once purchase is validated and acknowledged billingClient can be closed.
billingClient.endConnection()

Note:- For all the steps mentioned above 'billingClient' connection should be intact. Hence retry mechanism on disconnection. It is better to start billing client connection as soon as app is opened to entitle the user with all his purchases. Also, there is no performance issue in always maintaining a connection with the billing client. However, it is up to developer how he grants user his purchases; either using his server or by any other means.

For further reference, refer the documentation: https://developer.android.com/google/play/billing/billing_library_overview

Nataraj KR
  • 1,001
  • 10
  • 22
  • How to consume product after purchase – Bad Boy Mar 30 '20 at 04:34
  • Use `ConsumeAsync` method of billingCient for one-time purchase products. – Nataraj KR Mar 30 '20 at 08:20
  • I want to allow user to purchase same product(coins) multiple times – Bad Boy Mar 30 '20 at 08:56
  • yes, you can but acknowledge to the purchase use `consumeasync` – Nataraj KR Mar 30 '20 at 09:20
  • https://github.com/Marsad-Ch/testappp : can you please check whether I did everything right or not, and also please tell me where to add reward code (add coins to user account) – Bad Boy Mar 30 '20 at 11:07
  • You can test your app by uploading to the internal testing track in playstore and specifying mail addresses of testers. Testers can purchase items without money. Try it. Also check the chat. – Nataraj KR Mar 30 '20 at 11:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210583/discussion-between-marsad-maqsood-and-nataraj-kr). – Bad Boy Mar 30 '20 at 11:33
  • Hey there, you dont need to read tutorial... all step done by my library https://stackoverflow.com/questions/26601734/how-to-implement-in-app-purchase-of-application-android/68227056#68227056 – Ucdemir Jul 02 '21 at 14:39