Handy for small apps with in-app purchase (IAP) items that need both Google Play store and Amazon App Store support - i.e. regular Android devices and Amazon Kindle Fire. We developed this as a convenient way to keep multiple apps updated with the latest IAP code for Play and Amazon.
A simple wrapper library that provides sample Google and Amazon in-app purchase APIs in a single API.
- non-consumable (Play) / Entitlements (Amazon)
- Google In-App Bill API version 3 (includes fixes for > 20 IAP keys)
- Amazon IAP v2
- fetch localised prices
- actively maintained by Egghead Games for their cross-platform mobile/tablet apps (quality brain puzzles with no ads!)
- subscriptions for Google Play
- subscriptions for Amazon
appstore
flavor support in version 3.0 ensures that Amazon library is not included for Google
- receipt validation (either local or server)
- consumable items
This library is in the same category as OpenIAB (supports many more stores), OPFLab (replacement for OpenIAB). If you do not need Amazon support, there are several libraries that support just the Play store (see the Android Arsenal list).
Add the JitPack.io repository to your root build.gradle
:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
Add an appstore
flavor to your application related build.gradle
and the corresponding google and amazon dependencies.
android {
flavorDimensions "appstore"
productFlavors {
google {
dimension "appstore"
}
amazon {
dimension "appstore"
}
…
}
dependencies {
googleImplementation "com.github.eggheadgames:android-in-app-payments:x.y.z:google@aar"
googleImplementation 'com.android.billingclient:billing:3.0.0' // fixme: not sure why this is not automatic
amazonImplementation "com.github.eggheadgames:android-in-app-payments:x.y.z:amazon@aar"
}
The following code snippet initializes the billing module:
IAPManager.build(context, skuList, ArrayList<>());
IAPManager.addPurchaseListener(new PurchaseServiceListener() {
@Override
public void onPricesUpdated(Map<String, String> map) {
// list of available products will be received here, so you can update UI with prices if needed
}
@Override
public void onProductPurchased(String sku) {
// will be triggered whenever purchase succeeded
}
@Override
public void onProductRestored(String sku) {
// will be triggered fetching owned products using IAPManager.init();
}
});
IAPManager.init(googleIapKey /*can be ignored for Amazon target*/);
- Setup billing module.
IAPManager.build(Context context, List<String> skuList, List<String> subscriptionSkuList)
List<String> skuList
- a list of products to fetch information aboutList<String> subscriptionSkuList
- a list of subscription products to fetch information about (or empty if none)
- Request info about available and owned products
IAPManager.init(String rot13LicenseKey)
String rot13LicenseKey
is relevant only for the google
flavor, and can be ignored for IAPManager.BUILD_TARGET_AMAZON
. Note that this is the required Google License Key obtained from the app's Google Play console, after applying the ROT 13 algorithm.
You might choose to store the key as ROT-13 in your app to avoid casual decoding of the strings, however, this is not really secure, so you are advised to follow Google's advice and then ROT-13 the key before passing it to the API:
Security Recommendation: Google highly recommends that you do not hard-code the exact public license key string value as provided by Google Play. Instead, construct the whole public license key string at runtime from substrings or retrieve it from an encrypted store before passing it to the constructor. This approach makes it more difficult for malicious third parties to modify the public license key string in your APK file.
To buy a product use the following method:
IAPManager.buy(Activity activity, String sku, int requestCode);
PurchaseServiceListener
will notify application about the operation result
The following listener can be used to obtain owned subscriptions and to get notification about subscription operation result
IAPManager.addSubscriptionListener(new SubscriptionServiceListener() {
@Override
public void onSubscriptionRestored(String s) {
// will be triggered upon fetching owned subscription using IAPManager.init();
}
@Override
public void onSubscriptionPurchased(String s) {
// will be triggered whenever subscription succeeded
}
@Override
public void onPricesUpdated(Map<String, String> map) {
// list of available products will be received here, so you can update UI with prices if needed
}
});
To start a subscription use the following method:
IAPManager.subscribe(Activity activity, String sku, int requestCode);
String sku
- a subscription ID
int requestCode
- a unique request code to be used to deliver result through onActivityResult
SubscriptionServiceListener.onSubscriptionPurchased()
will notify application about successful operation result
Use the following method to remove subscription
IAPManager.unsubscribe(Activity activity, String sku, int requestCode);
Please keep in mind that for Google In App Billing it will just lead user to the Google Pay Account Settings page where user may cancel subscription manually.
When you are integrating Google In App Billing Subscriptions into your application please pay attention for the subscription cancellation handling.
As long as currently subscription can be cancelled only through Google Play Account settings - application won't be notified about this event.
Thus you have to query for the owned subscriptions each time you open the application (if needed), which is done via IAPManager.init()
method.