I am trying to create a in-app billing for android application that will allow users to buy a subscription. I have managed to do that and I am able to buy subscriptions but what I don't get is how can I limit some features in app to those who have not subscribed?
I cant seem to find any tutorials on that. What I want is on a button press if user have not subscribed prompt a in-app billing window. I can achieve that with this code.
public void launchBillingFLow(@SkuType String skuType, String productId) {
Runnable launchBillingRequest = () -> {
BillingFlowParams mBillingFlowParams;
mBillingFlowParams = BillingFlowParams.newBuilder()
.setSku(productId)
.setType(skuType)
.build();
mBillingClient.launchBillingFlow((Activity) context, mBillingFlowParams);
};
executeServiceRequest(launchBillingRequest);
}
But what if user have already subscribed? So the question is, how can I check if user is subscribed and execute button press and if not show billing window. Can I get that information only if user is connected to the Internet? Do I need to store that information on device?
Use this Subscription methods to get the purchase details -
Show In-App Billing Flow using BillingFlowParams.Builder
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
if (billingResponseCode == BillingClient.BillingResponse.OK) {
// The billing client is ready. You can query purchases here.
final Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
for (Purchase sourcePurchase : purchasesResult.getPurchasesList()) {
if (sourcePurchase != null) {
ConsumeResponseListener listener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(final int responseCode, final String purchaseToken) {
// Log.d("anupam2", responseCode + " <-------> "+ purchasesResult.getPurchasesList() + " <-------> " + purchaseToken);
}
};
mBillingClient.consumeAsync(sourcePurchase.getPurchaseToken(), listener);
} else {
}
}
if (purchasesResult.getPurchasesList().size() > 0) {
// Log.d("anupam3", purchasesResult.getPurchasesList().size() + "");
} else {
// Log.d("anupam4", purchasesResult.getPurchasesList().size() + "");
BillingFlowParams.Builder builder = BillingFlowParams.newBuilder().setSku("234r23446").setType(BillingClient.SkuType.SUBS);
int responseCode = mBillingClient.launchBillingFlow(SplashActivity.this, builder.build());
if (responseCode == 7) {
//Item already purchased
}
}
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Toast.makeText(SplashActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
});
mBillingClient = BillingClient.newBuilder(this).setListener(new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
// Log.d("anupam", responseCode + "");
if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
for (Purchase purchase : purchases) {
//List of purchases
}
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
Toast.makeText(SplashActivity.this, "Sorry, you have canceled purchase Subscription.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SplashActivity.this, "Sorry, some error occurred.", Toast.LENGTH_SHORT).show();
}
}
}).build();
Now what you can do is call this in your splashscreen and save a value using sharedpreference or a global variable in your app based on the response code. Check if the response code is 7 for your subscription id or not. If suscribed (for responseCode == 7, you saved the value as suscribed), show premium features else not.
Hope it helps!