在应用计费中,购买验证在调试和使用实际产品时均失败

时间:2014-07-07 13:25:11

标签: android in-app-purchase in-app-billing signature

编辑:我重写了整个代码,它现在有效。

嗨,我一直试图解决这个问题几天,需要你的帮助。我使用了琐碎的驱动器样本(由于某种原因,它不会在nexus 4上运行)。我试过这个使用debug(android.test.purchased)和来自控制台的实际产品(签名APK,确认密钥,检查版本,产品是活动的等等 - 找不到项目... LogCat说它由于签名验证错误)。所以我回去用“android.test.purchased”测试它,它让我第一次购买。但它不会消耗它。下次我打开活动时它会给出消息,这是要消耗的东西(来自查询库存方法的吐司),但它不会消耗。我究竟做错了什么?我找不到有关stackoverflow的答案。

    import com.android.vending.billing.IInAppBillingService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class BuyCoins extends Activity {

    UpdateCharacter shopChar;

    String TAG = null;
    IInAppBillingService mService;
    IabHelper mHelper;
    Purchase purchased;
    ImageButton goog, twit, fbook, buy1000, buy3000, buy5000;

    String SKU_BUY1000, SKU_BUY3000, SKU_BUY5000;
    String base64EncodedPublicKey = "key removed";

    String cash;
    int money, rowId;

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

        shopChar = new UpdateCharacter(this);
        shopChar.open();
        rowId = shopChar.getLastId();
        cash = shopChar.getMoney(rowId);
        money = Integer.parseInt(cash);
        shopChar.close();

        SKU_BUY1000 = "android.test.purchased";
        SKU_BUY3000 = "buy3000";
        SKU_BUY5000 = "buy5000";

        goog = (ImageButton) findViewById(R.id.google);
        twit = (ImageButton) findViewById(R.id.twitter);
        fbook = (ImageButton) findViewById(R.id.facebook);
        buy1000 = (ImageButton) findViewById(R.id.buy1000);
        buy3000 = (ImageButton) findViewById(R.id.buy3000);
        buy5000 = (ImageButton) findViewById(R.id.buy5000);

    }

    // end of oncreate

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

            if (result.isFailure()) {
                // handle error here
                Toast.makeText(getApplicationContext(),
                        "error in inventory finished", Toast.LENGTH_LONG)
                        .show();

            } else {
                // does the user have the premium upgrade?
                Toast.makeText(getApplicationContext(),
                        "purchase waiting to be consumed", Toast.LENGTH_LONG)
                        .show();
                if(inventory.hasPurchase(SKU_BUY1000))

                try {
                    purchased = inventory.getPurchase(SKU_BUY1000);
                    if (purchased != null) {
                        mHelper.consumeAsync(
                                inventory.getPurchase(SKU_BUY1000),
                                mConsumeFinishedListener);
                        return;
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(getApplicationContext(),
                            "consumption call error", Toast.LENGTH_LONG).show();
                }
                 else if(inventory.hasPurchase(SKU_BUY3000))
                 mHelper.consumeAsync(inventory.getPurchase(SKU_BUY1000),mConsumeFinishedListener);
                 else if(inventory.hasPurchase(SKU_BUY5000))
                 mHelper.consumeAsync(inventory.getPurchase(SKU_BUY1000),mConsumeFinishedListener);
                // update UI accordingly
            }
        }
    };
    // end of check inventory

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
        public void onConsumeFinished(Purchase purchase, IabResult result) {
            if (result.isSuccess()) {
                // provision the in-app purchase to the user
                // (for example, credit 50 gold coins to player's character)
                Toast.makeText(getApplicationContext(), "you get 1000 coins",
                        Toast.LENGTH_LONG).show();

                buy1000.setEnabled(true);
                shopChar.open();
                shopChar.updateMoney(rowId, money + 1000);
                shopChar.close();

            } else {
                // handle error
                Toast.makeText(getApplicationContext(),
                        "error in starting consumption", Toast.LENGTH_LONG)
                        .show();

            }
        }
    };

    // end of consume purchase

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        mHelper = new IabHelper(this, base64EncodedPublicKey);

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                if (!result.isSuccess()) {
                    // Oh noes, there was a problem.
                    Log.d(TAG, "Problem setting up In-app Billing: " + result);
                }

                // Have we been disposed of in the meantime? If so, quit
                if (mHelper == null)
                    return;

                // Hooray, IAB is fully set up!
                Toast.makeText(getApplicationContext(), "IAB",
                        Toast.LENGTH_LONG).show();
                try {
                    mHelper.queryInventoryAsync(mGotInventoryListener);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(getApplicationContext(),
                            "inventory query error", Toast.LENGTH_LONG).show();
                }

            }
        });

        goog.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "google",
                        Toast.LENGTH_LONG).show();

            }

        });
        // end of goog onclick
        twit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "twitter",
                        Toast.LENGTH_LONG).show();

            }

        });
        // end of twitter onclick
        fbook.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "facebook",
                        Toast.LENGTH_LONG).show();

            }

        });
        // end of fbook onclick
        buy1000.setOnClickListener(new OnClickListener() {

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

                mHelper.launchPurchaseFlow(BuyCoins.this, SKU_BUY1000, 10001,
                        mPurchaseFinishedListener);
                buy1000.setEnabled(false);
            }

        });
        // end of buy1000
        buy3000.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mHelper.launchPurchaseFlow(BuyCoins.this, SKU_BUY3000, 10001,
                        mPurchaseFinishedListener);
                buy3000.setEnabled(false);

            }

        });
        // end of buy3000
        buy5000.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mHelper.launchPurchaseFlow(BuyCoins.this, SKU_BUY5000, 10001,
                        mPurchaseFinishedListener);
                buy5000.setEnabled(false);

            }

        });
        // end of buy5000

    }

    // end of on resume

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            if (result.isFailure()) {
                Log.d(TAG, "Error purchasing: " + result);
                Toast.makeText(getApplicationContext(), " no purchase error",
                        Toast.LENGTH_LONG).show();

                return;
            } else if (purchase.getSku().equals(SKU_BUY1000)) {
                // consume the gas and update the UI

                mHelper.consumeAsync(purchase, mConsumeFinishedListener);


            } else if (purchase.getSku().equals(SKU_BUY3000)) {
                // give user access to premium content and update the UI
                mHelper.queryInventoryAsync(mGotInventoryListener);
            } else if (purchase.getSku().equals(SKU_BUY5000)) {
                // give user access to premium content and update the UI
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }
        }
    };

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

    } // end of destroy

} // end of activity

1 个答案:

答案 0 :(得分:0)

我重写了整个代码,它现在有效。不确定到底出了什么问题。但是我确实知道你必须在Alpha中发布你的应用程序(实际上是发布而不是草稿)才能测试实际的产品。