androidin-app-purchasein-app-billing

How to do restore purchase and get whether the Product is Purchase or not?


I have implemented the in-app billing in my Android app. The code I use it as below:

oncreate method:

startService(new Intent(this, BillingService.class));
    System.out.println(" - - - - CHECK FOR THE PURCHASE PRODUCT - - - - ");
    BillingHelper.setCompletedHandler(mTransactionHandler);

Handler is:

 public Handler mTransactionHandler = new Handler(){
    public void handleMessage(android.os.Message msg) {
        System.out.println("SEE FOR THE PRODUCTS PURCHASE OR NOT");
        
        Log.i(TAG, "Transaction complete");
        Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
        Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);
        
        // For Premium Pro Purchase
        if(BillingHelper.latestPurchase.productId == "premium_pro"){
            if(BillingHelper.latestPurchase.isPurchased()){
                System.out.println("START OPENING PREMIUM_PRO_BUY CONTENT");
                myPrefs = getApplicationContext().getSharedPreferences("myPrefs",MODE_WORLD_WRITEABLE);
                prefsEditor = myPrefs.edit();                 
                prefsEditor.putBoolean("PREMIUM_PRO_BUY", true); // value to store                 
                prefsEditor.commit(); 
                PREMIUM_PRO_BUY = true;
                System.out.println("NOW.. . .  PREMIUM_PRO_BUY is as PURCHASED. :-)");
                System.out.println("FINISH TO OPENING BLOCK CONTENT");
                //  showItem();
            }
        }
        
        // For Feature Upgrade Purchase
        if(BillingHelper.latestPurchase.productId == "feature_upgrade"){
            if(BillingHelper.latestPurchase.isPurchased()){
                System.out.println("START OPENING FEATURE_UPGRADE_BUY CONTENT");
                myPrefs = getApplicationContext().getSharedPreferences("myPrefs",MODE_WORLD_WRITEABLE);
                prefsEditor = myPrefs.edit();                 
                prefsEditor.putBoolean("FEATURE_UPGRADE_BUY", true); // value to store                 
                prefsEditor.commit(); 
                FEATURE_UPGRADE_BUY = true;
                System.out.println("NOW.. . .  FEATURE_UPGRADE_BUY is as PURCHASED. :-)");
                System.out.println("FINISH TO OPENING BLOCK CONTENT");
                //  showItem();
            }
        }
        
        
        
        
        
        
    };
};

And in Button click my code is:

//      CODE TO GET PURCHASED
        if(BillingHelper.isBillingSupported()){
            
            BillingHelper.requestPurchase(getApplicationContext(), "feature_upgrade"); // MY OWN
            
        } else {
            Log.i(TAG,"Can't purchase on this device");
            Toast.makeText(getApplicationContext(), "Please check internet and make sure that you have latest GooglePlay App in your Device.", Toast.LENGTH_LONG).show();
        }

Now when I click on Restore Purchase Button, I want to see which Product is Purchase or not. So what should I have to do?

I have seen this SO question: this, and this. But not getting which product is purchase for that user and which not.


Solution

  • This page explains details of in-app billing and offers the "Dungeons" sample application download. Please, download the sample application and look at the mBillingService.restoreTransactions(); function, most probably it does exactly what you need.