(3)Purchasing In-app Billing Products
Once your application is connected to Google Play, you can initiate purchase requests for in-app products. Google Play provides a checkout interface for users to enter their payment method, so your application does not need to handle payment transactions directly.
When an item is purchased, Google Play recognizes that the user has ownership of that item and prevents the user from purchasing another item with the same product ID until it is consumed. You can control how the item is consumed in your application, and notify Google Play to make the item available for purchase again.
You can also query Google Play to quickly retrieve the list of purchases that were made by the user. This is useful, for example, when you want to restore the user's purchases when your user launches your app.
Purchase an Item
To start a purchase request from your app, call launchPurchaseFlow(Activity, String, int, OnIabPurchaseFinishedListener, String)
on your IabHelper
instance. You must make this call from the main thread of your Activity
. Here’s an explaination of the launchPurchaseFlow
method parameters:
- The first argument is the calling
Activity
. - The second argument is the product ID (also called its SKU) of the item to purchase. Make sure that you are providing the ID and not the product name. You must have previously defined and activated the item in the Developer Console, otherwise it won’t be recognized.
- The third argument is a request code value. This value can be any positive integer. Google Play reurns this request code to the calling
Activity
’sonActivityResult
along with the purchase response. - The fourth argument is a listener that is notified when the purchase operation has completed and handles the purchase response from Google Play.
- The fifth argument contains a ‘developer payload’ string that you can use to send supplemental information about an order (it can be an empty string). Typically, this is used to pass in a string token that uniquely identifies this purchase request. If you specify a string value, Google Play returns this string along with the purchase response. Subsequently, when you make queries about this purchase, Google Play returns this string together with the purchase details.
Security Recommendation: It’s good practice to pass in a string that helps your application to identify the user who made the purchase, so that you can later verify that this is a legitimate purchase by that user. For consumable items, you can use a randomly generated string, but for non-consumable items you should use a string that uniquely identifies the user.
The following example shows how you can make a purchase request for a product with ID SKU_GAS
, using an arbitrary value of 10001 for the request code, and an encoded developer payload string.
mHelper.launchPurchaseFlow(this, SKU_GAS,10001, mPurchaseFinishedListener,"bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
If the purchase order is successful, the response data from Google Play is stored in an Purchase
object that is passed back to the listener.
The following example shows how you can handle the purchase response in the listener, depending on whether the purchase order was completed successfully, and whether the user purchased gas or a premium upgrade. In this example, gas is an in-app product that can be purchased multiple times, so you should consume the purchase to allow the user to buy it again. To learn how to consume purchases, see the Consuming Products section. The premium upgrade is a one-time purchase so you don’t need to consume it. It is good practice to update the UI immediately so that your users can see their newly purchased items.
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener =newIabHelper.OnIabPurchaseFinishedListener(){ publicvoid onIabPurchaseFinished(IabResult result,Purchase purchase) { if(result.isFailure()){ Log.d(TAG,"Error purchasing: "+ result); return; } elseif(purchase.getSku().equals(SKU_GAS)){ // consume the gas and update the UI } elseif(purchase.getSku().equals(SKU_PREMIUM)){ // give user access to premium content and update the UI } } };
Security Recommendation: When you receive the purchase response from Google Play, make sure to check the returned data signature, the orderId
, and the developerPayload
string in the Purchase
object to make sure that you are getting the expected values. You should verify that the orderId
is a unique value that you have not previously processed, and the developerPayload
string matches the token that you sent previously with the purchase request. As a further security precaution, you should perform the verification on your own secure server.
Query Purchased Items
Upon a successful purchase, the user’s purchase data is cached locally by Google Play’s In-app Billing service. It is good practice to frequently query the In-app Billing service for the user’s purchases, for example whenever the app starts up or resumes, so that the user’s current in-app product ownership information is always reflected in your app.
To retrieve the user’s purchases from your app, call queryInventoryAsync(QueryInventoryFinishedListener)
on your IabHelper
instance. The QueryInventoryFinishedListener
argument specifies a listener that is notified when the query operation has completed and handles the query response. It is safe to make this call fom your main thread.
mHelper.queryInventoryAsync(mGotInventoryListener);
If the query is successful, the query results are stored in an Inventory
object that is passed back to the listener. The In-app Billing service returns only the purchases made by the user account that is currently logged in to the device.
IabHelper.QueryInventoryFinishedListener mGotInventoryListener =newIabHelper.QueryInventoryFinishedListener(){ publicvoid onQueryInventoryFinished(IabResult result, Inventory inventory){ if(result.isFailure()){ // handle error here } else{ // does the user have the premium upgrade? mIsPremium = inventory.hasPurchase(SKU_PREMIUM); // update UI accordingly } } };
Consume a Purchase
You can use the In-app Billing Version 3 API to track the ownership of purchased items in Google Play. Once an item is purchased, it is considered to be "owned" and cannot be purchased again from Google Play while in that state. You must send a consumption request for the item before Google Play makes it available for purchase again. All managed in-app products are consumable. How you use the consumption mechanism in your app is up to you. Typically, you would implement consumption for products with temporary benefits that users may want to purchase multiple times (for example, in-game currency or replenishable game tokens). You would typically not want to implement consumption for products that are purchased once and provide a permanent effect (for example, a premium upgrade).
It's your responsibility to control and track how the in-app product is provisioned to the user. For example, if the user purchased in-game currency, you should update the player's inventory with the amount of currency purchased.
Security Recommendation: You must send a consumption request before provisioning the benefit of the consumable in-app purchase to the user. Make sure that you have received a successful consumption response from Google Play before you provision the item.
To record a purchase consumption, call consumeAsync(Purchase, OnConsumeFinishedListener)
on your IabHelper
instance. The first argument that the method takes is the Purchase
object representing the item to consume. The second argument is a OnConsumeFinishedListener
that is notified when the consumption operation has completed and handles the consumption response from Google Play. It is safe to make this call fom your main thread.
In this example, you want to consume the gas item that the user has previously purchased in your app.
mHelper.consumeAsync(inventory.getPurchase(SKU_GAS), mConsumeFinishedListener);
The following example shows how to implement the OnConsumeFinishedListener
.
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = newIabHelper.OnConsumeFinishedListener(){ publicvoid onConsumeFinished(Purchase purchase,IabResult result){ if(result.isSuccess()){ // provision the in-app purchase to the user // (for example, credit 50 gold coins to player's character) } else{ // handle error } } };
Check for Consumable Items on Startup
It’s important to check for consumable items when the user starts up your application. Typically, you would first query the In-app Billing service for the items purchased by the user (via queryInventoryAsync
), then get the consumable Purchase
objects from the Inventory. If your application detects that are any consumable items that are owned by the user, you should send a consumption request to Google Play immediately and provision the item to the user. See the TrivialDrive
sample for an example of how to implement this checking at startup.
相关推荐
### Android Developer Guide - In-app Billing #### 一、In-app Billing 概览 In-app Billing 是一项由 Android Market 提供的服务,它允许开发者在应用程序内销售数字内容。这些内容可以包括但不限于可下载的...
**Google Play 应用内支付(In-app Billing V2 + V3)** Google Play 的应用内支付服务(In-app Billing)允许开发者在他们的应用程序中销售数字商品和服务,为用户提供便捷的购买体验,同时帮助开发者实现收入增长...
Android In-App Billing v3 Library This is a simple, straight-forward implementation of the Android v3 In-app billing API. It supports: In-App Product Purchases (both non-consumable and consumable) ...
### Google Play In-app Billing知识点详解 #### 一、概述 Google Play In-app Billing(应用内购)是Google为Android开发者提供的一个服务,允许用户在已安装的应用程序内进行购买操作,如购买虚拟商品、订阅服务...
**Android_Billing: Google Play In-app Billing 深度解析** 在移动应用开发领域,Google Play In-app Billing 是一个至关重要的功能,它允许开发者在应用内销售数字商品和服务,如额外的游戏关卡、订阅服务或者...
2. **集成Library**:将anjlab-android-inapp-billing-v3-88f7efa库导入到项目中,确保依赖正确配置。 3. **初始化**:在应用启动时,初始化`BillingClient`并建立连接,以便进行后续操作。 4. **查询商品**:使用`...
- **支付网关**:如Apple的App Store In-App Purchases、Google Play的Billing System,它们处理用户的支付信息,并确保交易安全。 - **后端服务器**:验证交易,管理用户账户,记录购买历史。 - **客户端集成**:...
react-native-billing, 在Android上,将本地桥反应到InApp计费 用于 Android 的InApp计费 本机计费 为 InApp Billing提供一个易于使用的界面,它通过包装anjlab计费库的 InApp来完成。const InAppBilling = require...
Godot-Myket-In-App-Billing插件的源代码包含在Godot-Myket-In-App-Billing-master压缩包中,开发者可以通过阅读源代码来了解其工作原理和实现细节,也可以根据自己的需求对其进行修改和扩展。这个插件的出现,极大...
Android In-App Billing Library which provides several Rx Methods for Purchasing, Consuming and Listing Products Based on the initial work of Anjlab (https://github.com/anjlab/android-inapp-billing-v3)...
Reactive Billing is a lightweight reactive wrapper around In App Billing API v3 for Android. Features Reactive: Exposes the In App Billing service methods as Observable, allowing to implement easy ...
标题"PyPI 官网下载 | django-aws-billing-0.1.3.tar.gz"表明这是一个在Python Package Index (PyPI)官网上可以找到的软件包,名为`django-aws-billing`,版本号为0.1.3,其分发形式是一个tar.gz压缩文件。...
一个基于Google的可示例代码来处理In-App-Billing V3( )的Android库。 该项目的目标是构建一个可靠且经过测试的库,可以轻松地将其作为包含在您的(基于Maven的)项目中。 Google的示例代码已经过重构并可以测试...
NULL 博文链接:https://jgtang82.iteye.com/blog/282364
Its main goal is to make integration of in-app products as simple and straightforward as possible: developers should not spend much time on implementing boring In-App Billing API but should focus on ...
在Android平台上,应用内购买(In-App Purchase,简称IAP)是一种常见的商业模式,允许开发者在应用程序中提供额外的功能、内容或服务以获取收入。本文将深入探讨如何在Android应用中实现IAP功能,主要关注Java语言...
3. **图元工厂(Figure Factory)**:用于创建和管理图形元素的工厂类,帮助开发者快速构建图形界面。 4. **手势和事件处理**:GEF支持多种手势(如鼠标点击、拖拽)和事件,允许开发者定义自定义的行为响应。 5. ...
google-billing-4.0.0.jar
在Android应用内实现Google Pay通常涉及到使用Google的In-app Billing服务。这是一个API,允许开发者在应用程序中销售数字内容和服务,包括但不限于应用内购买、订阅和一次性付费项目。Google Pay可以作为这些交易的...