Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InApp Purchase RESTORE_TRANSACTIONS, I am not able to figure the code out

I am adding a in app purchase in my coding, it's working well while purchase but gives error and application closes when I try to add Restore_Transaction code when application is removed and installed again, I have added below coding

in onCreate i wrote

startService(new Intent(mContext, BillingService.class));
        BillingHelper.setCompletedHandler(mTransactionHandler);

        if (BillingHelper.isBillingSupported()) {
            BillingHelper.restoreTransactionInformation(BillingSecurity
                    .generateNonce());
        }

and then i called handler using

public Handler mTransactionHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (BillingHelper.latestPurchase.isPurchased()) {
                showItem();
            }
        };
    };

    private void showItem() {
        purchased = Purchased.getPurchaseInfo(getApplicationContext());
        if (purchased == null) {
            Date d = new Date();
            Toast.makeText(getApplicationContext(), "--- Upgrated ---",
                    Toast.LENGTH_LONG).show();
            purchased = new Purchased(getApplicationContext());
            purchased.isPurchased = 1;
            purchased.purchasedDate = d.getTime();
            purchased.save();
            Intent intent = new Intent(ActorGenieActivity.this,
                    SplashScreen.class);
            startActivity(intent);
        }
    }
like image 352
Maulik J Avatar asked Dec 26 '11 19:12

Maulik J


People also ask

How do I restore previous app purchases?

To restore your purchases:Open the drawer from the upper left corner of the screen and select Support. Select Purchases and Paid App from the menu. Tap on the menu option, located in the upper right-hand corner of the screen. Tap on Recover Paid App.

What happens when you restore in-app purchases?

Restoring purchases is a mechanism by which your user can restore their in-app purchases, reactivating any content that had previously been purchased from the same store account (Apple, Google, or Amazon).


2 Answers

I found the answer to my question, thanx to anddev

You have to check for purchases not to be null

public static void verifyPurchase(String signedData, String signature) {
    ArrayList<VerifiedPurchase> purchases = BillingSecurity.verifyPurchase(
            signedData, signature);
    if (purchases != null && !purchases.isEmpty()) {
        latestPurchase = purchases.get(0);
        confirmTransaction(new String[] { latestPurchase.notificationId });
        if (mCompletedHandler != null) {
            mCompletedHandler.sendEmptyMessage(0);
        } else {
            Log
                    .e(
                            TAG,
                            "verifyPurchase error. Handler not instantiated. Have you called setCompletedHandler()?");
        }
    }
}

and in Confirm_Notification u hav to check for

if (notifyIds[0] != null)
like image 68
Maulik J Avatar answered Sep 22 '22 17:09

Maulik J


Follow this:

    confirmTransaction(new String[] { latestPurchase.notificationId });

here and do this:

protected static void confirmTransaction(String[] notifyIds) {
        if (amIDead()) {
            return;
        }
        // there isn't a notifyid then this was the restore transaction call and this should be skipped 
        if (notifyIds[0] != null){
        Log.i(TAG, "confirmTransaction()");
        Bundle request = makeRequestBundle("CONFIRM_NOTIFICATIONS");
             ......
             ......
}

Works like a charm form me.. Thanks Guys...

like image 28
Abraham Ventura Avatar answered Sep 21 '22 17:09

Abraham Ventura