public ActionResult AsMoney() { string PAYEE_ACCOUNT = Request.Params["PAYEE_ACCOUNT"]; string PAYER_ACCOUNT = Request.Params["PAYER_ACCOUNT"]; string PAYMENT_AMOUNT = Request.Params["PAYMENT_AMOUNT"]; string PAYMENT_UNITS = Request.Params["PAYMENT_UNITS"]; string BATCH_NUM = Request.Params["BATCH_NUM"]; string PAYMENT_ID = Request.Params["PAYMENT_ID"]; string CF_1 = Request.Params["CF_1"]; string CF_2 = Request.Params["CF_2"]; string MD5_HASH = Request.Params["MD5_HASH"]; string PAYMENT_STATUS = Request.Params["PAYMENT_STATUS"]; // Check PAYEE_ACCOUNT if (PAYEE_ACCOUNT != "Managerwar") { return(View()); } // Find batch number int iBatchNumber = Convert.ToInt32(BATCH_NUM); Deposit dep; ApplicationDbContext db = new ApplicationDbContext(); // Find duplicate payments bool bDuplicate = false; var DuplicateDeposit = db.Deposits.Where(q => q.BatchNo == iBatchNumber); if (DuplicateDeposit.Count() > 0) { bDuplicate = true; dep = DuplicateDeposit.First(); // The deposit is duplicate return if (dep.Status != 0) { return(null); } } else { dep = new Deposit(); } // check PAYER_ACCOUNT switch (PAYER_ACCOUNT) { case "Bitcoin": dep.PaymentType = 2; break; case "Litecoin": dep.PaymentType = 3; break; default: dep.PaymentType = 1; break; } // Find PAYMENT_AMOUNT decimal dAmount = Convert.ToDecimal(PAYMENT_AMOUNT); // Check PAYMENT_UNITS if (PAYMENT_UNITS != "USD") { return(View()); } // Find user var user = db.Users.Find(PAYMENT_ID); dep.User_Id = PAYMENT_ID; // Find fish if (CF_1 == "Squid") { dep.Red = true; } else if (CF_1 == "Bob") { dep.Red = false; } else { throw new Exception("Fish not found"); } // Check round var round = db.Rounds.OrderByDescending(q => q.ID).First(); if (!bDuplicate) { dep.RoundID_ID = round.ID; } dep.RoundID = round; // Status is 1 for now if (PAYMENT_STATUS == "Complete") { dep.Status = 1; } else { dep.Status = 0; } // Check MD5 hash string mypasshash = "4DE35585E43001E7436DE75DAE44B67F"; string md5hash = PAYEE_ACCOUNT + "|" + PAYER_ACCOUNT + "|" + PAYMENT_AMOUNT + "|" + PAYMENT_UNITS + "|" + BATCH_NUM + "|" + PAYMENT_ID + "|" + CF_1 + "|" + CF_2 + "|" + mypasshash; md5hash = asmoneyapi.AsmoneyAPI.CalculateMD5Hash(md5hash); if (md5hash != MD5_HASH) { throw new Exception("Hash dose not match"); } if (round.ID.ToString() != CF_2) { if (!bDuplicate || dep.PaymentType == 1) { return(null); } dep.RoundID_ID = Convert.ToInt64(CF_2); if (PAYMENT_STATUS == "Complete") { dep.Status = 2; // send money back to sender asmoneyapi.AsmoneyAPI api = new asmoneyapi.AsmoneyAPI("managerwar", "api", "1532648sx"); user.Balance += dAmount; int txid; if (!string.IsNullOrEmpty(user.AsmoneyAccount)) { api.Transfer(user.AsmoneyAccount, (double)dAmount, "USD", "Back to sender " + dAmount + " USD in round #" + dep.RoundID_ID, out txid); } else if (!string.IsNullOrEmpty(user.BitcoinAddress)) { api.TransferBTC(user.BitcoinAddress, (double)dAmount, "USD", "Back to sender " + dAmount + " USD in round #" + dep.RoundID_ID, out txid); } else if (!string.IsNullOrEmpty(user.LitecoinAddress)) { api.TransferLTC(user.LitecoinAddress, (double)dAmount, "USD", "Back to sender " + dAmount + " USD in round #" + dep.RoundID_ID, out txid); } dep.Amount = dAmount; dep.Fee = 0; dep.FeePercentage = 0; dAmount = 0; } } else { if (!bDuplicate) { // Calculate amount and fee DateTime now = DateTime.UtcNow; double feerate = now.Subtract(round.StartTime).TotalSeconds * 0.00002316; dep.Amount = (decimal)(1 - feerate) * dAmount; dep.Fee = (decimal)feerate * dAmount + (decimal)0.0001; dep.FeePercentage = (decimal)feerate * 100; } } // If we recevied the money after round finished if (dep.FeePercentage >= 50) { dep.Status = 3; dep.Amount = 0; dep.Fee = dAmount; dep.FeePercentage = 100; } if (PAYMENT_STATUS == "Complete") { round.TotalDeposit += dAmount; round.TotalFee += dep.Fee; user.Balance -= dAmount; } // Add deposit to database if (!bDuplicate) { dep.BatchNo = iBatchNumber; db.Deposits.Add(dep); } db.SaveChanges(); return(View()); } // asmoney
} // CreateRound public void PayWinners(ref ApplicationDbContext db) { // TODO: Calculate the winners var RDepQueryConfirmed = db.Deposits.Where(q => q.RoundID_ID == ID && q.Status == 1 && q.Red == true); var GDepQueryConfirmed = db.Deposits.Where(q => q.RoundID_ID == ID && q.Status == 1 && q.Red == false); decimal RT = 0; decimal GT = 0; if (RDepQueryConfirmed.Count() > 0) { RT = RDepQueryConfirmed.Sum(q => q.Amount); } if (GDepQueryConfirmed.Count() > 0) { GT = GDepQueryConfirmed.Sum(q => q.Amount); } IQueryable <Deposit> winners; decimal WinTotal; decimal LoseTotal; if (RT > GT) { winners = RDepQueryConfirmed; WinTotal = RT; LoseTotal = GT + RT + Jackpot; } else { winners = GDepQueryConfirmed; WinTotal = GT; LoseTotal = GT + RT + Jackpot; } asmoneyapi.AsmoneyAPI api = new asmoneyapi.AsmoneyAPI("managerwar", "api", "1532648sx"); PerfectMoney pmapi = new PerfectMoney(); int txid = 0; foreach (Deposit d in winners) { decimal share = d.Amount / WinTotal; decimal win = share * LoseTotal; d.User = db.Users.Where(q => q.Id == d.User_Id).First(); // send money d.User.Balance += win; if (!string.IsNullOrEmpty(d.User.AsmoneyAccount)) { api.Transfer(d.User.AsmoneyAccount, (double)win, "USD", "Win " + win + " USD in round #" + d.RoundID_ID, out txid); } else if (!string.IsNullOrEmpty(d.User.BitcoinAddress)) { api.TransferBTC(d.User.BitcoinAddress, (double)win, "USD", "Win " + win + " USD in round #" + d.RoundID_ID, out txid); } else if (!string.IsNullOrEmpty(d.User.LitecoinAddress)) { api.TransferLTC(d.User.LitecoinAddress, (double)win, "USD", "Win " + win + " USD in round #" + d.RoundID_ID, out txid); } else if (!string.IsNullOrEmpty(d.User.PerfectMoney)) { int t = (int)(win * 100); double a = (double)t / 100; Dictionary <string, string> r = pmapi.Transfer("8701096", "159159sx", "U8289470", d.User.PerfectMoney, a, 0, 0); if (r.Keys.Contains("ERROR")) { SendEmail.Send("Send " + a + " USD to " + d.User.PerfectMoney + " Username is " + d.User.UserName + " Error is " + r["ERROR"], false, "Need PM", "*****@*****.**"); } } } WinnerPayed = true; // Add fees to total profit var TotalProfitObj = db.Settings.Where(q => q.Name == "TotalProfit").First(); decimal TotalProfit = Convert.ToDecimal(TotalProfitObj.Value); // Check if site profit is more than 100$ if (TotalProfit > 100) { TotalProfit += TotalFee / 2; // Send 50% of fee to my user api.Transfer("cyrus", (double)TotalFee / 2, "USD", "50% fee of round " + ID, out txid); } else { TotalProfit += TotalFee; } TotalProfitObj.Value = TotalProfit.ToString(); db.SaveChanges(); } // PayWinners