IAPCross Metro Example Codes

IAPCross Metro Example;


using UnityEngine;
using System.Collections;
using IAPC.Metro.Core;
using IAPC.Metro.Util;
using System.Collections.Generic;
using System.Linq;

public class IAPCrossMetroExample : MonoBehaviour
{
    private float yPosInterval;
    public string response;
    private string productId = "product1";
    private string consumableProductId = "consumable1";

    private void OnEnable()
    {
        yPosInterval = Screen.height / 18;
    }

    private void OnApplicationQuit()
    {
        if (IAPCrossMetro.Instance != null)
        {
            IAPCrossMetro.Instance.Dispose();
        }
    }

    public void OnGUI()
    {
        Rect buttonRects = new Rect(10, 10, 200, yPosInterval);

        GUI.Label(new Rect(buttonRects.width + buttonRects.x + 10, 20, Screen.width - buttonRects.x - 10 - buttonRects.width, Screen.height - 20), response);

        if (Settings.Instance.isSimulator && !IAPCrossMetro.Instance.IsSimulatorReady)
        {
            response = "Simulator is loading...";
            return;
        }

        if (IAPCrossMetro.Instance.IsLoadInProgress)
        {
            response = "Load in progress";
        }

        GUI.enabled = !IAPCrossMetro.Instance.IsLoadInProgress;
        //LoadListingInformation  -> Getting All Products (Obsolete : Init)
        if (GUI.Button(buttonRects, "LoadListingInformation"))
        {
            IAPCrossMetro.Instance.LoadListingInformation(delegate(LoadCompleteTemplate loadComplete)
            {
                if (loadComplete.IsSuccess)
                {
                    response = "LoadListingInformation completed successfully . Product Count : " + loadComplete.Products.Count;
                }
                else
                {
                    response = "LoadListingInformation complete with Error : " + loadComplete.Error;
                }
            });
        }

        buttonRects.y += yPosInterval;

        //LoadListingInformationByKeywords  -> Load Products by Keyword (Obsolete : Init) . Do not forget to add keyword when you create new Product in Windows Phone Dev Store
        if (GUI.Button(buttonRects, "Product Details"))
        {
            if (IAPCrossMetro.Instance.Products.Count == 0)
            {
                response = "No product is loaded before";
            }
            else
            {
                string productResponse = string.Empty;
                foreach (ProductTemplate item in IAPCrossMetro.Instance.Products)
                {
                    productResponse += string.Format("ProductId: {0}\nName: {1}\nFormattedPrice: {2}\nIsActive: {3}\n",
                        item.ProductId, item.Name, item.FormattedPrice, item.IsActive);
                }

                response = productResponse;
            }
        }

        if (IAPCrossMetro.Instance.IsPurchaseInProgress)
        {
            response = "Purchase in progress";
        }

        GUI.enabled = !IAPCrossMetro.Instance.IsPurchaseInProgress;

        buttonRects.y += yPosInterval;
        //Buy Full version of Trial App
        if (GUI.Button(buttonRects, "Trial To Full"))
        {
            IAPCrossMetro.Instance.TrialToFullLicence(delegate(PurchaseTemplate purchase)
            {
                if (purchase.IsSuccess)
                {
                    response = string.Format("Receipt : {0}", purchase.Receipt);
                    Debug.Log(response);
                }
                else
                {
                    response = purchase.Error;
                }
            });
        }

        buttonRects.y += yPosInterval;
        //Purchase        
        if (GUI.Button(buttonRects, "Purchase"))
        {
            IAPCrossMetro.Instance.Purchase(productId, delegate(PurchaseTemplate purchase)
            {
                if (purchase.IsSuccess)
                {
                    response = string.Format("Receipt : {0}", purchase.Receipt);
                    Debug.Log(response);
                }
                else
                {
                    response = purchase.Error;
                }
            });
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "Purchase Consumable"))
        {
            //ATTENTION : SUPPORTED FOR Windows 8.1
            IAPCrossMetro.Instance.Purchase(consumableProductId, "OFFER_ID",
                new ProductPurchaseDisplayPropertiesTemplate() { Description = "Description", Image = new System.Uri("http://www.ersindemir.net/resume/images/iap_wp.png"), Name = "Name" },
                delegate(PurchaseTemplate purchase)
                {
                    if (purchase.IsSuccess)
                    {
                        response = string.Format("Receipt : {0}", purchase.Receipt);
                        Debug.Log(response);
                    }
                    else
                    {
                        response = purchase.Error;
                    }
                });
        }

        //Return UnfulFillled Consumables
        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "GetUnfulfilledConsumables"))
        {
            //ATTENTION : SUPPORTED FOR Windows 8.1
            IAPCrossMetro.Instance.GetUnfulfilledConsumables(delegate(List<UnfulfilledConsumableTemplate> list)
            {
                if (list != null)
                {
                    response = string.Empty;
                    foreach (var item in list)
                    {
                        response += string.Format("OfferId: {0}\nProductId: {1}\nTransactionId: {2}\n\n", item.OfferId, item.ProductId, item.TransactionId);
                    }
                }
            });
        }

        buttonRects.y += yPosInterval;
        if (GUI.Button(buttonRects, "ReportFulFillmentConsumable"))
        {
            //ATTENTION : SUPPORTED FOR Windows 8.1
            IAPCrossMetro.Instance.GetUnfulfilledConsumables(delegate(List<UnfulfilledConsumableTemplate> list)
            {
                if (list != null)
                {
                    UnfulfilledConsumableTemplate unfilledConsumable = list.FirstOrDefault(x => x.ProductId == consumableProductId);
                    if (unfilledConsumable != null)
                    {
                        IAPCrossMetro.Instance.ReportConsumableFulfillment(consumableProductId, unfilledConsumable.TransactionId, delegate(FulfillmentResults result)
                        {
                            response = "ReportConsumableFulfillment : " + result.ToString();
                        });
                    }
                    else
                    {
                        response = "UnfulfilledConsumable Product 2  is not exist";
                    }
                }
                else
                {
                    response = "UnfulfilledConsumables is not exist";
                }
            });
        }

        buttonRects.y += yPosInterval;
        //GetProductReceipt
        if (GUI.Button(buttonRects, "Get Receipt"))
        {
            IAPCrossMetro.Instance.GetProductReceipt(productId, delegate(PurchaseTemplate purchase)
            {
                if (purchase.IsSuccess)
                {
                    response = string.Format("Receipt : {0}", purchase.Receipt);
                    Debug.Log(response);
                }
                else
                {
                    response = purchase.Error;
                }
            });
        }

        buttonRects.y += yPosInterval;
        GUI.enabled = true;

        //IsProductLicenceActive
        if (GUI.Button(buttonRects, "IsProductLicenceActive"))
        {
            bool isActive = IAPCrossMetro.Instance.IsProductLicenceActive(productId);
            response = string.Format("{0} IsActive : {1}", productId, isActive);
        }

        buttonRects.y += yPosInterval;

        //IAPCrossMetro Version
        if (GUI.Button(buttonRects, "IAPCrossVersion"))
        {
            response = "IAPCrossMetro.Version : " + IAPCrossMetro.Version;
            //IAPCrossMetro.Instance.IsDebug);
        }

        buttonRects.y += yPosInterval;

        //Get App Id 
        if (GUI.Button(buttonRects, "AppID"))
        {
            response = "AppID : " + IAPCrossMetro.Instance.AppID;
        }

        buttonRects.y += yPosInterval;

        //Get Trial ExpirationDate
        if (GUI.Button(buttonRects, "AppExpirationDate"))
        {
            response = "AppExpirationDate : " + IAPCrossMetro.Instance.AppExpirationDate;
        }

        buttonRects.y += yPosInterval;

        //Get App Link Uri on Store
        if (GUI.Button(buttonRects, "AppLinkUri"))
        {
            response = "AppLinkUri : " + IAPCrossMetro.Instance.AppLinkUri;
        }

        buttonRects.y += yPosInterval;

        //Is App Active ?
        if (GUI.Button(buttonRects, "IsAppActive"))
        {
            response = "IsAppActive : " + IAPCrossMetro.Instance.IsAppActive;
        }

        buttonRects.y += yPosInterval;

        //Is App Trial ? 
        if (GUI.Button(buttonRects, "IsAppTrial"))
        {
            response = "IsAppTrial : " + IAPCrossMetro.Instance.IsAppTrial;
        }

        buttonRects.y += yPosInterval;

        //Is AppCross on Debug ? 
        if (GUI.Button(buttonRects, "IsDebug"))
        {
            response = "IsDebug : " + IAPCrossMetro.Instance.IsDebug;
        }

        buttonRects.y += yPosInterval;

        //Application Quit
        if (GUI.Button(buttonRects, "Quit"))
        {
            Application.Quit();
        }
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *