IAPCross WP Example Code

using UnityEngine;
using System.Collections;
using IAPC.WP.Core;
using IAPC.WP.Util;

public class IAPCrossWPExample : MonoBehaviour
{
    public string response;
    private string productId = "badbuddybeta.wpmarket.37";
    private float yPosOfGUI;
    private float yPosInterval = 40;

    private void Start()
    {
        IAPCross.Instance.OnLoadCompleteEvent += OnLoadComplete;
        IAPCross.Instance.OnPurchaseCompleteEvent += OnPurchaseComplete;
        IAPCross.Instance.OnLicenceChangeEvent += OnLicenceChange;
        IAPCross.Instance.IsDebug = true;
    }

    private void OnApplicationQuit()
    {
        IAPCross.Instance.Dispose();
    }

    private void OnLicenceChange()
    {
        Debug.Log(string.Format("OnLicenceChange  : isTrial : {0} ", IAPCross.Instance.IsAppTrial));
    }

    private void OnPurchaseComplete(PurchaseTemplate purchaseComplete)
    {
        Debug.Log(string.Format("Purchase Complete from Event IsSuccess : {0} {1}", purchaseComplete.IsSuccess, purchaseComplete.IsSuccess ? purchaseComplete.Receipt : string.Empty));
    }

    private void OnLoadComplete(LoadCompleteTemplate loadComplete)
    {
        Debug.Log(string.Format("Load Complete from Event IsSuccess : {0}", loadComplete.IsSuccess));
    }

    public void OnGUI()
    {
        yPosOfGUI = 20;

        GUI.Label(new Rect(180, 20, Screen.width - 190, Screen.height), response);

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

        GUI.enabled = !IAPCross.Instance.IsLoadInProgress;
        //LoadListingInformation  -> Getting All Products (Obsolete : Init)
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "LoadListingInformation"))
        {
            IAPCross.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;
                }
            });
        }

        yPosOfGUI += yPosInterval;

        //LoadListingInformationByProductIds  -> Load Products by Product Ids (Obsolete : Init)
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "LoadListingInformationByProductIds"))
        {
            IAPCross.Instance.LoadListingInformationByProductIds(new string[] { productId }, delegate(LoadCompleteTemplate loadComplete)
            {
                if (loadComplete.IsSuccess)
                {
                    response = "LoadListingInformationByProductIds completed successfully. Product Count : " + loadComplete.Products.Count;
                }
                else
                {
                    response = "LoadListingInformationByProductIds complete with Error : " + loadComplete.Error;
                }
            });
        }

        yPosOfGUI += 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(new Rect(20, yPosOfGUI, 150, 30), "LoadListingInformationByKeywords"))
        {
            IAPCross.Instance.LoadListingInformationByKeywords(new string[] { "KEYWORD0", "KEYWORD1" }, delegate(LoadCompleteTemplate loadComplete)
            {
                if (loadComplete.IsSuccess)
                {
                    response = "LoadListingInformationByKeywords completed successfully. Product Count : " + loadComplete.Products.Count;
                }
                else
                {
                    response = "LoadListingInformationByKeywords complete with Error : " + loadComplete.Error;
                }
            });
        }

        yPosOfGUI += 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(new Rect(20, yPosOfGUI, 150, 30), "Product Details"))
        {
            if (IAPCross.Instance.Products.Count == 0)
            {
                response = "No product is loaded before";
            }
            else
            {
                string productResponse = string.Empty;
                foreach (var item in IAPCross.Instance.Products)
                {
                    productResponse += string.Format("ProductId: {0}\nName: {1}\nFormattedPrice: {2}\nIsActive: {3}\nImageUri: {4}\nDescription: {5}\n\n\n",
                        item.ProductId, item.Name, item.FormattedPrice, item.IsActive, item.ImageUri, item.Description);
                }

                response = productResponse;
            }
            
        }

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

        GUI.enabled = !IAPCross.Instance.IsPurchaseInProgress;

        yPosOfGUI += yPosInterval;

        //Purchase        
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "Purchase"))
        {
            IAPCross.Instance.Purchase(productId, delegate(PurchaseTemplate purchase)
            {
                if (purchase.IsSuccess)
                {
                    response = string.Format("Receipt : {0}", purchase.Receipt);
                    Debug.Log(response);
                }
                else
                {
                    response = purchase.Error;
                }
            });
        }

        yPosOfGUI += yPosInterval;

        //GetProductReceipt
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "Get Receipt"))
        {
            IAPCross.Instance.GetProductReceipt(productId, delegate(PurchaseTemplate purchase)
            {
                if (purchase.IsSuccess)
                {
                    response = string.Format("Receipt : {0}", purchase.Receipt);
                    Debug.Log(response);
                }
                else
                {
                    response = purchase.Error;
                }
            });
        }

        yPosOfGUI += yPosInterval;
        GUI.enabled = true;

        //IsProductLicenceActive
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "IsProductLicenceActive"))
        {
            bool isActive = IAPCross.Instance.IsProductLicenceActive(productId);
            response = string.Format("{0} IsActive : {1}", productId, isActive);
        }

        yPosOfGUI += yPosInterval;

        //Report Full Fillment and complete purchase flow
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "Report Ful Fillment"))
        {
            IAPCross.Instance.ReportProductFulfillment(productId, delegate(BaseOfCallbackTemplate result)
            {
                if (result.IsSuccess)
                {
                    response = "ReportProductFulfillment : Success";
                }
                else
                {
                    response = "ReportProductFulfillment : Failed  => " + result.Error;
                }
            });
        }

        yPosOfGUI += yPosInterval;

        //Buy Full version of Trial App
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "Trial To Full"))
        {
            IAPCross.Instance.TrialToFullLicence(delegate(PurchaseTemplate purchase)
            {
                if (purchase.IsSuccess)
                {
                    response = string.Format("Receipt : {0}", purchase.Receipt);
                    Debug.Log(response);
                }
                else
                {
                    response = purchase.Error;
                }
            });
        }

        yPosOfGUI += yPosInterval;

        //IAPCross Version
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "IAPCrossVersion"))
        {
            response = "IAPCross.Version : " + IAPCross.Version;
            //IAPCross.Instance.IsDebug);
        }

        yPosOfGUI += yPosInterval;

        //Get App Id 
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "AppID"))
        {
            response = "AppID : " + IAPCross.Instance.AppID;
        }

        yPosOfGUI += yPosInterval;

        //Get Trial ExpirationDate
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "AppExpirationDate"))
        {
            response = "AppExpirationDate : " + IAPCross.Instance.AppExpirationDate;
        }

        yPosOfGUI += yPosInterval;

        //Get App Link Uri on Store
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "AppLinkUri"))
        {
            response = "AppLinkUri : " + IAPCross.Instance.AppLinkUri;
        }

        yPosOfGUI += yPosInterval;

        //Is App Active ?
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "IsAppActive"))
        {
            response = "IsAppActive : " + IAPCross.Instance.IsAppActive;
        }

        yPosOfGUI += yPosInterval;

        //Is App Trial ? 
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "IsAppTrial"))
        {
            response = "IsAppTrial : " + IAPCross.Instance.IsAppTrial;
        }

        yPosOfGUI += yPosInterval;

        //Is AppCross on Debug ? 
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "IsDebug"))
        {
            response = "IsDebug : " + IAPCross.Instance.IsDebug;
        }

        yPosOfGUI += yPosInterval;

        //Application Quit
        if (GUI.Button(new Rect(20, yPosOfGUI, 150, 30), "Quit"))
        {
            Application.Quit();
        }
    }
}

One thought on “IAPCross WP Example Code”

  1. I just ought to tell you which you have written an exceptional and distinctive post that I really enjoyed reading. Im fascinated by how nicely you laid out your material and presented your views. Thank you. cfafbfcdbbgd

Leave a Reply

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