Developer Console
Ti ringraziamo per la visita. Questa pagina è per il momento disponibile solo in inglese.

IAP Best Practice - Track Customer Purchase Trends

This guide outlines tracking your customers’ purchase trends by monitoring the cancelled receipts rate as a strategy to make the most out of your app.

Use the Appstore UserId

The UserId is a unique identifier for every customer that uses IAP. You can retrieve your customer’s UserId by invoking the PurchasingService class member getUserData() method. For details, see Get User Information.

If you have implemented your app in Unity, fetch the UserId by implementing IAmazonExtensions. For details, see Amazon Appstore and Amazon Underground Store Extended Functionality. Alternatively, use the getUserData()  method of the Amazon IAP plugin. In that case, see GetUserData for details.

To track your customer's purchase history and trends, link the customer's Appstore UserId to your identity management system. Introduce an identity system to your app by integrating with an identity platform, such as Login with Amazon. Alternatively, you can use other services (for example, Facebook’s login service), or create your own system.

IAP Receipt Validation

Once a user completes a purchase, process the receipt in your onResume() call without requiring further user interaction. See Implement getPurchaseUpdates method for details. 

Validate the receipt by integrating with the Receipt Verification Service (RVS), and call it by providing: 

  • A receiptId: This is the unique identifier of every purchase. Retrieve receiptId from onPurchaseResponse() callbacks as illustrated below:

       @Override
       public void onPurchaseResponse(PurchaseResponse purchaseResponse) {
          switch (purchaseResponse.getRequestStatus()) {
              case SUCCESSFUL:
                  receiptId = purchaseResponse.getReceipt().getReceiptId();
                  PurchasingService.notifyFulfillment(purchaseResponse.getReceipt().getReceiptId(),
                  FulfillmentResult.FULFILLED);
                  break ;
              case FAILED:
                  break ;
          }
       }
    
  • A userId: This is the unique identifier of the customer. Retrieve userId from onUserDataResponse callbacks as outlined below:

      @Override
      public void onUserDataResponse(UserDataResponse response) {
          final UserDataResponse.RequestStatus status = response.getRequestStatus();
          switch (status) {
              case SUCCESSFUL:
                  currentUserId = response.getUserData().getUserId();
                  currentMarketplace = response.getUserData().getMarketplace();
                  break;
              case FAILED:
              case NOT_SUPPORTED:
                  // Fail gracefully.
                  break;
          }
      }
    

Here is the RVS request syntax:

https://<RVS_server_Url>/version/<Operation_version_number>/verifyReceiptId/developer/<Shared_Secret>/user/<UserId>/receiptId/<ReceiptId>

Request and Response Examples

The example below illustrates a request that verifies a receipt on the RVS production server: 

https://appstore-sdk.amazon.com/version/1.0/verifyReceiptId/developer/2:smXBjZkWCxDMSBvQ8HBGsUS1PK3jvVc8tuTjLNfPHfYAga6WaDzXJPoWpfemXaHg:iEzHzPjJ-XwRdZ4b4e7Hxw==/user/LRyD0FfW_3zeOlfJyxpVll-Z1rKn6dSf9xD3mUMSFg0=/receiptId/wE1EG1gsEZI9q9UnI5YoZ2OxeoVKPdR5bvPMqyKQq5Y=:1:11

The RVS API returns a response code (HTTP 200), if the request is successful. Here is an example response for a successful request:

{
    "betaProduct": false,
    "cancelDate": null,
    "parentProductId": null,
    "productId": "com.amazon.iapsamplev2.gold_medal",
    "productType": "CONSUMABLE",
    "purchaseDate": 1399070221749,
    "quantity": 1,
    "receiptId": "wE1EG1gsEZI9q9UnI5YoZ2OxeoVKPdR5bvPMqyKQq5Y=:1:11",
    "renewalDate": null,
    "term": null,
    "termSku": null,
    "testTransaction": true
}

Receipt Verification

To verify the receipt, check the cancelDate & renewalDate fields:

  • For Consumable or Entitlement purchases, the receipt is valid if both fields are null, or if the cancelDate precedes the purchaseDate.
  • For Subscription purchases, the receipt is valid if the cancelDate field is null.

Here is an example of a cancelled receipt:

{
    "betaProduct": false,
    "cancelDate": 1583136000000,
    "parentProductId": null,
    "productId": "com.amazon.iapsamplev2.gold_medal",
    "productType": "CONSUMABLE",
    "purchaseDate": 1583049600000,
    "quantity": 1,
    "receiptId": "wE1EG1gsEZI9q9UnI5YoZ2OxeoVKPdR5bvPMqyKQq5Y=:1:11",
    "renewalDate": null,
    "term": null,
    "termSku": null,
    "testTransaction": true
}

Additional Recommendations

  1. Monitor trends of the cancelled receipts by creating a process to call the RVS within 72 hours for each active receipt
  2. Keep the rate of the polling below 10 Transactions Per Second (TPS) to avoid request throttling

Next Steps

Implementing the above strategy can help you create a reliable process to track customer purchase trends, and ensure this important source of revenue is secure. Check out our In-App Purchasing Overview for more details on IAP and how to implement it in your projects.