In our previous post in the series, we shared the 2 ways to integrate the Amazon IAP API into your Unity game: using the built-in cross-platform Purchasing API from Unity or using the Amazon Appstore Unity plugin. After comparing the advantages and disadvantages of each method, we provided a detailed walk through for implementing the Unity Purchasing API in your game.
In this episode, we walk through implementing the Amazon IAP Unity plugin. The steps for setting up your IAP items on the Amazon Developer Console are exactly the same. Review the previous post now if you need a refresher.
Download the Amazon Unity plugin from here, and unzip the file. Follow these 7 easy steps to import the plugin and fix the manifest file to be able to receive IAP callbacks from our servers.
Next, take the same Purchaser class and rewrite it to use the Amazon Appstore Unity plugin:
using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using com.amazon.device.iap.cpt; public class Purchaser : MonoBehaviour, IStoreListener { // Obtain object used to interact with plugin, this is the IAP service object IAmazonIapV2 iapService = AmazonIapV2Impl.Instance; }
Add a method that will handle the response from the purchase function first:
// 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);
Now we are ready to make some purchases. Paste the below code in any function that is going to actually do the purchasing of the previously used SKU “100_gold_stars”.
// Construct object passed to operation as input SkuInput request = new SkuInput(); // Set input value request.Sku = "100_gold_stars"; // Call synchronous operation with input object RequestOutput response = iapService.Purchase(request); // Get return value string requestIdString = response.RequestId;
The method EventHandler() will run when the response from the purchase function is received by the game.
For more details on other operations you can use with the Amazon IAP Unity API click here.
Thanks for reading!