android billing how to enable enablePendingPurchases()

Vampi picture Vampi · Jun 8, 2019 · Viewed 9.6k times · Source

I've moved from an old gradle of billing api, to the most recent to date, and now I've tried adding

  BillingClient.Builder enablePendingPurchases = BillingClient.newBuilder(this).setListener(this);

but I can not get it to work, here's the error

   Caused by: java.lang.IllegalArgumentException: Support for pending purchases must be enabled. Enable this by calling 'enablePendingPurchases()' on BillingClientBuilder.
        at com.android.billingclient.api.BillingClient$Builder.build(BillingClient.java:309)
        at com.aplicacion.vivaluganoapp.ar.ponerDineroActivity.setupBillingClient(ponerDineroActivity.java:144)
        at com.aplicacion.vivaluganoapp.ar.ponerDineroActivity.onCreate(ponerDineroActivity.java:125)

complete code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poner_dinero);

        recyclerProduct.setHasFixedSize(true);
        recyclerProduct.setLayoutManager(new LinearLayoutManager(this));
        BillingClient.Builder enablePendingPurchases = BillingClient.newBuilder(this).setListener(this);

 enablePendingPurchases.build();
setupBillingClient();
    }




    private void setupBillingClient() {


        billingClient = BillingClient.newBuilder (this).setListener(this).build();

        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult responseCode) {
                int maca = BillingClient.BillingResponseCode.OK;
                String maca2 = String.valueOf(maca);

                String maca3 = String.valueOf(responseCode);
                if (maca3 == maca2)
                {
                    Toast.makeText(ponerDineroActivity.this, "WORKS", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(ponerDineroActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onBillingServiceDisconnected() {
                Toast.makeText(ponerDineroActivity.this, "Disconnected from Billing", Toast.LENGTH_SHORT).show();
            }
        });

    }

if I place only:

BillingClient.Builder enablePendingPurchases = BillingClient.newBuilder(this);

the error is:

Caused by: java.lang.IllegalArgumentException: Please provide a valid listener for purchases updates.

any help? i'm tired of trying

Answer

Hardy picture Hardy · Jun 8, 2019

From the first stacktrace in your question

Enable this by calling 'enablePendingPurchases()'

we can find documentation for method enablePendingPurchases()

This method is required to be called to acknowledge your application has been updated to support purchases that are pending. Pending purchases are not automatically enabled since your application will require updates to ensure entitlement is not granted before payment has been secured. For more information on how to handle pending transactions see https://developer.android.com/google/play/billing/billing_library_overview

If this method is not called, BillingClient instance creation fails.

Your line of code should be:-

enablePendingPurchases = BillingClient.newBuilder(this)
   .enablePendingPurchases()
   .setListener(this);

Instead of :-

enablePendingPurchases = BillingClient.newBuilder(this).setListener(this);