Ejemplo n.º 1
0
        public async Task <ActionResult> AccountPaymentResult(string paymentUniqueId, int state,
                                                              CancellationToken cancellationToken)
        {
            var uniqueId = Guid.Parse(paymentUniqueId);
            var payment  = await _dataContext.Payments.Include(q => q.ShopItem).FirstOrDefaultAsync(q => q.UniqueId == uniqueId, cancellationToken);

            if (payment == null)
            {
                return(NotFound());
            }

            //TODO: Real online payment verification
            if (state == 0)
            {
                payment.State = PaymentStateIds.Paid;

                var accountItem = await _dataContext.AccountItems.FirstOrDefaultAsync(
                    q => q.AccountId == payment.AccountId && q.ItemTypeId == payment.ShopItem.TypeId,
                    cancellationToken);

                var playerStats = await _dataContext.AccountStatsSummaries.FirstAsync(q =>
                                                                                      q.IsArchived == false && q.AccountId == payment.AccountId, cancellationToken : cancellationToken);

                if (accountItem != null)
                {
                    accountItem.Quantity += payment.ShopItem.Quantity ?? 1;
                }
                else
                {
                    accountItem = new AccountItem
                    {
                        AccountId  = payment.AccountId,
                        ShopItemId = payment.ShopItemId,
                        Quantity   = payment.ShopItem.Quantity ?? 1,
                        ItemTypeId = payment.ShopItem.TypeId
                    };

                    _dataContext.AccountItems.Add(accountItem);
                }

                if (payment.ShopItem.TypeId == ShopItemTypeIds.AvatarItem)
                {
                    playerStats.CoinsSpentOnAvatarItems += (long)payment.Amount;
                }
                else if (payment.ShopItem.TypeId == ShopItemTypeIds.Booster)
                {
                    playerStats.CoinsSpentOnBoosterItems += (long)payment.Amount;
                }

                await _achievementService.ProcessPaymentAchievementsAsync(playerStats, payment, cancellationToken);
            }
            else
            {
                payment.State = PaymentStateIds.Failed;
            }

            await _dataContext.SaveChangesAsync(cancellationToken);

            return(Redirect($"{payment.RedirectUrl}?isSuccess={(payment.State == PaymentStateIds.Paid ? "true" : "false")}"));
        }