article

justin avatar image
justin posted

IAP V1 to V2 migration for Unity

Amazon IAP V2 plugin for Unity 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 Amazon IAP V1 plugin for Unity code base to the Amazon IAP V2 plugin for Unity code base.

Modifying your AndroidManifest.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 Amazon IAP V2 plugin for Unity:

Refactoring Your App's Source Code

Callback methods for handling your events should be defined in your OnEnable() or Start() methods:

void OnEnable () {
    // Set up event handlers
    iapService.AddPurchaseResponseListener(PurchaseResponseHandler);
    iapService.AddGetUserDataResponseListener(GetUserDataResponseHandler);
    iapService.AddGetProductDataResponseListener(ProductDataResponseHandler);
    iapService.AddGetPurchaseUpdatesResponseListener(PurchaseUpdatesResponseHandler);
}

// Define event handlers
private void PurchaseResponseHandler(PurchaseResponse args) {
    //Event Logic
}

private void GetUserDataResponseHandler(GetUserDataResponse args) {
    //Event Logic
}

Handling User Data:

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;

Handling the product data:

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 list = new List();
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;

To purchase IAP items:

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;

To get the purchase record of the user:

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;

One new features of Amazon IAP V2 plugin for Unity 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 notifyFulfillment:

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

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, Unity, RVS, migration, plugin

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

Justin@Amazon contributed to this article