diff options
author | Quddus Chong <quddusc@google.com> | 2012-12-08 17:06:30 -0800 |
---|---|---|
committer | Quddus Chong <quddusc@google.com> | 2012-12-09 18:24:06 -0800 |
commit | 768dba6d8cd6ad56fb6d4e7e30b3260e0d5e0ffd (patch) | |
tree | fa9512cf27bd2e1cc049893294da0da00e82925c /docs/html/training/in-app-billing | |
parent | 40f7a513bd12dd334e75f6aa7e2b59acdb29ba0f (diff) | |
download | frameworks_base-768dba6d8cd6ad56fb6d4e7e30b3260e0d5e0ffd.zip frameworks_base-768dba6d8cd6ad56fb6d4e7e30b3260e0d5e0ffd.tar.gz frameworks_base-768dba6d8cd6ad56fb6d4e7e30b3260e0d5e0ffd.tar.bz2 |
docs: Merged commits for final release of IABv3 docs.
Change-Id: Ib7c26052bb1b4de9a44d78817d7012839eaa953a
Diffstat (limited to 'docs/html/training/in-app-billing')
-rw-r--r-- | docs/html/training/in-app-billing/index.jd | 46 | ||||
-rw-r--r-- | docs/html/training/in-app-billing/list-iab-products.jd | 91 | ||||
-rw-r--r-- | docs/html/training/in-app-billing/preparing-iab-app.jd | 144 | ||||
-rw-r--r-- | docs/html/training/in-app-billing/purchase-iab-products.jd | 138 | ||||
-rw-r--r-- | docs/html/training/in-app-billing/test-iab-app.jd | 56 |
5 files changed, 475 insertions, 0 deletions
diff --git a/docs/html/training/in-app-billing/index.jd b/docs/html/training/in-app-billing/index.jd new file mode 100644 index 0000000..3d07481 --- /dev/null +++ b/docs/html/training/in-app-billing/index.jd @@ -0,0 +1,46 @@ +page.title=Selling In-app Products + +trainingnavtop=true +startpage=true +next.title=Preparing Your In-app Billing Application +next.link=preparing-iab-app.html + +@jd:body + +<div id="tb-wrapper"> +<div id="tb"> + +<!-- Required platform, tools, add-ons, devices, knowledge, etc. --> +<h2>Dependencies and prerequisites</h2> +<ul> + <li>Android 2.2 or higher</li> +</ul> + +<h2>You Should Also Read</h2> +<ul> +<li><a href="{@docRoot}google/play/billing/billing_overview.html">In-app Billing + Overview</a></li> +</ul> + +</div> +</div> +<p>In this class, you'll learn how to perform common In-app Billing operations from Android applications.</p> +<p>In-app billing is a service hosted on Google Play that lets you charge for digital content or for upgrades in your app. The In-app Billing API makes it easy for you to integrate In-app Billing into your applications. You can request product details from Google Play, issue orders for in-app products, and quickly retrieve ownership information based on users' purchase history. You can also query the Google Play service for details about in-app products, such as local pricing and availability. Google Play provides a checkout interface that makes user interactions with the In-app Billing service seamless, and provides a more intuitive experience to your users.</p> +<p>This class describes how to get started with the Version 3 API. To learn how to use the version 2 API, see <a href="{@docRoot}google/play/billing/v2/billing_integrate.html">Implementing In-App Billing (V2)</a>.</p> + +<h2>Lessons</h2> + +<!-- Create a list of the lessons in this class along with a short description of each lesson. +These should be short and to the point. It should be clear from reading the summary whether someone +will want to jump to a lesson or not.--> + +<dl> + <dt><b><a href="preparing-iab-app.html">Preparing Your In-app Billing Application</a></b></dt> + <dd>In this lesson, you will learn how to prepare your application to use the In-app Billing API and communicate with Google Play. You will also learn how to establish a connection to communicate with Google Play and verify that the In-app Billing API version that you are using is supported.</dd> + <dt><b><a href="list-iab-products.html">Establishing In-app Billing Products for Sale</a></b></dt> + <dd>In this lesson, you will learn how to specify the In-app Billing products for your app in Google Play and query the product details.</dd> + <dt><b><a href="purchase-iab-products.html">Purchase In-app Billing Products</a></b></dt> + <dd>In this lesson, you will learn how to purchase In-app Billing products, track consumption of purchased items, and query for details of purchased items.</dd> + <dt><b><a href="test-iab-app.html">Testing Your In-app Billing Application</a></b></dt> + <dd>In this lesson, you will learn how to test your application to ensure that In-app Billing is functioning correctly.</dd> +</dl> diff --git a/docs/html/training/in-app-billing/list-iab-products.jd b/docs/html/training/in-app-billing/list-iab-products.jd new file mode 100644 index 0000000..36ff34a --- /dev/null +++ b/docs/html/training/in-app-billing/list-iab-products.jd @@ -0,0 +1,91 @@ +page.title=Establishing In-app Billing Products for Sale +parent.title=Selling In-app Products +parent.link=index.html + +trainingnavtop=true +previous.title=Preparing Your In-app Billing Application +previous.link=preparing-iab-app.html +next.title=Purchasing In-app Billing Products +next.link=purchase-iab-products.html + +@jd:body + +<div id="tb-wrapper"> +<div id="tb"> + +<h2>This lesson teaches you to</h2> +<ol> + <li><a href="#DefineProducts">Specify In-app Products in Google Play</a></li> + <li><a href="#QueryDetails">Query In-app Product Details</a></li> +</ol> + +<h2>You should also read</h2> +<ul> +<li><a href="{@docRoot}google/play/billing/billing_overview.html">In-app Billing + Overview</a></li> +</ul> + + +</div> +</div> + +<p>Before publishing your In-app Billing application, you'll need to define the product list of digital goods available for purchase in the <a href="https://play.google.com/apps/publish/">Google Play Developer Console</a>. </p> + +<h2 id="DefineProducts">Specify In-app Products in Google Play</h2> +<p>From the Developer Console, you can define product information for in-app products and associate the product list with your application.</p> + +<p>To add new in-app products to your product list:</p> +<ol> +<li>Build a signed APK file for your In-app Billing application. To learn how to build and sign your APK, see <a href="{@docRoot}tools/publishing/preparing.html#publishing-build">Building Your Application for Release</a>. Make sure that you are using your final (not debug) certificate and private key to sign your application. +</li> +<li>In the Developer Console, open the application entry that you created earlier.</li> +<li>Click on the APK tab then click on Upload new APK. Upload the signed APK file to the Developer Console. Don’t publish the app yet!</li> +<li>Navigate to the uploaded app listing, and click on <strong>In-app Products</strong>. +<li>Click on the option to add a new product, then complete the form to specify the product information such as the item’s unique product ID (also called its <em>SKU</em>), description, price, and country availability. Note down the product ID since you might need this information to query purchase details in your application later. <p class="note"><strong>Important:</strong> The In-app Billing Version 3 service only supports managed in-app products, so make sure that you specify that the purchase type is 'Managed' when you add new items to your product list in the Developer Console.</p></li> +<li>Once you have completed the form, activate the product so that your application can purchase it. <p class="note"><strong>Warning:</strong> It may take up to 2-3 hours after uploading the APK for Google Play to recognize your updated APK version. If you try to test your application before your uploaded APK is recognized by Google Play, your application will receive a ‘purchase cancelled’ response with an error message “This version of the application is not enabled for In-app Billing.”</p></li> +</ol> + +<h2 id="QueryDetails">Query Items Available for Purchase</h2> +<p>You can query Google Play to programmatically retrieve details of the in-app products that are associated with your application (such as the product’s price, title, description, and type). This is useful, for example, when you want to display a listing of unowned items that are still available for purchase to users.</p> +<p class="note"><strong>Note:</strong> When making the query, you will need to specify the product IDs for the products explicitly. You can manually find the product IDs from the Developer Console by opening the <strong>In-app Products</strong> tab for your application. The product IDs are listed under the column labeled <strong>Name/ID</strong>.</p> +<p>To retrieve the product details, call {@code queryInventoryAsync(boolean, List, QueryInventoryFinishedListener)} on your IabHelper instance. +<ul> +<li>The first input argument indicates whether product details should be retrieved (should be set to {@code true}).</li> +<li>The {@code List} argument consists of one or more product IDs (also called SKUs) for the products that you want to query.</li> +<li>Finally, the {@code QueryInventoryFinishedListener} argument specifies a listener is notified when the query operation has completed and handles the query response.</li> +</ul> +If you use the the convenience classes provided in the sample, the classes will handle background thread management for In-app Billing requests, so you can safely make queries from the main thread of your application. +</p> +<p>The following code shows how you can retrieve the details for two products with IDs {@code SKU_APPLE} and {@code SKU_BANANA} that you previously defined in the Developer Console.</p> + +<pre> +List<String> additionalSkuList = new List<String>(); +additionalSkuList.add(SKU_APPLE); +additionalSkuList.add(SKU_BANANA); +mHelper.queryInventoryAsync(true, additionalSkuList, + mQueryFinishedListener); +</pre> + +<p>If the query is successful, the query results are stored in an {@code Inventory} object that is passed back to the listener.</p> +<p>The following code shows how you can retrieve the item prices from the result set.</p> + +<pre> +IabHelper.QueryInventoryFinishedListener + mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() { + public void onQueryInventoryFinished(IabResult result, Inventory inventory) + { + if (result.isFailure()) { + // handle error + return; + } + + String applePrice = + inventory.getSkuDetails(SKU_APPLE).getPrice(); + String bananaPrice = + inventory.getSkuDetails(SKU_BANANA).getPrice(); + + // update the UI + } +} +</pre> + diff --git a/docs/html/training/in-app-billing/preparing-iab-app.jd b/docs/html/training/in-app-billing/preparing-iab-app.jd new file mode 100644 index 0000000..ab33ccc --- /dev/null +++ b/docs/html/training/in-app-billing/preparing-iab-app.jd @@ -0,0 +1,144 @@ +page.title=Preparing Your In-app Billing Application +parent.title=Selling In-app Products +parent.link=index.html + +trainingnavtop=true +next.title=Establishing In-app Billing Products for Sale +next.link=list-iab-products.html + +@jd:body + +<!-- This is the training bar --> +<div id="tb-wrapper"> +<div id="tb"> + +<h2>This lesson teaches you to</h2> +<ol> + <li><a href="#GetSample">Download the Sample App</a></li> + <li><a href="#AddToDevConsole">Add Your App to the Developer Console</a></li> + <li><a href="#AddLibrary">Add the In-app Billing Library</a></li> + <li><a href="#SetPermission">Set the Billing Permission</a></li> + <li><a href="#Connect">Initiate a Connection with Google Play</a></li> +</ol> + +<h2>You should also read</h2> +<ul> +<li><a href="{@docRoot}google/play/billing/billing_overview.html">In-app Billing + Overview</a></li> +</ul> + +</div> +</div> + +<p>Before you can start using the In-app Billing service, you'll need to add the library that contains the In-app Billing Version 3 API to your Android project. You also need to setting the permissions for your application to communicate with Google Play. In addition, you'll need to establish a connection between your application and Google Play. You should also verify that the In-app Billing API version that you are using in your application is supported by Google Play.</p> + +<h2 id="GetSample">Download the Sample Application</h2> +<p>In this training class, you will use a reference implementation for the In-app Billing Version 3 API called the {@code TrivialDrive} sample application. The sample includes convenience classes to quickly set up the In-app Billing service, marshal and unmarshal data types, and handle In-app Billing requests from the main thread of your application. </p> +<p>To download the sample application:</p> +<ol> +<li>Open the <a href="{@docRoot}tools/help/sdk-manager.html">Android SDK Manager</a>.</li> +<li>In the SDK Manager, expand the {@code Extras} section.</li> +<li>Select <strong>Google Play Billing Library</strong>. Make sure to select the download for In-app Billing Version 3 or above.</li> +<li>Click <strong>Install</strong> to complete the download.</li> +</ol> +<p>The sample files will be installed to {@code /your/sdk/location/extras/google/play_billing/in-app-billing-v03}.</p> + +<h2 id="AddToDevConsole">Add Your Application to the Developer Console</h2> +<p>The Google Play Developer Console is where you publish your In-app Billing application and manage the various digital goods that are available for purchase from your application. When you create a new application entry in the Developer Console, it automatically generates a public license key for your application. You will need this key to establish a trusted connection from your application to the Google Play servers. You only need to generate this key once per application, and don’t need to repeat these steps when you update the APK file for your application.</p> +<p>To add your application to the Developer Console:</p> +<ol> +<li>Go to the <a href="http://play.google.com/apps/publish">Google Play Developer Console</a> site and log in. You will need to register for a new developer account, if you have not registered previously. To sell in-app items, you also need to have a <a href="http://www.google.com/wallet/merchants.html">Google Checkout Merchant</a> account.</li> +<li>Click on <strong>Try the new design</strong> to access the preview version of the Developer Console, if you are not already logged on to that version. </li> +<li>In the <strong>All Applications</strong> tab, add a new application entry. +<ol type="a"> +<li>Click <strong>Add new application</strong>.</li> +<li>Enter a name for your new In-app Billing application.</li> +<li>Click <strong>Prepare Store Listing</strong>.</li> +</ol> +</li> +<li>In the <strong>Services & APIs</strong> tab, find and make a note of the public license key that Google Play generated for your application. This is a Base64 string that you will need to include in your application code later.</li> +</ol> +<p>Your application should now appear in the list of applications in Developer Console.</p> + +<h2 id="AddLibrary">Add the In-app Billing Library</h2> +<p>To use the In-app Billing Version 3 features, you must add the {@code IInAppBillingService.aidl} file to your Android project. This Android Interface Definition Language (AIDL) file defines the interface to the Google Play service.</p> +<p>You can find the {@code IInAppBillingService.aidl} file in the provided sample app. Depending on whether you are creating a new application or modifying an existing application, follow the instructions below to add the In-app Billing Library to your project.</p> +<h3>New Project</h3> +<p>To add the In-app Billing Version 3 library to your new In-app Billing project:</p> +<ol> +<li>Copy the {@code TrivialDrive} sample files into your Android project.</li> +<li>Modify the package name in the files you copied to use the package name for your project. In Eclipse, you can use this shortcut: right-click the package name, then select <strong>Refactor</strong> > <strong>Rename</strong>.</li> +<li>Open the {@code AndroidManifest.xml} file and update the package attribute value to use the package name for your project.</li> +<li>Fix import statements as needed so that your project compiles correctly. In Eclipse, you can use this shortcut: press <strong>Ctrl+Shift+O</strong> in each file showing errors.</li> +<li>Modify the sample to create your own application. Remember to copy the Base64 public license key for your application from the Developer Console over to your {@code MainActivity.java}.</li> +</ol> + +<h3>Existing Project</h3> +<p>To add the In-app Billing Version 3 library to your existing In-app Billing project:</p> +<ol> +<li>Copy the {@code IInAppBillingService.aidl} file to your Android project. + <ul> + <li>If you are using Eclipse: Import the {@code IInAppBillingService.aidl} file into your {@code /src} directory.</li> + <li>If you are developing in a non-Eclipse environment: Create the following directory {@code /src/com/android/vending/billing} and copy the {@code IInAppBillingService.aidl} file into this directory.</li> + </ul> +</li> +<li>Build your application. You should see a generated file named {@code IInAppBillingService.java} in the {@code /gen} directory of your project.</li> +<li>Add the helper classes from the {@code /util} directory of the {@code TrivialDrive} sample to your project. Remember to change the package name declarations in those files accordingly so that your project compiles correctly.</li> +</ol> + +<p>Your project should now contain the In-app Billing Version 3 library.</p> + +<h2 id="SetPermission">Set the Billing Permission</h2> +<p>Your app needs to have permission to communicate request and response messages to the Google Play’s billing service. To give your app the necessary permission, add this line in your {@code AndroidManifest.xml} manifest file:</p> +<pre> +<uses-permission android:name="com.android.vending.BILLING" /> +</pre> + +<h2 id="Connect">Initiate a Connection with Google Play</h2> +<p>You must bind your Activity to Google Play’s In-app Billing service to send In-app Billing requests to Google Play from your application. The convenience classes provided in the sample handles the binding to the In-app Billing service, so you don’t have to manage the network connection directly.</p> +<p>To set up synchronous communication with Google Play, create an {@code IabHelper} instance in your activity's {@code onCreate} method. In the constructor, pass in the {@code Context} for the activity, along with a string containing the public license key that was generated earlier by the Google Play Developer Console. </p> +<p class="note"><strong>Security Recommendation:</strong> It is highly recommended that you do not hard-code the exact public license key string value as provided by Google Play. Instead, you can construct the whole public license key string at runtime from substrings, or retrieve it from an encrypted store, before passing it to the constructor. This approach makes it more difficult for malicious third-parties to modify the public license key string in your APK file.</p> + +<pre> +IabHelper mHelper; + +@Override +public void onCreate(Bundle savedInstanceState) { + // ... + String base64EncodedPublicKey; + + // compute your public key and store it in base64EncodedPublicKey + mHelper = new IabHelper(this, base64EncodedPublicKey); +} +</pre> + +<p>Next, perform the service binding by calling the {@code startSetup} method on the {@code IabHelper} instance that you created. Pass the method an {@code OnIabSetupFinishedListener} instance, which is called once the {@code IabHelper} completes the asynchronous setup operation. As part of the setup process, the {@code IabHelper} also checks if the In-app Billing Version 3 API is supported by Google Play. If the API version is not supported, or if an error occured while establishing the service binding, the listener is notified and passed an {@code IabResult} object with the error message.</p> + +<pre> +mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { + public void onIabSetupFinished(IabResult result) { + if (!result.isSuccess()) { + // Oh noes, there was a problem. + Log.d(TAG, "Problem setting up In-app Billing: " + result); + } + // Hooray, IAB is fully set up! + } +}); +</pre> + +<p>If the setup completed successfully, you can now use the {@code mHelper} reference to communicate with the Google Play service. When your application is launched, it is a good practice to query Google Play to find out what in-app items are owned by a user. This is covered further in the <a href="{@docRoot}training/in-app-billing/purchase-iab-products.html#QueryPurchases">Query Purchased Items</a> section.</p> + +<p class="note"><strong>Important:</strong> Remember to unbind from the In-app Billing service when you are done with your activity. If you don’t unbind, the open service connection could cause your device’s performance to degrade. To unbind and free your system resources, call the {@code IabHelper}'s {@code dispose} method when your {@code Activity} gets destroyed.</p> + +<pre> +@Override +public void onDestroy() { + if (mHelper != null) mHelper.dispose(); + mHelper = null; +} +</pre> + + + + + diff --git a/docs/html/training/in-app-billing/purchase-iab-products.jd b/docs/html/training/in-app-billing/purchase-iab-products.jd new file mode 100644 index 0000000..d5f6634 --- /dev/null +++ b/docs/html/training/in-app-billing/purchase-iab-products.jd @@ -0,0 +1,138 @@ +page.title=Purchasing In-app Billing Products +parent.title=Selling In-app Products +parent.link=index.html + +trainingnavtop=true +previous.title=Establishing In-app Billing Products for Sale +previous.link=list-iab-products.html +next.title=Testing Your In-app Billing Application +next.link=test-iab-app.html + +@jd:body + +<div id="tb-wrapper"> +<div id="tb"> + +<h2>This lesson teaches you to</h2> +<ol> + <li><a href="#Purchase">Purchase an Item</a></li> + <li><a href="#QueryPurchases">Query Purchased Items</a></li> + <li><a href="#Consume">Consume a Purchase</a></li> +</ol> + +<h2>You should also read</h2> +<ul> +<li><a href="{@docRoot}google/play/billing/billing_overview.html">In-app Billing + Overview</a></li> +</ul> +</div> +</div> + +<p>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.</p> +<p>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.</p> +<p>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.</p> + +<h2 id="Purchase">Purchase an Item</h2> + +<p>To start a purchase request from your app, call {@code launchPurchaseFlow(Activity, String, int, OnIabPurchaseFinishedListener, String)} on your {@code IabHelper} instance. You must make this call from the main thread of your {@code Activity}. Here’s an explaination of the {@code launchPurchaseFlow} method parameters:</p> +<ul> +<li>The first argument is the calling {@code Activity}.</li> +<li>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. </li> +<li>The third argument is a request code value. This value can be any positive integer. Google Play reurns this request code to the calling {@code Activity}’s {@code onActivityResult} along with the purchase response.</li> +<li>The fourth argument is a listener that is notified when the purchase operation has completed and handles the purchase response from Google Play.</li> +<li>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. <p class="note"><strong>Security Recommendation:</strong> 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.</p></li> +</ul> +<p>The following example shows how you can make a purchase request for a product with ID {@code SKU_GAS}, using an arbitrary value of 10001 for the request code, and an encoded developer payload string.</p> + +<pre> +mHelper.launchPurchaseFlow(this, SKU_GAS, 10001, + mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ"); +</pre> + +<p>If the purchase order is successful, the response data from Google Play is stored in an {@code Purchase} object that is passed back to the listener.</p> + +<p>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 <a href="{@docRoot}training/in-app-billing/purchase-iab-products.html#Consume">Consuming Products</a> 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.</p> + +<pre> +IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener + = new IabHelper.OnIabPurchaseFinishedListener() { + public void onIabPurchaseFinished(IabResult result, Purchase purchase) + { + if (result.isFailure()) { + Log.d(TAG, "Error purchasing: " + result); + return; + } + else if (purchase.getSku().equals(SKU_GAS)) { + // consume the gas and update the UI + } + else (purchase.getSku().equals(SKU_PREMIUM)) { + // give user access to premium content and update the UI + } + } +}; +</pre> + +<p class="note"><strong>Security Recommendation:</strong> When you receive the purchase response from Google Play, make sure to check the returned data signature, the {@code orderId}, and the {@code developerPayload} string in the {@code Purchase} object to make sure that you are getting the expected values. You should verify that the {@code orderId} is a unique value that you have not previously processed, and the {@code 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. </p> + +<h2 id="QueryPurchases">Query Purchased Items</h2> +<p>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.</p> + +<p>To retrieve the user’s purchases from your app, call {@code queryInventoryAsync(QueryInventoryFinishedListener)} on your {@code IabHelper} instance. The {@code 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.</p> + +<pre> +mHelper.queryInventoryAsync(mGotInventoryListener); +</pre> + +<p>If the query is successful, the query results are stored in an {@code 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.</p> + +<pre> +IabHelper.QueryInventoryFinishedListener mGotInventoryListener + = new IabHelper.QueryInventoryFinishedListener() { + public void 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 + } + } +}; +</pre> + +<h2 id="Consume">Consume a Purchase</h2> +<p>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 replensihable 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).</p> +<p>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.</p> +<p class="note"><strong>Security Recommendation:</strong> 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.</p> +<p>To record a purchase consumption, call {@code consumeAsync(Purchase, OnConsumeFinishedListener)} on your {@code IabHelper} instance. The first argument that the method takes is the {@code Purchase} object representing the item to consume. The second argument is a {@code 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.</p> +<p>In this example, you want to consume the gas item that the user has previously purchased in your app.</p> + +<pre> +mHelper.consumeAsync(inventory.getPurchase(SKU_GAS), + mConsumeFinishedListener); +</pre> + +<p>The following example shows how to implement the {@code OnConsumeFinishedListener}.</p> + +<pre> +IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = + new IabHelper.OnConsumeFinishedListener() { + public void 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 + } + } +}; +</pre> + +<h3>Check for Consumable Items on Startup</h3> +<p>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 {@code queryInventoryAsync}), then get the consumable {@code 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 {@code TrivialDrive} sample for an example of how to implement this checking at startup.</p> + + diff --git a/docs/html/training/in-app-billing/test-iab-app.jd b/docs/html/training/in-app-billing/test-iab-app.jd new file mode 100644 index 0000000..376b981 --- /dev/null +++ b/docs/html/training/in-app-billing/test-iab-app.jd @@ -0,0 +1,56 @@ +page.title=Testing Your In-app Billing Application +parent.title=Selling In-app Products +parent.link=index.html + +trainingnavtop=true +previous.title=Purchasing In-app Billing Products +previous.link=purchase-iab-products.html + +@jd:body + +<div id="tb-wrapper"> +<div id="tb"> + +<h2>This lesson teaches you to</h2> +<ol> + <li><a href="#StaticTest">Test with Static Responses</a></li> + <li><a href="#PlayItemTest">Test with Your Own Product IDs</a></li> +</ol> + +<h2>You should also read</h2> +<ul> +<li><a href="{@docRoot}google/play/billing/billing_overview.html">In-app Billing + Overview</a></li> +</ul> + +</div> +</div> + +<p>To ensure that In-app Billing is functioning correctly in your application, you should test the test the application before you publish it on Google Play. Early testing also helps to ensure that the user flow for purchasing in-app items is not confusing or slow, and that users can see their newly purchased items in a timely way.</p> + +<h2 id="StaticTest">Test with Static Responses</h2> +Test your In-app Billing application with static responses by using Google Play’s reserved product IDs.By using reserved product IDs instead of actual product IDs, you can test the purchase flow without specifying an actual payment method or transferring money. To learn more about the reserved product IDs, see <a href="{@docRoot}google/play/billing/billing_testing.html">Testing In-app Billing</a>. + +<h2 id="PlayItemTest">Test with Your Own Product IDs</h2> +<p>Because Google Play does not allow you to use your developer account to directly purchase in-app products that you have created yourself, you'll need to create test acccounts under your developer account profile. To create a test account, simply enter a valid Google email address. Users with these test accounts will then be able to make in-app-billing purchases from uploaded, unpublished applications that you manage.</p> +<p>To test your In-app Billing Version 3 application using your own product IDs: +<ol> +<li>In the Developer Console, add one or more tester accounts to the developer account that you are using to publish your application. + <ol type="a"> + <li>Login to the <a href="https://play.google.com/apps/publish/" target="_blank">Developer Console</a> with your developer account.</li> + <li>Click <strong>Settings</strong> > <strong>Account</strong> details, then in the <strong>License Testing</strong> section, add the Google email addresses for your tester accounts.</li> +</ol> +<li>Build a signed APK file for your In-app Billing application. To learn how to build and sign your APK, see <a href="{@docRoot}tools/publishing/preparing.html#publishing-build">Building Your Application for Release</a>. Make sure that you are using your final (not debug) certificate and private key to sign your application. +</li> +<li>Make sure that you have uploaded the signed APK for your application to the Developer Console, and associated one or more in-app products with your application. You don't need to publish the application on Google Play to test it. <p class="note"><strong>Warning:</strong> It may take up to 2-3 hours after uploading the APK for Google Play to recognize your updated APK version. If you try to test your application before your uploaded APK is recognized by Google Play, your application will receive a ‘purchase cancelled’ response with an error message “This version of the application is not enabled for In-app Billing.”</p></li> +<li>Install the APK file to your physical test device by using the {@code adb} tool. To learn how to install the application, see <a href="{@docRoot}tools/building/building-cmdline.html#RunningOnDevice">Running on a Device</a>. Make sure that: + <ul> + <li>Your test device is running on Android SDK Version 2.2 (API level 8) or higher, and is installed with Google Play client Version 3.9.16 or higher.</li> + <li>The {@code android:versionCode} and {@code android:versionName} attributes values in the {@code AndroidManifest.xml} of the application that you are installing matches the values of your APK in the Developer Console.</li> + <li>Your application is signed with the same certificate that you used for the APK that you uploaded to the Developer Console, before installing it on your device.</li> + </ul> +</li> +<li>Login to the test device by using a tester account. Test your In-app Billing application by purchasing a few items, and fix any issues that you encounter. Remember to refund the purchases if you don’t want your testers to be actually charged!</li> +</ol> +</p> + |