Skip to content

Validating In-App Purchase

Google Play token authenticity validation is used as a protection measure against fraudulent transactions. The token becomes available in the app after the first in-app purchase, and depending on the platform can be validated using the API described below.

Signatures

Blocking API

public GooglePlayPurchaseStatus validatePlayPurchase( String packageName, String productId, String token )

where:

Argument                Description
packageName A package name of the application associated with an active in-app subscription purchase. (for example, 'com.some.thing').
productId ID of the purchased subscription (for example, 'com.some.thing.monthly001' - where monthly001 is the ID).
token A token provided to the application after the subscription purchase.

Non-blocking API

public void validatePlayPurchase( String packageName, String productId, String token, AsyncCallback<GooglePlayPurchaseStatus> callback )

where:

Argument                Description
packageName A package name of the application associated with an active in-app subscription purchase. (for example, 'com.some.thing').
productId ID of the purchased subscription (for example, 'com.some.thing.monthly001' - where monthly001 is the ID).
token A token provided to the application after the subscription purchase.

Return Value

Returns the GooglePlayPurchaseStatus object:

public class GooglePlayPurchaseStatus  
{  
  private String kind;  
  private long purchaseTimeMillis;  
  private int purchaseState;  
  private int consumptionState;  
  private String developerPayload;  
}

where:

Argument                Description
kind Represents the inappPurchase object in the androidpublisher service.
purchaseTimeMillis Time when a subscription was purchased. Time is measured in milliseconds passed since the Unix epoch (1 January 1970 00:00:00 UT).
purchaseState The purchase state of the order, where: 0 - Purchased, 1 - Canceled, 2 - Pending.
consumptionState The consumption state of the order, where 0 - Yet to be consumed, 1 - Consumed.
developerPayload A developer-specified string that contains information about an order.

Example Blocking API

String packageName = "<your package name>";  
String productId = "<your product Id>";  
String token = "<your token>";  
String subscriptionId = "<your subscription Id>";  

GooglePlayPurchaseStatus googlePlayPurchaseStatus = Backendless.Commerce.validatePlayPurchase( packageName, productId, token );

Example Non-Blocking API

String packageName = "<your package name>";  
String productId = "<your product Id>";  
String token = "<your token>";  
String subscriptionId = "<your subscription Id>";  

Backendless.Commerce.validatePlayPurchase( packageName, productId, token, new AsyncCallback<GooglePlayPurchaseStatus>()  
    {  
      @Override  
      public void handleResponse( GooglePlayPurchaseStatus googlePlayPurchaseStatus )  
      {  
        // result handling logic  
      }  

      @Override  
      public void handleFault( BackendlessFault backendlessFault )  
      {  
        // error handling logic  
      }  
    } );