-
Notifications
You must be signed in to change notification settings - Fork 171
How To add OpenIAB to an app
-
Add library to your project.
You can clone the library
git clone https://github.com/onepf/OpenIAB.git
and add /library as a Library Project.Or:
- Download latest release JAR on https://github.com/onepf/OpenIAB/releases page.
- Download dependencies: fortumo-in-app-9.1.2.jar and in-app-purchasing-2.0.1.jar
Or use Gradle:
- Add repository to resolve third party dependencies:
allprojects { repositories { maven { url 'https://raw.githubusercontent.com/onepf/OPF-mvn-repo/master/' } jcenter() } }
- Add dependency:
compile 'org.onepf:openiab:0.9.8.4'
- Add Amazon and Fortumo dependencies if required (other stores are integrated into the library)
// Amazon AppStore compile 'com.amazon:in-app-purchasing:2.0.1' // Fortumo SDK compile 'com.braintree:fortumo-in-app:9.1.2'
}
-
Map Google Play SKUs to Yandex/Amazon/etc SKUs like this: https://github.com/onepf/OpenIAB/blob/dev/samples/trivialdrive/src/main/java/org/onepf/sample/trivialdrive/InAppConfig.java#L63
-
Create and set up an
Options
objectOpenIabHelper.Options options = new OpenIabHelper.Options.Builder() //set params .build();
Set store search strategy
//only the app installer can be a billing provider, default strategy .setStoreSearchStrategy(OpenIabHelper.Options.SEARCH_STRATEGY_INSTALLER) //or //if the installer was not found, use other options to choose a billing provider .setStoreSearchStrategy(OpenIabHelper.Options.SEARCH_STRATEGY_INSTALLER_THEN_BEST_FIT) //or //do not check installers, other options are used to choose a billing provider .setStoreSearchStrategy(OpenIabHelper.Options.SEARCH_STRATEGY_BEST_FIT) //... .build();
Note!
To emulate an installation from a certain store, use adbadb install -i store_package_to_test /path/to/apk
Set available stores to restrict the set of stores to check
//string library-specific names - not packages! - like OpenIABHelper.NAME_GOOGLE .addAvailableStoreNames() //or //pass store objects .addAvailableStores() //... .build();
Set verifying mode (applicable only for Google Play, Appland, Aptoide, AppMall, SlideMe, Yandex.Store)
//OpenIAB will not work without public RSA keys //purchases will be verified by the library .setVerifyMode(OpenIabHelper.Options.VERIFY_EVERYTHING) //OpenIAB doesn't require public keys to work with store .setVerifyMode(OpenIabHelper.Options.VERIFY_SKIP) //... .build();
Set preferred store names (works only for store search strategy OpenIabHelper.Options.SEARCH_STRATEGY_BEST_FIT and OpenIabHelper.Options.SEARCH_STRATEGY_INSTALLER_THEN_BEST_FIT)
//string library-specific name - not package! - like OpenIABHelper.NAME_GOOGLE .addPreferredStoreName() //... .build();
-
Using the
Options
object, instantiatenew OpenIabHelper
and callhelper.startSetup()
. When setup is done callhelper.queryInventory()
//to work with Samsung Apps, pass Activity context, in other cases - any context helper = new OpenIabHelper(context, options); helper.startSetup(new IabHelper.OnIabSetupFinishedListener() { public void onIabSetupFinished(IabResult result) { if (!result.isSuccess()) { complain("Problem setting up in-app billing: " + result); return; } Log.d(TAG, "Setup successful. Querying inventory."); helper.queryInventoryAsync(mGotInventoryListener); } });
-
Handle the results of
helper.queryInventory()
in an inventory listener and update UI to show what was purchased https://github.com/onepf/OpenIAB/blob/dev/samples/trivialdrive/src/main/java/org/onepf/sample/trivialdrive/MainActivity.java#L124 -
To process purchases you need to override
onActivityResult()
of your Activity@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Pass on the activity result to the helper for handling mHelper.handleActivityResult(requestCode, resultCode, data)); }
When the user requests purchase of an item, call helper.launchPurchaseFlow()
https://github.com/onepf/OpenIAB/blob/dev/samples/trivialdrive/src/main/java/org/onepf/sample/trivialdrive/MainActivity.java#L265
and handle the results with the listener
https://github.com/onepf/OpenIAB/blob/dev/samples/trivialdrive/src/main/java/org/onepf/sample/trivialdrive/MainActivity.java#L377
-
If the user has purchased a consumable item, call
helper.consume()
to remove it from the inventory. If it is not removed from the inventory, the store assumes it as a non-consumable item and will not allow it to be purchased more than once. Also it will be returned byhelper.queryInventory()
next time https://github.com/onepf/OpenIAB/blob/dev/samples/trivialdrive/src/main/java/org/onepf/sample/trivialdrive/MainActivity.java#L396 -
Specify keys for different stores like this: https://github.com/onepf/OpenIAB/blob/dev/samples/trivialdrive/src/main/java/org/onepf/sample/trivialdrive/InAppConfig.java#L54
-
Add the required permissions to the AndroidManifest.xml
<!--all--> <uses-permission android:name="android.permission.INTERNET"/> <!--Google Play--> <uses-permission android:name="com.android.vending.BILLING" /> <!--Open Store--> <uses-permission android:name="org.onepf.openiab.permission.BILLING" /> <!--Amazon requires no permissions --> <!--Samsung Apps--> <uses-permission android:name="com.sec.android.iap.permission.BILLING" /> <!--Nokia--> <uses-permission android:name="com.nokia.payment.BILLING"/> <!--Fortumo--> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.SEND_SMS" /> <!--SlideME--> <uses-permission android:name="com.slideme.sam.manager.inapp.permission.BILLING" />
Be careful using sms permissions. If you want to support devices without sms functionality, don't forget to add
<uses-feature android:name="android.hardware.telephony" android:required="false"/>
-
Edit your proguard config file
# GOOGLE -keep class com.android.vending.billing.** # AMAZON -dontwarn com.amazon.** -keep class com.amazon.** {*;} -keepattributes *Annotation* -dontoptimize # SAMSUNG -keep class com.sec.android.iap.** # NOKIA -keep class com.nokia.payment.iap.aidl.** #FORTUMO -keep class mp.** { *; }
-
Troubleshooting: additional logging is very helpful if you are trying to understand what's wrong with configuration or raise an issue:
Logger.setLoggable(true);