inappbilling is a Google Play Billing library that demonstrates InApp purchase in your Android application
Add maven repository in project level build.gradle or in latest project setting.gradle file
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
Add inappbilling dependencies in App level build.gradle. Latest Version
dependencies {
implementation 'com.github.hypersoftdev:inappbilling:x.x.x'
}
Declare BillingManger Variable, "this" can be an application context.
also declare your original productId e.g. packageName
private val billingManager by lazy { BillingManager(this) }
private val productId:String = "Paste your original Product ID"
billingManager.setCheckForSubscription(true)
Get debugging ids for testing using "getDebugProductIDList()" method
val productId = when (BuildConfig.DEBUG) {
true -> diComponent.billingManager.getDebugProductIDList()
false -> listOf(globalContext.packageName)
}
diComponent.billingManager.startConnection(productId, object : OnConnectionListener {
override fun onConnectionResult(isSuccess: Boolean, message: String) {
Log.d("TAG", "onConnectionResult: $isSuccess - $message")
binding.mbMakePurchase.isEnabled = isSuccess
}
override fun onOldPurchaseResult(isPurchased: Boolean, purchaseDetail: PurchaseDetail?) {
// Update your shared-preferences here!
Log.d("TAG", "onOldPurchaseResult: $isPurchased")
}
})
Access the full details of the item you have currently purchased by utilizing the purchaseDetail method.
data class PurchaseDetail (
val productType: ProductType, // e.g. SUBS/INAPP
val purchaseType: String, // e.g. Weekly, Monthly, Yearly, etc
val purchaseTime: String // e.g. 2023-01-01 12:00
)
Observe the states by using BillingManager
TAG
billingManager.makeInAppPurchase(activity, object : OnPurchaseListener {
override fun onPurchaseResult(isPurchaseSuccess: Boolean, message: String) {
Toast.makeText(this@MainActivity, message, Toast.LENGTH_SHORT).show()
Log.d("TAG", "makeInAppPurchase: $isPurchaseSuccess - $message")
}
})
billingManager.makeSubPurchase(activity, SubscriptionPlans.basicPlanMonthly, object : OnPurchaseListener {
override fun onPurchaseResult(isPurchaseSuccess: Boolean, message: String) {
Toast.makeText(this@MainActivity, message, Toast.LENGTH_SHORT).show()
binding.mbMakePurchase.isEnabled = !isPurchaseSuccess
}
})
Add plans and tags on Play Console There are few subsciption tags as follow, to generate plans.
For Weekly Subscription
-> Product ID: basic_product_weekly
-> Plan ID: basic-plan-weekly
For Montly Subscription
-> Product ID: basic_product_monthly
-> Plan ID: basic-plan-monthly
For Yearly Subscription
-> Product ID: basic_product_yearly
-> Plan ID: basic-plan-yearly
For 06 Months Subscription
-> Product ID: basic_product_semi_yearly
-> Plan ID: basic-plan-semi-yearly
The model class for ProductDetail
data class ProductDetail(
var productId: String,
var price: String,
var currencyCode: String,
var freeTrialPeriod: Int,
var priceAmountMicros: Long = 0,
var freeTrial: Boolean = false,
var productType: ProductType = ProductType.SUBS
)
Following observer observes all the active subscription and in-App Product
billingManager.productDetailsLiveData.observe(this) { list ->
var month = 0L
var year = 0L
list.forEach { productDetail: ProductDetail ->
Log.d(TAG, "initObservers: $productDetail")
when (productDetail.productId) {
SubscriptionProductIds.basicProductMonthly -> {
//binding.mtvOfferPrice1.text = productDetail.price
month = productDetail.priceAmountMicros / 1000000
}
SubscriptionProductIds.basicProductYearly -> {
//binding.mtvOfferPrice2.text = productDetail.price
year = productDetail.priceAmountMicros / 1000000
}
SubscriptionProductIds.basicProductSemiYearly -> {
//binding.mtvOfferPrice3Premium.text = productDetail.price
}
productId -> {
//binding.mtvOfferPrice3Premium.text = productDetail.price
}
}
}
// Best Offer
if (month == 0L || year == 0L) return@observe
val result = 100 - (year * 100 / (12 * month))
val text = "Save $result%"
//binding.mtvBestOffer.text = text
val perMonth = (year / 12L).toString()
//binding.mtvOffer.text = perMonth
}
Copyright 2023 Hypersoft Inc
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.