Florian Walther
Florian Walther

Reputation: 6971

Play Billing Library 1.0 - no connection

i am trying to implement google's new Play Billing Library 1.0, but i can't get a connection going. It worked with the old Trivial Drive classes.

I always get responseCode = 3/UNAVAILABLE

I run the app on a real device, downloaded as an alpha tester from google play. Any ideas?

public BillingManager(Activity activity, final BillingUpdatesListener updatesListener) {
    mActivity = activity;
    mBillingUpdatesListener = updatesListener;
    mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();

    startServiceConnection(new Runnable() {
        @Override
        public void run() {
            mBillingUpdatesListener.onBillingClientSetupFinished();
            queryPurchases();
        }
    });
}

public void startServiceConnection(final Runnable executeOnSuccess) {
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingClient.BillingResponse int responseCode) {
            if (responseCode == BillingClient.BillingResponse.OK) {
                mIsServiceConnected = true;
                if (executeOnSuccess != null) {
                    executeOnSuccess.run();
                }
            }
            mBillingClientResponseCode = responseCode;
        }

        @Override
        public void onBillingServiceDisconnected() {
            mIsServiceConnected = false;
        }
    });
}

public void initiatePurchaseFlow(final String skuId, final ArrayList<String> oldSkus, final @BillingClient.SkuType String billingType) {
    Runnable purchaseFlowRequest = new Runnable() {
        @Override
        public void run() {
            BillingFlowParams.Builder mParams = BillingFlowParams.newBuilder().setSku(skuId).setType(billingType).setOldSkus(oldSkus);
            mBillingClient.launchBillingFlow(mActivity, mParams.build());
        }
    };
    executeServiceRequest(purchaseFlowRequest);
}

private void executeServiceRequest(Runnable runnable) {
    if (mIsServiceConnected) {
        runnable.run();
    } else {
        startServiceConnection(runnable);
    }
}

@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
    if (responseCode == BillingClient.BillingResponse.OK) {
        for (Purchase purchase : purchases) {
            handlePurchases(purchase);
        }
        mBillingUpdatesListener.onPurchasesUpdated(mPurchases);
    } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {

    } else {

    }
}

}

EDIT: The problem was, that i passed null as ArrayList oldSkus. Can anyone tell me what else to pass, if i dont have old Skus? It works if i remove .setOldSkus, but i still want to know what would be the right approach.

Upvotes: 1

Views: 738

Answers (1)

goRGon
goRGon

Reputation: 4451

If you are using an emulator, then please first check, that you have Google Play Services enabled, Play Store app installed and that it allows you to buy apps there. But it's much easier to integrate with Play billing on the real device.

And Play Billing library is rather verbose regarding all developer errors (including your problem with setOldSkus method). Just enable its logs as recommended inside BillingClient.

E.g.:

adb shell setprop log.tag.BillingClient VERBOSE

Upvotes: 1

Related Questions