Advertisement
Guest User

Test

a guest
Nov 6th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. package com.example.testbilling
  2.  
  3. import androidx.appcompat.app.AppCompatActivity
  4. import android.os.Bundle
  5. import android.view.View
  6. import android.widget.Button
  7. import android.widget.Toast
  8. import com.android.billingclient.api.*
  9. import kotlinx.coroutines.*
  10.  
  11.  
  12. class MainActivity : AppCompatActivity() {
  13.  
  14. lateinit private var billingClient: BillingClient
  15.  
  16. override fun onCreate(savedInstanceState: Bundle?) {
  17. super.onCreate(savedInstanceState)
  18. setContentView(R.layout.activity_main)
  19.  
  20. val purchasesUpdatedListener =
  21. PurchasesUpdatedListener { billingResult, purchases ->
  22. // To be implemented in a later section.
  23. }
  24.  
  25. var billingClient = BillingClient.newBuilder(this)
  26. .setListener(purchasesUpdatedListener)
  27. .enablePendingPurchases()
  28. .build()
  29.  
  30.  
  31.  
  32. val startBC = findViewById<Button>(R.id.buttonStartBillingClient)
  33. startBC.setOnClickListener(View.OnClickListener {
  34.  
  35. billingClient.startConnection(object : BillingClientStateListener {
  36. override fun onBillingSetupFinished(billingResult: BillingResult) {
  37. if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
  38. // The BillingClient is ready. You can query purchases here.
  39. Toast.makeText(applicationContext, "The BillingClient is ready.",Toast.LENGTH_LONG).show()
  40.  
  41.  
  42.  
  43. //////////////////////////////////////////
  44.  
  45. val response = billingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS)
  46.  
  47. if (response.responseCode == BillingClient.BillingResponseCode.OK) {
  48. Toast.makeText(applicationContext, "Subscription supported",Toast.LENGTH_LONG).show()
  49.  
  50.  
  51.  
  52. //////////////////////
  53. val mycoolSKUS = listOf("onemonthsub")
  54. val params = SkuDetailsParams.newBuilder().setSkusList(mycoolSKUS).setType(BillingClient.SkuType.SUBS).build()
  55.  
  56.  
  57. billingClient.querySkuDetailsAsync(params) { billingResult, skuDetailsList ->
  58.  
  59.  
  60. when (billingResult.responseCode) {
  61.  
  62. BillingClient.BillingResponseCode.OK -> {
  63.  
  64. if (skuDetailsList.orEmpty().isNotEmpty()) {
  65.  
  66. Toast.makeText(applicationContext,
  67. "Cool!", Toast.LENGTH_LONG).show()
  68.  
  69. }
  70. else {
  71. Toast.makeText(applicationContext,
  72. "Not cool!", Toast.LENGTH_LONG).show()
  73.  
  74. //////////// I AM HERE NOW///////////////
  75. // Toast shows "Not cool!"
  76. ////////////////////////////////////////
  77.  
  78. }
  79.  
  80.  
  81. }
  82.  
  83.  
  84. else -> {
  85. Toast.makeText(applicationContext, "Not ok...", Toast.LENGTH_LONG)
  86. .show()
  87. }
  88.  
  89. }
  90.  
  91.  
  92. }
  93. /////////////////////
  94.  
  95.  
  96. }
  97. else
  98. {
  99. Toast.makeText(applicationContext,
  100. "Subscription not supported, error response: \n$response",
  101. Toast.LENGTH_LONG).show()
  102. }
  103.  
  104. /////////////////////////////////////////
  105.  
  106.  
  107. }
  108. }
  109. override fun onBillingServiceDisconnected() {
  110. // Try to restart the connection on the next request to
  111. // Google Play by calling the startConnection() method.
  112. Toast.makeText(applicationContext, "Disconnected!",Toast.LENGTH_LONG).show()
  113. }
  114. })
  115.  
  116.  
  117. })
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement