consuming in App purchased item

2019-06-12 11:52发布

I'm trying to use in app purchase, after I want to purchase my item, I get the following error in the result of " IabHelper.OnConsumeFinishedListener mConsumeFinishedListener " my Item is not a subscription !

the result.getmessage is as following: "Items of type 'subs' can't be consumed. (response: -1010:Invalid consumption attempt)"

what shall I do ?

my full code:

public class BuyCoins extends Activity{

String TAG="TESTPURCHASE";
IabHelper mHelper;
static final String ITEM_100_SKU = "ir.e_rundev.brainwars.test";
static final String ITEM_500_SKU = "ir.e_rundev.brainwars.test2";
static final String ITEM_1000_SKU = "ir.e_rundev.brainwars.1000coins";
String ITEM_SKU="";





protected 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.buy_coins);

    Swarm.setActive(this);



    String base64EncodedPublicKey =
            "(I HAVE MY CODE HERE)";

    mHelper = new IabHelper(this, base64EncodedPublicKey);

    mHelper.startSetup(new
                               IabHelper.OnIabSetupFinishedListener() {
                                   public void onIabSetupFinished(IabResult result)
                                   {
                                       if (!result.isSuccess()) {
                                       //    Log.d(TAG, "In-app Billing setup failed: " +
                                                 //  result);
                                       } else {
                                        //   Log.d(TAG, "In-app Billing is set up OK");
                                       }
                                   }
                               });

    Button btn100 = (Button) findViewById(R.id.btn_Buy_100);
    Button btn500 = (Button) findViewById(R.id.btn_Buy_500);
    Button btn1000 = (Button) findViewById(R.id.btn_Buy_1000);

    btn100.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ITEM_SKU=ITEM_100_SKU;
            buyCoin(view);
        }
    });

    btn500.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ITEM_SKU=ITEM_500_SKU;
            buyCoin(view);
        }
    });

    btn1000.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // ITEM_SKU=ITEM_1000_SKU;
           // buyCoin(view);

        }
    });


}

public void buyCoin (View view) {
    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
            mPurchaseFinishedListener, "mypurchasetoken");
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data)
{
    if (!mHelper.handleActivityResult(requestCode,
            resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}


IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
        = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result,
                                      Purchase purchase)

    {
        if (result.isFailure()) {
            // Handle error
            return;
        } else if (purchase.getSku().equals(ITEM_100_SKU)) {
            consumeItem();
            Log.d(TAG, "Here"+purchase.getItemType());

        } else if (purchase.getSku().equals(ITEM_500_SKU)) {

            consumeItem();


        } else if (purchase.getSku().equals(ITEM_1000_SKU)) {
            consumeItem();


        }

    }



    public void consumeItem() {
        mHelper.queryInventoryAsync(mReceivedInventoryListener);
        Log.d(TAG,"here 3");
    }

    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
            = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory) {

            if (result.isFailure()) {
                // Handle failure
            } else {
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                        mConsumeFinishedListener);
                Log.d(TAG,"here 4"+inventory.getPurchase(ITEM_SKU).getItemType());
            }
        }
    };

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
            new IabHelper.OnConsumeFinishedListener() {
                public void onConsumeFinished(Purchase purchase,
                                              IabResult result) {

                    if (result.isSuccess()) {
                        //clickButton.setEnabled(true);

                        //  Log.d(TAG,"SUCCESSFUL");
                        purchaseFinished(ITEM_SKU);
                        Log.d(TAG,"here2");

                        SharedPreferences settings = getSharedPreferences("MYSETTINGS", 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean("isPremium", true);
                        editor.apply();

                        Adad.setDisabled(true);


                    } else {
                        // handle error
                        Log.d(TAG,"here 5");
                        Log.d(TAG,result.getMessage());
                        Log.d(TAG,purchase.toString());
                    }
                }
            };
};

@Override
public void onDestroy() {
    super.onDestroy();
    if (mHelper != null) {
        mHelper.dispose();

        mHelper = null;
    }
}


public void onResume() {
    super.onResume();
    Swarm.setActive(this);
    TextView tvCoins = (TextView) findViewById(R.id.tv_Buy_Coins);
    GameInventory gameInventory = new GameInventory(getApplicationContext());
    tvCoins.setText(String.valueOf(gameInventory.getCoinsCount()));
}

public void onPause() {
    super.onPause();
    Swarm.setInactive(this);
}


private void purchaseFinished(String SKU){
    GameInventory gameInventory = new GameInventory(getApplicationContext());

    if (SKU==ITEM_100_SKU) {

        gameInventory.updateCoinsCount(gameInventory.getCoinsCount() + 100);
    }else if(SKU==ITEM_500_SKU){
        gameInventory.updateCoinsCount(gameInventory.getCoinsCount() + 520);
    }else if (SKU==ITEM_1000_SKU){
        gameInventory.updateCoinsCount(gameInventory.getCoinsCount() + 1100);
    }
    ITEM_SKU = "";
}

}

1条回答
The star\"
2楼-- · 2019-06-12 12:22

Hey I'm also working on InApp Purchase since 10 days and I've successfully integrated in my existing app and ready to make it live. Initially when i had started doing this I've downloaded google InApp Billing Example called "Trivial Drive" from here.

But it didn't help me much as it has lots of issues and bugs, So I've decided do it on my own from scratch using new v3 api which you can find here. This tutorial has clear explanation that would help you and also if you have time, see this youtube video where google employee had explained clearly how to integrate it.

Also if you want quick example, I've a sample app which you can download from here.

The following video also explains how to integrate InApp Purchase. Please go through it.

https://www.youtube.com/watch?v=-h2ESH71hAI

Thank you

查看更多
登录 后发表回答