Android - Making Purchases

Learn how to make and restore mobile purchases using Adapty Android SDK. Adapty handles server-side validation, including renewals and grace periods

To make the purchase, you have to call .makePurchase() method:

Adapty.makePurchase(activity, product) { purchaserInfo, purchaseToken, googleValidationResult, product, error ->
    if (error == null) {
        // successful purchase
    }
}

Request parameters:

  • Product (required): a ProductModel object retrieved from the paywall.

Response parameters:

  • Purchaser info: a PurchaserInfoModel object. This model contains info about access levels, subscriptions, and non-subscription purchases. Generally, you have to check only access level status to determine whether the user has premium access to the app.
  • Purchase token: a string purchase token of the transaction from Google Play used to validate the purchase.
  • Google validation result: a dictionary containing raw validation result of subscription or product from Google.
  • Product: a ProductModel object you've passed to this method.

🚧

Make sure you've uploaded Service Account Key File in Adapty Dashboard, without it, we can't validate purchases.

Below is a complete example of making the purchase and checking the user's access level.

Adapty.makePurchase(activity, product) { purchaserInfo, purchaseToken, googleValidationResult, product, error ->
    if (error == null) {
        // "premium" is an identifier of default access level
        if (purchaserInfo?.accessLevels["premium"]?.isActive == true) {
            // grant access to premium features
        }
    }
}

🚧

Make sure to set up Real-time Developer Notifications (RTDN) to receive subscription updates without significant delays.

Change subscription

If you need a subscription to be replaced with another one, you have to call .makePurchase() method with additional parameter:

Adapty.makePurchase(activity, product, subscriptionUpdateParams) { purchaserInfo, purchaseToken, googleValidationResult, product, error ->
    if (error == null) {
        // successful purchase
    }
}

Additional request parameter:

You can read more about proration modes here.

πŸ“˜

Please read recommendations from Google for proration modes.

πŸ“˜

IMMEDIATE_AND_CHARGE_PRORATED_PRICE proration mode is available only for a subscription upgrade, where the price per unit of time increases.

πŸ“˜

If you use DEFERRED proration mode, in case of success, purchaserInfo, purchaseToken and googleValidationResult in the callback are null since real subscription change will occur only when current subscription's billing period ends.

Restoring purchases

To restore purchases, you have to call .restorePurchases() method:

Adapty.restorePurchases { purchaserInfo, googleValidationResultList, error ->
    if (error == null) {
        // successful restore
    }
}

Response parameters:

  • Purchaser info: a PurchaserInfoModel object. This model contains info about access levels, subscriptions, and non-subscription purchases. Generally, you have to check only access level status to determine whether the user has premium access to the app.
  • Google validation result: a list containing raw validation results of subscription or product from Google Play.

In Observer mode, after any purchase or restore in your application, you should call .restorePurchases() method to record the action in Adapty.


Did this page help you?