How to add Premium Options easily in your Android app
--
Okay so you have created an app that is awesome but you have added some more great features to your app. Now to monetize your app you could add that feature as a premium feature where users need to pay for it.
Using Google Billing API seems tedious to use but there is a solution for that. RevenueCat, It's a platform that provides easy-to-add implementation in your app that you can add in an hour.
Another great thing is that it supports multiple channels such as Android, iOS, and the web, and has multiple integrations. You will get good analytics for your app and subscriptions.
In this article, I will show you how you can use RevenueCat to add premium features to your android app.
Setting up in-app in Google Play Console
Add an in-app product in your app from Google Play Console
Fill in the details and click save. This will create an in-app product for your app. remember that Product ID is the identifier for this in-app product.
Setting up in-app in RevenueCat
Now head over to RevenueCat, Add a new app, and follow the instructions to connect your app with Google Play Console which is required for RevenueCat to get statistics data from Google Play Store.
After finishing the initial setup, Add Entitlement in RevenueCat which is an identifier for your in-app product.
Also, add Offerings and Products (This is required to create the product that will be used by RevenueCat to connect with Google billing).
So now your setup should be completed. Let's head over to some code.
Implementations
Add the following dependency to your app build.gradle
file.
implementation 'com.revenuecat.purchases:purchases:5.4.0'
Define your Entitlement Id in your app.
public static final String PREMIUM_ENTITLEMENT_ID = "YOUR_ENTITILEMENT_ID";
Create a class PurchaseInstance.java which will build the RevenueCat SDK.
public class PurchaseInstance {
public static void getOrSetRevenueCatSdk(Context context) {
Purchases.setDebugLogsEnabled(true);
PurchasesConfiguration.Builder builder = null;
builder = new PurchasesConfiguration.Builder(context, "YOUR_REVENUECAT_ANDROID_API_KEY");
Purchases.configure(builder.build());
}
}
In your Main Activity class, initialize the SDK.
PurchaseInstance.getOrSetRevenueCatSdk(MainActivity.this);
Now we just need the 3 methods for everything.
Check User Premium Status
The following method checks for the premium status of the user. and return the results, You can then use this result to show the premium content/features or ask for payment by showing the paywall.
public void checkActivePurchase() {
Purchases.getSharedInstance().getCustomerInfo(new ReceiveCustomerInfoCallback() {
@Override
public void onReceived(@NonNull CustomerInfo customerInfo) {
if (customerInfo.getEntitlements().get(PREMIUM_ENTITLEMENT_ID) != null) {
isPremium = customerInfo.getEntitlements().get(PREMIUM_ENTITLEMENT_ID).isActive();
} else {
isPremium = false;
}
}
@Override
public void onError(@NonNull PurchasesError purchasesError) {
}
});
}
Get Product Details
The following method will get the in-app product details as well as the price. another good thing is it will return pricing based on the user’s country.
public void getOfferingDetails() {
Purchases.getSharedInstance().getOfferings(new ReceiveOfferingsCallback() {
@Override
public void onReceived(@NonNull Offerings offerings) {
try {
Offering t = offerings.getCurrent();
purchasePrice = t.getLifetime().getProduct().getPrice();
purchasePackage = t.getLifetime();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Something went wrong, please try again later",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(@NonNull PurchasesError error) {
/* Optional error handling */
}
});
}
Make Purchase
And finally, the method to make the purchase which will call Google Billing and launch the Play Store Payment screen and will listen to the results.
public void makePurchase(Dialog dialog) {
Purchases.getSharedInstance().purchasePackage(this, purchasePackage, new PurchaseCallback() {
@Override
public void onCompleted(@NonNull StoreTransaction storeTransaction, @NonNull CustomerInfo customerInfo) {
if (customerInfo.getEntitlements().get(PREMIUM_ENTITLEMENT_ID).isActive()) {
isPremium = true;
try {
dialog.dismiss();
Toast.makeText(getApplicationContext(),
"You got lifetime access now",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"You got lifetime access now, Please click on cancel to continue",
Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onError(@NonNull PurchasesError purchasesError, boolean b) {
// No purchase
isPremium = false;
}
});
}
And that’s it, You have integrated Google billing into your app.
Hope this helps you to move ahead with adding premium features to your app.
Cheers!!