Skip to content

Instantly share code, notes, and snippets.

@ardaerbaharli
Last active April 13, 2023 15:32
Show Gist options
  • Select an option

  • Save ardaerbaharli/1ff15241d40324e91988536c7612df07 to your computer and use it in GitHub Desktop.

Select an option

Save ardaerbaharli/1ff15241d40324e91988536c7612df07 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using Ads;
using SocialPlatforms;
// using Analytics;
using UnityEngine;
using UnityEngine.Purchasing;
namespace IAP
{
public class IAPEventHandler : MonoBehaviour
{
public static IAPEventHandler Instance;
[SerializeField] private GameObject waitingPanel;
[SerializeField] private GameObject failPanel;
[SerializeField] private GameObject loadingImage;
[SerializeField] private List<GameObject> objectsToHideAfterRemoveAds;
private void Awake()
{
Instance = this;
}
public void OpenWaitingScreen()
{
failPanel.SetActive(false);
waitingPanel.SetActive(true);
}
public void OnPurchaseCompleted(Product p)
{
print("Purchase completed: " + p.definition.id);
print("Receipt: " + p.receipt);
AchievementsManager.Instance.GetAchievement(AchievementType.Supporter).SetCompleted();
var didRestoredProduct = false;
if (p.definition.type == ProductType.NonConsumable)
{
var key = "DidAlreadyBuy" + p.definition.id;
didRestoredProduct = PlayerPrefs.GetInt(key, 0) == 1;
}
var iapItemType = IAPItems.GetIAPItemType(p.definition.id);
if (!didRestoredProduct)
{
// AnalyticsManager.Instance.LogIAP(p,iapItemType);
}
switch (iapItemType)
{
case IAPItemType.RemoveAdsStandard:
BoughtRemoveAds();
break;
case IAPItemType.Unknown:
print("Unknown IAP item type: " + p.definition.id);
break;
default:
throw new ArgumentOutOfRangeException();
}
waitingPanel.SetActive(false);
}
private void BoughtRemoveAds()
{
// AdManager.instance.RemoveAds();
objectsToHideAfterRemoveAds.ForEach(o => o.SetActive(false));
}
public void OnPurchaseFailed(Product p, PurchaseFailureReason r)
{
loadingImage.SetActive(false);
failPanel.SetActive(true);
switch (r)
{
case PurchaseFailureReason.PurchasingUnavailable:
break;
case PurchaseFailureReason.ExistingPurchasePending:
break;
case PurchaseFailureReason.ProductUnavailable:
break;
case PurchaseFailureReason.SignatureInvalid:
break;
case PurchaseFailureReason.UserCancelled:
break;
case PurchaseFailureReason.PaymentDeclined:
break;
case PurchaseFailureReason.DuplicateTransaction:
break;
case PurchaseFailureReason.Unknown:
break;
}
print("Purchase failed: " + p.definition.id + " " + r);
print("Failure reason: " + r);
waitingPanel.SetActive(false);
loadingImage.SetActive(true);
}
public void RestoreButton()
{
}
}
}
using System.Collections.Generic;
namespace IAP
{
public class IAPItems
{
private static Dictionary<string, IAPItemType> IAPItemsDictionary = new()
{
{"remove_ads_standard", IAPItemType.RemoveAdsStandard}
};
public static IAPItemType GetIAPItemType(string identifier)
{
return IAPItemsDictionary.ContainsKey(identifier) ? IAPItemsDictionary[identifier] : IAPItemType.Unknown;
}
}
}
namespace IAP
{
public enum IAPItemType
{
RemoveAdsStandard,
Unknown
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment