Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.testbilling
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.view.View
- import android.widget.Button
- import android.widget.Toast
- import com.android.billingclient.api.*
- import kotlinx.coroutines.*
- class MainActivity : AppCompatActivity() {
- lateinit private var billingClient: BillingClient
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val purchasesUpdatedListener =
- PurchasesUpdatedListener { billingResult, purchases ->
- // To be implemented in a later section.
- }
- var billingClient = BillingClient.newBuilder(this)
- .setListener(purchasesUpdatedListener)
- .enablePendingPurchases()
- .build()
- val startBC = findViewById<Button>(R.id.buttonStartBillingClient)
- startBC.setOnClickListener(View.OnClickListener {
- billingClient.startConnection(object : BillingClientStateListener {
- override fun onBillingSetupFinished(billingResult: BillingResult) {
- if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
- // The BillingClient is ready. You can query purchases here.
- Toast.makeText(applicationContext, "The BillingClient is ready.",Toast.LENGTH_LONG).show()
- //////////////////////////////////////////
- val response = billingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS)
- if (response.responseCode == BillingClient.BillingResponseCode.OK) {
- Toast.makeText(applicationContext, "Subscription supported",Toast.LENGTH_LONG).show()
- //////////////////////
- val mycoolSKUS = listOf("onemonthsub")
- val params = SkuDetailsParams.newBuilder().setSkusList(mycoolSKUS).setType(BillingClient.SkuType.SUBS).build()
- billingClient.querySkuDetailsAsync(params) { billingResult, skuDetailsList ->
- when (billingResult.responseCode) {
- BillingClient.BillingResponseCode.OK -> {
- if (skuDetailsList.orEmpty().isNotEmpty()) {
- Toast.makeText(applicationContext,
- "Cool!", Toast.LENGTH_LONG).show()
- }
- else {
- Toast.makeText(applicationContext,
- "Not cool!", Toast.LENGTH_LONG).show()
- //////////// I AM HERE NOW///////////////
- // Toast shows "Not cool!"
- ////////////////////////////////////////
- }
- }
- else -> {
- Toast.makeText(applicationContext, "Not ok...", Toast.LENGTH_LONG)
- .show()
- }
- }
- }
- /////////////////////
- }
- else
- {
- Toast.makeText(applicationContext,
- "Subscription not supported, error response: \n$response",
- Toast.LENGTH_LONG).show()
- }
- /////////////////////////////////////////
- }
- }
- override fun onBillingServiceDisconnected() {
- // Try to restart the connection on the next request to
- // Google Play by calling the startConnection() method.
- Toast.makeText(applicationContext, "Disconnected!",Toast.LENGTH_LONG).show()
- }
- })
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement