article

Amelia@Amazon avatar image
Amelia@Amazon posted

In-App Purchasing v2 for Adobe Air   

Summary

Amazon IAP AIR Plugin v2.0 enables your apps to retrieve unfulfilled consumable items, to notify Amazon upon fulfillment of consumable purchases, and to provide unique transaction IDs. This post describes how to upgrade your app from using the IAP AIR Plugin v1.0 code base to the IAP AIR Plugin v2.0 code base.

Modifying your app.xml File

You can determine whether you are using the previous version or the current version by inspecting the AndroidManifest.xml file:

previous version: <receiver android:name="com.amazon.inapp.purchasing.ResponseReceiver"> current version: <receiver android:name="com.amazon.device.iap.ResponseReceiver">

The following example shows the correct value for the android:name in the receiver element for an app using IAP AIR Plugin v2.0:

<android>
        <manifestAdditions><![CDATA[ ><manifest android:installLocation="auto">
                ...
                <application>
                    <receiver android:name="com.amazon.device.iap.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>

Refactoring Your App's Source Code The pacakgename has been changed from com.amazon.nativeextensions.android in IAP AIR Plugin v1.0 to com.amazon.device.iap.cpt in IAP AIR Plugin v2.0. You will need to use classes AmazonIapV2 and AmazonIapV2Event. Please check https://developer.amazon.com/public/apis/earn/in-app-purchasing/docs-v2/using-the-iap-plugin-for-adobe-air for more information.

So to register your listeners:

    AmazonIapV2.addEventListener(AmazonIapV2Event.GET_USER_DATA_RESPONSE, getUserDataResponseEventHandler);
    AmazonIapV2.addEventListener(AmazonIapV2Event.GET_PRODUCT_DATA_RESPONSE, getProductDataResponseEventHandler);
    AmazonIapV2.addEventListener(AmazonIapV2Event.GET_PURCHASE_UPDATES_RESPONSE, getPurchaseUpdatesEventHandler);
    AmazonIapV2.addEventListener(AmazonIapV2Event.PURCHASE_RESPONSE, purchaseResponseEventHandler);

To get and handle the user data:

public function GetUserData():void{
	...
	// Call the synchronous GetUserData AmazonIapV2, which does not take input
	var requestOutput:RequestOutput = AmazonIapV2.getUserData();
	// Get the requestId associated with this request from the response object
	var requestId:String = requestOutput.requestId;
	...
}

private function getUserDataResponseEventHandler(event:AmazonIapV2Event):void{
	var response:GetUserDataResponse = event.getUserDataResponse;
	var requestId:String = response.requestId;
	var userId:String = response.amazonUserData.userId;
	var marketplace:String = response.amazonUserData.marketplace;
	var status:String = response.status;
	...
} 

To get and handle the product data:

public function GetProductData():void
{
	...
	// Construct the input object to be passed to the GetProductData operation
	var request:SkusInput = new SkusInput();
	// Create list of SKU strings
	var list:Vector.<String> = new Vector.<String>();
	list.push("com.amazon.sample.iap.consumable");
	list.push("com.amazon.sample.iap.entitlement");
	list.push("com.amazon.sample.iap.subscription");
	// Set input value
	request.skus = list;
	// Call the synchronous GetProductData operation, passing in the input object
	var requestOutput:RequestOutput = AmazonIapV2.getProductData(request);
	// Get the requestId associated with this request from the response object
	var requestId:String = requestOutput.requestId;
	...
}

private function getProductDataResponseEventHandler(event:AmazonIapV2Event):void
{
	var response:GetProductDataResponse = event.getProductDataResponse;
	var requestId:String = response.requestId;
	var productDataMap:Object = response.productDataMap;
	var unavailableSkus:Vector.<String> = response.unavailableSkus;
	var status:String = response.status;
	// for each item in the productDataMap you can get the following values for a given SKU
	// (replace "sku" with the actual SKU)
	for (var key:String in productDataMap) {
		var productData:ProductData = productDataMap[key];
		var sku:String = productData.sku;
		var productType:String = productData.productType;
		var price:String = productData.price;
		var title:String = productData.title;
		var description:String = productData.description;
		var smallIconUrl:String = productData.smallIconUrl;
		...
	}
} 

To purchase IAP items:

private function purchaseConsumable():void{
	var request:SkuInput = new SkuInput();
	// Set the value of the input object's member variable
	request.sku = "com.amazon.sample.iap.consumable";
	// Call the synchronous Pu	rchase operation, passing in the input object
	var requestOutput:RequestOutput = AmazonIapV2.purchase(request);
	// Get the requestId associated with this request from the response object
	var requestId:String = requestOutput.requestId;
	...
}	
private function purchaseResponseEventHandler(event:AmazonIapV2Event):void{
	var response:PurchaseResponse = event.purchaseResponse;
	var requestId:String = response.requestId;
	var userId:String = response.amazonUserData.userId;
	var marketplace:String = response.amazonUserData.marketplace;
	var receiptId:String = response.purchaseReceipt.receiptId;
	var cancelDate:Number = response.purchaseReceipt.cancelDate;
	var purchaseDate:Number = response.purchaseReceipt.purchaseDate;
	var sku:String = response.purchaseReceipt.sku;
	var productType:String = response.purchaseReceipt.productType;
	var status:String = response.status;
	...
}

To get the purchase record of the user:

private function getPurchaseUpdates():void{
	log("getPurchaseUpdates");
	// Construct the input object to be passed to the GetPurchaseUpdates operation
	var request:ResetInput = new ResetInput();
	// Set input value
	request.reset = true;
	// Call the synchronous GetPurchaseUpdates operation, passing in the input object
	var requestOutput:RequestOutput = AmazonIapV2.getPurchaseUpdates(request);
	// Get the requestId associated with this request from the response object
	var requestId:String = requestOutput.requestId;
	log("requestId:" + requestId);
}	
private function getPurchaseUpdatesEventHandler(event:AmazonIapV2Event):void
{
	var response:GetPurchaseUpdatesResponse = event.getPurchaseUpdatesResponse;
	var requestId:String = response.requestId;
	var userId:String = response.amazonUserData.userId;
	var marketplace:String = response.amazonUserData.marketplace;
	var receipts:Vector.<PurchaseReceipt> = response.receipts;
	var status:String = response.status;
	var hasMore:Boolean = response.hasMore;
	...
	// for each receipt you can get the following values
	var i:int;
	for(i=0;i<receipts.length;i++){
		var receipt:PurchaseReceipt = receipts[i];
		var receiptId:String = receipt.receiptId;
		var cancelDate:Number = receipt.cancelDate;
		var purchaseDate:Number = receipt.purchaseDate;
		var sku:String = receipt.sku;
		var productType:String = receipt.productType;
		...
	}
} 

One new feature of IAP AIR Plugin v2.0 is the addition of new methods to track the fulfillment of a purchase. These methods check the status of purchase receipts and notify Amazon about the status of an item's fulfillment. To implement notifiyFulfillment:

private function NotifyFulfillment():void{
	// Construct the input object to be passed to the NotifyFulfillment operation as input
	var request:NotifyFulfillmentInput = new NotifyFulfillmentInput();
	// Set input values
	request.receiptId = consumbleReceiptID;
	request.fulfillmentResult = "FULFILLED";
	// Call the synchronous NotifyFulfillment operation, passing in the input object.
	AmazonIapV2.notifyFulfillment(request);
	...
} 

The Receipt Verification Service (RVS) underwent several changes from IAP v1.0 to IAP v2.0. If you are using RVS to verify receipts from your app, you will need to update the RVS URLs and response mappings. Please check https://developer.amazon.com/public/apis/earn/in-app-purchasing/docs-v2/migrate-iapv1-apps-to-iapv2#Updating the Receipt Verification Service (RVS) URLs and Responses.

Keywords: IAP, AIR, RVS, migration, plugin

KB_0160

iap
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Article

Contributors

wangchao contributed to this article brizzlebrazzle contributed to this article rossbria contributed to this article