应用程序内订阅

时间:2017-09-19 09:14:32

标签: android in-app-billing subscription

当我在测试帐户的应用程序中测试订阅它返回成功回调并根据回调我将一些信息存储在我的远程服务器上......但是当我发布它并用其他帐户测试时的金额订阅已扣除并且已成功执行但我无法收到成功回调,因为我无法访问该内容。我们怎样才能从购买模型或库存模型中获取交易信息,然后我们如何才能获得有关特定订单ID的信息,例如其自己管理的剩余时间等。我们如何识别每个设备上的用户和(他/她)订阅以访问内容

mHelper = new IabHelper(this, "Key");
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                Log.d(TAG, "In-app Billing setup failed: " + result);
            } else {
                Log.d(TAG, "In-app Billing is set up OK");
            }
            // Hooray, IAB is fully set up!
        }
    });

    levelSubscriptionPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result,
                                          Purchase purchase) {
            if (result.isFailure()) {
                Log.d(TAG, "failed billing");
            } else {
                Log.d(TAG, "purchase success Callback billing");
                Log.d(TAG, purchase.getToken());
                setSubscription(session.getUserDetail().getEmail());
            }

        }
    };


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

            if (!result.isFailure()) {
                Log.d(TAG, "Query Inventory  failed Callback billing");

                /*Toast.makeText(Dashboard.this, "InventoryListener", Toast.LENGTH_SHORT).show();
                // Handle failure
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                        mConsumeFinishedListener);*/
            } else {
                Log.d(TAG, "Query Inventory success Callback billing");
            }
        }
    };

    mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
        public void onConsumeFinished(Purchase purchase,
                                      IabResult result) {

            if (result.isSuccess()) {
                Log.d(TAG, "Consume finish success Callback billing");
            } else {
                Log.d(TAG, "Consume finish failed Callback billing");
            }
        }
    };

1 个答案:

答案 0 :(得分:0)

您可以在QueryInventoryFinishedListener中获取购买详细信息,如

Purchase monthlySubscription = inv.getPurchase(SKU_SUBSCRIPTION_MONTHLY);

        if (monthlySubscription != null) {
            Log.d(TAG, "User has already subscribed for monthly benefits.");
            Utils.Log(monthlySubscription.toString());

        } else {
            launchSubscriptions();
        }



private void launchSubscriptions() {
    try {
        iabHelper.launchSubscriptionPurchaseFlow((Activity) context, 
        SKU_SUBSCRIPTION_MONTHLY, REQUEST_CODE, new 
        IabHelper.OnIabPurchaseFinishedListener() {
            @Override
            public void onIabPurchaseFinished(IabResult result, 
            Purchase monthlySubscription) throws 
            IabHelper.IabAsyncInProgressException {
                if (result.isFailure()) {
                    Log.e(TAG, "Error purchasing: " + result);
                } else if (result.isSuccess()) {
                    Log.d(TAG, "---Success---");
                } else {
                    Log.d(TAG, "---Error---");
                }
            }
        }, String.valueOf(preferences.getUserId()));
    } catch (IabHelper.IabAsyncInProgressException e) {
        e.printStackTrace();
    }
}

您还可以在“launchSubscriptionPurchaseFlow”方法中添加一个额外参数。无论何时获得购买详细信息,您还可以获得额外的参数。所以使用此参数可以识别用户明智的购买 (在我的情况下,我在launchSubscriptions()方法中添加userId)