Beispiel #1
0
 public bool GetOTP(long accountId, string tel)
 {
     try
     {
         var status = OTP.OTP.GenerateOTP(accountId, tel);
         NLogManager.LogMessage("OTP Agency: " + status);
         if (int.Parse(status) < 0)
         {
             return(false);
         }
         bool deduct = TransactionDAO.DeductGold(accountId, 1000, "Phí dịch vụ OTP", 2);
         if (!deduct)
         {
             NLogManager.LogMessage("Tru phi dich vu OTP dai ly that bai: " + accountId);
             return(false);
         }
         SmsService.SendMessage(tel, $"Ma xac nhan: " + status);
         return(true);
     }
     catch (Exception ex)
     {
         NLogManager.PublishException(ex);
     }
     return(false);
 }
        public string ReceiveForgotPassOTP(string username, int otpType, string phoneNumber)
        {
            try
            {
                NLogManager.LogMessage("ReceiveForgotPassOTP: " + username + "|" + otpType + "|" + phoneNumber);
                if (otpType == 1)
                {
                    if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(phoneNumber))
                    {
                        return("-99");
                    }
                }

                var account = AccountDAO.GetAccountByUsername(username);
                if (!string.IsNullOrEmpty(account.Tel))
                {
                    if (!PhoneDetector.IsValidPhone(phoneNumber))
                    {
                        return("-54");
                    }
                }

                if (!string.IsNullOrEmpty(phoneNumber) && account.Tel != phoneNumber)
                {
                    return("-73");
                }
                else
                {
                    var status = OTP.OTP.GenerateOTP(account.AccountID, phoneNumber);
                    NLogManager.LogMessage("OTP: " + status);
                    if (int.Parse(status) < 0)
                    {
                        return(status);
                    }
                    bool deduct = TransactionDAO.DeductGold(account.AccountID, 1000, "Phí dịch vụ OTP", 2);
                    if (!deduct)
                    {
                        return("-62");
                    }
                    SmsService.SendMessage(phoneNumber, $"Ma xac nhan: " + status);

                    string token = $"{DateTime.Now.Ticks}|{account.AccountID}|{account.Tel}";
                    return(Security.TripleDESEncrypt(ConfigurationManager.AppSettings["OTPKey"], token));
                }
            }
            catch (Exception ex)
            {
                NLogManager.PublishException(ex);
            }
            return("-99");
        }
        public string ReceiveLoginOTP(string tokenOTP)
        {
            try
            {
                NLogManager.LogMessage("ReceiveLoginOTP: " + tokenOTP);
                if (string.IsNullOrEmpty(tokenOTP))
                {
                    NLogManager.LogMessage("RETURN ERROR ReceiveLoginOTP: -60");
                    return("-60");
                }

                string   decryptToken = Security.TripleDESDecrypt(ConfigurationManager.AppSettings["OTPKey"], System.Web.HttpUtility.UrlDecode(tokenOTP).Replace(" ", "+"));
                string[] splData      = decryptToken.Split('|');

                long time = long.Parse(splData[0]);
                if (TimeSpan.FromTicks(DateTime.Now.Ticks - time).TotalSeconds > 120)
                {
                    NLogManager.LogMessage("RETURN ERROR ReceiveLoginOTP: -1");
                    return("-1"); //Experied captcha
                }
                long accountId = Convert.ToInt64(splData[1]);
                var  account   = AccountDAO.GetAccountById(accountId);

                var status = OTP.OTP.GenerateOTP(accountId, account.Tel);
                if (int.Parse(status) < 0)
                {
                    return(status);
                }
                bool deduct = TransactionDAO.DeductGold(account.AccountID, 1000, "Phí dịch vụ OTP", 2);
                if (!deduct)
                {
                    NLogManager.LogMessage("RETURN ERROR ReceiveLoginOTP: -62");
                    return("-62");
                }
                SmsService.SendMessage(account.Tel, $"Ma xac nhan: " + status);
                NLogManager.LogMessage("RETURN ReceiveLoginOTP SUCCESS: 1, " + status);
                return("1");
            }
            catch (Exception ex)
            {
                NLogManager.PublishException(ex);
                NLogManager.LogMessage("ERROR ReceiveLoginOTP: " + ex);
            }
            NLogManager.LogMessage("RETURN ERROR ReceiveLoginOTP: -99");
            return("-99");
        }
        public int ReceiveOTP(string phoneNumber = "")
        {
            try
            {
                NLogManager.LogMessage("ReceiveOTP: " + phoneNumber);
                var account = AccountDAO.GetAccountById(AccountSession.AccountID);
                NLogManager.LogMessage("ReceiveOTP: " + JsonConvert.SerializeObject(account));
                //chua dang ky sdt
                if (string.IsNullOrEmpty(account.Tel))
                {
                    if (!PhoneDetector.IsValidPhone(phoneNumber))
                    {
                        NLogManager.LogMessage("FAIL PHONE: " + phoneNumber);
                        return(-54);
                    }
                    //send to phonenumber
                    //this case is for the first time update phone
                    var status = OTP.OTP.GenerateOTP(AccountSession.AccountID, phoneNumber);
                    NLogManager.LogMessage("OTP: " + status);
                    if (int.Parse(status) < 0)
                    {
                        return(int.Parse(status));
                    }
                    bool deduct = TransactionDAO.DeductGold(account.AccountID, 1000, "Phí dịch vụ OTP", 2);
                    NLogManager.LogMessage("DEDUCT OTP STATUS: " + deduct);
                    if (!deduct)
                    {
                        return(-62);
                    }
                    //send the otp to phone
                    SmsService.SendMessage(phoneNumber, $"Ma xac nhan: " + status);

                    return(1);
                }
                else
                {
                    NLogManager.LogMessage("OTP to phone: " + account.Tel);
                    var status = OTP.OTP.GenerateOTP(AccountSession.AccountID, account.Tel);
                    NLogManager.LogMessage("OTP: " + status);
                    if (int.Parse(status) < 0)
                    {
                        return(int.Parse(status));
                    }
                    bool deduct = TransactionDAO.DeductGold(account.AccountID, 1000, "Phí dịch vụ OTP", 2);
                    NLogManager.LogMessage("deduct: " + deduct);
                    if (!deduct)
                    {
                        return(-62);
                    }
                    //send the otp to phone
                    SmsService.SendMessage(account.Tel, $"Ma xac nhan: " + status);

                    return(1);
                }
            }
            catch (Exception ex)
            {
                NLogManager.PublishException(ex);
                NLogManager.LogMessage("ERROR ReceiveOTP: " + ex);
            }
            return(-99);
        }
        public APIResponse GetLoginAppOTPCode(string token)
        {
            try
            {
                // NLogManager.LogMessage("TOKEN: " + token);
                string   parseToken = Security.TripleDESDecrypt("APP_deviceToken", token);
                string[] split      = parseToken.Split('_');
                long     accountId  = long.Parse(split[1]);
                long     time       = long.Parse(split[2]);
                if (TimeSpan.FromTicks(DateTime.Now.Ticks - time).TotalMinutes > 5)
                {
                    return new APIResponse
                           {
                               Message      = "Phiên đăng nhập của bạn đã hết hạn",
                               ResponseCode = -80
                           }
                }
                ;

                var account = AccountDAO.GetAccountInfo(accountId);
                if (string.IsNullOrEmpty(account.Tel))
                {
                    return new APIResponse
                           {
                               Message      = "Tài khoản này chưa được kích hoạt tính năng bảo mật đăng nhập",
                               ResponseCode = -81
                           }
                }
                ;

                bool deduct = TransactionDAO.DeductGold(account.AccountID, 1000, "Phí dịch vụ OTP", 2);
                //NLogManager.LogMessage("DEDUCT OTP STATUS: " + deduct + "|" + account.DisplayName);
                if (!deduct)
                {
                    return new APIResponse
                           {
                               Message      = "Số dư tài khoản của bạn không đủ để thực hiện giao dịch này",
                               ResponseCode = -81
                           }
                }
                ;
                var status = OTP.OTP.GenerateOTP(accountId, account.Tel);
                if (status == "-70")
                {
                    return new APIResponse
                           {
                               Message      = "Chỉ có thể nhận mã OTP 5 phút một lần",
                               ResponseCode = -81
                           }
                }
                ;
                SmsService.SendMessage(account.Tel, $"Ma xac nhan: " + status);
                return(new APIResponse
                {
                    ResponseCode = 1
                });
            }
            catch (Exception ex)
            {
                NLogManager.PublishException(ex);
            }

            return(new APIResponse
            {
                Message = "Hệ thống của chúng tôi đang bận, xin bạn vui lòng thử lại sau!",
                ResponseCode = -99
            });
        }