无法购买商品回复:7:商品已经拥有

时间:2016-02-17 06:09:42

标签: android in-app-purchase codenameone consuming

我正在测试我的Android应用并面对"无法购买项目(回复:7:项目已经拥有)"。该项目是一个"托管的应用内商品"类型。我做了一些研究并找到了这个(http://developer.android.com/google/play/billing/api.html#managed):

Managed in-app products are items that have their ownership information tracked and managed
by Google Play. When a user purchases a managed in-app item, Google Play stores the
purchase information for each item on a per-user basis. This enables you to later query
Google Play at any time to restore the state of the items a specific user has purchased.
This information is persistent on the Google Play servers even if the user uninstalls the
application or if they change devices.

If you are using the Version 3 API, you can also consume managed items within your
application. You would typically implement consumption for items that can be purchased
multiple times (such as in-game currency, fuel, or magic spells). Once purchased, a managed
item cannot be purchased again until you consume the item, by sending a consumption request
to Google Play. To learn more about in-app product consumption, see Consuming Items.

...

You can use the consumption mechanism to track the user's ownership of in-app products.

In Version 3, all in-app products are managed. This means that the user's ownership of all
in-app item purchases is maintained by Google Play, and your application can query the
user's purchase information when needed. When the user successfully purchases an in-app
product, that purchase is recorded in Google Play. Once an in-app product is purchased, it
is considered to be "owned". In-app products in the "owned" state cannot be purchased from
Google Play. You must send a consumption request for the "owned" in-app product before
Google Play makes it available for purchase again. Consuming the in-app product reverts it
to the "unowned" state, and discards the previous purchase data.

我如何" 发送消费请求 "在Codename One?

2 个答案:

答案 0 :(得分:1)

假设您在其IabHelper.java中使用谷歌提供的SDK。以下是路径:

Android_SDK\sdk\extras\google\play_billing\samples\TrivialDrive\src\com\example\android\trivialdrivesample\util

以下是初始化应用内结算,查询Google Play以供用户购买和使用购买的代码。

  1. 初始化应用内结算。

    /* base64EncodedPublicKey should be YOUR APPLICATION'S PUBLIC KEY
       (that you got from the Google Play developer console). 
    */
    IabHelper mHelper = new IabHelper(this, base64EncodedPublicKey);
    
    // Check whether in-app billing is supported or not.
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
    {
        @Override
        public void onIabSetupFinished(IabResult result)
        {
            if (result.isSuccess())
            {
                // In-app billing is supported. Now, queryInventoryAsync to whether user already purchased.
                ArrayList<String> productIDs = new ArrayList<String>();
                // ITEM_SKU -> Name of the billing item in google play developer console
                productIDs.add(ITEM_SKU);
                mHelper.queryInventoryAsync(true, productIDs, mReceivedInventoryListener);
            }
            else
            {
                // Billing not supported by the device
            }
        }
    });
    
  2. 查询用户的购买

    mReceivedInventoryListener
            = new IabHelper.QueryInventoryFinishedListener()
    {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory)
        {
            if (result.isSuccess())
            {
                // Will be true if already purchased
                boolean hasPurchase = inventory.hasPurchase(ITEM_SKU);
    
                // Here enable or disable your app features
                ...
    
                    /* How to consume the purchase */
    
                // First, get the Purchase from inventory
                Purchase purchase = inventory.getPurchase(ITEM_SKU);
    
                // Consume the purchase
                mHelper.consumeAsync(purchase, mConsumeFinishedListener);
    
            }
            else
            {
                // Failed to query google play for purchases.
            }
        }
    };
    
  3. 购买消费者倾听者

    mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener()
    {
    
        @Override
        public void onConsumeFinished(Purchase purchase, IabResult result)
        {
            if (result.isSuccess())
            {
                // Purchase consumed.
            }
            else
            {
                // Purchase not consumed.
            }
        }
    };
    

答案 1 :(得分:1)

使用

Purchase.getInAppPurchase().wasPurchased(String sku)

如果该商品是托管商品且已购买,则会返回true