This is an example how to query the purchase list with the Google Play Billing Library 5.0

IF you want to get all the „active“ subscriptions you can query the list with this piece of code:

The subscription SKU´s are now stored i the purchaseList Array and you can query every sku in the list.

Setting up the billing client :

private var skuList : MutableList<QueryProductDetailsParams.Product> = ArrayList()
private val subsProductsList: MutableList<String> = ArrayList()
private var productsDetailsList: MutableList<ProductDetails> = ArrayList()
  private var mBillingClient: BillingClient? = null




  // Add all active SKU´s to your list
 subsProductsList.add(res.getString(R.string.product_id_subscribe_monthly)) 


setupBillingClient()





// Add all product-ids to the list (to get availabe products with prices later)

    for (i in 0 until  subsProductsList.size) {

        skuList.add(
            QueryProductDetailsParams.Product.newBuilder()
                .setProductId(subsProductsList[i])
                .setProductType(BillingClient.ProductType.SUBS)
                .build()
        )

    }



private fun setupBillingClient() {
    mBillingClient = BillingClient
            .newBuilder(context!!)
            .enablePendingPurchases() // Useful for physical stores
            .setListener(this)
            .build()


    mBillingClient?.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                



                queryPurchaseList()



            }
        }

        override fun onBillingServiceDisconnected() {
            


            Log.e("Billing", "onBillingServiceDisconnected")
        }
    })
}



fun queryPurchaseList() {



    // Returns only active subscriptions - no inactive subscription will be in this list !

    mBillingClient?.queryPurchasesAsync(
        QueryPurchasesParams.newBuilder()
            .setProductType(BillingClient.ProductType.SUBS)
            .build()
    ) { billingResult, purchaseList ->
        // Process the result


        // Returns all active SUBS SKU´s !!
        checkPurchase(billingResult, purchaseList)
    }


}






fun checkPurchase(bresult:BillingResult, list:List<Purchase>?) {

    // Init all the purchases to false 
    prefs.SubscriptionPurchase = false


    // Retrieve and loop all the purchases done by the user

    if (list != null && !list.isEmpty()) {

        for (purchase in list) {


            if (purchase.isAcknowledged) {

                // getting thesingle product-sku of every purchase in the list
                val compareSKU = purchase.products.get(0)

                Log.e("Billing", "Compare SKU is $compareSKU")




                for (i in 0 until subsProductsList.size) {

                    if (subsProductsList[i].contains(compareSKU)) {
                        Log.e("Billing", " Product is subscribed and setting purchase to true ")

                        prefs.SubscriptionPurchase = true
                    }


                }


            }

      

            if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {

                // getting the product-id = sku
                val compareSKU = purchase.products.get(0)


             

                for (i in 0 until subsProductsList.size) {

                 
                    if (!purchase.isAcknowledged) {

                        Log.e("Billing", "The product is purchased but not yet acknowledged")



                        val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
                            .setPurchaseToken(purchase.purchaseToken)
                            .build()
                        mBillingClient!!.acknowledgePurchase(acknowledgePurchaseParams, this)



                        // After purchase is acknowledeged you can set the purchase to true
                        prefs.SubscriptionPurchase = true



                    }




                }