Making Purchases
Process a transaction with Apple or Google
The SDK has a simple method, purchase(package:), that takes a package from the fetched Offering and purchases the underlying product with Apple, Google, or Amazon.
- Swift
- Obj-C
- Kotlin
- Kotlin MP
- Java
- Flutter
- React Native
- Cordova
- Capacitor
- Unity
- Web (JS/TS)
- Vega
Purchases.shared.purchase(package: package) { (transaction, customerInfo, error, userCancelled) in
if customerInfo.entitlements["your_entitlement_id"]?.isActive == true {
// Unlock that great "pro" content
}
}
[[RCPurchases sharedPurchases] purchasePackage:package withCompletion:^(RCStoreTransaction *transaction, RCCustomerInfo *customerInfo, NSError *error, BOOL cancelled) {
if (customerInfo.entitlements[@"your_entitlement_id"].isActive) {
// Unlock that great "pro" content
}
}];
Purchases.sharedInstance.purchaseWith(
PurchaseParams.Builder(this, aPackage).build(),
onError = { error, userCancelled -> /* No purchase */ },
onSuccess = { storeTransaction, customerInfo ->
if (customerInfo.entitlements["my_entitlement_identifier"]?.isActive == true) {
// Unlock that great "pro" content
}
}
)
Purchases purchases = GetComponent<Purchases>();
purchases.PurchasePackage(package, (purchaseResult) =>
{
if (purchaseResult.CustomerInfo.Entitlements.Active.ContainsKey("my_entitlement_identifier")) {
// Unlock that great "pro" content
}
});
The purchase(package:) completion block will contain an updated CustomerInfo object if successful, along with some details about the transaction.
If the error object is present, then the purchase failed. See our guide on Error Handling for the specific error types.
The userCancelled boolean is a helper for handling user cancellation errors. There will still be an error object if the user cancels, but you can optionally check the boolean instead of unwrapping the error completely.
Transactions (new and previous transactions that are synced) will be automatically completed (finished on iOS, acknowledged and consumed in Android), and will be made available through the RevenueCat SDK / Dashboard / ETL Exports.
If you are migrating an existing app to RevenueCat and want to continue using your own in-app purchase logic, you can tell the SDK that your app is completing transactions if you don't wish to have transactions completed automatically, but you will have to make sure that you complete them yourself.
Next Steps
- Don't forget to provide some way for customers to restore their purchases
- With purchases coming through, make sure they're linked to the correct app user ID
- If you're ready to test, start with our guides on sandbox testing