Advertisement
Vagem

IAPListenerHandler

Jul 30th, 2021 (edited)
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. // -----------------------------------------------------------------------
  2. //               Copyright (c) Dreams on Demand
  3. // -----------------------------------------------------------------------
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.Purchasing;
  8. using System.Linq;
  9. using com.dod.spacezoo.Core;
  10. using com.dod.spacezoo.iap;
  11. using UnityEngine.SceneManagement;
  12. using System;
  13.  
  14. namespace com.dod.spacezoo.Store
  15. {
  16.     public class IAPListenerHandler : MonoBehaviour
  17.     {
  18.         [SerializeField] private List<IAPStoreButton> iapButtons;
  19.  
  20.         private WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();
  21.  
  22.         void Start()
  23.         {
  24.             if (PlayerDataHandler.Data.TutorialFinished)
  25.             {
  26.                 Debug.Log($"[IAPListenerHandler] Tutorial completed, calling TryToHandleUnprocessedProducts");
  27.                 TryToHandleUnprocessedProducts();
  28.             }
  29.             else
  30.             {
  31.                 StartCoroutine(WaitForTutorial());
  32.             }
  33.         }
  34.  
  35.         //TODO substitute this for an event subscription when tutorial ends
  36.         private IEnumerator WaitForTutorial()
  37.         {
  38.             while (!PlayerDataHandler.Data.TutorialFinished)
  39.             {
  40.                 yield return endOfFrame;
  41.             }
  42.             Debug.Log($"[IAPListenerHandler] Tutorial completed, calling TryToHandleUnprocessedProducts");
  43.             TryToHandleUnprocessedProducts();
  44.         }
  45.  
  46.         private void TryToHandleUnprocessedProducts()
  47.         {
  48.             if (IAPStoreHandler.UnhandledProducts == null )
  49.             {
  50.                 Debug.Log($"[IAPListenerHandler] IAPStoreHandler.UnhandledProducts is null");
  51.                 return;
  52.             }
  53.             Debug.Log($"[IAPListenerHandler] Trying to Handle Unprocessed Products: {IAPStoreHandler.UnhandledProducts.Count}");
  54.  
  55.             Product product = IAPStoreHandler.UnhandledProducts[0];
  56.  
  57.             //We only process purchases if the button is disabled
  58.             var iapTargetButtons = from iapButton in iapButtons
  59.                                    where iapButton.ProductID == IAPStoreHandler.UnhandledProducts[0].definition.id
  60.                                    select iapButton;
  61.  
  62.             foreach (var iapTargetButton in iapTargetButtons)
  63.             {
  64.                 Debug.Log($"[IAPListenerHandler] Checking IAP Button to complete purchase: {iapTargetButton.gameObject.name}");
  65.                 try
  66.                 {
  67.                     if (!iapTargetButton.gameObject.activeInHierarchy)
  68.                     {
  69.                         Debug.Log($"[IAPListenerHandler] Calling onPurchaseComplete for IAPButton: {iapTargetButton.gameObject.name} with unhandled product: {product.definition.id}");
  70.                         //iapTargetButton.onPurchaseComplete?.Invoke(product);
  71.                         //StartCoroutine(ProcessPurchase(iapTargetButton, product));
  72.                         //moved ConfirmPendingPurchase outside the for loop to confirm the purchase regardless if the button was found
  73.                         //IAPStoreHandler.Instance.Controller.ConfirmPendingPurchase(IAPStoreHandler.UnhandledProducts[0]);
  74.                         iapTargetButton.OnPurchaseCompleted(product);
  75.                         Debug.Log($"[IAPListenerHandler] Removed IAPButton: {iapTargetButton.gameObject.name} with unhandled product: {product.definition.id} from the unhandled list");
  76.                         IAPStoreHandler.UnhandledProducts.RemoveAt(0);
  77.                         break;
  78.                     }
  79.                 }
  80.                 catch (Exception e)
  81.                 {
  82.                     Debug.Log($"[IAPListenerHandler] Error with button: {iapTargetButton.gameObject.name}");
  83.                     Debug.LogError(e);
  84.                 }
  85.             }
  86.  
  87.             IAPStoreHandler.Instance.Controller.ConfirmPendingPurchase(product);
  88.             Debug.Log($"[IAPListenerHandler] Tried to consume product: {product.definition.id}");
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement