简体   繁体   中英

In-app purchase Error you already own this item Android

Help me!!!

I'm an Android starter, and currently doing a test app. This app only got 2 button and when you press on the "purchase test item" button, it go through an in app purchase. And when you press the use button, it runs consumePurchase function.

Here is my code:

package com.ovation.testpmt;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.vending.billing.IInAppBillingService;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;`enter code here`
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener{


    IInAppBillingService mservice;
    ServiceConnection connection;

    //String inappid = "android.test.purchased"; //replace this with your in-app product id
    String inappid ="234";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mservice = IInAppBillingService.Stub.asInterface(service);  
                try {
                    mservice.getPurchases(3, getPackageName(), "inapp", null);
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub
                mservice = null;
            }
        };

        bindService(new Intent(
                "com.android.vending.billing.InAppBillingService.BIND"),
                connection, Context.BIND_AUTO_CREATE);

        Button purchaseBtn = (Button) findViewById(R.id.purchase);
        Button consumeBtn = (Button) findViewById(R.id.consume); 

        purchaseBtn.setOnClickListener(this);       
        consumeBtn.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == 1001) {           
              int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
              String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
              String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

              if (resultCode == RESULT_OK) {
                 try {
                    JSONObject jo = new JSONObject(purchaseData);
                    String sku = jo.getString("productId");
                    Log.i("test","You have bought the " + sku + ". Excellent choice, adventurer!");
                  }
                  catch (JSONException e) {
                     Log.i("test","Failed to parse purchase data.");
                     e.printStackTrace();
                  }
              }
           }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (connection != null) {
            unbindService(connection);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        // TODO Auto-generated method stub
        if(v.getId()==R.id.purchase){
            ArrayList<String> skuList = new ArrayList<String>();
            skuList.add(inappid);
            Bundle querySkus = new Bundle();
            querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
            Bundle skuDetails;
            try {
                skuDetails = mservice.getSkuDetails(3, getPackageName(),
                        "inapp", querySkus);

                int response = skuDetails.getInt("RESPONSE_CODE");

                if (response == 0) {

                    ArrayList<String> responseList = skuDetails
                            .getStringArrayList("DETAILS_LIST");

                    for (String thisResponse : responseList) {
                        JSONObject object = new JSONObject(thisResponse);
                        String sku = object.getString("productId");
                        String price = object.getString("price");

                        if (sku.equals(inappid)) {

                            Bundle buyIntentBundle = mservice
                                    .getBuyIntent(3, getPackageName(), sku,
                                            "inapp",
                                            "aaa");

                            PendingIntent pendingIntent = buyIntentBundle
                                    .getParcelable("BUY_INTENT");
                            if(buyIntentBundle==null){
                                Log.i("test", "Your everything is empty123");
                            }else{
                                if(pendingIntent == null){


                                }else{

                                    startIntentSenderForResult(
                                            pendingIntent.getIntentSender(), 1001,
                                            new Intent(), Integer.valueOf(0),
                                            Integer.valueOf(0), Integer.valueOf(0));

                                }

                            }   

                        }
                    }
                }
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SendIntentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else if(v.getId()==R.id.consume){
            Bundle ownedItem = null;
            try {
                ownedItem = mservice.getPurchases(3, getPackageName(), "inapp", null);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int res = ownedItem.getInt("RESPONSE_CODE");
            Log.i("test", "response ---> "+res);
            if(res==0){
                Log.i("test", "res is 0");
                ArrayList<String> ownedSkus = 
                          ownedItem.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");

                Log.i("test", "the size of ownedItem is "+ownedSkus.size());

                for(String str:ownedSkus){
                    Log.i("test", "Str ---> "+str);
                }
                 ArrayList<String> purchaseDataList = 
                         ownedItem.getStringArrayList("INAPP_PURCHASE_DATA_LIST");

                 for(String str:purchaseDataList){
                        Log.i("test", "Str ---> "+str);
                    }
            }

            String token = "my actual string token";
            try {
                int Buyresponse = mservice.consumePurchase(3, getPackageName(), token);


            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
}

I have the in app product on my developer console, and I give the permission in manifest file.

At the first time, it runs very well, but when i trying to remove the purchase by pressing the 'use button' and run it again, it gave me "Error, you already own this item" message.

Can anyone give me a solution for this issue?

You are getting this problem because the product is not getting consumed. Because you can only buy a product another time after its consumption.

Please check your consume button code.

Also i would recommend you to use the latest inAppBilling API's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM