implementing In app purchase with updated library in android

孤街浪徒 提交于 2020-04-11 18:16:04

问题


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...


回答1:


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



来源:https://stackoverflow.com/questions/60870706/implementing-in-app-purchase-with-updated-library-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!