Android In app Billing V3 不工作

标签 android in-app-billing cocos2d-x-2.x

我将使用 IabHelper (trivialdrivesample) 实现 Android 应用内结算 V3。 我用 cocos2d-x 制作了游戏。 在谷歌开发者控制台中已经有十个应用内产品。 它们几乎可以正常工作,但有些项目不起作用。没有显示应用程序内 bundle Activity 。 我不知道为什么有些效果好,有些效果不好。 这很奇怪。

这是我的一些代码块。

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

        if (result.isFailure()) {
            JSONObject prmsToSend = null;
            String jsonStr = "{\"Message\":\"Failed 1\"}";
            try {
                prmsToSend = new JSONObject(jsonStr);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            AndroidNDKHelper.SendMessageWithParameters("failTrans", prmsToSend);
            return;
        }

        Log.d(TAG, "Purchase successful.");

            // bought 1/4 tank of gas. So consume it.
            Log.d(TAG, "Purchase is gas. Starting gas consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);
    }
};

// Called when consumption is complete
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
    public void onConsumeFinished(Purchase purchase, IabResult result) {
        Log.d(TAG, "Consumption finished. Purchase: " + purchase + ", result: " + result);

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

        // We know this is the "gas" sku because it's the only one we consume,
        // so we don't check which sku was consumed. If you have more than one
        // sku, you probably should check...
        if (result.isSuccess()) {
            // successfully consumed, so we apply the effects of the item in our
            // game world's logic, which in our case means filling the gas tank a bit
            Log.d(TAG, "Consumption successful. Provisioning.");
        }
        else {
        }
        AndroidNDKHelper.SendMessageWithParameters("completeTrans", null);
        Log.d(TAG, "End consumption flow.");
    }
};

// Listener that's called when we finish querying the items and subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        Log.d(TAG, "Query inventory finished.");

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

        // Is it a failure?
        if (result.isFailure()) {
            return;
        }

        Log.d(TAG, "Query inventory was successful.");

        /*
         * Check for items we own. Notice that for each purchase, we check
         * the developer payload to see if it's correct! See
         * verifyDeveloperPayload().
         */


        Log.d(TAG, "Initial inventory query finished; enabling main UI.");
    }
};

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    myAndroidContext = EasyAsPi.getContext();

    // 
    AndroidNDKHelper.SetNDKReciever(this);

    // Try to get your hash key
    try {
        PackageInfo info = getPackageManager().getPackageInfo("co.easyaspi.piguys", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("MY KEY HASH:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            System.out.println("MY KEY HASH:"+ Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    }
    catch (NameNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    }

    // Initialize EziSocial Plugin Manager for Android.
    // ToDo Make sure you add the Facebook App ID in strings.xml file.
    EziSocialManager.initWithActivity(this,
                                      this.getApplicationContext().getString(R.string.app_id),
                                      false, // Set to see ezisocial plugin logs
                                      savedInstanceState
                                      );

    String base64EncodedPublicKey = 
            "My 64bit key";

    mHelper = new IabHelper(this, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            Log.d(TAG, "Setup finished.");

            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                return;
            }

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

            // IAB is fully set up. Now, let's get an inventory of stuff we own.
            Log.d(TAG, "Setup successful. Querying inventory.");
            mHelper.queryInventoryAsync(mGotInventoryListener);
        }
    });
}

@Override
protected void onResume()
{
    super.onResume();
    EziSocialManager.applicationResumed();
}

@Override
public void onPause()
{
    super.onPause();
    EziSocialManager.applicationPaused();
}

@Override
public void onDestroy()
{
    super.onDestroy();
    EziSocialManager.applicationDestroyed();

    Log.d(TAG, "Destroying helper.");
    if (mHelper != null) {
        mHelper.dispose();
        mHelper = null;
    }
}


public void requestProductData(final JSONObject prms)
{
    Log.v("requestProductData", "Passed params are : " + prms.toString());

    String param = null;
    try {
        param = prms.getString("item");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String item = null;
    if(param.equals("1"))
    {
        item = strItem1;
    }
    else if(param.equals("2"))
    {
        item = strItem2;
    }
    else if(param.equals("3"))
    {
        item = strItem3;
    }
    else if(param.equals("4"))
    {
        item = strItem4;
    }
    else if(param.equals("5"))
    {
        item = strItem5;
    }
    else if(param.equals("6"))
    {
        item = strItem6;
    }
    else if(param.equals("7"))
    {
        item = strItem7;
    }
    else if(param.equals("8"))
    {
        item = strItem8;
    }
    else if(param.equals("9"))
    {
        item = strItem9;
    }
    else if(param.equals("10"))
    {
        item = strItem10;
    }
    else if(param.equals("11"))
    {
        item = strItem11;
    }
    else if(param.equals("12"))
    {
        item = strItem12;
    }

    Log.d(TAG, "Buy gas button clicked.");

    Log.d(TAG, "Launching purchase flow for gas.");

    strItem = item;

    mHelper.launchPurchaseFlow(this, item, 10001,   
           mPurchaseFinishedListener, "mypurchasetoken");
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    EziSocialManager.onActivityResult(requestCode, resultCode, data);

    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
    if (mHelper == null) return;

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        // not handled, so handle it ourselves (here's where you'd
        // perform any handling of activity results not related to in-app
        // billing...
        super.onActivityResult(requestCode, resultCode, data);
    }
    else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
    }
}

最佳答案

因为我遇到了应用内结算版本 3。如果你想为版本 3 创建测试应用,它看起来像谷歌提供的复杂解决方案。

我在 github 上找到了最好的库。

https://github.com/anjlab/android-inapp-billing-v3

您可以使用它实现简单性。

关于Android In app Billing V3 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23235995/

相关文章:

android-ndk - UnsatisfiedLinkError,Cocos2D-x 应用程序启动时崩溃

android - OpenGL 错误 0x0501 和 0x0505 cocos2d-x, samsung quattro Gl_out_of_memory

android - Cocos2D-x 示例应用程序在启动时崩溃

java - 如何在分页和使用 order by 时列出 Google Drive API v3 上的所有根文件夹和共享?

android - 将 Android ScrollView 滚动到 X 轴和 Y 轴

android - 从多帐户模糊性中购买单个应用程序内商品

google-play - 无需发布到 Google Play 商店的应用内结算

Android 使用 Dagger 2 为动态更改 url 实现 HostSelectionInterceptor

java - 我应该在 Spring 和 Android 中使用一种 Java 模型表示吗?

c# - xamarin.forms 中的应用内购买