Riz1Ahmed

Android Billing Client in Kotlin

Oct 26th, 2020
1,595
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     private fun setupBillingClient() {
  2.         billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build()
  3.         billingClient.startConnection(object : BillingClientStateListener {
  4.             override fun onBillingSetupFinished(billingResult: BillingResult) {
  5.                 if (billingResult.responseCode == BillingClient.BillingResponseCode.OK)
  6.                     loadAllSKUs()
  7.             }
  8.  
  9.             override fun onBillingServiceDisconnected() {
  10.                 toast("Connection fail")
  11.             }
  12.         })
  13.     }
  14.  
  15.     private fun loadAllSKUs() = if (billingClient.isReady) {
  16.  
  17.         //val paramsSubs = SkuDetailsParams.newBuilder().setSkusList(skuListSubs).setType(BillingClient.SkuType.SUBS).build()
  18.         billingClient.querySkuDetailsAsync(
  19.             SkuDetailsParams.newBuilder().setSkusList(skuListSubs)
  20.                 .setType(BillingClient.SkuType.SUBS).build()
  21.         ) { _, skuDetailsList ->
  22.             for (skuDetails in skuDetailsList!!) {
  23.                 if (skuDetails.sku == AppConst.MONTHLY_PACK) {
  24.  
  25.                     val offerPrice = skuDetails.priceAmountMicros.toDouble() / 1000000
  26.                     val currencyCode = skuDetails.price.substring(0, 3)
  27.                     val currencySymbol = CurrencySymbols().symbol(currencyCode)
  28.                     val regularPrice =
  29.                         String.format("%.2f", (((offerPrice / 1.99) * 2.99))).toDouble()
  30.  
  31.                     findViewById<TextView>(R.id.regularPriceMonth).text =
  32.                         "$regularPrice $currencySymbol"
  33.                     findViewById<TextView>(R.id.offerPriceMonth).text =
  34.                         "$offerPrice $currencySymbol"
  35.                 } else if (skuDetails.sku == AppConst.YEARLY_PACK) {
  36.  
  37.                     val offerPrice = skuDetails.priceAmountMicros.toDouble() / 1000000
  38.                     val currencyCode = skuDetails.price.substring(0, 3)
  39.                     val currencySymbol = CurrencySymbols().symbol(currencyCode)
  40.                     val regularPrice =
  41.                         String.format("%.2f", (((offerPrice / 14.99) * 35.88))).toDouble()
  42.  
  43.                     findViewById<TextView>(R.id.regularPriceYear).text =
  44.                         "$regularPrice $currencySymbol"
  45.                     findViewById<TextView>(R.id.offerPriceYear).text = "$offerPrice $currencySymbol"
  46.                 }
  47.             }
  48.         }
  49.         //val paramInAp = SkuDetailsParams.newBuilder().setSkusList(skuListInApp).setType(BillingClient.SkuType.INAPP).build()
  50.         billingClient.querySkuDetailsAsync(
  51.             SkuDetailsParams.newBuilder().setSkusList(skuListInApp)
  52.                 .setType(BillingClient.SkuType.INAPP).build()
  53.         ) { _, skuDetailsList ->
  54.             for (skuDetails in skuDetailsList!!) {
  55.                 if (skuDetails.sku == AppConst.LIFETIME_PACK) {
  56.                     val offerPrice = skuDetails.priceAmountMicros.toDouble() / 1000000
  57.                     val currencyCode = skuDetails.price.substring(0, 3)
  58.                     val currencySymbol = CurrencySymbols().symbol(currencyCode)
  59.  
  60.                     findViewById<TextView>(R.id.offerPriceLifetime).text =
  61.                         "$offerPrice $currencySymbol"
  62.                 }
  63.             }
  64.         }
  65.  
  66.         confirmBtn.setOnClickListener {
  67.             if (AppSharedPref.getInstance(this).getPremiumUserState())
  68.             toast("Congratulations!!!\nYou are already Premium User")
  69.         else {
  70.             val paramsSubs = SkuDetailsParams.newBuilder().setSkusList(skuListSubs)
  71.                 .setType(BillingClient.SkuType.SUBS).build()
  72.             billingClient.querySkuDetailsAsync(paramsSubs) { billingResult, skuDetailsList ->
  73.  
  74.                 if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
  75.                     for (skuDetails in skuDetailsList) {
  76.  
  77.                         if (skuDetails.sku == AppConst.MONTHLY_PACK && productType == 1) {
  78.                             val billingFlowParams =
  79.                                 BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build()
  80.                             billingClient.launchBillingFlow(this, billingFlowParams)
  81.                             Log.d("Riz1", "Month Selected")
  82.                         } else if (skuDetails.sku == AppConst.YEARLY_PACK && productType == 2) {
  83.                             val billingFlowParams =
  84.                                 BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build()
  85.                             billingClient.launchBillingFlow(this, billingFlowParams)
  86.                             Log.d("Riz1", "Yearly Selected")
  87.                         }
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             val paramInAp = SkuDetailsParams.newBuilder().setSkusList(skuListInApp)
  93.                 .setType(BillingClient.SkuType.INAPP).build()
  94.             billingClient.querySkuDetailsAsync(paramInAp) { billingResult, skuDetailsList ->
  95.                 if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
  96.                     for (skuDetails in skuDetailsList) {
  97.                         if (skuDetails.sku == AppConst.LIFETIME_PACK && productType == 3) {
  98.                             val billingFlowParams =
  99.                                 BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build()
  100.                             billingClient.launchBillingFlow(this, billingFlowParams)
  101.                             Log.d("Riz1", "Lifetime Selected")
  102.                         }
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.  
  108.  
  109.         }
  110.     } else this.toast("Billing Client not ready")
  111.  
  112.     override fun onPurchasesUpdated(billingResult: BillingResult, purchases: MutableList<Purchase>?) {
  113.         Log.d("Riz1", "Inside Purchase Update")
  114.         if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
  115.             Log.d("Riz1", "Inside Purchase Update, Pass Condition")
  116.             for (purchase in purchases) {
  117.  
  118.                 val param = AcknowledgePurchaseParams.newBuilder()
  119.                     .setPurchaseToken(purchase.purchaseToken).build()
  120.                 billingClient.acknowledgePurchase(param) {
  121.  
  122.                     Log.d("Riz1", "Inside Purchase Update, Acknowledge test")
  123.                     if (it.responseCode == BillingClient.BillingResponseCode.OK) {
  124.                         AppSharedPref.getInstance(this).chnagePremiumUserState()
  125.  
  126.                         Log.d("Riz1", "Successful purchase")
  127.                         toast("Congratulations!!! for PRO user")
  128.                         //ProcessPhoenix.triggerRebirth(this);//Restart Library method
  129.                     }
  130.                 }
  131.  
  132.             }
  133.         } else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED)
  134.             toast("User has cancelled purchase!")
  135.         else toast("Billing unavailable. Please check your device")
  136.     }
RAW Paste Data