Exemple #1
0
        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();
                    }
                }
            }
        }