开发者

Verifying in-app purchases after re-install or user prefs deletion

开发者 https://www.devze.com 2023-03-28 02:49 出处:网络
I\'ve implemented in-app billing from this tutorial. The item to buy is the ability to set a custom background. It works great, but when I uninstall and re-install the app (or clear the user prefs), I

I've implemented in-app billing from this tutorial. The item to buy is the ability to set a custom background. It works great, but when I uninstall and re-install the app (or clear the user prefs), I'm having trouble figuring out how to verify that somebody has already purchased the in-app item.

    public void buySelected() {

    if (backgroundColorsPurchased == true) {
        this.colorChangeDialog(); //if user has already purchased, just call the dialog instead of re-buying.
        //if the person has cleared their prefs, they'll have to be online to re-verify that they did indeed buy the item.

    }else{
        if(BillingHelper.isBillingSupported()){
            BillingHelper.requestPurchase(mContext, "background.colors");
            BillingHelper.setCompletedHandler(mTransactionHandler);

        } else {
            Log.i(TAG,"Can't purchase on this device");

        }
    }
}

Then I have the handler:

    public Handler mTransactionHandler = new Handler(){
    public 开发者_StackOverflowvoid handleMessage(android.os.Message msg) {
        Log.i(TAG, "Transaction complete");
        Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
        Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);

        if(BillingHelper.latestPurchase.isPurchased()){
        //this is where we show the stuff that the person purchased. In this case, the dialog to change the background colour.
            backgroundColorsPurchased = true; //just setting this to true so that the next time somebody clicks the donate button it'll just open the dialog.

            //call the change background dialog
            colorChangeDialog();

        }else{
            //fail
            Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_SHORT).show();

        }
    }

 };

How am I able to verify that the item has been purchased before? The market just keeps popping a dialog window that says, "You have already purchased this item, or the purchase is still pending." When I try using something like if(BillingHelper.latestPurchase.isPurchased()){ I get a force close if it's not within the handler.


You need to call restoreTransactions take a look at the default example that Android has provided for In App Billing for more reference.


You need to restore transactions if the preferences/data have been cleared. That will send you information of the items the user has purchased, and you can process authorizations as usual. Read reference and check the Dungeons sample about how to do this.


you can simply use Fire-base login & realtime database with field premium_enabled - true/false (this will help if user goes for another mobile with old google id for Play-store)

step wise

add google login in your App. make an entry of user in fire-base user_id/email_id and premium_enabled field and store data in it. (you can add fields as per your requirement)

if user purchased than make an entry premium_enabled - true & also use shared preference for offline check

0

精彩评论

暂无评论...
验证码 换一张
取 消