This is a Library to make in App purchases ans subscriptions integration to Jetpack Compose projects more easier
purchase.mp4
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
if you're using kotlin dsl use
repositories {
...
maven { url = uri("https://www.jitpack.io" ) }
}
dependencies {
implementation 'com.github.Felix-Kariuki:InAppPurchasesComposeLibrary:<latest-version>'
}
-
Create a new compose project
-
Publish the apk to playstore under closed testing or whichever you prefer
-
On your playstore console on your newly created project under products, select in-app products and create a new product. on Product Id use
test_product
then on product details add a product name and description and choose your prefered subscription amount. Once done remember to activate the subscription by clicking activate -
On your newly created or existing application on android studio,
and pass the activity and product_id of your in app purchase as you named it on playstore
val billingPurchaseHelper = InAppPurchasesHelper(this,"test_product") billingPurchaseHelper.setUpBillingPurchases()
NOTE
val billingPurchaseHelper = SubscriptionsHelper(this,"test_product_id") billingPurchaseHelper.setUpBillingPurchases()
if you're implementing it on a compossable screen you can initiliaze it this way
val billingPurchaseHelper = InAppPurchasesHelper(LocalContext.current,"test_product_id") billingPurchaseHelper.setUpBillingPurchases()
for subscriptions
val billingPurchaseHelper = SubscriptionsHelper(LocalContext.current,"test_product_id") billingPurchaseHelper.setUpBillingPurchases()
you can also collect the purchse process, prduct name and purchase status by implementing this:
val purchaseDone by billingPurchaseHelper.purchaseDone.collectAsState(false) val productName by billingPurchaseHelper.productName.collectAsState("") val purchaseStatus by billingPurchaseHelper.purchaseStatus.collectAsState("")
billingPurchaseHelper.initializePurchase()
if it's a one time purchase you can disable the button by passing
purchaseDone
as the enabled parameter to disable the buttonButton( onClick = { billingPurchaseHelper.initializePurchase() }, shape = RoundedCornerShape(32.dp), colors = ButtonDefaults.buttonColors( backgroundColor = MaterialTheme.colors.surface, contentColor = MaterialTheme.colors.onSurface ), modifier = Modifier.wrapContentSize(), enabled = purchaseDone ) { Icon( Icons.Filled.More, contentDescription = "InApp Purchases", modifier = Modifier.size(ButtonDefaults.IconSize) ) Spacer(Modifier.size(ButtonDefaults.IconSpacing)) Text( "In App Purchases", fontSize = 16.sp, modifier = Modifier.padding(8.dp) ) }
Chekout This project to get an insight and more understanding of how to implement this or read this Blog Post
Copyright 2023 Felix Kariuki 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.