Using the SDK with your own IAP Code (formerly: Observer Mode)
If you already have your own in-app purchase handling logic in your app and want to start using RevenueCat alongside it, you might need to set the SDK configuration option that your app will complete the purchases itself (configuration option purchasesAreCompletedBy
, see code below). Completing a purchase tells the device billing library that a purchase has been processed and can be discarded. RevenueCat does this by default, however, this may interfere with pre-existing purchase handling code in your app.
If you set the SDK option that your app will complete purchases itself, you can still make use of most of RevenueCat's features, without replacing your existing purchase handling code.
The setting where your app completes purchases was formerly called "observer mode". Older versions of the SDK (currently including the iOS and cross-platform SDKs) still refer to this terminology.
For Amazon Appstore apps, you will need to call the syncAmazonPurchase()
method after making a purchase using your own IAP code.
SDK Configuration
- Swift
- Obj-C
- Java
- Flutter
- React Native
- Unity
Purchases.configure(
with: .init(withAPIKey: "<public_sdk_key>")
.with(purchasesAreCompletedBy: .myApp, storeKitVersion: .storeKit2)
)
RCPurchases.logLevel = RCLogLevelDebug;
[RCPurchases configureWithAPIKey:@<public_sdk_key>
appUserID:@<app_user_id>
purchasesAreCompletedBy:RCPurchasesAreCompletedByRevenueCat
storeKitVersion:RCStoreKitVersion2];
// If you're targeting only Google Play Store
public class MainApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Purchases.setLogLevel(LogLevel.DEBUG);
Purchases.configure(
new PurchasesConfiguration.Builder(this, <public_google_sdk_key>)
.purchasesAreCompletedBy(PurchasesAreCompletedBy.MY_APP)
.build()
);
}
}
// If you're building for the Amazon Appstore
public class MainApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Purchases.setLogLevel(LogLevel.DEBUG);
PurchasesConfiguration.Builder builder = null;
if (BuildConfig.STORE.equals("amazon")) {
builder = new AmazonConfiguration.Builder(this, <public_amazon_sdk_key>);
} else if (BuildConfig.STORE.equals("google")) {
builder = new PurchasesConfiguration.Builder(this, <public_google_sdk_key>);
}
Purchases.configure(
builder
.purchasesAreCompletedBy(PurchasesAreCompletedBy.MY_APP)
.build()
);
}
}
PurchasesConfiguration configuration = PurchasesConfiguration(<public_sdk_key>);
configuration.purchasesAreCompletedBy = PurchasesAreCompletedByMyApp(
storeKitVersion: StoreKitVersion.storeKit2,
);
await Purchases.configure(configuration);
Purchases.configure({
apiKey: "your_api_key_here",
purchasesAreCompletedBy: {
type: PURCHASES_ARE_COMPLETED_BY_TYPE.MY_APP,
storeKitVersion: STOREKIT_VERSION.STOREKIT_2,
},
});
// Observer mode can be configured through the Unity Editor.
// If you'd like to do it programmatically instead,
// make sure to check "Use runtime setup" in the Unity Editor, and then:
Purchases.PurchasesConfiguration.Builder builder = Purchases.PurchasesConfiguration.Builder.Init(<api_key>);
Purchases.PurchasesConfiguration purchasesConfiguration =
builder.SetUserDefaultsSuiteName("user_default")
.SetObserverMode(true)
.SetAppUserId(appUserId)
.Build();
purchases.Configure(purchasesConfiguration);