Пример #1
0
        public async Task <IActionResult> TopupWithPaypal(int accountId, [FromBody] PaypalDTO paypal)
        {
            var id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (accountId != id)
            {
                return(Unauthorized());
            }
            var accountFromDb = await _repo.GetAccountDetail(accountId);

            accountFromDb.Point += paypal.Payer.Purchase.Amount.Value;

            if (await _repo.SaveAll())
            {
                var topupHistory = new TopupHistory()
                {
                    OrderId       = paypal.Id,
                    AccountId     = accountFromDb.Id,
                    TopupDate     = DateTime.Now,
                    PaymentMethod = "Paypal",
                    Amount        = paypal.Payer.Purchase.Amount.Value,
                    Currency      = paypal.Payer.Purchase.Amount.Currency,
                    Status        = paypal.Status
                };
                _cRUDRepo.Create(topupHistory);
                if (await _cRUDRepo.SaveAll())
                {
                    return(Ok());
                }
                return(StatusCode(500));
            }
            return(StatusCode(500));
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,ApplicationUserID,TopupDate,TopupAmount")] TopupHistory topupHistory)
        {
            if (id != topupHistory.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(topupHistory);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TopupHistoryExists(topupHistory.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ApplicationUserID"] = new SelectList(_context.ApplicationUsers, "Id", "Id", topupHistory.ApplicationUserID);
            return(View(topupHistory));
        }
Пример #3
0
        public async Task <IActionResult> Create([Bind("ID,ApplicationUserID,TopupDate,TopupAmount")] TopupHistory topupHistory)
        {
            if (ModelState.IsValid)
            {
                _context.Add(topupHistory);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ApplicationUserID"] = new SelectList(_context.ApplicationUsers, "Id", "Id", topupHistory.ApplicationUserID);
            return(View(topupHistory));
        }
Пример #4
0
        public async Task <IActionResult> Topup(int topupAmount)
        {
            APIContext aPIContext = Configuration.GetAPIContext();
            var        user       = _db.ApplicationUsers.Where(u => u.Email == User.Identity.Name).FirstOrDefault();
            var        payerId    = Request.Query["PayerID"].ToString();

            if (string.IsNullOrEmpty(payerId))
            {
                string baseUrl           = GetRawUrl(Request) + "?";
                var    guid              = Convert.ToString(new Random().Next(100000));
                var    payment           = CreatePayment(aPIContext, baseUrl + "guid=" + guid, topupAmount, user);
                string paypalRedirectUrl = "";
                var    links             = payment.links.GetEnumerator();
                while (links.MoveNext())
                {
                    Links link = links.Current;
                    if (link.rel.ToLower().Trim().Equals("approval_url"))
                    {
                        paypalRedirectUrl = link.href;
                    }
                }
                HttpContext.Session.SetString(guid, payment.id);
                HttpContext.Session.SetString("topupAmount", topupAmount.ToString());
                return(Redirect(paypalRedirectUrl));
            }
            else
            {
                var guid            = Request.Query["guid"];
                var topup           = HttpContext.Session.GetString("topupAmount");
                var executedPayment = ExecutePayment(aPIContext, payerId, HttpContext.Session.GetString(guid));
                if (executedPayment.state.ToLower() != "approved")
                {
                    return(View("Failure"));
                }
                TopupHistory topupHistory = new TopupHistory()
                {
                    ApplicationUserId = user.Id,
                    TopupAmount       = Int32.Parse(topup),
                    TopupDate         = DateTime.Now
                };
                user.Balance += Int32.Parse(topup) * 22000;
                _db.ApplicationUsers.Update(user);
                _db.TopupHistories.Add(topupHistory);
                await _db.SaveChangesAsync();
            }
            return(View("Successful"));
        }