Check for active subscriptions with Billing Android App

Asked

Viewed 221 times

0

Hello, I am implementing subscriptions in my app that will serve to remove the ads. I have an Activity that is doing the shopping and is working.

When the purchase is successfully completed I save a code in Shared preferences to inform me that there is an active subscription. However I came across a question, as the purchase is performed outside the main Activity, if the user does not renew the subscription when it expires the code saved in Shared preferences will always be as purchased.

I thought about making a new connection with Billing in the main Activity, so every time the application was opened it would be checked if there is an active signature and would make an Edit in the code in Shared preferences.

However, I was a little confused as to which parts of the Billing app code to use to make this check.

How can I do this check using as little code as possible?

Already I thank you.

  • Each case is a case. Saving only in sharedpreference may not even be a good option. For example in an app like Spotify, if I use it on tablet and mobile, would I have to make 2 purchases of subscriptions or only one? I believe that you must store this information externally so that you can use it on multiple devices. But of course, it will depend on your business plan.

  • Unless I’m wrong, if you buy something in a particular app and want to use it on two devices, I think you’d have to use the same play store account on both. Thank you for the reply.

1 answer

1

I had a case that looked like this and I did so:

public class MainActivity ... {
    // ...
    private boolean mIsPremium = false;
    static final String SKU_PREMIUM = "premium";
    private IabHelper mHelper;
    private String payload = "";
    // ...
    void complain(String message) {
        alert("Error: " + message);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        isEmulator = Build.MODEL.contains("google_sdk") || Build.MODEL.contains("Emulator") || Build.MODEL.contains("Android SDK");
        final String BASE64_KEY = "SUA CHAVE PUBLICA BASE_64";
        if (!isEmulator) {
            mHelper = new IabHelper(this, BASE64_KEY);
            mHelper.enableDebugLogging(true);
            Log.d(TAG, "Starting setup.");
            mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
                public void onIabSetupFinished(IabResult result) {
                    Log.d(TAG, "Setup finished.");
                    if (!result.isSuccess()) {
                        complain("Problem setting up in-app billing: " + result);
                        return;
                    }
                    if (mHelper == null) return;
                    Log.d(TAG, "Setup successful. Querying inventory.");
                    mHelper.queryInventoryAsync(mGotInventoryListener);
                }
            });
        }
    }
    // ...
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_REQUEST) {
            if (mHelper == null)
                return;
            if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
                super.onActivityResult(requestCode, resultCode, data);
            } else {
                Log.d(TAG, "onActivityResult handled by IABUtil.");
            }
        }
    }
    // ...
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            Log.d(TAG, "Query inventory finished.");
            if (mHelper == null) return;
            if (result.isFailure()) {
                complain("Failed to query inventory: " + result);
                return;
            }
            Log.d(TAG, "Query inventory was successful.");
            Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
            mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
            Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
        }
    };

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);

            if (mHelper == null) return;

            if (result.isFailure()) {
                complain("Error purchasing: " + result);
                return;
            }
            if (!verifyDeveloperPayload(purchase)) {
                complain("Error purchasing. Authenticity verification failed.");
                return;
            }

            Log.d(TAG, "Purchase successful.");

            if (purchase.getSku().equals(SKU_PREMIUM)) {
                // bought the premium upgrade!
                Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
                alert("Thank you for upgrading to premium!");
                mIsPremium = true;
                // aqui eu passo verdadeiro para a variável mIsPremium uma única vez e somente se o cara pagou mesmo
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHelper != null) mHelper.dispose();
        mHelper = null;
    }
    // ...
    boolean verifyDeveloperPayload(Purchase p) {
        payload = p.getDeveloperPayload();
        return true;
    }
}

In my case an app that purchases the premium version, (which when you remove ads), I just check if my item in the store was purchased and if yes, I pass the value "true" to the variable mIsPremium, then when I will call any other Activity from my mainactivity I pass the value of this variable mIsPremium into a Bundle to read on that Activity I am calling.

By this method Voce checks directly on the user’s signature for the specific app and it already does the control normally, without having to record this in the device’s preferences.

I hope you solve for Voce, here it works blzura, taking away the users "blind us" who always say they paid and the claims are not gone, but we all know that these did not pay anything...

Very lucky there

  • Thank you for the reply. I will do some tests here and I will return.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.