Home | History | Annotate | Download | only in v2
      1 page.title=In-app Billing Reference <span style="font-size:16px;">(IAB Version 2)</span>
      2 excludeFromSuggestions=true
      3 @jd:body
      4 
      5 <div style="background-color:#fffdeb;width:100%;margin-bottom:1em;padding:.5em;">In-app Billing Version 2 is superseded. Please <a href="{@docRoot}google/play/billing/billing_overview.html#migration">migrate to Version 3</a> at your earliest convenience.</div>
      6     <div id="qv-wrapper" style="margin-top:0;">
      7 <div id="qv">
      8 <h2>In this document</h2>
      9   <ol>
     10   <li><a href="#billing-codes">Server Response Codes</a></li>
     11   <li><a href="#billing-interface">API Reference</a></li>
     12   <li><a href="#billing-intents">Broadcast Intents</a></li>
     13   <li><a href="#http-api">REST API for Subscriptions</a></li>
     14   </ol>	
     15 
     16 <h2>Related Samples</h2>
     17   <ol>
     18   <li><a href="{@docRoot}google/play/billing/v2/billing_integrate.html#billing-download">Sample
     19     Application (V2)</a></li>
     20   </ol>  
     21 
     22 </div>
     23 </div>
     24 <p>This documentation provides technical reference information for using the In-app Billing Version 2 API. </p>
     25 
     26 <h2 id="billing-codes">Server Response Codes</h2>
     27 <p>The following table lists all of the server response codes that are sent from Google Play to
     28 your application. Google Play sends these response codes asynchronously as
     29 <code>response_code</code> extras in the <code>com.android.vending.billing.RESPONSE_CODE</code>
     30 broadcast intent. Your application must handle all of these response codes.</p>
     31 
     32 <p class="table-caption" id="response-codes-table"><strong>Table 6.</strong> Summary of response
     33 codes returned by Google Play.</p>
     34 
     35 <table>
     36 <tr>
     37 <th>Response Code</th>
     38 <th>Value</th>
     39 <th>Description</th>
     40 </tr>
     41 <tr>
     42   <td><code>RESULT_OK</code></td>
     43   <td>0</td>
     44   <td>Indicates that the request was sent to the server successfully. When this code is returned in
     45   response to a <code>CHECK_BILLING_SUPPORTED</code> request, indicates that billing is
     46   supported.</td>
     47 </tr>
     48 <tr>
     49   <td><code>RESULT_USER_CANCELED</code></td>
     50   <td>1</td>
     51   <td>Indicates that the user pressed the back button on the checkout page instead of buying the
     52   item.</td>
     53 </tr>
     54 <tr>
     55   <td><code>RESULT_SERVICE_UNAVAILABLE</code></td>
     56   <td>2</td>
     57   <td>Indicates that the network connection is down.</td>
     58 </tr>
     59 <tr>
     60   <td><code>RESULT_BILLING_UNAVAILABLE</code></td>
     61   <td>3</td>
     62   <td>Indicates that In-app Billing is not available because the <code>API_VERSION</code> that you
     63   specified is not recognized by the Google Play application or the user is ineligible for in-app
     64   billing (for example, the user resides in a country that prohibits in-app purchases).</td>
     65 </tr>
     66 <tr>
     67   <td><code>RESULT_ITEM_UNAVAILABLE</code></td>
     68   <td>4</td>
     69   <td>Indicates that Google Play cannot find the requested item in the application's product
     70   list. This can happen if the product ID is misspelled in your <code>REQUEST_PURCHASE</code>
     71   request or if an item is unpublished in the application's product list.</td>
     72 </tr>
     73 <tr>
     74   <td><code>RESULT_DEVELOPER_ERROR</code></td>
     75   <td>5</td>
     76   <td>Indicates that an application is trying to make an In-app Billing request but the application
     77   has not declared the com.android.vending.BILLING permission in its manifest. Can also indicate
     78   that an application is not properly signed, or that you sent a malformed request, such as a
     79   request with missing Bundle keys or a request that uses an unrecognized request type.</td>
     80 </tr>
     81 <tr>
     82   <td><code>RESULT_ERROR</code></td>
     83   <td>6</td>
     84   <td>Indicates an unexpected server error. For example, this error is triggered if you try to
     85 purchase an item from yourself, which is not allowed by Google Wallet.</td>
     86 </tr>
     87 </table>
     88 </p>
     89 
     90 <h3 id="billing-interface">In-app billing Version 2 API reference</h3>
     91 
     92 <p>The following section describes the interface for Google Play's In-app Billing service. The
     93 interface is defined in the <code>IMarketBillingService.aidl</code> file, which is included with the
     94 In-app Billing <a
     95 href="{@docRoot}google/play/billing/v2/billing_integrate.html#billing-download">sample
     96 application</a>.</p>
     97 <p>The interface consists of a single request method <code>sendBillingRequest()</code>. This method
     98 takes a single {@link android.os.Bundle} parameter. The Bundle parameter includes several key-value
     99 pairs, which are summarized in table 7.</p>
    100 
    101 <p class="table-caption"><strong>Table 7.</strong> Description of Bundle keys passed in a
    102 <code>sendBillingRequest()</code> request.</p>
    103 
    104 <table>
    105 
    106 <tr>
    107 <th>Key</th>
    108 <th>Type</th>
    109 <th>Possible Values</th>
    110 <th>Required?</th>
    111 <th>Description</th>
    112 </tr>
    113 <tr>
    114   <td><code>BILLING_REQUEST</code></td>
    115   <td><code>String</code></td>
    116   <td><code>CHECK_BILLING_SUPPORTED</code>, <code>REQUEST_PURCHASE</code>,
    117   <code>GET_PURCHASE_INFORMATION</code>, <code>CONFIRM_NOTIFICATIONS</code>, or
    118   <code>RESTORE_TRANSACTIONS</code></td>
    119   <td>Yes</td>
    120   <td>The type of billing request you are making with the <code>sendBillingRequest()</code> request.
    121   The possible values are discussed more below this table.</td>
    122 </tr>
    123 <tr>
    124   <td><code>API_VERSION</code></td>
    125   <td><code>int</code></td>
    126   <td>  <ul>
    127   <li><code>"2"</code> [<a href="#version_2">details</a>]</li>
    128   <li><code>"1"</code> [<a href="#version_1">details</a>]</li>
    129   </ul></td>
    130   <td>Yes</td>
    131   <td>The version of Google Play's In-app Billing service you want to use.</td>
    132 </tr>
    133 <tr>
    134   <td><code>PACKAGE_NAME</code></td>
    135   <td><code>String</code></td>
    136   <td>A valid package name.</td>
    137   <td>Yes</td>
    138   <td>The name of the application that is making the request.</td>
    139 </tr>
    140 <tr>
    141   <td><code>ITEM_ID</code></td>
    142   <td><code>String</code></td>
    143   <td>Any valid product identifier.</td>
    144   <td>Required for <code>REQUEST_PURCHASE</code> requests.</td>
    145   <td>The product ID of the item you are making a billing request for. Every in-app item that you
    146   sell using Google Play's In-app Billing service must have a unique product ID, which you
    147   specify on the Google Play Developer Console.</td>
    148 </tr>
    149 <tr>
    150   <td><code>NONCE</code></td>
    151   <td><code>long</code></td>
    152   <td>Any valid <code>long</code> value.</td>
    153   <td>Required for <code>GET_PURCHASE_INFORMATION</code> and <code>RESTORE_TRANSACTIONS</code>
    154   requests.</td>
    155   <td>A number used once. Your application must generate and send a nonce with each
    156   <code>GET_PURCHASE_INFORMATION</code> and <code>RESTORE_TRANSACTIONS</code> request. The nonce is
    157   returned with the <code>PURCHASE_STATE_CHANGED</code> broadcast intent, so you can use this value
    158   to verify the integrity of transaction responses form Google Play.</td>
    159 </tr>
    160 <tr>
    161   <td><code>NOTIFY_IDS</code></td>
    162   <td>Array of <code>long</code> values</td>
    163   <td>Any valid array of <code>long</code> values</td>
    164   <td>Required for <code>GET_PURCHASE_INFORMATION</code> and <code>CONFIRM_NOTIFICATIONS</code>
    165   requests.</td>
    166   <td>An array of notification identifiers. A notification ID is sent to your application in an
    167   <code>IN_APP_NOTIFY</code> broadcast intent every time a purchase changes state. You use the
    168   notification to retrieve the details of the purchase state change.</td>
    169 </tr>
    170 <tr>
    171   <td><code>DEVELOPER_PAYLOAD</code></td>
    172   <td><code>String</code></td>
    173   <td>Any valid <code>String</code> less than 256 characters long.</td>
    174   <td>No</td>
    175   <td>A developer-specified string that can be specified when you make a
    176   <code>REQUEST_PURCHASE</code> request. This field is returned in the JSON string that contains
    177   transaction information for an order. You can use this key to send supplemental information with
    178   an order. For example, you can use this key to send index keys with an order, which is useful if
    179   you are using a database to store purchase information. We recommend that you do not use this key
    180   to send data or content.</td>
    181 </tr>
    182 </table>
    183 
    184 <p>The <code>BILLING_REQUEST</code> key can have the following values:</p>
    185 
    186 <ul>
    187   <li><code>CHECK_BILLING_SUPPORTED</code>
    188     <p>This request verifies that the Google Play application supports In-app Billing. You
    189     usually send this request when your application first starts up. This request is useful if you
    190     want to enable or disable certain UI features that are relevant only to In-app Billing.</p>
    191   </li>
    192   <li><code>REQUEST_PURCHASE</code>
    193     <p>This request sends a purchase message to the Google Play application and is the foundation
    194     of In-app Billing. You send this request when a user indicates that he or she wants to purchase
    195     an item in your application. Google Play then handles the financial transaction by displaying
    196     the checkout user interface.</p>
    197   </li>
    198   <li><code>GET_PURCHASE_INFORMATION</code>
    199     <p>This request retrieves the details of a purchase state change. A purchase state change can
    200     occur when a purchase request is billed successfully or when a user cancels a transaction during
    201     checkout. It can also occur when a previous purchase is refunded. Google Play notifies your
    202     application when a purchase changes state, so you only need to send this request when there is
    203     transaction information to retrieve.</p>
    204   </li>
    205   <li><code>CONFIRM_NOTIFICATIONS</code>
    206     <p>This request acknowledges that your application received the details of a purchase state
    207     change. That is, this message confirms that you sent a <code>GET_PURCHASE_INFORMATION</code>
    208     request for a given notification and that you received the purchase information for the
    209     notification.</p>
    210   </li>
    211   <li><code>RESTORE_TRANSACTIONS</code>
    212     <p>This request retrieves a user's transaction status for managed purchases (see <a
    213     href="{@docRoot}google/play/billing/billing_admin.html#billing-purchase-type">Choosing a
    214     Purchase Type</a> for more information). You should send this message only when you need to
    215     retrieve a user's transaction status, which is usually only when your application is reinstalled
    216     or installed for the first time on a device.</p>
    217   </li>
    218 </ul>
    219 
    220 <p>Every In-app Billing request generates a synchronous response. The response is a {@link
    221 android.os.Bundle} and can include one or more of the following keys:</p>
    222 
    223 <ul>
    224   <li><code>RESPONSE_CODE</code>
    225     <p>This key provides status information and error information about a request.</p>
    226   </li>
    227   <li><code>PURCHASE_INTENT</code>
    228     <p>This key provides a {@link android.app.PendingIntent}, which you use to launch the checkout
    229     activity.</p>
    230   </li>
    231   <li><code>REQUEST_ID</code>
    232     <p>This key provides you with a request identifier, which you can use to match asynchronous
    233     responses with requests.</p>
    234   </li>
    235 </ul>
    236 
    237 <p>Some of these keys are not relevant to certain types of requests. Table 8 shows which keys are
    238 returned for each request type.</p>
    239 
    240 <p class="table-caption"><strong>Table 8.</strong> Description of Bundle keys that are returned with
    241 each In-app Billing request type.</p>
    242 
    243 <table>
    244 
    245 <tr>
    246 <th>Request Type</th>
    247 <th>Keys Returned</th>
    248 <th>Possible Response Codes</th>
    249 </tr>
    250 <tr>
    251   <td><code>CHECK_BILLING_SUPPORTED</code></td>
    252   <td><code>RESPONSE_CODE</code></td>
    253   <td><code>RESULT_OK</code>, <code>RESULT_BILLING_UNAVAILABLE</code>, <code>RESULT_ERROR</code>,
    254   <code>RESULT_DEVELOPER_ERROR</code></td>
    255 </tr>
    256 <tr>
    257   <td><code>REQUEST_PURCHASE</code></td>
    258   <td><code>RESPONSE_CODE</code>, <code>PURCHASE_INTENT</code>, <code>REQUEST_ID</code></td>
    259   <td><code>RESULT_OK</code>, <code>RESULT_ERROR</code>, <code>RESULT_DEVELOPER_ERROR</code></td>
    260 </tr>
    261 <tr>
    262   <td><code>GET_PURCHASE_INFORMATION</code></td>
    263   <td><code>RESPONSE_CODE</code>, <code>REQUEST_ID</code></td>
    264   <td><code>RESULT_OK</code>, <code>RESULT_ERROR</code>, <code>RESULT_DEVELOPER_ERROR</code></td>
    265 </tr>
    266 <tr>
    267   <td><code>CONFIRM_NOTIFICATIONS</code></td>
    268   <td><code>RESPONSE_CODE</code>, <code>REQUEST_ID</code></td>
    269   <td><code>RESULT_OK</code>, <code>RESULT_ERROR</code>, <code>RESULT_DEVELOPER_ERROR</code></td>
    270 </tr>
    271 <tr>
    272   <td><code>RESTORE_TRANSACTIONS</code></td>
    273   <td><code>RESPONSE_CODE</code>, <code>REQUEST_ID</code></td>
    274   <td><code>RESULT_OK</code>, <code>RESULT_ERROR</code>, <code>RESULT_DEVELOPER_ERROR</code></td>
    275 </tr>
    276 </table>
    277 
    278 <h3 id="billing-intents">In-app billing broadcast intents</h3>
    279 
    280 <p>The following section describes the In-app Billing broadcast intents that are sent by the Google
    281 Play application. These broadcast intents inform your application about In-app Billing actions
    282 that have occurred. Your application must implement a {@link android.content.BroadcastReceiver} to
    283 receive these broadcast intents, such as the <code>BillingReceiver</code> that's shown in the in-app
    284 billing <a href="{@docRoot}google/play/billing/v2/billing_integrate.html#billing-download">sample
    285 application</a>.</p>
    286 
    287 <h4>com.android.vending.billing.RESPONSE_CODE</h4>
    288 
    289 <p>This broadcast intent contains a Google Play response code, and is sent after you make an
    290 In-app Billing request. A server response code can indicate that a billing request was successfully
    291 sent to Google Play or it can indicate that some error occurred during a billing request. This
    292 intent is not used to report any purchase state changes (such as refund or purchase information).
    293 For more information about the response codes that are sent with this response, see <a
    294 href="#billing-codes">Google Play Response Codes for In-app Billing</a>. The sample application
    295 assigns this broadcast intent to a constant named <code>ACTION_RESPONSE_CODE</code>.</p>
    296 
    297 <h5>Extras</h5>
    298 
    299 <ul type="none">
    300   <li><code>request_id</code>&mdash;a <code>long</code> representing a request ID. A request ID
    301   identifies a specific billing request and is returned by Google Play at the time a request is
    302   made.</li>
    303   <li><code>response_code</code>&mdash;an <code>int</code> representing the Google Play server
    304   response code.</li>
    305 </ul>
    306 
    307 <h4>com.android.vending.billing.IN_APP_NOTIFY</h4>
    308 
    309 <p>This response indicates that a purchase has changed state, which means a purchase succeeded, was
    310 canceled, or was refunded. This response contains one or more notification IDs. Each notification ID
    311 corresponds to a specific server-side message, and each messages contains information about one or
    312 more transactions. After your application receives an <code>IN_APP_NOTIFY</code> broadcast intent,
    313 you send a <code>GET_PURCHASE_INFORMATION</code> request with the notification IDs to retrieve the
    314 message details. The sample application assigns this broadcast intent to a constant named
    315 <code>ACTION_NOTIFY</code>.</p>
    316 
    317 <h5>Extras</h5>
    318 
    319 <ul type="none">
    320   <li><code>notification_id</code>&mdash;a <code>String</code> representing the notification ID for
    321   a given purchase state change. Google Play notifies you when there is a purchase state change
    322   and the notification includes a unique notification ID. To get the details of the purchase state
    323   change, you send the notification ID with the <code>GET_PURCHASE_INFORMATION</code> request.</li>
    324 </ul>
    325 
    326 <h4>com.android.vending.billing.PURCHASE_STATE_CHANGED</h4>
    327 
    328 <p>This broadcast intent contains detailed information about one or more transactions. The
    329 transaction information is contained in a JSON string. The JSON string is signed and the signature
    330 is sent to your application along with the JSON string (unencrypted). To help ensure the security of
    331 your In-app Billing messages, your application can verify the signature of this JSON string. The
    332 sample application assigns this broadcast intent to a constant named
    333 <code>ACTION_PURCHASE_STATE_CHANGED</code>.</p>
    334 
    335 <h5>Extras</h5>
    336 
    337 <ul type="none">
    338   <li><code>inapp_signed_data</code>&mdash;a <code>String</code> representing the signed JSON
    339   string.</li>
    340   <li><code>inapp_signature</code>&mdash;a <code>String</code> representing the signature.</li>
    341 </ul>
    342 
    343 <p class="note"><strong>Note:</strong> Your application should map the broadcast intents and extras
    344 to constants that are unique to your application. See the <code>Consts.java</code> file in the
    345 sample application to see how this is done.</p>
    346 
    347 <p>The fields in the JSON string are described in the following table (see table 9):</p>
    348 
    349 <p class="table-caption"><strong>Table 9.</strong> Description of JSON fields that are returned with
    350 a <code>PURCHASE_STATE_CHANGED</code> intent.</p>
    351 
    352 <table>
    353 
    354 <tr>
    355 <th>Field</th>
    356 <th>Description</th>
    357 </tr>
    358 <tr>
    359   <td>nonce</td>
    360   <td>A number used once. Your application generates the nonce and sends it with the
    361   <code>GET_PURCHASE_INFORMATION</code> request. Google Play sends the nonce back as part of the
    362   JSON string so you can verify the integrity of the message.</td>
    363 </tr>
    364 <tr>
    365   <td>notificationId</td>
    366   <td>A unique identifier that is sent with an <code>IN_APP_NOTIFY</code> broadcast intent. Each
    367   <code>notificationId</code> corresponds to a specify message that is waiting to be retrieved on
    368   the Google Play server. Your application sends back the <code>notificationId</code> with the
    369   <code>GET_PURCHASE_INFORMATION</code> message so Google Play can determine which messages you
    370   are retrieving.</td>
    371 </tr>
    372 <tr>
    373   <td>orderId</td>
    374   <td>A unique order identifier for the transaction. This corresponds to the Google Wallet Order
    375   ID.</td>
    376 </tr>
    377 <tr>
    378   <td>packageName</td>
    379   <td>The application package from which the purchase originated.</td>
    380 </tr>
    381 <tr>
    382   <td>productId</td>
    383   <td>The item's product identifier. Every item has a product ID, which you must specify in the
    384   application's product list on the Google Play Developer Console.</td>
    385 </tr>
    386 <tr>
    387   <td>purchaseTime</td>
    388   <td>The time the product was purchased, in milliseconds since the epoch (Jan 1, 1970).</td>
    389 </tr>
    390 
    391 <tr>
    392   <td>purchaseState</td>
    393   <td>The purchase state of the order. Possible values are 0 (purchased), 1 (canceled), 2
    394   (refunded), or 3 (expired, for subscription purchases only).</td>
    395 </tr>
    396 <tr>
    397   <td>purchaseToken</td>
    398   <td>A token that uniquely identifies a subscription purchase for a given item and user pair.
    399   You can use the token to specify the subscription when querying for subscription validity.
    400   
    401   <p><br><em>Supported only in In-app Billing API Version 2 and higher.</em></p></td>
    402 </tr>
    403 <tr>
    404   <td>developerPayload</td>
    405   <td>A developer-specified string that contains supplemental information about an order. You can
    406   specify a value for this field when you make a <code>REQUEST_PURCHASE</code> request.</td>
    407 </tr>
    408 </table>
    409 
    410 <!--<h2 id="other-intents">Other Intents</h2> 
    411 
    412 <p>The following Intents related to In-app Billing may be useful in your
    413 implemention. </p> -->
    414 
    415 <h3 id="http-api">REST API for subscriptions</h3>
    416 <p>Google Play offers an HTTP-based API that you can use to remotely query the validity of a specific subscription at any time or cancel a subscription. The API is designed to be used from your backend servers as a way of securely managing subscriptions, as well as extending and integrating subscriptions with other services. See <a href="{@docRoot}google/play/billing/gp-purchase-status-api.html">Purchase Status API</a> for more information.</p>
    417