User42590
User42590

Reputation: 2521

Google In-APP Purchase Request After reinstalling APP

I implemented Google in App. its working fine after publishing the app. but i am facing a problem . When user install the app and then purchase the items and then uninstall the app and reinstall it and goes to the purchased items then app agian lock the purchsed item and ask for purchase request. is there any problem in OnRestoreTransaction? I used code from the Google In app Purchse site. Here is the code :

 private class DungeonsPurchaseObserver extends PurchaseObserver {
        public DungeonsPurchaseObserver(Handler handler) {
            super(in_app.this, handler);
        }

        @Override
        public void onBillingSupported(boolean supported) {
            if (Consts.DEBUG) {
                Log.i(TAG, "supported: " + supported);
            }
            if (supported) {
                restoreDatabase();  

            } else {
                showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
            }
        }

        @Override
        public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
                int quantity, long purchaseTime, String developerPayload) {
            if (Consts.DEBUG) {
                Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState);
            }

            if (purchaseState == PurchaseState.PURCHASED ) {
                finish();
            }
        }


        @Override
        public void onRequestPurchaseResponse(RequestPurchase request,
                ResponseCode responseCode) {
            if (Consts.DEBUG) {
                Log.d(TAG, request.mProductId + ": " + responseCode);
            }
            if (responseCode == ResponseCode.RESULT_OK) {
                if (Consts.DEBUG) {
                    Log.i(TAG, "purchase was successfully sent to server");
                }
            } else if (responseCode == ResponseCode.RESULT_USER_CANCELED) {
                if (Consts.DEBUG) {
                    Log.i(TAG, "user canceled purchase");
                }
            } else {
                if (Consts.DEBUG) {
                    Log.i(TAG, "purchase failed");
                }
            }
        }

        @Override
        public void onRestoreTransactionsResponse(RestoreTransactions request,
                ResponseCode responseCode) {
            if (responseCode == ResponseCode.RESULT_OK) {
                if (Consts.DEBUG) {
                    Log.d(TAG, "completed RestoreTransactions request");
                }
                // Update the shared preferences so that we don't perform
                // a RestoreTransactions again.
                SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = prefs.edit();
                edit.putBoolean(DB_INITIALIZED, true);
                edit.commit();
            } else {
                if (Consts.DEBUG) {
                    Log.d(TAG, "RestoreTransactions error: " + responseCode);
                }
            }
        }
    }


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.in_app);

        mHandler = new Handler();
        mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler);
        mBillingService = new BillingService();
        mBillingService.setContext(this);

        mPurchaseDatabase = new PurchaseDatabase(this);
        //setupWidgets();

        // Check if billing is supported.
        ResponseHandler.register(mDungeonsPurchaseObserver);
        if (!mBillingService.checkBillingSupported()) {
            showDialog(DIALOG_CANNOT_CONNECT_ID);
        }



        purchase=(Button) findViewById(R.id.purchase);
        cancel=(Button) findViewById(R.id.cancel);

        purchase.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(!ownedItems.contains("android.test.refunded")){      
                    if (Consts.DEBUG) {
                        Log.d(TAG, "buying: " + "  product" + " Product Name: " + "Product");
                    }

                    if (!mBillingService.requestPurchase("android.test.refunded", mPayloadContents)) {
                        showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
                    }
            }
            }
        });

Edited:

I call this in onCrearte of in-app java file : this first checks that whehter the current user purchased the item for which he is demanding and if not then iniate request for Purchase. In onPurchsestateChanged i only checked that state is Purchased or not.

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.in_app);

    mHandler = new Handler();
    mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler);
    mBillingService = new BillingService();
    mBillingService.setContext(this);

    mPurchaseDatabase = new PurchaseDatabase(this);
    //setupWidgets();

    // Check if billing is supported.
    ResponseHandler.register(mDungeonsPurchaseObserver);
    if (!mBillingService.checkBillingSupported()) {
        showDialog(DIALOG_CANNOT_CONNECT_ID);
    }
    System.out.println("for check"+getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false));

    if (getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false)) {
        System.out.println(mBillingService.restoreTransactions());
    }
    else{
    purchase=(Button) findViewById(R.id.purchase);
    cancel=(Button) findViewById(R.id.cancel);

    purchase.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(!ownedItems.contains("android.test.refunded")){      
                if (Consts.DEBUG) {
                    Log.d(TAG, "buying: " + " product" + " Product Name: " + "i");
                }


                if (!mBillingService.requestPurchase("android.test.refunded", mPayloadContents)) {
                    showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
                }
        }
        }
    });


    cancel.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            startActivity(new Intent(in_app.this, Main.class));
            overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
        }
    });
    }
}

OnPurchaseStateChanged:

    public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
            int quantity, long purchaseTime, String developerPayload) {

        if (Consts.DEBUG) {
            Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState);
        }
        System.out.println("here for new fun");
        if (purchaseState == PurchaseState.PURCHASED ) {

            finish();
            Intent intent1 = new Intent(context,BodyParts1.class);
            context.startActivity(intent1);
        }


    }

Upvotes: 1

Views: 409

Answers (1)

Vladimir Mironov
Vladimir Mironov

Reputation: 30864

In order to restore all purchased items you have to explicitly call mBillingService.restoreTransactions(). Then you will receive onPurchaseStateChange callback for each previously purchased item.

Put this code in onCreate method and it should work well:

if (!getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false)) {
    mBillingService.restoreTransactions();
}

Upvotes: 1

Related Questions