Android 在应用程序登录时进行Google InAppBilling订阅验证

Android 在应用程序登录时进行Google InAppBilling订阅验证,android,google-play-services,Android,Google Play Services,我在我的应用程序中设置了一个订阅产品,它可以用于购买。我在谷歌开发者控制台中看到了购买。当我注销并重新登录到应用程序时,我会再次进入订阅页面。应用程序无法识别订阅已购买,应允许用户访问。如果我再次单击“订阅”按钮,它会显示“您已订阅…管理订阅”。我不确定需要执行哪些操作才能使其在验证订阅后将用户发送到订阅信息,或者在未找到订阅时拒绝,并将用户发送到订阅页面。我想我只是还不知道如何建造它。这是我的密码。我是否缺少一段可以处理这个问题的代码 我正在使用firebase作为注册/登录的后端。不确定我是

我在我的应用程序中设置了一个订阅产品,它可以用于购买。我在谷歌开发者控制台中看到了购买。当我注销并重新登录到应用程序时,我会再次进入订阅页面。应用程序无法识别订阅已购买,应允许用户访问。如果我再次单击“订阅”按钮,它会显示“您已订阅…管理订阅”。我不确定需要执行哪些操作才能使其在验证订阅后将用户发送到订阅信息,或者在未找到订阅时拒绝,并将用户发送到订阅页面。我想我只是还不知道如何建造它。这是我的密码。我是否缺少一段可以处理这个问题的代码

我正在使用firebase作为注册/登录的后端。不确定我是否需要使用firebase来保存令牌等。如有任何帮助,将不胜感激。我知道我是一个新手,正在学习,但我的项目完成了98%,这让我很犹豫

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.iliprivateequity.btfxforexalerts.Util.IabHelper;
import com.iliprivateequity.btfxforexalerts.Util.IabResult;
import com.iliprivateequity.btfxforexalerts.Util.Inventory;
import com.iliprivateequity.btfxforexalerts.Util.Purchase;

public class GoogleInAppBilling extends AppCompatActivity implements View.OnClickListener {

    private String base64EncodedPublicKey = "*****";

    public Button buttonSubscribe;
    public Button buttonSubscribed;
    IabHelper mHelper;
    static final String SKU_SUBSCRIBE_BTFX = "btfx_alerts_service_50";
    static final String TAG = "BTFX_Alerts";


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

        loadInAppPurchase();

        buttonSubscribe = (Button) findViewById(R.id.buttonsubscribe);
        buttonSubscribed = (Button) findViewById(R.id.button_subscribed);
        buttonSubscribed.setEnabled(false); //todo remember to set back to false. its true just for testing the build

        buttonSubscribe.setOnClickListener(this);
        buttonSubscribed.setOnClickListener(this);

    }

    private void loadInAppPurchase() {
        mHelper = new IabHelper(this, base64EncodedPublicKey);
        mHelper.enableDebugLogging(true);

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {

            @Override
            public void onIabSetupFinished(IabResult result) {
                if (!result.isSuccess()) {
                    Log.d(TAG, "Problem setting up in-app billing: " + result);
                } else {
                    Log.d(TAG, "Setup successful. Querying inventory.");
                }
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }


        });
    }

    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {

        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            Log.d(TAG, "Query inventory finished.");
            if (result.isFailure()) {
                Log.d(TAG, "Failed to query inventory: " + result);
                return;

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

            Purchase sku_purchase = inventory.getPurchase(SKU_SUBSCRIBE_BTFX);
            if (sku_purchase != null) {
                mHelper.consumeAsync(inventory.getPurchase(SKU_SUBSCRIBE_BTFX),
                        mConsumeFinishedListener);
                return;

            }
        }
    };

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener(){
        public void  onConsumeFinished(Purchase purchase, IabResult result) {
            Log.d("Tag", "Consumption finished. Purchase: " + purchase + ", result: " + result);

            if (mHelper == null)
                return;

            if (result.isSuccess()) {
                Log.d("TAG", "Consumption successful. Provisioning.");
            } else {
                Log.d("TAG", "Error while consuming: " + result);
            }
            Log.d("Tag", "End consumption flow.");
        }
    };

    @Override
    public void onClick(View v) {
        if (v == buttonSubscribe) {
            mHelper.launchPurchaseFlow(GoogleInAppBilling.this, SKU_SUBSCRIBE_BTFX, IabHelper.ITEM_TYPE_SUBS
                    , 10001, mPurchaseFinishedListener, "alertservicepurchased");
        }
        if (v == buttonSubscribed ){
            startActivity(new Intent(this, ProfileActivity.class));
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (!mHelper.handleActivityResult(requestCode, resultCode, data)){
            super.onActivityResult(requestCode,resultCode,data);
        }else{
            Log.d("TAG", "onActivityResult handled by IABUtil.");
        }
    }

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

            if (mHelper == null) return;

            if (result.isFailure()) {
                Log.d(TAG, "Error purchasing: " + result);
                return;

            }
            Log.d(TAG, "BTFX Alerts subscription purchased.");
            if (purchase.getSku().equals(SKU_SUBSCRIBE_BTFX)) {
                Log.d("TAG", "Thank you for subscribing!");
                mHelper.consumeAsync(purchase, mConsumeFinishedListener);
                // bought the btfx alerts subscription

                buttonSubscribe.setEnabled(false);
                buttonSubscribed.setEnabled(true);

            }

        }
    };
};

您只需检查用户是否已经订阅,并根据结果显示/隐藏付费内容

举例说明如何做到这一点。你也可以考虑使用它,这简化了Android上IAP的工作。