应用登录时的Google InAppBilling订阅验证

时间:2017-01-21 23:15:37

标签: android google-play-services

我在我的应用中设置了订阅产品,它适用于购买。我在google开发者控制台中看到了这次购买。当我退出然后返回应用程序时,我再次进入订阅页面。该应用无法识别订阅已购买且应允许用户访问。如果我再次单击订阅按钮,它会说"您已经订阅了...管理订阅。"我不确定需要做什么才能使用户在验证订阅后将用户发送到订阅的信息,或者拒绝未找到订阅并将用户发送到订阅页面。我想我还没有知识如何建立它。这是我的代码。我错过了一大堆可以处理的代码吗?

我使用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);

            }

        }
    };
};

1 个答案:

答案 0 :(得分:1)

您只需要检查用户是否已经订阅并依赖于结果显示/隐藏付费内容。

There is示例如何做到这一点。此外,您可以考虑使用library,这简化了在Android上使用IAP的工作。