1

Migrating from Billing library 4.0 to 5.0, i am unable to check the purchase state.

billingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build(), new PurchasesResponseListener() {

        @Override
        public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
            if(billingResult.getResponseCode() == RESULT_OK && list != null){




               Toast.makeText(getApplicationContext(),"purchase is done", Toast.LENGTH_LONG).show();

            }
            else if(billingResult.getResponseCode() == RESULT_CANCELED && list == null){

                Toast.makeText(getApplicationContext(),"purchase not done", Toast.LENGTH_LONG).show();
            }
        }
    });
Vivek
  • 115
  • 2
  • 13

2 Answers2

0

Calling queryPurchasesAsync return the below response (convert in string respresentation)

   {"orderId":"GPA.3376-0182","packageName":"yourPackage","productId":"yourProductId","purchaseTime":1666868818195,"purchaseState":0,"purchaseToken":"kpomfmjcbkbdpnocfhoplnbm.AO-J1OwRcn0dSHxAYs0611","quantity":1,"autoRenewing":true,"acknowledged":true}

Here the key purchaseState tell's your purchase state. You can refer more detail about purchase state from the following link here

Arpit
  • 1,259
  • 11
  • 21
0

Your billingClient is not initialization.

billingClient!!.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {

            if (billingResult.responseCode ==  BillingClient.BillingResponseCode.OK) {
                val params = QueryPurchasesParams.newBuilder()
                    .setProductType(ProductType.INAPP)

                val purchasesResult = billingClient!!.queryPurchasesAsync(params.build(), object : PurchasesResponseListener {
                    override fun onQueryPurchasesResponse(p0: BillingResult, p1: List<Purchase>) {
                    }
                } )



            }
        }

        override fun onBillingServiceDisconnected() {

        }
    })