KinApp


Source link: https://github.com/StephenVinouze/KinApp

KinApp

Gradle Dependency

Add this in your root build.gradle file:

allprojects {
  repositories {

// ... other repositories
maven {
 url "https://jitpack.io" 
}
  
}
 
}

Then add the following dependency in your project.

dependencies {

compile "com.github.StephenVinouze:KinApp:{
latest_version
}
" 
}
  kotlin {

  experimental {

coroutines "enable"
  
}
 
}

⚠? Do not forget to enable coroutines in your project as this library needs them

Configuring your InApp Purchase

Before integrating this library to manage your IAP, you probably must have already configured your application to integrate some products that you want your end users to buy. Understanding how this is done is rather well documented, but can be quite confusing as to how it really works. In addition to this library, I have documented the complete workflow to properly configure your IAP step by step. This document is attached to this project here

Getting started

In your AndroidManifest.xml file, add the following line if your manifest merger is disabled :

 <uses-permission android:name="com.android.vending.BILLING" />

You will need a developer payload that can be found in your Google Developer Console from your already set up application. This id is uniquely attached to this application and will refer to your InApp products that you will declare.

In your calling Activity, instanciate the KinAppManager by adding the following lines :

private val billingManager = KinAppManager(this, "<YOUR_DEVELOPER_PAYLOAD_HERE>")  override fun onCreate(savedInstanceState: Bundle?) {

  super.onCreate(savedInstanceState)
  setContentView(R.layout.<your_activity_layout>)
  billingManager.bind(this) 
}
  override fun onDestroy() {

  billingManager.unbind()
  super.onDestroy() 
}
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

  if (!billingManager.verifyPurchase(requestCode, resultCode, data)) {

super.onActivityResult(requestCode, resultCode, data)
  
}
 
}

Then implement the KinAppListener interface :

override fun onBillingReady() {

  // From this point you can use the Manager to fetch/purchase/consume/restore items 
}
  override fun onPurchaseFinished(purchaseResult: KinAppPurchaseResult, purchase: KinAppPurchase?) {

  // Handle your purchase result here
  when (purchaseResult) {

KinAppPurchaseResult.SUCCESS -> {

 // Purchase successful with a non-null KinAppPurchase object.

 // You may choose to consume this item right now if you want to be able to re-buy it

}

KinAppPurchaseResult.ALREADY_OWNED -> {

 // You already own this item. If you need to buy it again, consider consuming it first (you may need to restore your purchases before that)

}

KinAppPurchaseResult.INVALID_PURCHASE -> {

 // Purchase invalid and cannot be processed

}

KinAppPurchaseResult.INVALID_SIGNATURE -> {

 // Marked as success from the Google Store but signature detected as invalid and should not be processed

}

KinAppPurchaseResult.CANCEL -> {

 // Manual cancel from the user

}

  
}
 
}

Basic usage

Once your KinAppManager is configured, you can use the various features offered by this library. Be advised that the library uses the experimental Kotlin coroutines to elegantly handle asynchronous requests in order to avoid blocking the main thread. You can refer to the sample application if you need some help.

Fetching products

As soon as you have configured your application on the Developer console and added some InApp products, you can retrieve them in your application ( ⚠? Suspend call) :

launch(UI) {
  val products = billingManager.fetchProducts(<your_products_id_here>, KinAppProductType.INAPP).await() 
}

You can specify the nature of the product you want to fetch. It can be either of type INAPP or SUBSCRIPTION. This call is marked as suspend as it needs to be run asynchronously without blocking the main thread.

Purchase product

Purchasing a product can be done using a product_id and by specifying the calling activity. This is mandatory in order to be called back in the onActivityResult method. Finally, same as fetching product, just specify the nature oy your product :

billingManager.purchase(this, <your_products_id_here>, KinAppProductType.INAPP)

Consuming product

This part is only relevant to INAPP product types. A InApp product can be either consumable or one-time-purchase type. When you purchase an item, you own it. Unless you decide to consume it, you won't be able to re-purchase this item. This is important to understand this and distinguish between the two types of InApp products. You can consume an item by using these lines ( ⚠? Suspend call) :

launch(UI) {
  val success = billingManager.consumePurchase(<your_purchase_object>).await() 
}

Restoring purchases

Finally, you must be able to handle purchase restoration in your application. This can be easily done using these lines :

val purchases = billingManager.restorePurchases(KinAppProductType.INAPP)

Note that this will only bring you all the purchases you own.

Subscriptions

This library should be able to manage subscription and was designed that way. However, no subscription were made while developing this library hence cannot be marked as fully supported. Any test/review/PR is welcomed to fully support this ;)

Testing InApp purchase

You can use the static responses provided by Google to test the basic cases and ensure your application is correctly responding even before adding any products to your Developer Console. Just use this :

billingManager.purchase(this, KinAppManager.TEST_PURCHASE_SUCCESS, KinAppProductType.INAPP)

This will present to you a test product dialog that always succeed. You can try the other static responses :

  • TEST_PURCHASE_CANCELED
  • TEST_PURCHASE_REFUNDED
  • TEST_PURCHASE_UNAVAILABLE

Pull requests

I welcome and encourage all pull requests. I might not be able to respond as fast as I would want to but I endeavor to be as responsive as possible.

All PR must:

  1. Be written in Kotlin
  2. Maintain code style
  3. Indicate whether it is a enhancement, bug fix or anything else
  4. Provide a clear description of what your PR brings
  5. Enjoy coding in Kotlin :)

Resources

A View whose pages and items both can be dragged, looking like a ViewPager.

An adapter to create Android RecyclerViews with sections, providing headers and footers.

A library to make pretty badges.

Android Orientation Sensor Library helps you to get more accurate vector values of orientation, using all available device sensors.

Mortar Architect provides a flexible stack for navigating and displaying views and their presenters.

Architect is Mortar scope-centric. A Mortar scope is the glue between a View and its ViewPresenter. Architect will create a Mortar scope for each View & ViewPresenter association. A StackScope is a class that Architect will look for when building a Mortar scope, and its role is to configure the Mortar scope.

In your applications you often need to change certain configuration settings, monitor internal state or simply try to understand what, DebugDrawer allows you to easily add a slide out drawer with the ability to do this. Classes are provided to allow you to easily create your own additions to customize to your needs.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes