Check for active signatures with Android App Billing

-1

Hello, I'm implementing signatures in my app that will remove the ads. I have an activity that is doing the shopping and it's working.

When the purchase is successfully completed I save a code in the shared preferences to let me know that there is an active subscription. However I have raised a doubt, as the purchase is performed outside the main activity, in case the user does not renew the signature when it expires the code saved in the shared preferences will always be as purchased.

I thought about making a new connection to Billing in the main activity, so every time the application was opened it would be checked if there is an active signature and would edit the code in the shared preferences.

However, I was kind of confused in the sense of knowing which parts of the app billing code to use to do this check.

How can I make this check using the least possible code?

Thank you in advance.

    
asked by anonymous 19.09.2017 / 00:43

1 answer

0

I had a case like this and 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 gets the premium version, (which to remove ads), I just check if my item in the store was purchased and if yes, pass the value "true" to the variable mIsPremium, then when I'm going to call any other activity starting from my mainactivity I pass the value of this mIsPremium variable in a Bundle to read in that activity I'm calling.

By this method you check directly on the user's subscription to the specific app and it already does the control normally, without having to record it in the preferences of the device.

I hope it solves for you, here it works bluray, taking users "blind people" who always say they paid and the complaints did not disappear, but we all know that they paid nothing ...

Lucky for you

    
19.09.2017 / 01:13