private void HandleTransaction(
     TransactionCache transaction,
     IEnumerator timeoutCoroutine,
     [CanBeNull] Action <CatalogItemModel> onSuccess = null,
     [CanBeNull] Action <Error> onError = null)
 {
     PurchaseHelper.Instance.ProcessOrder(transaction.projectId, transaction.transactionId, () =>
     {
         StopCoroutine(timeoutCoroutine);
         PutItemToInventory(transaction.item);
         ClearTransactionCache(transaction);
         UserInventory.Instance.Refresh();
         onSuccess?.Invoke(transaction.item);
     }, error =>
     {
         if (error.IsNetworkError)
         {
             StartCoroutine(LostConnectionHandler(transaction, timeoutCoroutine, onSuccess, onError));
         }
         else
         {
             StopCoroutine(timeoutCoroutine);
             ClearTransactionCache(transaction);
             GetErrorCallback(onError)?.Invoke(error);
         }
     });
     StartCoroutine(timeoutCoroutine);
 }
        private IEnumerator CheckOldTransaction(TransactionCache transactionCache,
                                                Action <TransactionCache> callback = null)
        {
            // Wait status update
            yield return(new WaitForSeconds(5.0F));

            if (IsLostTimeoutExpired(transactionCache))
            {
                // Stop server polling
                PurchaseHelper.Instance.StopProccessing(transactionCache.transactionId);
                // If user complete purchase flow and status still `In progress`, then something went wrong
                if (!_isUserNotified &&
                    PurchaseHelper.Instance.IsUserCompletePurchaseFlow(transactionCache.transactionId))
                {
                    ShowTransactionErrorMessage();
                    ClearTransactionCache(transactionCache);
                    _isUserNotified = true;
                }

                callback?.Invoke(transactionCache);
            }
            else
            {
                yield return(StartCoroutine(FailedTransactionNotification(transactionCache, callback)));
            }
        }
        private bool IsLostTimeoutExpired(TransactionCache transactionCache)
        {
            // Calculate wait transaction time
            var elapsedDateTime = transactionCache.dateTime.AddMinutes(LOST_TRANSACTION_NOTIFICATION_TIMEOUT_MIN);

            // LOST_TRANSACTION_NOTIFICATION_TIMEOUT_MIN timeout elapsed?
            return(DateTime.Now.CompareTo(elapsedDateTime) > 0);
        }
        private IEnumerator LostConnectionHandler(
            TransactionCache transaction,
            IEnumerator timeoutCoroutine,
            [CanBeNull] Action <CatalogItemModel> onSuccess = null,
            [CanBeNull] Action <Error> onError = null)
        {
            StopCoroutine(timeoutCoroutine);
            yield return(new WaitForSeconds(10.0F));

            HandleTransaction(transaction, timeoutCoroutine, onSuccess, onError);
        }
Ejemplo n.º 5
0
        private TransactionCache TransactionCaching(string projectId, string transactionId, CatalogItemModel item)
        {
            var cache = new TransactionCache
            {
                projectId     = projectId,
                transactionId = transactionId,
                item          = item,
                dateTime      = DateTime.Now
            };

            _transactions.Add(cache);
            SaveUserData(SimplifiedDemoConstants.USER_TRANSACTIONS, _transactions);
            return(cache);
        }
        private IEnumerator FailedTransactionNotification(TransactionCache transactionCache,
                                                          Action <TransactionCache> callback = null)
        {
            // Wait while user complete purchase flow
            yield return(new WaitUntil(() =>
                                       PurchaseHelper.Instance.IsUserCompletePurchaseFlow(transactionCache.transactionId)));

            // Wait timeout
            yield return(new WaitUntil(() => IsLostTimeoutExpired(transactionCache)));

            // Stop server polling
            PurchaseHelper.Instance.StopProccessing(transactionCache.transactionId);
            ClearTransactionCache(transactionCache);
            ShowTransactionErrorMessage();
            callback?.Invoke(transactionCache);
        }
Ejemplo n.º 7
0
 private void ClearTransactionCache(TransactionCache cache)
 {
     _transactions.Remove(cache);
     SaveUserData(SimplifiedDemoConstants.USER_TRANSACTIONS, _transactions);
 }