开发者控制台
感谢您的访问。此页面目前仅提供英语版本。我们正在开发中文版本。谢谢您的理解。

IAP Plugin for Adobe Air

This document provides sample code showing how to use the In-App Purchasing (IAP) v2.0 plugin for Adobe Air. You can download the Adobe Air plugin here:

Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at https://aws.amazon.com/apache-2-0/ or in the "license" file accompanying this file. This file 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.

Methods and events

You can initiate method calls and listen for events. Method calls initiate requests, some of which return a response. Events are asynchronous system-generated messages that are sent in response to method calls to return the requested data to you.

All of the method calls, except for NotifyFulfillment, request data from the system. When you initiate a request for data, the system immediately returns an object containing a requestId string, which is a unique request identification string. The requested data is returned asynchronously via an event. The NotifyFulfillment method call does not return a response because its purpose is to notify Amazon that your app has delivered the purchased item to the user.

To get the data contained in an event, you must define a handler function and bind it to an event listener for the specific type of event. The returned data, accessible from within your event handler, includes a unique requestId that matches the requestId that was returned in the request method call. You can use the requestId to match a specific request to a specific response, if necessary.

Initiate method calls in Adobe AIR

To initiate a method call, you must perform the following general steps:

  1. Import the API package.
  2. If needed, construct the object used to pass input to the operation. There are several ways to construct the input object. See Constructing an Input Object for a complete description.
  3. For asynchronous operations, define a callback function that is invoked when a response is ready.
  4. Call the operation, passing in any required input and/or callback function as arguments.

Construct an input object

You may define an input object in one of several ways:

  • By passing arguments to the constructor.
  • By constructing an empty object and then using implicit setters to set the object's properties.
  • By constructing an empty object and then chaining together with-setter method calls.

The following table describes the various methods that are defined on input objects and show an example of each of the three ways in which you can construct an input object.

NotifyFulfillmentInput

public function NotifyFulfillmentInput(receiptId:String=null, fulfillmentResult:String=null)

Constructor. The constructor optionally takes arguments corresponding to the member variables. If you do not supply any arguments to the constructor, you can set the values of the member variables at a later time either by using the implicit setters, or by chaining together with-setter calls.

public function withReceiptId(receiptId:String):NotifyFulfillmentInput

With-setter. Sets the receipt id of a NotifyFulfillmentInput object and returns the object so that calls can be chained together

public function get receiptId():String

Implicit getter. Allows private variable access through dot notation. Returns the string value representing the receipt id.

public function set receiptId(receiptId:String):void

Implicit setter. Allows private variable access through dot notation. Sets the string value representing the receipt id.

public function withFulfillmentResult(fulfillmentResult:String):NotifyFulfillmentInput

With-setter. Sets the fulfillment result of a NotifyFulfillmentInput object and returns the object so that calls can be chained together.

public function get fulfillmentResult():String

Implicit getter. Allows private variable access through dot notation. Returns the string value representing the fulfillment result.

public function set fulfillmentResult(fulfillmentResult:String):void

Implicit setter. Allows private variable access through dot notation. Sets the string value representing the fulfillment result.

Sample code

 / Constructor example
notifyFulfillmentInput:NotifyFulfillmentInput = new NotifyFulfillmentInput("12345", "FULFILLED");

// Implicit setters example
notifyFulfillmentInput:NotifyFulfillmentInput = new NotifyFulfillmentInput();
notifyFulfillmentInput.receiptId = "12345";
notifyFulfillmentInput.fulfillmentResult = "FULFILLED";

// With-setters example
notifyFulfillmentInput:NotifyFulfillmentInput =
    new NotifyFulfillmentInput().withReceiptId("12345").withFulfillmentResult("FULFILLED");

ResetInput

public function ResetInput(reset:Boolean=false)

Constructor. The constructor optionally takes arguments corresponding to the member variables. If you do not supply any arguments to the constructor, you can set the values of the member variables at a later time either by using the implicit setters, or by chaining together with-setter calls.

public function withReset(reset:Boolean):ResetInput

With-setter. Sets the reset of a ResetInput object and returns the object so that calls can be chained together

public function get reset():Boolean

Implicit getter. Allows private variable access through dot notation. Returns the boolean value representing whether to get a list of all purchases.

public function set reset(reset:Boolean):void

Implicit setter. Allows private variable access through dot notation. Sets the boolean value that determines whether to get a list of all purchases.

Sample code

 // Constructor example
resetInput:ResetInput = new ResetInput(true);

// Implicit setter example
resetInput:ResetInput = new ResetInput();
resetInput.reset = true;

// With-setter example
resetInput:ResetInput = new ResetInput().withReset(true);

SkuInput

public function SkuInput(sku:String=null)

Constructor. The constructor optionally takes arguments corresponding to the member variables. If you do not supply any arguments to the constructor, you can set the values of the member variables at a later time either by using the implicit setters, or by chaining together with-setter calls.

public function withSku(sku:String):SkuInput

With-setter. Sets the SKU of a SkuInput object and returns the object so that calls can be chained together.

` public function get sku():String `

Implicit getter. Allows private variable access through dot notation. Returns the string value of the SKU.

public function set sku(sku:String):void

Implicit setter. Allows private variable access through dot notation. Sets the string value of the SKU.

Sample code

 // Constructor example
skuInput:SkuInput = new SkuInput("SKU_20156789");

// Implicit setter example
skuInput:SkuInput = new SkuInput();
skuInput.sku = "SKU_201387654";

// With-setter example
skuInput:SkuInput = new SkuInput().withSku("SKU_201954321");

SkusInput

public function SkusInput(skus:Vector.<String>=null)

Constructor. The constructor optionally takes arguments corresponding to the member variables. If you do not supply any arguments to the constructor, you can set the values of the member variables at a later time either by using the implicit setters, or by chaining together with-setter calls.

public function withSkus(skus:Vector.<String>):SkusInput

With-setter. Sets the SKUs of a SkusInput object and returns the object so that calls can be chained together.

public function get skus():Vector.<String>

Implicit getter. Allows private variable access through dot notation. Returns the string vector that holds the SKUs.

public function set skus(skus:Vector.<String>):void

Implicit setter. Allows private variable access through dot notation. Sets the string vector that holds the SKUs

Sample code

 // Helper object
skus:Vector.<String> = new Vector.<String>();
skus.push("SKU_012345", "SKU_246810", "SKU_135792", "SKU_987654");

// Constructor example
skusInput:SkusInput = new SkusInput(skus);

// Implicit setter example
skusInput:SkusInput = new SkusInput();
skusInput.skus = skus;

// With-setter example
skusInput:SkusInput = new SkuInput().withSkus(skus);

GetUserData

GetUserData initiates a request to retrieve the user ID and marketplace of the currently logged-in user.

// Import the API package
import com.amazon.device.iap.cpt.*;

// Call the synchronous GetUserData operation, 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;

Purchase

Purchase initiates a purchase flow for a product.

// Import the API package
import com.amazon.device.iap.cpt.*;

// Construct the input object to be passed to the Purchase operation
var request:SkuInput = new SkuInput();

// Set the value of the input object's member variable
request.sku = "MAS-123456";

// Call the synchronous Purchase 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;

GetProductData

GetProductData initiates a request to retrieve item data for up to 100 SKUs.

// Import the API package
import com.amazon.device.iap.cpt.*;

// 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("sku1");
list.push("sku2");
list.push("sku3");

// 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;

GetPurchaseUpdates

GetPurchaseUpdates initiates a request to retrieve updates about items the customer has purchased and/or cancelled.

 // Import the API package
import com.amazon.device.iap.cpt.*;

// 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;

NotifyFulfillment

NotifyFulfillment notifies Amazon about the purchase fulfillment.

// Import the API package
import com.amazon.device.iap.cpt.*;

// Construct the input object to be passed to the NotifyFulfillment operation as input
var request:NotifyFulfillmentInput = new NotifyFulfillmentInput();

// Set input values
request.receiptId = "receiptID";
request.fulfillmentResult = "FULFILLED";

// Call the synchronous NotifyFulfillment operation, passing in the input object.
var requestOutput:RequestOutput = AmazonIapV2.notifyFulfillment(request);

Handle events in Adobe AIR

To handle an event, you must perform the following general steps:

  1. Import the API package.
  2. Define an event handler.
  3. Register your event handler with an event listener.

GetUserDataResponse

GetUserDataResponse returns the response from the GetUserData call.

// Import the API package
import com.amazon.device.iap.cpt.*;

// Define event handler
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;
}

// Register for an event
AmazonIapV2.addEventListener(AmazonIapV2Event.GET_USER_DATA_RESPONSE, getUserDataResponseEventHandler);

PurchaseResponse

PurchaseResponse returns the response from the Purchase call.

// Import the API package
import com.amazon.device.iap.cpt.*;

// Define event handler
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;
}

// Register for an event
AmazonIapV2.addEventListener(AmazonIapV2Event.PURCHASE_RESPONSE, purchaseResponseEventHandler);

GetProductDataResponse

GetProductDataResponse returns the response from the GetProductData call.

// Import the API package
import com.amazon.device.iap.cpt.*;

// Define event handler
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)
    var productData:ProductData = productDataMap["sku"];
    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;
}

// Register for an event
AmazonIapV2.addEventListener(AmazonIapV2Event.GET_PRODUCT_DATA_RESPONSE, getProductDataResponseEventHandler);

GetPurchaseUpdatesResponse

GetPurchaseUpdatesResponse returns the response from the GetProductData call.

// Import the API package
import com.amazon.device.iap.cpt.*;

// Define event handler
private function getPurchaseUpdatesResponseEventHandler(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 receipt:PurchaseReceipt = receipts[0];
    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;
}

// Register for an event
AmazonIapV2.addEventListener(AmazonIapV2Event.GET_PURCHASE_UPDATES_RESPONSE, getPurchaseUpdatesResponseEventHandler);

Last updated: Jan 15, 2022