In-App Billing test: android.test.purchased already owned

后端 未结 16 1339
情深已故
情深已故 2020-12-07 08:09

I am currently testing In-App Billing for a future app, and after I successfully \"bought\" the test item \"android.test.purchased\" the first time, I now receive the respon

相关标签:
16条回答
  • 2020-12-07 08:30

    For testing purposes I also suggest you to insert a piece of code that will be clearing all the products that you've bought before calling a method that initializes gp purchase flow. That is especially comfortable, when you test just one item at the moment. E.g. like this:

    PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP);
        for (Purchase sourcePurchase : purchasesResult.getPurchasesList()) {
            if(sourcePurchase != null){
    
                ConsumeResponseListener listener = new ConsumeResponseListener() {
                    @Override
                    public void onConsumeResponse(String outToken, @BillingResponse int responseCode) {
    
                        System.out.println("all consumed");
                    }
                };
                mBillingClient.consumeAsync(sourcePurchase.getPurchaseToken(), listener);
            }else{
                System.out.println("null");
            }
        }
    
    // and then initiate whole process with clear "shoping basket"
    
    BillingFlowParams.Builder builder = new BillingFlowParams.Builder()
            .setSku(itemName).setType(BillingClient.SkuType.INAPP);
    
    0 讨论(0)
  • 2020-12-07 08:31

    If you are in test environment

    1) In the case of android.test.purchased, I can reset the fake payment by restarting android device(consumed the inventory).

    2) In InApp util there is a file called Security.java make it as following, for temporary. Since the testing payment(fake) always return false due to security exception.

    public static boolean verifyPurchase(String base64PublicKey,
                                         String signedData, String signature) {
        return true; }
    

    Then in your OnIabPurchaseFinishedListener call fechInvForconsumeItem()

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
                = new IabHelper.OnIabPurchaseFinishedListener() {
            public void onIabPurchaseFinished(IabResult result,
                                              Purchase purchase)
            {
                if (result.isFailure()) {
                    // Handle error
                    Log.e("123","Failure");
    
                    return;
                }
                else if (purchase.getSku().equals(ITEM_SKU)) {
                    Log.e("123","PURCAsed");
                    fechInvForconsumeItem(); // Restart device if not consume
    
                }
    
            }
        };
    

    The fechInvForconsumeItem() is

        public void fechInvForconsumeItem() {
        mHelper.queryInventoryAsync(mReceivedInventoryListener);
    }
    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
            = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory) {
    
    
            if (result.isFailure()) {
                // Handle failure
                Log.e("11","Failure");
    
    
    
            } else {
                Log.e("11","suc");
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                        mConsumeFinishedListener);
            }
    
    
        }
    };
    

    Consume Listener is

        IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
            new IabHelper.OnConsumeFinishedListener() {
                public void onConsumeFinished(Purchase purchase,
                                              IabResult result) {
    
                    if (result.isSuccess()) {
                    } else {
                        // handle error
                        Log.e("11","sucConsume");
                    }
                }
            };
    
    0 讨论(0)
  • 2020-12-07 08:38
    IabHelper.QueryInventoryFinishedListener 
           mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
           public void onQueryInventoryFinished(IabResult result, Inventory inventory)   
           {
              if (result.isFailure()) {
                 return;
               }          
              try {
    
                    if(inventory.hasPurchase("product_sku_id"))
                    {   
                         isItemEnable= true;
                         mHelper.consumeAsync(inventory.getPurchase("product_sku_id"),null);            
                    }
                    else
                    {
                           isItemEnable = false;
                    }           
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
           }
    
        };
    
    0 讨论(0)
  • 2020-12-07 08:38

    This is the difference between consumable and non-consumable items; non-consumable items (what you seem to be dealing with here) have their state tracked persistently, while consumable items can be purchased multiple times. You'll have to go into your Play management console and cancel/refund the sale to test it again.

    0 讨论(0)
  • 2020-12-07 08:39

    go to Google Play console and open Order Management tab. There you can refund / cancel test purchases.

    0 讨论(0)
  • 2020-12-07 08:42

    This is how we can consume the Item

     consume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String purchaseToken = "inapp:" + getPackageName() + ":android.test.purchased";
                        try {
                            Log.d("","Running");
                            int response = mService.consumePurchase(3, getPackageName(), purchaseToken);
                            if(response==0)
                            {
                                Log.d("Consumed","Consumed");
                            }else {
                                Log.d("","No"+response);
                            }
                        }catch (RemoteException e)
                        {
                            Log.d("Errorr",""+e);
                        }
    
                    }
                });
                t.start();
            }
        });
    
    0 讨论(0)
提交回复
热议问题