【问题标题】:Need piece of code for validation of existing subscriptions需要一段代码来验证现有订阅
【发布时间】:2013-03-08 03:14:10
【问题描述】:

我已经为此苦苦挣扎了一个多星期。我正在 android 上进行订阅,使用他们的应用内计费版本 3 我构建了代码并让它运行以供购买,但正如去年所做的那样,这个版本不支持订阅,因为我从示例中将它放在一起我不'没有代码来构建使用订阅的代码。

这是我用于购买的代码,据我了解,我需要更改这些代码以处理订阅而不是购买,并添加代码以确定订阅在谷歌服务器上是否有效。我现在没有看到代码。

    String base64EncodedPublicKey = "MI...QAB";
    mHelper = new IabHelper(this, base64EncodedPublicKey);

    // enable debug logging (for a production application, you should set
    // this to false).
    mHelper.enableDebugLogging(false);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                // complain("1 "+"Problem setting up in-app billing: " +
                // result);
                return;
            }
            mHelper.queryInventoryAsync(mGotInventoryListener);
        }
    });



// Listener that's called when we finish querying the items we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,
            Inventory inventory) {
        // ///Log.d(TAG, "Query inventory finished.");
        if (result.isFailure()) {
            // complain("2 "+"Failed to query inventory: " + result);
            return;
        }
        // ///Log.d(TAG, "Query inventory was successful.");
        // place code here to proceess purchase without reaccessing database
        if (inventory.hasPurchase(appSKU)) {
            // ///Log.i("Purchase","purchase consumed here");

        //mHelper.consumeAsync(inventory.getPurchase(appSKU),mConsumeFinishedListener);

            return;
        }
    }
};

public void order(String appSKU) {
    mHelper.launchPurchaseFlow(this, appSKU, RC_REQUEST,
            mPurchaseFinishedListener);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + ","
    // + data);

    // 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.");
    }
}

// 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(purchase == null) {
            ;
        }else {
        String tester1 = purchase.toString();

        // ///Log.d(TAG, "Purchase successful.");
    //  mHelper.consumeAsync(purchase, mConsumeFinishedListener);



           // myCallServer(udid, calltype, data, OrderID, ProductId, PurchaseToken) {


        Bundle myBundle = new Bundle();
        myBundle.putString("AppTitle", AppTitle);

        Intent myIntent = new Intent(getBaseContext(),
                IntroALevActivity.class);
        myIntent.putExtras(myBundle);
        startActivity(myIntent);
    }

    }

};

// 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);

        // 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
            alert("You made a purchase");
        } else {
                    ;
        }
    }
};

我在网上查看过,但我只找到了一些损坏的代码来帮助我确定检查现有订阅的确切方法代码。如果有人可以查看我的代码并提供我缺少的方法,那就太好了。 Google 示例通常很好,但由于订阅刚刚添加到 V3 到 2 月 15 日,因此没有很多编码示例可供构建。

我正在苦苦挣扎,所以如果您有任何问题或需要更多信息,请告诉我,我会尽我所能提供。

感谢 stackoverflow 成员

【问题讨论】:

    标签: android in-app-purchase subscriptions


    【解决方案1】:

    即使我没有在我的实现中使用订阅,您也可以在 IabHelper.QueryInventoryFinishedListener mGotInventoryListener 函数中进行检查。所以不要这样:

    // ///Log.d(TAG, "Query inventory was successful.");
        // place code here to proceess purchase without reaccessing database
        if (inventory.hasPurchase(appSKU)) {
            // ///Log.i("Purchase","purchase consumed here");
    
        //mHelper.consumeAsync(inventory.getPurchase(appSKU),mConsumeFinishedListener);
    
            return;
        }
    

    使用这个:

    Purchase haspurchase = inventory.getPurchase(appSKU);
    if (haspurchase = null) {
        // do something since there wasn't a purchase
    }
    else {
       // do something if you have the purchase 
    }
    

    【讨论】:

      【解决方案2】:

      要使用应用内订阅,我认为您将在您创建的 IabHelper 实例上调用“launchSubscriptionPurchaseFlow()”方法。

          mHelper.launchSubscriptionPurchaseFlow(Activity act, String sku, int requestCode,
          OnIabPurchaseFinishedListener listener, String extraData);
      

          mHelper.launchPurchaseFlow(Activity act, String sku, String itemType, int requestCode,
                          OnIabPurchaseFinishedListener listener, String extraData);
      

      其中 itemType = IabHelper.ITEM_TYPE_SUBS

      【讨论】:

        猜你喜欢
        • 2011-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多