Android Checkout Library


Source link: https://github.com/serso/android-checkout

Checkout (Android In-App Billing Library)

Description

Checkout is an implementation of Android In-App Billing API (v3+). Its main goal is to make integration of in-app products as simple and straightforward as possible: developers should not spend much time on implementing boring In-App Billing API but should focus on more important things - their apps. With this in mind, the library was designed to be fast, flexible and secure.

Current version: 1.2.1

Why?

Checkout solves common problems that developers face while working with billing on Android, such as:

  • How to cancel all billing requests when Activity is destroyed?
  • How to query purchase information in the background? See also Querying for Items Available for Purchase
  • How to verify a purchase? See also Security And Design
  • How to load all the purchases using continuationToken or SKU details (one request is limited by 20 items)?
  • How to add billing with a minimum amount of boilerplate code?

Checkout can be used with any dependency injection framework or without it. It has a clear distinction of a functionality available in different contexts: purchase can be done only from Activity while SKU information can be loaded in Service or Application. Moreover, it has a good test coverage and is continuously build on Travis CI:

Getting started

Setup

  • Gradle/Android Studio in build.gradle:
compile 'org.solovyev.android:checkout:1.2.1'

Note: if you get the following warning

Conflict with dependency 'com.google.code.findbugs:jsr305'. Resolved versions for app (a.b.c) and test app (x.y.z) differ.

you should change the dependencies of com.android.support.test.espresso:espresso-core to

androidTestCompile('com.android.support.test.espresso:espresso-core:x.y.z', {

  // use version of jsr305 provided by Checkout
  exclude group: 'com.google.code.findbugs', module: 'jsr305' 
}
)

See Android Studio and Gradle documentation for details.

  • Maven in pom.xml:
<dependency>
  <groupId>org.solovyev.android</groupId>
  <artifactId>checkout</artifactId>
  <version>1.2.1</version>
  <type>aar</type> </dependency>
  • Download sources from github and either copy them to your project or import them as a project dependency
  • Download artifacts from the repository

Permissions

In-app billing requires com.android.vending.BILLING permission to be set in the app. This permission is automatically added to your app's AndroidManifest.xml by Gradle. You can declare this permission explicitly by adding the following line to the AndroidManifest.xml:

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

Tutorial

A tutorial for the sample app is available on Medium. Take a look if you prefer step-by-step guides over documentation.

Example

Say there is an app that contains one in-app product with "sku_01" id. Then Application class might look like this:

public class MyApplication extends Application {

private static MyApplication sInstance;

private final Billing mBilling = new Billing(this, new Billing.DefaultConfiguration() {

@Override

public String getPublicKey() {

 return "Your public key, don't forget abput encryption";

}

  
}
);

public MyApplication() {

sInstance = this;
  
}

public static MyApplication get() {

return sInstance;
  
}

public Billing getBilling() {

return mBilling;
  
}
 
}

And Activity class like this:

public class MyActivity extends Activity implements View.OnClickListener {

private class PurchaseListener extends EmptyRequestListener<Purchase> {

@Override

public void onSuccess(Purchase purchase) {

// here you can process the loaded purchase

}

@Override

public void onError(int response, Exception e) {

 // handle errors here

}

  
}

private class InventoryCallback implements Inventory.Callback {

@Override

public void onLoaded(Inventory.Products products) {

 // your code here

}

  
}

private final ActivityCheckout mCheckout = Checkout.forActivity(this, MyApplication.get().getBilling());

  private Inventory mInventory;

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mCheckout.start();

 mCheckout.createPurchaseFlow(new PurchaseListener());

 mInventory = mCheckout.makeInventory();

mInventory.load(Inventory.Request.create()

  .loadAllPurchases()

  .loadSkus(ProductTypes.IN_APP, "sku_01"), new InventoryCallback());

  
}

@Override
  protected void onDestroy() {

mCheckout.stop();

super.onDestroy();

  
}

@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

mCheckout.onActivityResult(requestCode, resultCode, data);

  
}

@Override
  public void onClick(View v) {

mCheckout.whenReady(new Checkout.EmptyListener() {

 @Override

 public void onReady(BillingRequests requests) {

  requests.purchase(ProductTypes.IN_APP, "sku_01", null, mCheckout.getPurchaseFlow());

 
}

}
);

  
}
 
}

Advanced usage

Samples

A sample app is available on Google Play ( source code). There is also a tutorial for it on Medium.

Building from the sources

Checkout is built by Gradle. The project structure and build procedure are standard for Android libraries. An environmental variable ANDROID_HOME must be set before building and should point to Android SDK installation folder (f.e. /opt/android/sdk). Please refer to Gradle User Guide for more information about the building.

Classes overview

Checkout contains three main classes: Billing, Checkout and Inventory.

Billing is a core class of Checkout's implementation of the billing API. It is responsible for:

Only one instance of Billing should be used in the app in order to avoid multiple connections to the billing service. Though this class might be used directly it's easier to work with Checkout instead.

Checkout is a middle tier of the library. It uses Billing in a certain context, e.g. in Application, Activity or Service, checks whether billing is supported and executes the requests. ActivityCheckout is a subclass capable of purchasing items.

Inventory loads information about products, SKUs and purchases. Its lifecycle is bound to the lifecycle of Checkout in which it was created.

Purchase verification

By default, Checkout uses simple purchase verification algorithm (see DefaultPurchaseVerifier). As explained in Android documentation it's better to verify purchases on a remote server. Checkout allows you to provide your own PurchaseVerifier via Billing.Configuration#getPurchaseVerifier. BasePurchaseVerifier can be used as a base class for purchase verifiers that should be executed on a background thread.

Proguard

Library's proguard rules are automatically added to the app project by Gradle. You can declare them explicitly by copying the contents of proguard-rules.txt to your proguard configuration.

License

Copyright 2016 serso aka se.solovyev

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.

Resources

A fully open source music player for Android.

Gradle plugin that provides convenience closure for configuring common dependencies.

A paginated gallery for Android with view indicators.

FAImageView is a Frame Animation ImageView for Android.

A material designed colorpicker.

A simple library to show licenses in your android application.

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