InAppPurchase is a Android Library to implement Google Billing Library integration easily in Android App.It is simple, lightweight and used for quick setup.
- In order to implement an
In-App purchase
or aSubscription
, you need an account in theGoogle play console
andPayment profile
setup with your application published in either beta/alpha/ or in Release mode.
-
Add the following to the
settings.gradle
file.dependencyResolutionManagement { repositories { maven { url 'https://jitpack.io' } }}
-
Add library dependency to the app level
build.gradle
file.dependencies { implementation 'com.github.mdrafi786:in-app-purchase-module:$latest_stable_version' }
-
Once you’ve added the library dependency, build a
Release APK
of your app and upload it to theGoogle Play Console
inInternal Testing
(Beta Testing). -
Once you’ve uploaded the APK, you can use the Google Play Console to start adding
In-App Products
orSubcriptions
to sell in your app. UnderMontetize >> Products
in play console, you’ll see a section forIn-App products
andSubscriptions
. This is where you can set up two types of items:
-
In App Products (or one-time purchases)
-
Subscriptions
-
When you create new
In-App Products
andSubcriptions
, you are required to enter aProduct ID
, orSKU
, for the item. This sameProduct ID
is going to be used again later in your application code as we’ll see in the next step. Before creating a managed product, make sure to plan your Product IDs carefully.Product IDs
need to be unique for your app, and they can’t be changed or reused after they’ve been created.
-
Firstly, you need to declare variable of
InAppPurchaseClient
withlazy
initailization in your activity or fragment.private val billingClient: InAppPurchaseClient by lazy { InAppPurchaseClientImpl(this) }
-
Set up billing connection by calling
startBillingConnection()
with required parameter inonCreate()
of your activity or fragment :Note: Here
this
isInAppPurchaseUpdateListener
that you have to implement in your activty or fragment.override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) billingClient.startBillingConnection(this) }
-
Implement
InAppPurchaseUpdateListener
and override methods to get callback of purchase state .class MainActivity : AppCompatActivity(),InAppPurchaseUpdateListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_billing) } override fun onBillingClientReady(billingResult: BillingResult) { // The BillingClient is ready. You can query purchases and product details here } override fun onPurchasesSuccess(purchaseList: List<Purchase?>) { // handle new purchase success here and also check state of purchase } override fun onPurchaseCancelled() { // handle purchase cancelled by user here } override fun onQueryProductDetailSuccess(productDetailsMap: Map<String, ProductDetails>) { // handle product details here // Here map key is productId and value is Product details } override fun onQueryPurchasesSuccess(purchaseList: List<Purchase?>) { // handle existing purchase list here } override fun onError(error: String) { // Get any occur during setup or purchase } override fun onAcknowledgementResponse(isAcknowledged: Boolean) { // Get acknowledgement status for purchase } }
BillingClient.ProductType.INAPP
( One time purchase)BillingClient.ProductType.SUBS
(Subscription)-
You can Query Google Play Biliing for existing purchases with specific product Type
billingClient.queryPurchases(BillingClient.ProductType.INAPP)
-
You can Query Google Play Biliing for existing purchases with specific product Type
val listOfProductQuery = listOf( ProductQuery( productId = "your_proudct_id", purchaseType = "your_prouduct_type" ) ) billingClient.queryProductDetails(listOfProductQuery)
-
Firstly get the ProductDetails Map from
onQueryProductDetailSuccess()
overriden method.private var productDetailsMap: Map<String, ProductDetails>? = emptyMap() override fun onQueryProductDetailSuccess(productDetailsMap: Map< String, ProductDetails>) { // handle product details here // Here map key is productId and value is Product details this.productDetailsMap = productDetailsMap }
-
Launch the flow
on button clickval productDetails = productDetailsMap?.getOrDefault("your_prouduct_id", null) productDetails?.let { val billingParams = billingClient.billingFlowParamsBuilder( productDetails = it ) billingClient.launchBillingFlow(this, billingParams) }
- Close the connection of blilling library when activity has been destroyed to avoid memory leaks
override fun onDestroy() { super.onDestroy() billingClient.terminateBillingConnection() }
-