Skip to content

Instantly share code, notes, and snippets.

@mattiaferigutti
Last active January 17, 2021 18:04
Show Gist options
  • Save mattiaferigutti/a6807a2a8ea797c6b31cabb9b0bbb7ce to your computer and use it in GitHub Desktop.
Save mattiaferigutti/a6807a2a8ea797c6b31cabb9b0bbb7ce to your computer and use it in GitHub Desktop.
package com.studio.mattiaferigutti.premiumsample
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.anjlab.android.iab.v3.BillingProcessor
import com.anjlab.android.iab.v3.TransactionDetails
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), BillingProcessor.IBillingHandler {
var billingProcessor: BillingProcessor? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpBillingProcessor()
}
private fun setUpBillingProcessor() {
billingProcessor = BillingProcessor(this, LICENSE_KEY, this)
billingProcessor?.initialize()
}
override fun onProductPurchased(productId: String, details: TransactionDetails?) {
Log.d(TAG, "$productId was successfully purchased")
/*
* Called when requested PRODUCT ID was successfully purchased
*/
}
override fun onPurchaseHistoryRestored() {
Log.d(TAG, "onPurchaseHistoryRestored has been called")
/*
* Called when purchase history was restored and the list of all owned PRODUCT ID's
* was loaded from Google Play
*/
}
override fun onBillingError(errorCode: Int, error: Throwable?) {
Log.d(TAG, "onBillingError has been called")
Log.e(TAG, "error: ${error.toString()}")
/*
* Called when some error occurred. See Constants class for more details
*
* Note - this includes handling the case where the user canceled the buy dialog:
* errorCode = Constants.BILLING_RESPONSE_RESULT_USER_CANCELED
*/
}
override fun onBillingInitialized() {
Log.d(TAG, "onBillingInitialized has been called")
/*
* Called when BillingProcessor was initialized and it's ready to purchase
*/
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (!billingProcessor?.handleActivityResult(requestCode, resultCode, data)!!) {
super.onActivityResult(requestCode, resultCode, data)
}
}
override fun onDestroy() {
super.onDestroy()
billingProcessor?.release()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment