在android中使用更新的库实现应用内购买 [英] implementing In app purchase with updated library in android

查看:259
本文介绍了在android中使用更新的库实现应用内购买的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在移动应用中实施应用内购买.我想在我的应用程序中实现应用程序内购买.任何人都可以逐步讲解在android应用程序中实现应用程序内购买的过程.我已经在Google上搜索了很多教程,但是它们都使用旧的计费库版本(1.2).我想使用最新版本(2.2.0).任何示例项目,教程...

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

推荐答案

这些步骤基于我对2.0.2版的经验.由于版本2.2.0中没有任何重大更改,因此在最大程度上适用于此.

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.

从BillingClient开始进行应用内购买:

To start with BillingClient for in-app purchases:

  1. 必须使用 BillingClient.Builder 创建计费客户端.
  1. A billing client has to be created using BillingClient.Builder.

billingClient = BillingClient.newBuilder(applicationContext)
                .enablePendingPurchases()
                .setListener(/* a PurchasesUpdatedListener object */)
                .build()

enablePendingPurchase 方法必须在 build 之前调用,因为Google支持

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. 创建billingClient之后,启动billingClient连接.

billingClient.startConnection(/* a BillingClientStateListener object */)

BillingClientStateListener 有两种方法:一个在成功建立连接时触发,另一个在连接失败或断开连接时触发.billingClient连接应始终维护,重试机制应使用 onBillingServiceDisconnected 方法处理.

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. 建立成功的连接后,使用billingClient的 querySkuDetailsAsync 方法进行调用,以异步方式获取'sku'详细信息.
  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*/)

此方法获取我们在Play商店中创建的应用内可购买产品,以将其显示在用户界面中,或使用它做我们想做的任何事情.

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. (可选)使用billingClient的 queryPurchases 方法进行调用,以详细了解应用内购买的所有商品.
  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"用于订阅购买详细信息,"INAPP"用于一次应用购买.

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

  1. 当用户尝试购买应用内或订阅产品时,请使用billngClient的 isFeatureSupported(BillingClient.FeatureType ./* SUBSCRIPTIONS或其他*/)方法检查该产品是否受支持,并调用billingClient的 launchBillingFlow 方法.
  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 构建器方法通过传递'sku'详细信息来构建

billingFlowParams.系统用户界面,即购买bottomSheet将会显示.购买完成后,将触发对步骤1中为billingClient指定的 PurchasesUpdatedListener.onPurchasesUpdated 方法的调用.

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. 用户成功购买后,必须得到确认立即根据购买类型使用billingClient的 consumAsync acknowledgePurchase 方法之一.
  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是通过传递'purchaseToken'的 AcknowledgePurchaseParams 构建器方法构建的.确认应在验证购买之后进行.

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

  1. 一旦确认购买并确认BillingClient,即可结清客户.

billingClient.endConnection()

注意:- 对于上述所有步骤,"billingClient"连接均应完好无损.因此,重试断开机制.最好在应用打开后立即开始计费客户端连接,以使用户拥有所有购买的权利.此外,始终保持与计费客户端的连接也没有性能问题.但是,由开发人员决定如何授权用户购买.使用他的服务器或其他任何方式.

有关更多参考,请参阅文档: https://developer.android.com/google/play/billing/billing_library_overview

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

这篇关于在android中使用更新的库实现应用内购买的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆