23 July 2012
Experience building apps for Android with Flash Builder or Flash Professional and Adobe AIR will help you make the most of this article.
Additional required other products
Milkman Games Amazon In-App Purchase Extension for Adobe AIR
Intermediate
The Amazon Appstore, and the immensely popular Kindle Fire, represent a huge market for developers and a great opportunity to monetize apps. With the Amazon In-App Purchase Extension for Adobe AIR from Milkman Games, you can integrate Amazon's In-App Purchase system in your AIR and Flash apps, using a familiar and straightforward ActionScript 3 interface.
The Amazon In-App Purchase Extension for Android and Kindle fire is a 100 percent native Java solution that enables you to:
Before you can use Amazon in-app purchases, you must define the purchasable items on the Amazon developer website.
On the In-App Items screen, select Add In-App Item on the top right of the screen. You'll be presented with three options: Add Consumable, Add Entitlement, or Add Subscription (see Figure 4).
A Consumable Item is a product that may be purchased multiple times and is then used up or consumed in your app. An example of a consumable item would be a pack of game coins. The users might buy the coins, spend them inside the game world, and then buy more again.
An Entitlement Item is a product that may be purchased only once, and then is owned forever. An example of Entitlement item would be a pack of new levels in a game, or a full-version feature unlock in a utility app.
A Subscription item is purchased to enable a feature or set of content during a given time frame. When the time expires, the subscription may be renewed. A magazine that delivers a new issue digitally every month is an example of a Subscription item.
Select the type of Item you want to add. The example below uses a Consumable item.
If you want to add additional in-app purchases to your app, repeat the process above as needed.
To test your in-app purchases before submitting your app to the Appstore, you'll need to create a special file in the JSON format that contains all the metadata for your purchases. While in sandbox mode, this file will determine how the app responds to purchase requests.
The following example shows the format this file should take. It includes a consumable item with the SKU my_spell for an entitlement item named my_levelpack as an example. Be sure that the SKUs, titles, descriptions, and other data match what you entered in the developer portal.
The file should be named amazon.sdktester.json. In the /example folder of the extension package, you can find a reference example of this file.
For more detailed information on the file format, see https://developer.amazon.com/sdk/in-app-purchasing/documentation/testing-iap.html#Create JSON .
{
“my_spell” : {
“itemType”: “CONSUMABLE”,
“price”: 0.99,
“title”: “Consumable Spell”,
“description”: “It's a spell”,
“smallIconUrl”: “http://some/image.jpg”
},
“my_levelpack” : {
“itemType”: “ENTITLED”,
“price”: 0.99,
“title”: “Level Pack 1”,
“description”: “More levels for this game!”,
“smallIconUrl”: “http://some/image.jpg”
}
}
On iOS, if the file was saved in /yourusername/dev, then type cd /Users/yourusername/dev and press Return.
adb program that's included with the Android SDK. (If you don't have the SDK, you can get it at http://developer.android.com/sdk/index.html). You'll be executing the push command, passing the location of the JSON file as the first parameter and /mnt/sdcard as the second parameter.
On Windows, if the Android SDK is located at C:\dev\android-sdk, type c:\dev\android-sdk\platform-tools\adb push amazon.sdktester.json /mnt/sdcard and press Enter.
Before you can test Amazon Purchases locally, you also must install a special test application called the Amazon SDK Tester. The App is included in the Amazon SDK, which can be downloaded from https://developer.amazon.com/sdk.html .
To install the app, you'll again use the adb command from the Android SDK:
install command and passing the Amazon SDK tester app APK as a parameter.cd c:\dev\In-App-Purchasing\tools
c:\dev\android-sdk\platform-tools\adb install -r AmazonSDKTester.apk
cd /Users/yourusername/dev/In-App-Purchasing/tools
./Users/yourusername/android-sdk/platform-tools/adb install -r AmazonSDKTester.apk
The next step is to add the com.milkmangames.nativeextensions.AmazonPurchase.ane library to your project. (If you are not using Flash Builder 4.6 and later or Flash Professional CS6 and later you'll need to add the AmazonPurchaseAPI.swc library instead.)
In Flash Professional CS6:
In Flash Builder 4.6:
In FlashDevelop:
The Amazon In-App Purchase extension can be up and running in a few simple calls. See example/AmazonExample.as for a full example.
Follow these steps to get started:
com.milkmangames.nativeextensions.android.*;
com.milkmangames.nativeextensions.android.events.*;
AmazonPurchase.create() . You can check the AmazonPurchase.isSupported() method first, to ensure the current platform is Android and not an unsupported platform (like iOS or Windows.):if (AmazonPurchase.isSupported())
{
AmazonPurchase.create();
}
create() has been called, an instance of the AmazonPurchase API is available statically by accessing AmazonPurchase.amazonPurchase. Add event listeners for all possible API responses:// item data listeners
AmazonPurchase.amazonPurchase.addEventListener(AmazonPurchaseEvent.ITEM_DATA_LOADED,onDataLoaded);
AmazonPurchase.amazonPurchase.addEventListener(AmazonPurchaseEvent.ITEM_DATA_FAILED,onDataFailed);
// item purchase listeners
AmazonPurchase.amazonPurchase.addEventListener(AmazonPurchaseEvent.PURCHASE_ALREADY_ENTITLED,onAlreadyEntitled);
AmazonPurchase.amazonPurchase.addEventListener(AmazonPurchaseEvent.PURCHASE_FAILED,onPurchaseFailed);
AmazonPurchase.amazonPurchase.addEventListener(AmazonPurchaseEvent.PURCHASE_SKU_INVALID,onInvalidSku);
AmazonPurchase.amazonPurchase.addEventListener(AmazonPurchaseEvent.PURCHASE_SUCCEEDED,onPurchaseSuccess);
// purchase update listeners
AmazonPurchase.amazonPurchase.addEventListener(AmazonPurchaseEvent.PURCHASES_UPDATE_FAILED,onUpdateFailed);
AmazonPurchase.amazonPurchase.addEventListener(AmazonPurchaseEvent.PURCHASES_UPDATED,onPurchasesUpdate);
At any time after you've initialized the API and set up the event handlers, you may receive an AmazonPurchaseEvent.PURCHASES_UPDATED event. This event includes a parameter, receipts , which contains an array of AmazonPurchaseReceipt objects for any non-consumables the user has purchased. The following code example shows how to iterate through those receipts:
private function onPurchasesUpdate(e:AmazonPurchaseEvent):void
{
for each(var receipt:AmazonPurchaseReceipt in e.receipts)
{
trace(“you previously bought a “+receipt.sku);
// update your internal state for the items here.
}
}
You may use the AmazonPurchase.amazonPurchase.loadItemData() method to retrieve details such as titles, descriptions, and icons as they relate to a given set of SKUs; for example:
AmazonPurchase.amazonPurchase.loadItemData([“my_spell”,”my_levelpack”]);
On completion, this function will dispatch an AmazonPurchaseEvent.ITEM_DATA_LOADED event that contains the details for the provided SKUs. If the request fails, AmazonPurchaseEvent.ITEM_DATA_FAILED will be dispatched.
The following code demonstrates iterating through itemDatas and displaying SKUs. It also checks the property unavailableSkus , which is an array of any SKUs you submitted that are not valid:
private function onDataLoaded(e:AmazonPurchaseEvent):void
{
for each(var item:AmazonItemData in e.itemDatas)
{
trace("sku="+item.sku+",price="+item.price+
",iconUrl="+item.smallIconUrl+",title="+item.title+
",description="+item.description);
}
for each(var badSku:String in e.unavilableSkus)
{
trace("this sku was not available:"+badSku);
}
}
Follow these steps to initiate and item purchase from your app's code:
purchaseItem(itemSku) method, where itemSku is one of the product SKUs you created earlier; for example:AmazonPurchase.amazonPurchase.purchaseItem("your_itemsku");
Table 1. Purchase events.
Event |
Description |
AmazonPurchaseEvent.PURCHASE_SUCCEEDED |
The purchase was successful. The event's |
AmazonPurchaseEvent.PURCHASE_FAILED |
The purchase was not successful, either due to an error or because the user declined the purchase. |
AmazonPurchaseEvent.SKU_INVALID |
The given |
AmazonPurchaseEvent.PURCHASE_ALREADY_ENTITLED |
The user already owns the specified non-consumable item. |
You need to configure your AIR application descriptor file to use the AIR 3.0 SDK (or later), include the Amazon In-App Purchase extension, and update the Android manifest additions with some Amazon In-App Purchase specific settings. For a working example, see example/app.xml.
<application xmlns=“http://ns.adobe.com/air/application/3.0”>
<extensions>
<extensionID>com.milkmangames.extensions.AmazonPurchase</extensionID>
</extensions>
android.permission.INTERNET permission must be present for Amazon In-App Purchase to work. You also must add the com.amazon.inapp.purchasing.NOTIFY actions within the <intent-filter> element, as shown below:<android>
<manifestAdditions><![CDATA[
<manifest android:installLocation=“auto”>
<uses-permission android:name=“android.permission.INTERNET”/>
<application>
<receiver android:name="com.amazon.inapp.purchasing.ResponseReceiver">
<intent-filter>
<action android:name="com.amazon.inapp.purchasing.NOTIFY" android:permission="com.amazon.inapp.purchasing.Permission.NOTIFY" /> </intent-filter>
</receiver>
</application>
</manifest>
]]></manifestAdditions>
</android>
If you're using Flash Builder 4.6 or later, or Flash Professional CS6 or later, and have added the Amazon In-App Purchase extension library as described above, then you can compile as you usually do directly from the IDE. If not and you are building your app with the extension from the command line, then you'll need to specify the directory containing the com.milkmangames.nativeextensions.AmazonPurchase.ane file.
Here is an example build command line:
[PATH_TO_AIR_SDK]\bin\adt -package -target apk-debug -storetype pkcs12 -keystore [YOUR_KEYSTORE_FILE]
-storepass [YOUR_PASSWORD] anesample.apk app.xml anesample.swf -extdir [DIRECTORY_CONTAINING_ANE_FILE]
If your app is not receiving events as expected, try these tips:
manifestAdditions to your application descriptor XML file as described above.Now that you have Amazon In-App Purchases up and running in your app, you may want to explore these other native extension tutorials: