Skip to content
This repository was archived by the owner on Apr 28, 2022. It is now read-only.

Mock implementation for testing OpenIAB integration in the Unity editor #37

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions unity_plugin/unity_src/Assets/Plugins/OpenIAB/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class Inventory
private Dictionary<String, SkuDetails> _skuMap = new Dictionary<String, SkuDetails>();
private Dictionary<String, Purchase> _purchaseMap = new Dictionary<String, Purchase>();

#if UNITY_EDITOR
public Inventory()
{
}
#endif

public Inventory(string json)
{
var j = new JSON(json);
Expand Down
111 changes: 111 additions & 0 deletions unity_plugin/unity_src/Assets/Plugins/OpenIAB/Mock/OpenIAB_Mock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
namespace OnePF
{
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;

using Object = UnityEngine.Object;

public class OpenIAB_Mock : IOpenIAB
{
#region Fields

private readonly Dictionary<string, string> skuToStoreSku = new Dictionary<string, string>();

private readonly Dictionary<string, string> storeSkuToSku = new Dictionary<string, string>();

private OpenIABEventManager eventManager;

#endregion

#region Public Methods and Operators

public bool areSubscriptionsSupported()
{
return true;
}

public void consumeProduct(Purchase purchase)
{
throw new NotImplementedException();
}

public void enableDebugLogging(bool enabled)
{
throw new NotImplementedException();
}

public void enableDebugLogging(bool enabled, string tag)
{
throw new NotImplementedException();
}

public void init(Options options)
{
this.eventManager = Object.FindObjectOfType<OpenIABEventManager>();
if (this.eventManager != null)
{
this.eventManager.SendMessage("OnBillingSupported");
}
}

public bool isDebugLog()
{
throw new NotImplementedException();
}

public void mapSku(string sku, string storeName, string storeSku)
{
this.skuToStoreSku[sku] = storeSku;
this.storeSkuToSku[storeSku] = sku;
}

public void purchaseProduct(string sku, string developerPayload = "")
{
if (this.eventManager != null)
{
this.eventManager.SendMessage("OnPurchaseSucceeded", sku);
}
}

public void purchaseSubscription(string sku, string developerPayload = "")
{
this.purchaseProduct(sku, developerPayload);
}

public void queryInventory()
{
this.queryInventory(this.skuToStoreSku.Keys.ToArray());
}

public void queryInventory(string[] inAppSkus)
{
// Build fake inventory for testing in editor.
var inventory = new Inventory();

foreach (var sku in inAppSkus)
{
inventory.AddSkuDetails(
new SkuDetails { Sku = sku, Price = "$9.99", PriceValue = "9.99", CurrencyCode = "$" });
}

if (this.eventManager != null)
{
this.eventManager.SendMessage("OnQueryInventorySucceeded", inventory);
}
}

public void restoreTransactions()
{
throw new NotImplementedException();
}

public void unbindService()
{
}

#endregion
}
#endif
}
5 changes: 4 additions & 1 deletion unity_plugin/unity_src/Assets/Plugins/OpenIAB/OpenIAB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public class OpenIAB
*/
static OpenIAB()
{
#if UNITY_ANDROID
#if UNITY_EDITOR
_billing = new OpenIAB_Mock();
Debug.Log("********** Mock OpenIAB plugin initialized **********");
#elif UNITY_ANDROID
_billing = new OpenIAB_Android();
Debug.Log("********** Android OpenIAB plugin initialized **********");
#elif UNITY_IOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,76 @@ private void Awake()
DontDestroyOnLoad(this);
}

#if UNITY_ANDROID
#if UNITY_EDITOR
private void OnBillingSupported()
{
if (billingSupportedEvent != null)
{
billingSupportedEvent();
}
}

private void OnBillingNotSupported(string error)
{
if (billingNotSupportedEvent != null)
billingNotSupportedEvent(error);
}

private void OnQueryInventorySucceeded(OnePF.Inventory inventory)
{
if (queryInventorySucceededEvent != null)
{
queryInventorySucceededEvent(inventory);
}
}

private void OnQueryInventoryFailed(string error)
{
if (queryInventoryFailedEvent != null)
queryInventoryFailedEvent(error);
}

private void OnPurchaseSucceeded(string sku)
{
if (purchaseSucceededEvent != null)
{
purchaseSucceededEvent(Purchase.CreateFromSku(sku));
}
}

private void OnPurchaseFailed(string error)
{
if (purchaseFailedEvent != null)
{
purchaseFailedEvent(-1, error);
}
}

private void OnConsumePurchaseSucceeded(string json)
{
throw new System.NotImplementedException();
}

private void OnConsumePurchaseFailed(string error)
{
throw new System.NotImplementedException();
}

public void OnPurchaseRestored(string sku)
{
throw new System.NotImplementedException();
}

public void OnRestoreFailed(string error)
{
throw new System.NotImplementedException();
}

public void OnRestoreFinished(string message)
{
throw new System.NotImplementedException();
}
#elif UNITY_ANDROID
private void OnMapSkuFailed(string exception)
{
Debug.LogError("SKU mapping failed: " + exception);
Expand Down Expand Up @@ -182,9 +251,7 @@ public void OnRestoreTransactionSucceeded(string message)
restoreSucceededEvent();
}
}
#endif

#if UNITY_IOS
#elif UNITY_IOS
private void OnBillingSupported(string empty)
{
if (billingSupportedEvent != null)
Expand Down Expand Up @@ -265,9 +332,7 @@ public void OnRestoreFinished(string message)
restoreSucceededEvent();
}
}
#endif

#if UNITY_WP8
#elif UNITY_WP8
public void OnBillingSupported()
{
if (billingSupportedEvent != null)
Expand Down
8 changes: 7 additions & 1 deletion unity_plugin/unity_src/Assets/Plugins/OpenIAB/SkuDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public class SkuDetails
public string CurrencyCode { get; private set; }
public string PriceValue { get; private set; }

// Used for Android
#if UNITY_EDITOR
public SkuDetails()
{
}
#endif

// Used for Android
public SkuDetails(string jsonString)
{
var json = new JSON(jsonString);
Expand Down