Google Play Purchase Validation

Top  Previous  Next

Backendless provides a server-side functionality to manage in-app purchases and subscriptions. The functionality is accessible from the Backendless SDK for Android. An android applications built with Backendless can use the APIs to:

validate Google Play inapp purchase.
retrieve Google Play subscription status.
cancel Google Play subscription.

 

Configuration


Before this functionality can be used in a client application, a Backendless app/backend must be properly configured with a service account email address and a certificate (.p12) file. This is required so that Backendless can authenticate via OAuth2.0 and communicate with the Google servers.

1. Navigate to Google Developers Console.
2.Select a project or create a new one.
3.Expand APIs and auth in the menu on the left and click APIs.
4.In the list of APIs locate Google Play Android Developer API and make sure the status is ON.
5.Select Credentials in the menu on the left.
6.If you already have a service account, skip to step 10.
7.Select Create new Client ID under the OAuth heading.
8.When prompted, select Service Account and click Create Client ID.
9.A dialog box appears. To proceed click Okay, got it.
10. Click Generate new P12 key. The browser will download the .p12 file.
11. Copy the email address under the Service Account heading.
12. Open Backendless Console, select your app and navigate to Manage > App Settings > Mobile Settings.
13. Click Upload Certificate, browse to and select the downloaded .p12 file.
14. Paste the service account email address to the Developer email field.
15. Click Save.

mobile-settings-google-play

 

API - Validating Google Play Purchase


Checks the purchase and consumption status of an inapp item.

// synchronous call
public GooglePlayPurchaseStatus Backendless.Commerce.validatePlayPurchase( String packageName, String productId, String token )
// asynchronous call
public void Backendless.Commerce.validatePlayPurchase( String packageName, 
                                                       String productId, 
                                                       String token,
                                                       AsyncCallback<GooglePlayPurchaseStatus> callback )

where:

packageName- the package name of the application the inapp product was sold in (for example, 'com.some.thing').
productId- the inapp product SKU (for example, 'com.some.thing.inapp1').
token- the token provided to the user's device when the inapp product was purchased.
callback- the callback used for asynchronous calls to indicate that the operation has either successfully completed or resulted in error.

 

Return value:

An instance of com.backendless.commerce.GooglePlayPurchaseStatus:

package com.backendless.commerce;

public class GooglePlayPurchaseStatus
{
  public String getKind();
  public Long getPurchaseTime();
  public Integer getPurchaseState();
  public Integer getConsumptionState();
  public String getDeveloperPayload();
}

where:

kind- this kind represents an inappPurchase object in the androidpublisher service.
purchaseTime- the time the product was purchased, in milliseconds since the epoch (Jan 1, 1970).
purchaseState- the purchase state of the order. Possible values are: 0 - Purchased, 1 - Cancelled.
consumptionState- the consumption state of the inapp product. Possible values are: 0 - Yet to be consumed, 1 - Consumed.
developerPayload- a developer-specified string that contains supplemental information about an order.

 

API - Retrieving Subscription Status


Checks whether a user's subscription purchase is valid and returns its expiry time.

// synchronous call
public GooglePlaySubscriptionStatus Backendless.Commerce.getPlaySubscriptionsStatus( String packageName, String subscriptionId, String token )
// asynchronous call
public void Backendless.Commerce.getPlaySubscriptionsStatus( String packageName, 
                                                       String subscriptionId, 
                                                       String token,
                                                       AsyncCallback<GooglePlaySubscriptionStatus> callback )

where:

packageName- the package name of the application for which this subscription was purchased (for example, 'com.some.thing').
subscriptionId- the purchased subscription ID (for example, 'monthly001').
token- the token provided to the user's device when the subscription was purchased.
callback- the callback used for asynchronous calls to indicate that the operation has either successfully completed or resulted in error.

 

Return value:

An instance of com.backendless.commerce.GooglePlaySubscriptionStatus:

package com.backendless.commerce;

public class GooglePlaySubscriptionStatus
{
  public boolean isAutoRenewing();
  public long getInitiationTimestampMsec();
  public String getKind();
  public long getValidUntilTimestampMsec();
}

where:

autoRenewing- whether the subscription will automatically be renewed when it reaches its current expiry time.
initiationTimestampMsec- time at which the subscription was granted, in milliseconds since Epoch.
kind- this kind represents a subscriptionPurchase object in the androidpublisher service.
validUntilTimestampMsec- time at which the subscription will expire, in milliseconds since Epoch.

 

API - Cancelling Subscription


Cancels a user's subscription purchase. The subscription remains valid until its expiration time.

// synchronous call
public void Backendless.Commerce.cancelPlaySubscription( String packageName, String subscriptionId, String token )
// asynchronous call
public void Backendless.Commerce.cancelPlaySubscription( String packageName, 
                                                         String subscriptionId, 
                                                         String token,
                                                         AsyncCallback<Void> callback )

where:

packageName- the package name of the application for which this subscription was purchased (for example, 'com.some.thing').
subscriptionId- the purchased subscription ID (for example, 'monthly001').
token- the token provided to the user's device when the subscription was purchased.
callback- the callback used for asynchronous calls to indicate that the operation has either successfully completed or resulted in error.

 

Return value:

None.


Please let us know how we can improve the documentation by leaving a comment. All technical questions should be posted to the Backendless Support forum. We do not respond to the technical questions on the documentation pages.: