Skip to content

Commit

Permalink
Added function to handle GetCurrentUserData flow of sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihz committed Nov 10, 2019
1 parent ef030da commit ede1826
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 63 deletions.
14 changes: 14 additions & 0 deletions Plugins/iOS/SibcheStoreKit/SibcheStoreKitWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,18 @@ void _SibcheStoreKitConsumePackage(const char *purchasePackageId){
SafeUnitySendMessage(MakeStringCopy(@"NotifyConsume"),MakeStringCopy(str));
}];
}

void _SibcheStoreKitGetCurrentUserData(){
[SibcheStoreKit getCurrentUserData:^(BOOL isSuccessful, SibcheError *error, LoginStatusType loginStatus, NSString *userCellphoneNumber, NSString *userId) {
NSDictionary* dictionary = @{
@"isSuccessful": isSuccessful ? @1 : @0,
@"error": error ? [error toJson] : @"",
@"loginStatus": [NSNumber numberWithInt:loginStatus],
@"userCellphoneNumber": userCellphoneNumber,
@"userId": userId,
};
NSString* str = changeDictionaryToJson(dictionary);
SafeUnitySendMessage(MakeStringCopy(@"NotifyGetCurrentUserData"),MakeStringCopy(str));
}];
}
}
129 changes: 79 additions & 50 deletions SibcheStoreKit/Sibche.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using System;
using System.Collections.Generic;

using System.Runtime.InteropServices;
using UnityEngine;

namespace SibcheStoreKit
{
public enum LoginStatusType
{
LoggedIn = 1,
LoggedOut = 2,
HaveTokenButFailedToCheck = 3,
}

public class Sibche
{
#if UNITY_IOS && !UNITY_EDITOR
[DllImport ("__Internal")]
private static extern void _SibcheStoreKitInit(string appKey, string appUrlScheme);
[DllImport ("__Internal")]
private static extern void _SibcheStoreKitInit (string appKey, string appUrlScheme);

[DllImport ("__Internal")]
private static extern void _SibcheStoreKitLogin();
[DllImport ("__Internal")]
private static extern void _SibcheStoreKitLogin ();

[DllImport ("__Internal")]
private static extern void _SibcheStoreKitLogout();
[DllImport ("__Internal")]
private static extern void _SibcheStoreKitLogout ();

[DllImport ("__Internal")]
private static extern void _SibcheStoreKitFetchInAppPurchasePackages();
[DllImport ("__Internal")]
private static extern void _SibcheStoreKitFetchInAppPurchasePackages ();

[DllImport ("__Internal")]
private static extern void _SibcheStoreKitFetchInAppPurchasePackage(string packageId);
[DllImport ("__Internal")]
private static extern void _SibcheStoreKitFetchInAppPurchasePackage (string packageId);

[DllImport ("__Internal")]
private static extern void _SibcheStoreKitFetchActiveInAppPurchasePackages();
[DllImport ("__Internal")]
private static extern void _SibcheStoreKitFetchActiveInAppPurchasePackages ();

[DllImport ("__Internal")]
private static extern void _SibcheStoreKitPurchasePackage(string packageId);
[DllImport ("__Internal")]
private static extern void _SibcheStoreKitPurchasePackage (string packageId);

[DllImport ("__Internal")]
private static extern void _SibcheStoreKitConsumePackage(string purchasePackageId);
[DllImport ("__Internal")]
private static extern void _SibcheStoreKitConsumePackage (string purchasePackageId);

[DllImport ("__Internal")]
private static extern void _SibcheStoreKitGetCurrentUserData ();
#endif

private static GameObject sibcheManager;
Expand All @@ -42,6 +51,7 @@ public class Sibche
private static List<Action<bool, SibcheError, List<SibchePurchasePackage>>> fetchActivePackagesPool = new List<Action<bool, SibcheError, List<SibchePurchasePackage>>>();
private static List<Action<bool, SibcheError, SibchePurchasePackage>> purchasePool = new List<Action<bool, SibcheError, SibchePurchasePackage>>();
private static List<Action<bool, SibcheError>> consumePool = new List<Action<bool, SibcheError>>();
private static List<Action<bool, SibcheError, LoginStatusType, string, string>> getCurrenUserDataPool = new List<Action<bool, SibcheError, LoginStatusType, string, string>>();

/// <summary>
/// Initializes Sibche SDK with the specified key and app open url scheme.
Expand All @@ -51,13 +61,12 @@ public class Sibche
public static void Initialize(string appKey, string appUrlScheme)
{
#if UNITY_IOS && !UNITY_EDITOR
if(sibcheManager==null)
{
sibcheManager = new GameObject ("SibcheManager");
UnityEngine.Object.DontDestroyOnLoad (sibcheManager);
sibcheManager.AddComponent<SibcheMessageHandler>();
if (sibcheManager == null) {
sibcheManager = new GameObject ("SibcheManager");
UnityEngine.Object.DontDestroyOnLoad (sibcheManager);
sibcheManager.AddComponent<SibcheMessageHandler> ();
}
_SibcheStoreKitInit(appKey, appUrlScheme);
_SibcheStoreKitInit (appKey, appUrlScheme);
#endif
}

Expand All @@ -68,8 +77,8 @@ public static void Initialize(string appKey, string appUrlScheme)
public static void Login(Action<bool, SibcheError, string, string> callback)
{
#if UNITY_IOS && !UNITY_EDITOR
loginPool.Add(callback);
_SibcheStoreKitLogin();
loginPool.Add (callback);
_SibcheStoreKitLogin ();
#endif
}

Expand All @@ -80,8 +89,8 @@ public static void Login(Action<bool, SibcheError, string, string> callback)
public static void Logout(Action callback)
{
#if UNITY_IOS && !UNITY_EDITOR
logoutPool.Add(callback);
_SibcheStoreKitLogout();
logoutPool.Add (callback);
_SibcheStoreKitLogout ();
#endif
}

Expand All @@ -92,8 +101,8 @@ public static void Logout(Action callback)
public static void FetchPackages(Action<bool, SibcheError, List<SibchePackage>> callback)
{
#if UNITY_IOS && !UNITY_EDITOR
fetchPackagesPool.Add(callback);
_SibcheStoreKitFetchInAppPurchasePackages();
fetchPackagesPool.Add (callback);
_SibcheStoreKitFetchInAppPurchasePackages ();
#endif
}

Expand All @@ -105,8 +114,8 @@ public static void FetchPackages(Action<bool, SibcheError, List<SibchePackage>>
public static void FetchPackage(string packageId, Action<bool, SibcheError, SibchePackage> callback)
{
#if UNITY_IOS && !UNITY_EDITOR
fetchPackagePool.Add(callback);
_SibcheStoreKitFetchInAppPurchasePackage(packageId);
fetchPackagePool.Add (callback);
_SibcheStoreKitFetchInAppPurchasePackage (packageId);
#endif
}

Expand All @@ -117,8 +126,8 @@ public static void FetchPackage(string packageId, Action<bool, SibcheError, Sibc
public static void FetchActivePackages(Action<bool, SibcheError, List<SibchePurchasePackage>> callback)
{
#if UNITY_IOS && !UNITY_EDITOR
fetchActivePackagesPool.Add(callback);
_SibcheStoreKitFetchActiveInAppPurchasePackages();
fetchActivePackagesPool.Add (callback);
_SibcheStoreKitFetchActiveInAppPurchasePackages ();
#endif
}

Expand All @@ -130,27 +139,39 @@ public static void FetchActivePackages(Action<bool, SibcheError, List<SibchePurc
public static void Purchase(string packageId, Action<bool, SibcheError, SibchePurchasePackage> callback)
{
#if UNITY_IOS && !UNITY_EDITOR
purchasePool.Add(callback);
_SibcheStoreKitPurchasePackage(packageId);
purchasePool.Add (callback);
_SibcheStoreKitPurchasePackage (packageId);
#endif
}

/// <summary>
///
/// This function tries to consume consumable in app purchased package with purchasePackageId
/// </summary>
/// <param name="purchasePackageId">Id of purchased (consumable) package which you want to consume that</param>
/// <param name="callback">Callback that called when task is finished</param>
public static void Consume(string purchasePackageId, Action<bool, SibcheError> callback)
{
#if UNITY_IOS && !UNITY_EDITOR
consumePool.Add(callback);
_SibcheStoreKitPurchasePackage(purchasePackageId);
consumePool.Add (callback);
_SibcheStoreKitConsumePackage (purchasePackageId);
#endif
}

/// <summary>
/// Gets current user data if logged in
/// </summary>
/// <param name="callback">Callback that called when task is finished</param>
public static void GetCurrentUserData(Action<bool, SibcheError, LoginStatusType, string, string> callback)
{
#if UNITY_IOS && !UNITY_EDITOR
getCurrenUserDataPool.Add (callback);
_SibcheStoreKitGetCurrentUserData ();
#endif
}

public static void OnLogin(bool isLoginSuccessful, SibcheError error, string userName, string userId)
{
for (int i = 0; i < loginPool.Count; i++)
for (int i = loginPool.Count - 1; i >= 0; i--)
{
loginPool[i](isLoginSuccessful, error, userName, userId);
loginPool.RemoveAt(i);
Expand All @@ -159,7 +180,7 @@ public static void OnLogin(bool isLoginSuccessful, SibcheError error, string use

public static void OnLogout()
{
for (int i = 0; i < logoutPool.Count; i++)
for (int i = logoutPool.Count - 1; i >= 0; i--)
{
logoutPool[i]();
logoutPool.RemoveAt(i);
Expand All @@ -168,7 +189,7 @@ public static void OnLogout()

public static void OnFetchPackages(bool isSuccessful, SibcheError error, List<SibchePackage> packages)
{
for (int i = 0; i < fetchPackagesPool.Count; i++)
for (int i = fetchPackagesPool.Count - 1; i >= 0; i--)
{
fetchPackagesPool[i](isSuccessful, error, packages);
fetchPackagesPool.RemoveAt(i);
Expand All @@ -177,7 +198,7 @@ public static void OnFetchPackages(bool isSuccessful, SibcheError error, List<Si

public static void OnFetchPackage(bool isSuccessful, SibcheError error, SibchePackage package)
{
for (int i = 0; i < fetchPackagePool.Count; i++)
for (int i = fetchPackagePool.Count - 1; i >= 0; i--)
{
fetchPackagePool[i](isSuccessful, error, package);
fetchPackagePool.RemoveAt(i);
Expand All @@ -186,7 +207,7 @@ public static void OnFetchPackage(bool isSuccessful, SibcheError error, SibchePa

public static void OnFetchActivePackages(bool isSuccessful, SibcheError error, List<SibchePurchasePackage> purchasePackages)
{
for (int i = 0; i < fetchActivePackagesPool.Count; i++)
for (int i = fetchActivePackagesPool.Count - 1; i >= 0; i--)
{
fetchActivePackagesPool[i](isSuccessful, error, purchasePackages);
fetchActivePackagesPool.RemoveAt(i);
Expand All @@ -195,7 +216,7 @@ public static void OnFetchActivePackages(bool isSuccessful, SibcheError error, L

public static void OnPurchase(bool isSuccessful, SibcheError error, SibchePurchasePackage purchasePackage)
{
for (int i = 0; i < purchasePool.Count; i++)
for (int i = purchasePool.Count - 1; i >= 0; i--)
{
purchasePool[i](isSuccessful, error, purchasePackage);
purchasePool.RemoveAt(i);
Expand All @@ -204,12 +225,20 @@ public static void OnPurchase(bool isSuccessful, SibcheError error, SibchePurcha

public static void OnConsume(bool isSuccessful, SibcheError error)
{
for (int i = 0; i < consumePool.Count; i++)
for (int i = consumePool.Count - 1; i >= 0; i--)
{
consumePool[i](isSuccessful, error);
consumePool.RemoveAt(i);
}
}
}
}

public static void OnGetCurrentUserData(bool isSuccessful, SibcheError error, LoginStatusType loginStatus, string userCellphoneNumber, string userId)
{
for (int i = getCurrenUserDataPool.Count - 1; i >= 0; i--)
{
getCurrenUserDataPool[i](isSuccessful, error, loginStatus, userCellphoneNumber, userId);
getCurrenUserDataPool.RemoveAt(i);
}
}
}
}
46 changes: 33 additions & 13 deletions SibcheStoreKit/SibcheMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using UnityEngine;
using System;
using SimpleJSON;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;

namespace SibcheStoreKit
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public void NotifyFetchPackages(String str)
try
{
JSONNode node = JSON.Parse(str);
bool isSuccessful = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
bool isSuccessfull = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
SibcheError error = node["error"].Value.Length > 0 ? new SibcheError(node["error"].Value) : null;
var packagesArray = node["packagesArray"].AsArray;
List<SibchePackage> packages = new List<SibchePackage>();
Expand All @@ -51,7 +51,7 @@ public void NotifyFetchPackages(String str)
var package = SibchePackageFactory.GetSibchePackage(item.Value);
packages.Add(package);
}
Sibche.OnFetchPackages(isSuccessful, error, packages);
Sibche.OnFetchPackages(isSuccessfull, error, packages);
}
catch (Exception ex)
{
Expand All @@ -64,10 +64,10 @@ public void NotifyFetchPackage(String str)
try
{
JSONNode node = JSON.Parse(str);
bool isSuccessful = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
bool isSuccessfull = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
SibcheError error = node["error"].Value.Length > 0 ? new SibcheError(node["error"].Value) : null;
SibchePackage package = node["package"].Value.Length > 0 ? SibchePackageFactory.GetSibchePackage(node["package"].Value) : null;
Sibche.OnFetchPackage(isSuccessful, error, package);
Sibche.OnFetchPackage(isSuccessfull, error, package);
}
catch (Exception ex)
{
Expand All @@ -80,7 +80,7 @@ public void NotifyFetchActivePackages(String str)
try
{
JSONNode node = JSON.Parse(str);
bool isSuccessful = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
bool isSuccessfull = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
SibcheError error = node["error"].Value.Length > 0 ? new SibcheError(node["error"].Value) : null;
var purchasePackagesArray = node["purchasePackagesArray"].AsArray;
List<SibchePurchasePackage> purchasePackages = new List<SibchePurchasePackage>();
Expand All @@ -89,7 +89,7 @@ public void NotifyFetchActivePackages(String str)
var purchasePackage = new SibchePurchasePackage(item.Value);
purchasePackages.Add(purchasePackage);
}
Sibche.OnFetchActivePackages(isSuccessful, error, purchasePackages);
Sibche.OnFetchActivePackages(isSuccessfull, error, purchasePackages);
}
catch (Exception ex)
{
Expand All @@ -102,11 +102,11 @@ public void NotifyPurchase(String str)
try
{
JSONNode node = JSON.Parse(str);
bool isSuccessful = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
bool isSuccessfull = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
SibcheError error = node["error"].Value.Length > 0 ? new SibcheError(node["error"].Value) : null;
SibchePurchasePackage purchasePackage = node["purchasePackage"].Value.Length > 0 ? new SibchePurchasePackage(node["purchasePackage"].Value) : null;

Sibche.OnPurchase(isSuccessful, error, purchasePackage);
Sibche.OnPurchase(isSuccessfull, error, purchasePackage);
}
catch (Exception ex)
{
Expand All @@ -119,15 +119,35 @@ public void NotifyConsume(String str)
try
{
JSONNode node = JSON.Parse(str);
bool isSuccessful = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
bool isSuccessfull = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
SibcheError error = node["error"].Value.Length > 0 ? new SibcheError(node["error"].Value) : null;

Sibche.OnConsume(isSuccessfull, error);
}
catch (Exception ex)
{
Debug.Log("SibcheStoreKit: " + ex.Message);
}
}

public void NotifyGetCurrentUserData(String str)
{
try
{
JSONNode node = JSON.Parse(str);
bool isSuccessfull = int.Parse(node["isSuccessful"].Value) > 0 ? true : false;
SibcheError error = node["error"].Value.Length > 0 ? new SibcheError(node["error"].Value) : null;
LoginStatusType loginStatus;
Enum.TryParse(node["loginStatus"].Value, out loginStatus);
string userCellphoneNumber = node["userCellphoneNumber"].Value;
string userId = node["userId"];

Sibche.OnConsume(isSuccessful, error);
Sibche.OnGetCurrentUserData(isSuccessfull, error, loginStatus, userCellphoneNumber, userId);
}
catch (Exception ex)
{
Debug.Log("SibcheStoreKit: " + ex.Message);
}
}
}
}
}

0 comments on commit ede1826

Please sign in to comment.