Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement restore purchase in android

Thanks for visiting my page.

Few days ago, I've developed simple android game with in app-billing.

Now I am going to implement restore purchase function but I don't know how can I dot it.

I've made few days of googling and found many links to help it but they didn't work me now.

Please let me know how to do it programmatically.

Where can i find sample of restore purchase ?

I've implemented in app purchase already but not restore purchase.

I used Android Studio 1.5.1.

I've refered http://www.techotopia.com/index.php/An_Android_Studio_Google_Play_In-app_Billing_Tutorial to implement in app purchase.

Please help me :(

Thanks in advance.

like image 772
gstream79 Avatar asked Feb 08 '16 14:02

gstream79


People also ask

Can I restore purchases from Google Play?

In order to restore purchase on a Google Play Android device, the device MUST be tied to the same account that originally made. This is a restriction by Google to limit piracy.

How do I restore purchases on a new phone?

Purchases may automatically update upon installing onto a subsequent device from Google Play. In the event that they do not, simply make sure you are logged into the same account which was used for purchasing the items in question, then select [Restore Transactions] from the bottom bar of the Store menu page.

How does restore purchase work?

If you have multiple devices signed in to the same ‌Apple ID, Restore Purchase will allow you to transfer your in-app purchases to your other devices without having to pay again. For example, if you make an in-app purchase inside an app on your iPhone, you may be able to restore that in-app purchase on your iPad.


1 Answers

If you are implemented the InApp Purchase using v3 you need not worry about the restore Purchase implementation. You can query the inventory and catch the existing Purchase information. Please check the implementation.

What I did here is I have already a purchase module. While I complete the purchase, I will send the information to our server. After relog in or come back to the application, the server will send the current user purchase info whether he is Purchased or not. if the server gives a negative result, I will check the query inventory that is there any existing purchase over there. For that, I am using the following code in the MainActivity onCreate().

 mHelper = new IabHelper(this, base64EncodedPublicKey);
        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()) {
                    Log.e(TAG, "In App Set UP error:: Please check gmail account settings/ Credit Card Info etc");
                    return;
                }
                if (mHelper == null) return;
                mBroadcastReceiver = new IabBroadcastReceiver(MainActivity.this);
                IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION);

                registerReceiver(mBroadcastReceiver, broadcastFilter);

                Log.d(TAG, "Setup successful. Querying inventory.");
                if (mSupplier.getmSubscriptionStatus() == 0) { // This is the Status given from Local Server 0- UnScubscribed User, 1- Subscribed User
                    mHelper.queryInventoryAsync(mGotInventoryListenerForPurchase);
                }
            }
        });

In the Result, You can Identify the existing purchase information.

IabHelper.QueryInventoryFinishedListener mGotInventoryListenerForPurchase = new IabHelper.QueryInventoryFinishedListener() {
            public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                try {
                    Log.d(TAG, "Query inventory For Purchase finished.");
                    String payload = // Your developer payload
                    if (mHelper == null) return;
                    if (result.isFailure()) {
                        Log.v(TAG, "PURCHSE RESULT ERROR- NO PURCHASE AVAILABLE");
                        return;
                    }
                    Log.v(TAG, "Query inventory For Purchase was successful.");
                    if (mSkuDetailList == null || mSkuDetailList.isEmpty()) {
                        Log.v(TAG, "SKU INFO FROM LOCAL SERVER- UNABLE TO PURCHASE");
                        return;
                    }
                    Purchase premiumPurchase = null;
                    for (IabSkuDetailModel data : mSkuDetailList) {

                        // Filter the purchase info using SKU:::
                        premiumPurchase = inventory.getPurchase(data.getmPackageName());
                        if (premiumPurchase != null) {
                            break;
                        }
                    }
                    if (premiumPurchase == null) {
                        Log.v(TAG, "NO Available Purchase for this user");
                        return;
                    }
                    if (verifyDeveloperPayload(premiumPurchase)) {
                        Log.v(TAG, "Purchase is there already ::: developer payload is Matching:: This need to update Local Server: No need to purchase agian");
                        if (premiumPurchase.getSku().equalsIgnoreCase(mSelectedSku)) {
                            IabPurchaseUpdateReq request = new IabPurchaseUpdateReq();
                            request.setmPurchaseToken(premiumPurchase.getToken());
                            request.setmUserId("" + mSupplier.getmUserId());
                            request.setmPublicKey(IabConstants.IAB_RSA_PUBLIC_KEY);
                            request.setmSignature(premiumPurchase.getSignature());
                            request.setmSubscriptionId(premiumPurchase.getSku());
                            request.setmPurchaseObj(premiumPurchase.getOriginalJson());

                            //Update "result to local Server"
                        } else {
                            Log.v(TAG, "SKU mismatch ::: ");
                        }
                    } else {
                         Log.v(TAG, "Developer payload error:: Wrong Purchase");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
like image 69
Nithinjith Avatar answered Sep 26 '22 00:09

Nithinjith