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

IAP Plugin for Xamarin

This document provides sample code showing how to use the In-App Purchasing (IAP) v2.0 plugin for Xamarin. You can download the Xamarin 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 requestIds to match a specific request to a specific response, if necessary.

Initiate method calls in Xamarin

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

  1. Obtain the object used to interact with the plugin.
  2. If needed, construct the object used to pass input to the method.
  3. For asynchronous operations, define a callback function that will be invoked when a system response is ready.
  4. Call the operation, passing in any required input and/or callback function as arguments.

GetUserData

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

using com.amazon.device.iap.cpt;

// Obtain object used to interact with plugin
IAmazonIapV2 iapService = AmazonIapV2Impl.Instance;

// Call synchronous operation with no input
RequestOutput response = iapService.GetUserData();

// Get return value
string requestIdString = response.RequestId;

Purchase

Purchase initiates a purchase flow for a product.

using com.amazon.device.iap.cpt;

// Obtain object used to interact with plugin
IAmazonIapV2 iapService = AmazonIapV2Impl.Instance;

// Construct object passed to operation as input
SkuInput request = new SkuInput();

// Set input value
request.Sku = "sku";

// Call synchronous operation with input object
RequestOutput response = iapService.Purchase(request);

// Get return value
string requestIdString = response.RequestId;

GetProductData

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

using com.amazon.device.iap.cpt;

// Obtain object used to interact with plugin
IAmazonIapV2 iapService = AmazonIapV2Impl.Instance;

// Construct object passed to operation as input
SkusInput request = new SkusInput();

// Create list of SKU strings
List<string> list = new List<string>();
list.Add("sku1");
list.Add("sku2");
list.Add("sku3");

// Set input value
request.Skus = list;

// Call synchronous operation with input object
RequestOutput response = iapService.GetProductData(request);

// Get return value
string requestIdString = response.RequestId;

GetPurchaseUpdates

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

using com.amazon.device.iap.cpt;

// Obtain object used to interact with plugin
IAmazonIapV2 iapService = AmazonIapV2Impl.Instance;

// Construct object passed to operation as input
ResetInput request = new ResetInput();

// Set input value
request.Reset = true;

// Call synchronous operation with input object
RequestOutput response = iapService.GetPurchaseUpdates(request);

// Get return value
string requestIdString = response.RequestId;

NotifyFulfillment

NotifyFulfillment notifies Amazon about the purchase fulfillment.

using com.amazon.device.iap.cpt;

// Obtain object used to interact with plugin
IAmazonIapV2 iapService = AmazonIapV2Impl.Instance;

// Construct object passed to operation as input
NotifyFulfillmentInput request = new NotifyFulfillmentInput();

// Set input values
request.ReceiptId = "receiptID";
request.FulfillmentResult = "FULFILLED";

// Call synchronous operation with input object
RequestOutput response = iapService.NotifyFulfillment(request);

Handle events in Xamarin

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

  1. Obtain the object used to interact with the plugin.
  2. Define an event handler.
  3. Register your event handler with an event listener.

GetUserDataResponse

GetUserDataResponse returns the response from the GetUserData call.

using com.amazon.device.iap.cpt;

// Obtain object used to interact with plugin
IAmazonIapV2 iapService = AmazonIapV2Impl.Instance;

// Define event handler
private void EventHandler(GetUserDataResponse args)
{
    string requestId = args.RequestId;
    string userId = args.AmazonUserData.UserId;
    string marketplace = args.AmazonUserData.Marketplace;
    string status = args.Status;
}

// Register for an event
iapService.AddGetUserDataResponseListener(EventHandler);

PurchaseResponse

PurchaseResponse returns the response from the Purchase call.

using com.amazon.device.iap.cpt;

// Obtain object used to interact with plugin
IAmazonIapV2 iapService = AmazonIapV2Impl.Instance;

// Define event handler
private void EventHandler(PurchaseResponse args)
{
    string requestId = args.RequestId;
    string userId = args.AmazonUserData.UserId;
    string marketplace = args.AmazonUserData.Marketplace;
    string receiptId = args.PurchaseReceipt.ReceiptId;
    long cancelDate = args.PurchaseReceipt.CancelDate;
    long purchaseDate = args.PurchaseReceipt.PurchaseDate;
    string sku = args.PurchaseReceipt.Sku;
    string productType = args.PurchaseReceipt.ProductType;
    string status = args.Status;
}

// Register for an event
iapService.AddPurchaseResponseListener(EventHandler);

GetProductDataResponse

GetProductDataResponse returns the response from the GetProductData call.

using com.amazon.device.iap.cpt;

// Obtain object used to interact with plugin
IAmazonIapV2 iapService = AmazonIapV2Impl.Instance;

// Define event handler
private void EventHandler(GetProductDataResponse args)
{
    string requestId = args.RequestId;
    Dictionary<string, ProductData> productDataMap = args.ProductDataMap;
    List<string> unavailableSkus = args.UnavailableSkus;
    string status = args.Status;

    // for each item in the productDataMap you can get the following values for a given SKU
    // (replace "sku" with the actual SKU)
    string sku = productDataMap["sku"].Sku;
    string productType = productDataMap["sku"].ProductType;
    string price = productDataMap["sku"].Price;
    string title = productDataMap["sku"].Title;
    string description = productDataMap["sku"].Description;
    string smallIconUrl = productDataMap["sku"].SmallIconUrl;
}

// Register for an event
iapService.AddGetProductDataResponseListener(EventHandler);

GetPurchaseUpdatesResponse

GetPurchaseUpdatesResponse returns the response from the GetProductData call.

using com.amazon.device.iap.cpt;

// Obtain object used to interact with plugin
IAmazonIapV2 iapService = AmazonIapV2Impl.Instance;

// Define event handler
private void EventHandler(GetPurchaseUpdatesResponse args)
{
    string requestId = args.RequestId;
    string userId = args.AmazonUserData.UserId;
    string marketplace = args.AmazonUserData.Marketplace;
    List<PurchaseReceipt> receipts = args.PurchaseReceipts;
    string status = args.Status;
    boolean hasMore = args.HasMore;

    // for each purchase receipt you can get the following values
    string receiptId = receipts[0].ReceiptId;
    long cancelDate = receipts[0].CancelDate;
    long purchaseDate = receipts[0].PurchaseDate;
    string sku = receipts[0].Sku;
    string productType = receipts[0].ProductType;
}

// Register for an event
iapService.AddGetPurchaseUpdatesResponseListener(EventHandler);

Last updated: Jan 15, 2022