Making Purchases

Make in-app purchases with Qonversion SDK

Make sure to configure Products, Entitlements and Offerings in the Qonversion dashboard before you start handling purchases with Qonversion SDK:

1. Make a purchase

When Products and Entitlements are set, you can start making purchases with thepurchaseProduct method for iOS and cross-platform SDKs, and purchase for Android

Qonversion.shared().purchaseProduct(product) { (entitlements, error, isCancelled) in  
  if let premium: Qonversion.Entitlement = entitlements["premium"], premium.isActive {
    // Flow for success state
  }
}
[[Qonversion sharedInstance] purchaseProduct:product completion:^(NSDictionary<NSString *,QONEntitlement *> * _Nonnull entitlements,
                                                                  NSError * _Nullable error,
                                                                  BOOL cancelled) {
  QONEntitlement *premiumEntitlement = entitlements[@"premium"];
  
  if (premiumEntitlement && premiumEntitlement.isActive) {
    // Flow for success state
  }
}];
Qonversion.getSharedInstance().purchase(this, product, new QonversionEntitlementsCallback() {
    @Override
    public void onSuccess(@NotNull Map<String, QEntitlement> entitlements) {
        QEntitlement premiumEntitlement = entitlements.get("premium");

        if (premiumEntitlement != null && premiumEntitlement.isActive()) {
            // Handle active entitlement here
        }
    }

    @Override
    public void onError(@NotNull QonversionError error) {
        // Handle error here
        if (error.getCode() == QonversionErrorCode.PurchaseCanceled) {
            // Purchase canceled by the user  
        }
    }
});
Qonversion.shared.purchase(this, product, callback = object: QonversionEntitlementsCallback {
    override fun onSuccess(entitlements: Map<String, QEntitlement>) {
        val premiumEntitlement = entitlements["premium"]
        if (premiumEntitlement != null && premiumEntitlement.isActive) {
            // Handle active entitlement here
        }
    }

    override fun onError(error: QonversionError) {
        // Handle error here
        if (error.code === QonversionErrorCode.PurchaseCanceled) {
            // Purchase canceled by the user
        }
    }
})
try {
  final Map<String, QEntitlement> entitlements = await Qonversion.getSharedInstance().purchaseProduct(product);
} on QPurchaseException catch (e) {
  if (e.isUserCancelled) {
    // Purchase canceled by the user
  }
  print(e);
}
try {
  const entitlements: Map<string, Entitlement> = await Qonversion.getSharedInstance().purchaseProduct(product);
} catch (e) {
  if (e.userCanceled) {
    // Purchase canceled by the user
  }
  console.log(e);
}
Qonversion.GetSharedInstance().PurchaseProduct(product, (entitlements, error, isCancelled) =>
{
      if (error == null)
      {
          if (entitlements.TryGetValue("premium", out Entitlement premium) && premium.IsActive)
          {
             // Handle the active entitlement here
          }
      }
      else
      {
        // Handle the error  
        Debug.Log("Error" + error.ToString());
      }
});
try {
  const entitlements = await Qonversion.getSharedInstance().purchaseProduct(product);
} catch (e) {
  if (e.userCanceled) {
    // Purchase canceled by the user
  }
  console.log(e);
}
try {
  const entitlements: Map<string, Entitlement> = await Qonversion.getSharedInstance().purchaseProduct(product);
} catch (e) {
  if (e.userCanceled) {
    // Purchase canceled by the user
  }
  console.log(e);
}

Where "product" is the Qonversion Product created in the Dashboard. See the previous step to display Products.

1.1. Define a specific offer (Android only)

Google Play Billing Library allows you to sell a subscription with different offers. You can get information about available offers from QProduct.storeDetails. Use one of the following options to provide the chosen offer for the purchase.

// Specify the concrete offer:
final QProductOfferDetails productOfferDetails = ...; // Choose an offer from `storeDetails`
final QPurchaseOptions purchaseOptions = new QPurchaseOptions.Builder()
        .setOffer(productOfferDetails)
        .build();

// or specify only the offer ID:
final QPurchaseOptions purchaseOptions = new QPurchaseOptions.Builder()
        .setOfferId("offer_id")
        .build();

// and then provide created `QPurchaseOptions` to the `purchase` method:
Qonversion.getSharedInstance().purchase(this, product, purchaseOftions, new QonversionEntitlementsCallback() {
    ...
});
// Specify the concrete offer:
val productOfferDetails = ... // Choose an offer from `storeDetails`
val purchaseOptions = QPurchaseOptions.Builder()
    .setOffer(productOfferDetails)
    .build()

// or specify only the offer ID:
val purchaseOptions = QPurchaseOptions.Builder()
    .setOfferId("offer_id")
    .build()

// and then provide created `QPurchaseOptions` to the `purchase` method:
Qonversion.shared.purchase(this, product, purchaseOptions, callback = object: QonversionEntitlementsCallback {
    ...
})
// Specify the concrete offer:
var productOfferDetails = ... // Choose an offer from `storeDetails`
var purchaseOptions = QPurchaseOptionsBuilder()
    .setOffer(productOfferDetails)
    .build()

// or specify only the offer ID:
var purchaseOptions = QPurchaseOptionsBuilder()
    .setOfferId('offer_id')
    .build()

// and then provide created `QPurchaseOptions` to the `purchase` method:
var entitlements = await Qonversion.getSharedInstance().purchaseProduct(
    product,
    purchaseOptions: purchaseOptions
);
// Specify the concrete offer:
const productOfferDetails = ...; // Choose an offer from `storeDetails`
const purchaseOptions = new PurchaseOptionsBuilder()
  .setOffer(productOfferDetails)
  .build();

// or specify only the offer ID:
const purchaseOptions = new PurchaseOptionsBuilder()
  .setOfferId('offer_id')
  .build();

// and then provide created `PurchaseOptions` to the `purchase` method:
const entitlements = await Qonversion.getSharedInstance().purchaseProduct(product, purchaseOptions);
// Specify the concrete offer:
const productOfferDetails = ...; // Choose an offer from `storeDetails`
var purchaseOptions = new PurchaseOptionsBuilder()
    .SetOffer(productOfferDetails)
    .Build();

// or specify only the offer ID:
var purchaseOptions = new PurchaseOptionsBuilder()
    .SetOfferId("ofofo")
    .Build();

// and then provide created `PurchaseOptions` to the `purchase` method:
Qonversion.GetSharedInstance().PurchaseProduct(product, purchaseOptions, (entitlements, error, isCancelled) =>
{
      ...
});
// Specify the concrete offer:
const productOfferDetails = ...; // Choose an offer from `storeDetails`
const purchaseOptions = new Qonversion.PurchaseOptionsBuilder()
  .setOffer(productOfferDetails)
  .build();

// or specify only the offer ID:
const purchaseOptions = new Qonversion.PurchaseOptionsBuilder()
  .setOfferId('offer_id')
  .build();

// and then provide created `PurchaseOptions` to the `purchase` method:
const entitlements = await Qonversion.getSharedInstance().purchaseProduct(product, purchaseOptions);
// Specify the concrete offer:
const productOfferDetails = ...; // Choose an offer from `storeDetails`
const purchaseOptions = new PurchaseOptionsBuilder()
  .setOffer(productOfferDetails)
  .build();

// or specify only the offer ID:
const purchaseOptions = new PurchaseOptionsBuilder()
  .setOfferId('offer_id')
  .build();

// and then provide created `PurchaseOptions` to the `purchase` method:
const entitlements = await Qonversion.getSharedInstance().purchaseProduct(product, purchaseOptions);

If provided, we will try to find and purchase the offer with the specified ID for the requested Qonversion product. If there is no offer with the specified ID, an error will be returned. If no offer ID is provided for the subscription purchase of Qonversion product with a specified base plan ID, then we will choose the most profitable offer for the client from all the available offers. We calculate the cheapest price for the client by comparing all the trial or intro phases and the base plan. For old Qonversion products (where the base plan ID is not specified), as well as for in-app products, the offer ID is ignored.

You can also remove any intro/trial offer from the purchase (to keep only a base plan). For that purpose, you should call removeOffer method of purchase options builder:

final QPurchaseOptions purchaseOptions = new QPurchaseOptions.Builder()
        .removeOffer()
        .build();
val purchaseOptions = QPurchaseOptions.Builder()
    .removeOffer()
    .build()
var purchaseOptions = QPurchaseOptionsBuilder()
    .removeOffer()
    .build()
const purchaseOptions = new PurchaseOptionsBuilder()
  .removeOffer()
  .build();
var purchaseOptions = new PurchaseOptionsBuilder()
    .RemoveOffer("ofofo")
    .Build();
const purchaseOptions = new Qonversion.PurchaseOptionsBuilder()
  .removeOffer()
  .build();
const purchaseOptions = new PurchaseOptionsBuilder()
  .removeOffer()
  .build();

2. Handle a purchase result

The product purchase method returns a dictionary with the user's entitlements on success (either via callback/completion block or as return value depending on the platform). If something goes wrong, the method returns an error or throws an exception with the failure description.

There is aisCancelled flag available, which equals true if a user cancels the purchasing process on iOS and Unity. You can check the same behavior for Android by comparing the error code with theQonversionErrorCode.PurchaseCanceled. For Flutter and React-Native, there is an additional field in the thrown exception for that purpose (see the code examples above).

Entitlement IDs are the keys to the entitlements dictionary.
The values are the objects of the Qonversion.Entitlement class.

3. Update purchases (Android only)

Upgrading, downgrading, or changing a subscription on Google Play Store requires setting additional options through the PurchaseOptions builder.
See Google Play Documentation for more details.

final QPurchaseOptions purchaseOptions = new QPurchaseOptions.Builder()
        .setOldProduct(oldProduct)
        .build();
Qonversion.getSharedInstance().purchase(this, product, purchaseOftions, new QonversionEntitlementsCallback() {
    ...
});
val purchaseOptions = QPurchaseOptions.Builder()
    .setOldProduct(oldProduct)
    .build()
Qonversion.shared.purchase(this, product, purchaseOptions, callback = object: QonversionEntitlementsCallback {
    ...
})
var purchaseOptions = QPurchaseOptionsBuilder()
    .setOldProduct(oldProduct)
    .build();
var entitlements = await Qonversion.getSharedInstance().purchaseProduct(
    product,
    purchaseOptions: purchaseOptions
);
const purchaseOptions = new PurchaseOptionsBuilder()
  .setOldProduct(oldProduct)
  .build();
const entitlements = await Qonversion.getSharedInstance().purchaseProduct(product, purchaseOptions);
var purchaseOptions = new PurchaseOptionsBuilder()
    .SetOldProduct(oldProduct)
    .SetUpdatePolicy(PurchaseUpdatePolicy.WithTimeProration)
    .Build();
Qonversion.GetSharedInstance().PurchaseProduct(product, purchaseOptions, (entitlements, error, isCancelled) =>
{
      ...
});
const purchaseOptions = new Qonversion.PurchaseOptionsBuilder()
  .setOldProduct(oldProduct)
  .build();
const entitlements = await Qonversion.getSharedInstance().purchaseProduct(product, purchaseOptions);
const purchaseOptions = new PurchaseOptionsBuilder()
  .setOldProduct(oldProduct)
  .build();
const entitlements = await Qonversion.getSharedInstance().purchaseProduct(product, purchaseOptions);

Also, Qonversion supports providing any replacement mode for the old purchase. Just provide the necessary purchase update policy while building purchase options as follows:

final QPurchaseOptions purchaseOptions = new QPurchaseOptions.Builder()
        .setOldProduct(oldProduct)
        .setUpdatePolicy(QPurchaseUpdatePolicy.WithTimeProration)
        .build();
val purchaseOptions = QPurchaseOptions.Builder()
    .setOldProduct(oldProduct)
    .setUpdatePolicy(QPurchaseUpdatePolicy.WithTimeProration)
    .build()
var purchaseOptions = QPurchaseOptionsBuilder()
    .setOldProduct(oldProduct)
    .setUpdatePolicy(QPurchaseUpdatePolicy.withTimeProration)
    .build();
const purchaseOptions = new PurchaseOptionsBuilder()
  .setOldProduct(oldProduct)
  .setUpdatePolicy(PurchaseUpdatePolicy.WITH_TIME_PRORATION)
  .build();
var purchaseOptions = new PurchaseOptionsBuilder()
    .SetOldProduct(oldProduct)
    .SetUpdatePolicy(PurchaseUpdatePolicy.WithTimeProration)
    .Build();
const purchaseOptions = new Qonversion.PurchaseOptionsBuilder()
  .setOldProduct(oldProduct)
  .setUpdatePolicy(PurchaseUpdatePolicy.WITH_TIME_PRORATION)
  .build();
const purchaseOptions = new PurchaseOptionsBuilder()
  .setOldProduct(oldProduct)
  .setUpdatePolicy(PurchaseUpdatePolicy.WITH_TIME_PRORATION)
  .build();

Purchase update policy can be one of the following values:

NameDescription
ChargeFullPriceThe new plan takes effect immediately, and the user is charged full price of new plan and is given a full billing cycle of subscription, plus remaining prorated time from the old plan.
ChargeProratedPriceThe new plan takes effect immediately, and the billing cycle remains the same.
WithTimeProrationThe new plan takes effect immediately, and the remaining time will be prorated and credited to the user.
DeferredThe new purchase takes effect immediately, the new plan will take effect when the old item expires.
WithoutProrationThe new plan takes effect immediately, and the new price will be charged on next recurrence time.

The default update policy is WithTimeProration.

4. Multi-quantity purchases (iOS only)

When buying in-app products, you have the option to choose how many items you want to purchase. On Android, you can adjust the quantity directly in the purchase popup. However, on iOS, you’ll need to set the quantity beforehand. You can do it while building purchase options as follows:

let purchaseOptions = Qonversion.PurchaseOptions(quantity: quantity)
Qonversion.shared().purchaseProduct(product, options: purchaseOptions) { (entitlements, error, isCancelled) in  
  ...
}
QONPurchaseOptions *purchaseOptions = [[QONPurchaseOptions alloc] initWithQuantity:3];
[[Qonversion sharedInstance] purchaseProduct:product
                                     options:purchaseOptions
                                  completion:^(NSDictionary<NSString *,QONEntitlement *> * _Nonnull entitlements,
                                               NSError * _Nullable error,
                                               BOOL cancelled) {
  ...
}];
var purchaseOptions = QPurchaseOptionsBuilder()
    .setQuantity(3)
    .build();
var entitlements = await Qonversion.getSharedInstance().purchaseProduct(
    product,
    purchaseOptions: purchaseOptions
);
const purchaseOptions = new PurchaseOptionsBuilder()
  .setQuantity(3)
  .build();
const entitlements = await Qonversion.getSharedInstance().purchaseProduct(
  product,
  purchaseOptions
);
var purchaseOptions = new PurchaseOptionsBuilder()
    .SetQuantity(3)
    .Build();
Qonversion.GetSharedInstance().PurchaseProduct(
  product,
  purchaseOptions,
  (entitlements, error, isCancelled) =>
  {
    ...
  });
const purchaseOptions = new Qonversion.PurchaseOptionsBuilder()
  .setQuantity(3)
  .build();
const entitlements = await Qonversion.getSharedInstance().purchaseProduct(
  roduct,
  purchaseOptions
);
const purchaseOptions = new PurchaseOptionsBuilder()
  .setQuantity(3)
  .build();
const entitlements = await Qonversion.getSharedInstance().purchaseProduct(
  product,
  purchaseOptions
);

5. Check user entitlements

Use the checkEntitlements() SDK method in case you want to check users’ entitlements separately from a purchase. Learn more here.

6. Restore purchases

When users, for example, upgrade to a new phone, they need to restore purchases so they can keep access to your premium features.

Call the restore() method to restore purchases:

Qonversion.shared().restore { [weak self] (entitlements, error) in
  if let error = error {
    // Handle error
  }
  
  if let entitlement: Qonversion.Entitlement = entitlements["plus"], entitlement.isActive {
    // Restored and entitlement is active 
  }
}
[[Qonversion sharedInstance] restore:^(NSDictionary<NSString *, QONEntitlement *> * _Nonnull result, NSError * _Nullable error) {
  if (error) {
    // Handle error
  }
  QONEntitlement *entitlement = result[@"active"];
  if (entitlement && entitlement.isActive) {
    // Restored and entitlement is active
  }
}];
Qonversion.getSharedInstance().restore(new QonversionEntitlementsCallback() {
    @Override
    public void onSuccess(@NotNull Map<String, QEntitlement> entitlements) {
        QEntitlement premiumEntitlement = entitlements.get("premium");

        if (premiumEntitlement != null && premiumEntitlement.isActive()) {
            // handle active entitlement here
        }
    }

    @Override
    public void onError(@NotNull QonversionError error) {
        // handle error here
    }
});
Qonversion.shared.restore(object : QonversionEntitlementsCallback {
    override fun onSuccess(entitlements: Map<String, QEntitlement>) {
        val premiumEntitlement = entitlements["premium"]
        if (premiumEntitlement != null && premiumEntitlement.isActive) {
            // handle active entitlement here
        }
    }

    override fun onError(error: QonversionError) {
        // handle error here
    }
})
try {
  final Map<String, QEntitlement> entitlements = await Qonversion.getSharedInstance().restore();
} catch (e) {
  print(e);
}
try {
  const entitlements: Map<string, Entitlement> = await Qonversion.getSharedInstance().restore();
 } catch (e) {
  console.log(e);
}
Qonversion.GetSharedInstance().Restore((entitlements, error) =>
{
      if (error == null)
      {
         // Handle entitlements here
      }
      else
      {
        // Handle the error  
        Debug.Log("Error" + error.ToString());
      }
});
try {
    const entitlements = await Qonversion.getSharedInstance().restore();
} catch (e) {
    console.log(e);
}
try {
  const entitlements: Map<string, Entitlement> = await Qonversion.getSharedInstance().restore();
 } catch (e) {
  console.log(e);
}