简体   繁体   English

在 android 中使用更新的库实现应用内购买

[英]implementing In app purchase with updated library in android

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.我想在我的应用程序中实现应用内购买。任何人都可以告诉我在 Android 应用程序中实现应用内购买的分步过程。 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).我用谷歌搜索并找到了很多教程,但它们都使用旧的计费库版本(1.2)。我想使用最新版本(2.2.0)。 Any sample project, tutorial...任何示例项目、教程...

These steps are based on my experience with version: 2.0.2.这些步骤基于我使用版本的经验:2.0.2。 Since there are not any breaking changes in version: 2.2.0, the same applies to the maximum extent.由于 version: 2.2.0 没有任何重大更改,因此同样适用于最大程度。

To start with BillingClient for in-app purchases:要开始使用 BillingClient 进行应用内购买:

  1. A billing client has to be created using BillingClient.Builder .必须使用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. enablePendingPurchase方法必须在build之前调用,因为谷歌支持未来现金支付,否则 billingClient 创建失败。 A callback will be triggered after creation to PurchaseUpdateListener.onPurchasesUpdated method to handle pending purchases.创建后会触发一个回调到PurchaseUpdateListener.onPurchasesUpdated方法来处理挂起的购买。

  1. After creating a billingClient start billingClient connection.创建 billingClient 后开始 billingClient 连接。
billingClient.startConnection(/* a BillingClientStateListener object */)

BillingClientStateListener has two methods; BillingClientStateListener有两种方法; 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. billingClient 连接应始终保持,重试机制应在onBillingServiceDisconnected方法中处理。

  1. After establishing a successful connection, make a call with billingClient's querySkuDetailsAsync method to fetch 'sku' details asyncronously .建立成功连接后,调用 billingClient 的querySkuDetailsAsync方法异步获取 'sku' 详细信息。
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.此方法获取我们在 Play 商店中创建的应用内可购买产品,以将其显示在 UI 中或使用它做任何我们想做的事情。

  1. (Optional) Make a call with billingClient's queryPurchases method to details for all items purchased within the app. (可选)使用 billingClient 的queryPurchases方法调用应用程序内购买的所有商品的详细信息。
vaultBillingClient.queryPurchases(/* BillingClient.SkuType.SUBS or BillingClient.SkuType.INAPP */)

'SUBS' for subscription purchase details and 'INAPP' for one time in app purchases. 'SUBS' 用于订阅购买详情,'INAPP' 用于一次性购买应用程序。

  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.当用户尝试购买应用内或订阅产品时,请使用 billngClient 的isFeatureSupported(BillingClient.FeatureType./* SUBSCRIPTIONS or other */)方法检查该产品是否受支持,并调用 billingClient 的launchBillingFlow方法。
billingClient.launchBillingFlow(activity, billingFlowParams)

billingFlowParams can be built using BillingFlowParams builder method by passing 'sku' details. billingFlowParams 可以使用BillingFlowParams构建器方法通过传递“sku”详细信息来构建。 System UI ie, 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 步中为 billingClient 提供的PurchasesUpdatedListener.onPurchasesUpdated方法的调用。

  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.用户成功购买后,它必须承认,无论使用的立即consumeAsyncacknowledgePurchase根据购买类型billingClient的方法。
billingClient.acknowledgePurchase(acknowledgePurchaseParam)

acknowledgePurchaseParams is built with AcknowledgePurchaseParams builder method by passing 'purchaseToken'. acknowledgePurchaseParams 是通过传递“purchaseToken”使用AcknowledgePurchaseParams构建器方法构建的。 Acknowledgment should be done after verifying purchase .确认购买后应进行确认

  1. Once purchase is validated and acknowledged billingClient can be closed.一旦购买被验证和确认 billingClient 可以关闭。
billingClient.endConnection()

Note:- For all the steps mentioned above 'billingClient' connection should be intact.注意:-对于上面提到的所有步骤,'billingClient' 连接应该完好无损。 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 : //developer.android.com/google/play/billing/billing_library_overview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM