Testing In-app Billing

The Google Play publisher site provides several tools that help you test your In-app Billing implementation before it is published. You can use these tools to create test accounts and purchase special reserved items that send static billing responses to your application.

To test In-app Billing in an application you must install the application on an Android-powered device. You cannot use the Android emulator to test In-app Billing. The device you use for testing must run a standard version of the Android 1.6 or later platform (API level 4 or higher), and have the most current version of the Google Play application installed. If a device is not running the most current Google Play application, your application won't be able to send In-app Billing requests to Google Play. For general information about how to set up a device for use in developing Android applications, see Using Hardware Devices.

The following section shows you how to set up and use the In-app Billing test tools.

Testing in-app purchases with static responses

We recommend that you first test your In-app Billing implementation using static responses from Google Play. This enables you to verify that your application is handling the primary Google Play responses correctly and that your application is able to verify signatures correctly.

To test your implementation with static responses, you make an In-app Billing request using a special item that has a reserved product ID. Each reserved product ID returns a specific static response from Google Play. No money is transferred when you make In-app Billing requests with the reserved product IDs. Also, you cannot specify the form of payment when you make a billing request with a reserved product ID. Figure 1 shows the checkout flow for the reserved item that has the product ID android.test.purchased.

Figure 1.Wallet flow for the special reserved item android.test.purchased.

You do not need to list the reserved products in your application's product list. Google Play already knows about the reserved product IDs. Also, you do not need to upload your application to the publisher site to perform static response tests with the reserved product IDs. You can simply install your application on a device, log into the device, and make billing requests using the reserved product IDs.

There are four reserved product IDs for testing static In-app Billing responses:

  • android.test.purchased

    When you make an In-app Billing request with this product ID, Google Play responds as though you successfully purchased an item. The response includes a JSON string, which contains fake purchase information (for example, a fake order ID). In some cases, the JSON string is signed and the response includes the signature so you can test your signature verification implementation using these responses.

  • android.test.canceled

    When you make an In-app Billing request with this product ID Google Play responds as though the purchase was canceled. This can occur when an error is encountered in the order process, such as an invalid credit card, or when you cancel a user's order before it is charged.

  • android.test.refunded

    When you make an In-app Billing request with this product ID, Google Play responds as though the purchase was refunded. Refunds cannot be initiated through Google Play's in-app billing service. Refunds must be initiated by you (the merchant). After you process a refund request through your Google Checkout account, a refund message is sent to your application by Google Play. This occurs only when Google Play gets notification from Google Checkout that a refund has been made. For more information about refunds, see Handling IN_APP_NOTIFY messages and In-app Billing Pricing.

  • android.test.item_unavailable

    When you make an In-app Billing request with this product ID, Google Play responds as though the item being purchased was not listed in your application's product list.

In some cases, the reserved items may return signed static responses, which lets you test signature verification in your application. To test signature verification with the special reserved product IDs, you may need to set up test accounts or upload your application as a unpublished draft application. Table 1 shows you the conditions under which static responses are signed.

Table 1. Conditions under which static responses are signed.

Application ever been published? Draft application uploaded and unpublished? User who is running the application Static response signature
No No Any Unsigned
No No Developer Signed
Yes No Any Unsigned
Yes No Developer Signed
Yes No Test account Signed
Yes Yes Any Signed

To make an In-app Billing request with a reserved product ID, you simply construct a normal REQUEST_PURCHASE request, but instead of using a real product ID from your application's product list you use one of the reserved product IDs.

To test your application using the reserved product IDs, follow these steps:

  1. Install your application on an Android-powered device.

    You cannot use the emulator to test In-app Billing; you must install your application on a device to test In-app Billing.

    To learn how to install an application on a device, see Running on a device.

  2. Sign in to your device with your developer account.

    You do not need to use a test account if you are testing only with the reserved product IDs.

  3. Verify that your device is running a supported version of the Google Play application or the MyApps application.

    If your device is running Android 3.0, In-app Billing requires version 5.0.12 (or higher) of the MyApps application. If your device is running any other version of Android, In-app Billing requires version 2.3.4 (or higher) of the Google Play application. To learn how to check the version of the Google Play application, see Updating Google Play.

  4. Run your application and purchase the reserved product IDs.

Note: Making In-app Billing requests with the reserved product IDs overrides the usual Google Play production system. When you send an In-app Billing request for a reserved product ID, the quality of service will not be comparable to the production environment.

Testing In-app Purchases Using Your Own Product IDs

After you finish your static response testing, and you verify that signature verification is working in your application, you can test your In-app Billing implementation by making actual in-app purchases. Testing real in-app purchases enables you to test the end-to-end In-app Billing experience, including the actual responses from Google Play and the actual checkout flow that users will experience in your application.

Note: You do not need to publish your application to do end-to-end testing. You only need to upload your application as a draft application to perform end-to-end testing.

To test your In-app Billing implementation with actual in-app purchases, you will need to register at least one test account on the Google Play publisher site. You cannot use your developer account to test the complete in-app purchase process because Google Checkout does not let you buy items from yourself. If you have not set up test accounts before, see Setting up test accounts.

Also, a test account can purchase an item in your product list only if the item is published. The application does not need to be published, but the item does need to be published.

When you use a test account to purchase items, the test account is billed through Google Checkout and your Google Checkout Merchant account receives a payout for the purchase. Therefore, you may want to refund purchases that are made with test accounts, otherwise the purchases will show up as actual payouts to your merchant account.

To test your In-app Billing implementation with actual purchases, follow these steps:

  1. Upload your application as a draft application to the publisher site.

    You do not need to publish your application to perform end-to-end testing with real product IDs; you only need to upload your application as a draft application. However, you must sign your application with your release key before you upload it as a draft application. Also, the version number of the uploaded application must match the version number of the application you load to your device for testing. To learn how to upload an application to Google Play, see Uploading applications.

  2. Add items to the application's product list.

    Make sure that you publish the items (the application can remain unpublished). See Creating a product list to learn how to do this.

  3. Install your application on an Android-powered device.

    You cannot use the emulator to test In-app Billing; you must install your application on a device to test In-app Billing.

    To learn how to install an application on a device, see Running on a device.

  4. Make one of your test accounts the primary account on your device.

    To perform end-to-end testing of In-app Billing, the primary account on your device must be one of the test accounts that you registered on the Google Play site. If the primary account on your device is not a test account, you must do a factory reset of the device and then sign in with one of your test accounts. To perform a factory reset, do the following:

    1. Open Settings on your device.
    2. Touch Privacy.
    3. Touch Factory data reset.
    4. Touch Reset phone.
    5. After the phone resets, be sure to sign in with one of your test accounts during the device setup process.
  5. Verify that your device is running a supported version of the Google Play application or the MyApps application.

    If your device is running Android 3.0, In-app Billing requires version 5.0.12 (or higher) of the MyApps application. If your device is running any other version of Android, In-app Billing requires version 2.3.4 (or higher) of the Google Play application. To learn how to check the version of the Google Play application, see Updating Google Play.

  6. Make in-app purchases in your application.

Note: The only way to change the primary account on a device is to do a factory reset, making sure you log on with your primary account first.

When you are finished testing your In-app Billing implementation, you are ready to publish your application on Google Play. You can follow the normal steps for preparing, signing, and publishing on Google Play.