public ActionResult ProcessOrder(int?id) { if (id != null) { var order = _orderService.GetOrder(id.Value, i => i.CurrentStatus, i => i.Seller.ApplicationUser, i => i.Buyer.ApplicationUser, i => i.StatusLogs); if (order.CurrentStatus != null) { if (order.CurrentStatus.Value == OrderStatuses.MiddlemanFinding) { order.StatusLogs.AddLast(new StatusLog() { OldStatus = order.CurrentStatus, NewStatus = _orderStatusService.GetOrderStatusByValue(OrderStatuses.SellerProviding), TimeStamp = DateTime.Now }); order.CurrentStatus = _orderStatusService.GetOrderStatusByValue(OrderStatuses.SellerProviding); order.BuyerChecked = false; order.SellerChecked = false; order.MiddlemanId = User.Identity.GetUserId(); if (order.JobId != null) { BackgroundJob.Delete(order.JobId); order.JobId = null; } _orderService.SaveOrder(); order.JobId = MarketHangfire.SetOrderCloseJob(order.Id, TimeSpan.FromDays(1)); if (Request.Url != null) { MarketHangfire.SetSendEmailChangeStatus(order.Id, order.Seller.ApplicationUser.Email, order.CurrentStatus.DuringName, Url.Action("SellDetails", "Order", new { id = order.Id }, protocol: Request.Url.Scheme)); MarketHangfire.SetSendEmailChangeStatus(order.Id, order.Buyer.ApplicationUser.Email, order.CurrentStatus.DuringName, Url.Action("BuyDetails", "Order", new { id = order.Id }, protocol: Request.Url.Scheme)); } _orderService.SaveOrder(); return(RedirectToAction("MyOrderList")); } } } return(HttpNotFound()); }
public void MoneyIn() { var secretKey = "fVdVZaZjAkkd8h3A"; var pars = new SortedDictionary <string, string>(); var keys = Request.Form.AllKeys; foreach (var key in keys.Where(key => key.IndexOf("ik_") >= 0 && key != "ik_sign")) { pars.Add(key, Request.Form[key]); } var hash = string.Join(":", pars.Select(x => x.Value).ToArray().Concat(new[] { secretKey })); var md5 = new MD5CryptoServiceProvider(); var isSame = Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(hash))) == Request.Form["ik_sign"]; bool isOrderPay = false; if (isSame) { var userId = Request.Form["ik_x_user_id"]; var offerId = Request.Form["ik_x_offer_id"]; var mainCup = _userProfileService.GetUserProfileByName("palyerup"); if (offerId != null && userId != null && mainCup != null) { var user = _userProfileService.GetUserProfile(u => u.Id == userId, i => i.ApplicationUser); Offer offer = _offerService.GetOffer(int.Parse(offerId), o => o.UserProfile, o => o.UserProfile.ApplicationUser, o => o.Order, o => o.Order.StatusLogs, o => o.Order.CurrentStatus); decimal buyerPay = 0; if (offer.SellerPaysMiddleman) { buyerPay = offer.Price; } else { buyerPay = offer.Price + offer.MiddlemanPrice.Value; } if (buyerPay == Decimal.Parse(Request.Form["ik_am"])) { if (offer != null && offer.UserProfile.Id != User.Identity.GetUserId()) { offer.Order = new Order { Buyer = user, Seller = offer.UserProfile, DateCreated = DateTime.Now }; Order order = offer.Order; offer.State = OfferState.closed; order.StatusLogs.AddLast(new StatusLog() { OldStatus = _orderStatusService.GetOrderStatusByValue(OrderStatuses.OrderCreating), NewStatus = _orderStatusService.GetOrderStatusByValue(OrderStatuses.BuyerPaying), TimeStamp = DateTime.Now }); order.StatusLogs.AddLast(new StatusLog() { OldStatus = _orderStatusService.GetOrderStatusByValue(OrderStatuses.BuyerPaying), NewStatus = _orderStatusService.GetOrderStatusByValue(OrderStatuses.MiddlemanFinding), TimeStamp = DateTime.Now }); order.CurrentStatus = _orderStatusService.GetOrderStatusByValue(OrderStatuses.MiddlemanFinding); _transactionService.CreateTransaction(new Transaction { Amount = buyerPay, Order = order, Receiver = mainCup, Sender = user, TransactionDate = DateTime.Now }); if (offer.JobId != null) { BackgroundJob.Delete(offer.JobId); offer.JobId = null; } _offerService.SaveOffer(); isOrderPay = true; MarketHangfire.SetSendEmailChangeStatus(order.Id, offer.UserProfile.ApplicationUser.Email, order.CurrentStatus.DuringName, Url.Action("SellDetails", "Order", new { id = order.Id }, protocol: Request.Url.Scheme)); MarketHangfire.SetSendEmailChangeStatus(order.Id, user.ApplicationUser.Email, order.CurrentStatus.DuringName, Url.Action("BuyDetails", "Order", new { id = order.Id }, protocol: Request.Url.Scheme)); offer.Order.JobId = MarketHangfire.SetOrderCloseJob(order.Id, TimeSpan.FromDays(1)); _orderService.SaveOrder(); } } } // если пополняем баланс if (userId != null && !isOrderPay) { var user = _userProfileService.GetUserProfile(u => u.Id == userId); var amount = Decimal.Parse(Request.Form["ik_am"]); user.Balance += amount; _billingService.CreateBilling(new Billing { User = user, DateCeated = DateTime.Now, Amount = amount }); _userProfileService.SaveUserProfile(); } else if (!isOrderPay) { string currentUserId = User.Identity.GetUserId(); var currentUser = _userProfileService.GetUserProfileById(currentUserId); if (currentUser != null) { var amount = Decimal.Parse(Request.Form["ik_am"]); currentUser.Balance += amount; _billingService.CreateBilling(new Billing { User = currentUser, DateCeated = DateTime.Now, Amount = amount }); _userProfileService.SaveUserProfile(); } } } }
public async Task <ActionResult> Buy(CheckoutViewModel model) { Offer offer = await offerService.GetOfferAsync(model.OfferId, o => o.UserProfile, o => o.UserProfile.User, o => o.Order, o => o.Order.StatusLogs, o => o.Order.CurrentStatus); if (offer != null && offer.Order == null && offer.State == OfferState.active && offer.UserProfileId != User.Identity.GetUserId <int>()) { var currentUserId = User.Identity.GetUserId <int>(); var user = userProfileService.GetUserProfile(u => u.Id == currentUserId, i => i.User); var mainCup = userProfileService.GetUserProfileByName("playerup"); if (user != null && mainCup != null && user.Balance >= model.OrderSum) { //var amount = Decimal.Parse(Request.Form["ik_am"]); offer.Order = new Order { BuyerId = currentUserId, SellerId = offer.UserProfileId, CreatedDate = DateTime.Now }; offer.State = OfferState.closed; if (mainCup != null) { var seller = offer.UserProfile; var buyer = user; if (offer.SellerPaysMiddleman) { offer.Order.Sum = offer.Price; offer.Order.AmmountSellerGet = offer.Price - offer.MiddlemanPrice.Value; if (buyer.Balance >= offer.Order.Sum) { transactionService.CreateTransaction(new Transaction { Amount = offer.Order.Sum, Order = offer.Order, Receiver = mainCup, Sender = buyer, CreatedDate = DateTime.Now }); buyer.Balance -= offer.Order.Sum; mainCup.Balance += offer.Order.Sum; } else { return(View("NotEnoughMoney")); } } else { offer.Order.Sum = offer.Price + offer.MiddlemanPrice.Value; offer.Order.AmmountSellerGet = offer.Price; if (buyer.Balance >= offer.Order.Sum) { transactionService.CreateTransaction(new Transaction { Amount = offer.Order.Sum, Order = offer.Order, Receiver = mainCup, Sender = buyer, CreatedDate = DateTime.Now }); buyer.Balance -= offer.Order.Sum; mainCup.Balance += offer.Order.Sum; } else { return(View("NotEnoughMoney")); } } offer.Order.StatusLogs.AddLast(new StatusLog() { OldStatus = orderStatusService.GetOrderStatusByValue(OrderStatuses.OrderCreating), NewStatus = orderStatusService.GetOrderStatusByValue(OrderStatuses.BuyerPaying), TimeStamp = DateTime.Now }); offer.Order.StatusLogs.AddLast(new StatusLog() { OldStatus = orderStatusService.GetOrderStatusByValue(OrderStatuses.BuyerPaying), NewStatus = orderStatusService.GetOrderStatusByValue(OrderStatuses.MiddlemanFinding), TimeStamp = DateTime.Now }); offer.Order.CurrentStatus = orderStatusService.GetOrderStatusByValue(OrderStatuses.MiddlemanFinding); offer.Order.BuyerChecked = false; offer.Order.SellerChecked = false; if (offer.Order.JobId != null) { BackgroundJob.Delete(offer.Order.JobId); offer.Order.JobId = null; } if (offer.JobId != null) { BackgroundJob.Delete(offer.JobId); offer.JobId = null; } orderService.SaveOrder(); MarketplaceMVCHangfire.SetSendEmailChangeStatus(offer.Order.Id, seller.User.Email, offer.Order.CurrentStatus.DuringName, Url.Action("SellDetails", "Order", new { id = offer.Order.Id }, protocol: Request.Url.Scheme)); MarketplaceMVCHangfire.SetSendEmailChangeStatus(offer.Order.Id, buyer.User.Email, offer.Order.CurrentStatus.DuringName, Url.Action("BuyDetails", "Order", new { id = offer.Order.Id }, protocol: Request.Url.Scheme)); offer.Order.JobId = MarketplaceMVCHangfire.SetOrderCloseJob(offer.Order.Id, TimeSpan.FromDays(1)); //_orderService.UpdateOrder(order); orderService.SaveOrder(); TempData["orderBuyStatus"] = "Оплата прошла успешно"; return(RedirectToAction("BuyDetails", "Order", new { id = offer.Order.Id })); } } } return(HttpNotFound()); }