android billing how to enable enablePendingPurchases()
Asked Answered
C

4

26

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

Chivers answered 7/6, 2019 at 23:49 Comment(0)
O
54

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);
Odeen answered 8/6, 2019 at 14:7 Comment(3)
what worked for me was to place: billingClient = BillingClient.newBuilder (this) .setListener (this) .enablePendingPurchases (). build (); but thanks to your answer I can achieve itChivers
enablePendingPurchases and setListener are required!Ky
When you enable pending purchases, you also need to check the Purchase object for the PURCHASED or PENDING purchase state, using getPurchaseState(). "Note that you should grant entitlement only when the state is PURCHASED." developer.android.com/google/play/billing/integrate#pendingLaveen
E
10

This worked for me.

Just add enablePendingPurchases() like below:

billingClient = BillingClient.newBuilder(this)
                             .setListener(this)
                             .enablePendingPurchases()
                             .build();
Erepsin answered 24/7, 2019 at 19:37 Comment(2)
When you enable pending purchases, you also need to check the Purchase object for the PURCHASED or PENDING purchase state, using getPurchaseState(). "Note that you should grant entitlement only when the state is PURCHASED." developer.android.com/google/play/billing/integrate#pendingLaveen
@ChrisCartland I am migrating AIDL to Billing v4. I have PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT"); Shall I use the above for this?Waist
S
6

Since BillingClient 7.0.0, one has to pass PendingPurchasesParams due to deprecation:

PendingPurchasesParams params = PendingPurchasesParams.newBuilder()
        .enableOneTimeProducts()
        .build();

BillingClient.Builder builder = BillingClient.newBuilder(requireActivity())
        .enablePendingPurchases(params) // this line is different.
        .setListener(this);

BillingClient client = builder.build();
Showbread answered 17/5 at 11:50 Comment(2)
it also has enablePrepaidPlans(), not sure what it meansVoltage
See developer.android.com/google/play/billing/subscriptions#pendingShowbread
K
3
BillingClient billingClient = 
BillingClient.newBuilder(context!!)
.enablePendingPurchases()
.setListener(this)
 build()
    billingClient.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode==BillingClient.BillingResponseCode.OK) {
                skuList =  HashMap()
                skuList.put(BillingClient.SkuType.SUBS, listOf(getString(R.string.subscription_monthly),getString(R.string.subscription_yearly)))

                querySkuDetailsAsync(BillingClient.SkuType.SUBS,skuList.get(BillingClient.SkuType.SUBS),object :SkuDetailsResponseListener{
                    override fun onSkuDetailsResponse(billingResult: BillingResult?, skuDetailsList: MutableList<SkuDetails>?) {

                        DebugLog.e("DATAAA "+skuDetailsList?.size+"")
                    }
                })
            }
        }
        override fun onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
    })
Kraal answered 5/7, 2019 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.