Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -----------------------------------------------------------------------
- // Copyright (c) Dreams on Demand
- // -----------------------------------------------------------------------
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Purchasing;
- using System.Linq;
- using com.dod.spacezoo.Core;
- using com.dod.spacezoo.iap;
- using UnityEngine.SceneManagement;
- using System;
- namespace com.dod.spacezoo.Store
- {
- public class IAPListenerHandler : MonoBehaviour
- {
- [SerializeField] private List<IAPStoreButton> iapButtons;
- private WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();
- void Start()
- {
- if (PlayerDataHandler.Data.TutorialFinished)
- {
- Debug.Log($"[IAPListenerHandler] Tutorial completed, calling TryToHandleUnprocessedProducts");
- TryToHandleUnprocessedProducts();
- }
- else
- {
- StartCoroutine(WaitForTutorial());
- }
- }
- //TODO substitute this for an event subscription when tutorial ends
- private IEnumerator WaitForTutorial()
- {
- while (!PlayerDataHandler.Data.TutorialFinished)
- {
- yield return endOfFrame;
- }
- Debug.Log($"[IAPListenerHandler] Tutorial completed, calling TryToHandleUnprocessedProducts");
- TryToHandleUnprocessedProducts();
- }
- private void TryToHandleUnprocessedProducts()
- {
- if (IAPStoreHandler.UnhandledProducts == null )
- {
- Debug.Log($"[IAPListenerHandler] IAPStoreHandler.UnhandledProducts is null");
- return;
- }
- Debug.Log($"[IAPListenerHandler] Trying to Handle Unprocessed Products: {IAPStoreHandler.UnhandledProducts.Count}");
- Product product = IAPStoreHandler.UnhandledProducts[0];
- //We only process purchases if the button is disabled
- var iapTargetButtons = from iapButton in iapButtons
- where iapButton.ProductID == IAPStoreHandler.UnhandledProducts[0].definition.id
- select iapButton;
- foreach (var iapTargetButton in iapTargetButtons)
- {
- Debug.Log($"[IAPListenerHandler] Checking IAP Button to complete purchase: {iapTargetButton.gameObject.name}");
- try
- {
- if (!iapTargetButton.gameObject.activeInHierarchy)
- {
- Debug.Log($"[IAPListenerHandler] Calling onPurchaseComplete for IAPButton: {iapTargetButton.gameObject.name} with unhandled product: {product.definition.id}");
- //iapTargetButton.onPurchaseComplete?.Invoke(product);
- //StartCoroutine(ProcessPurchase(iapTargetButton, product));
- //moved ConfirmPendingPurchase outside the for loop to confirm the purchase regardless if the button was found
- //IAPStoreHandler.Instance.Controller.ConfirmPendingPurchase(IAPStoreHandler.UnhandledProducts[0]);
- iapTargetButton.OnPurchaseCompleted(product);
- Debug.Log($"[IAPListenerHandler] Removed IAPButton: {iapTargetButton.gameObject.name} with unhandled product: {product.definition.id} from the unhandled list");
- IAPStoreHandler.UnhandledProducts.RemoveAt(0);
- break;
- }
- }
- catch (Exception e)
- {
- Debug.Log($"[IAPListenerHandler] Error with button: {iapTargetButton.gameObject.name}");
- Debug.LogError(e);
- }
- }
- IAPStoreHandler.Instance.Controller.ConfirmPendingPurchase(product);
- Debug.Log($"[IAPListenerHandler] Tried to consume product: {product.definition.id}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement