public OutputModel AuthCode([FromBody] tblT_User_Token tkn)
        {
            OutputModel output = new OutputModel();

            try
            {
                var data = db.tblT_User_Token.Where(p =>
                                                    p.UserID == tkn.UserID &&
                                                    p.Token == tkn.Token).Select(p => p).FirstOrDefault();

                if (data != null)
                {
                    if (DateTime.Now <= data.DateExpired)
                    {
                        tblM_User update = db.tblM_User.Where(p => p.UserID.Equals(tkn.UserID)).Single <tblM_User>();
                        update.isActive      = 1;
                        update.isVerified    = 1;
                        update.DateVerified  = DateTime.Now;
                        update.DateActivated = update.DateVerified;
                        db.SaveChanges();

                        //cm.activity(Convert.ToInt32(usr.UserID), "Authentication success");

                        output.status  = "success";
                        output.message = "User has been verified.";
                        output.data    = update;
                    }
                    else
                    {
                        output.status  = "failed";
                        output.message = "Kode telah kadaluarsa. Silahkan ambil kode verifikasi dengan fitur kirim kode.";
                        output.data    = data;
                    }
                }
                else
                {
                    output.status  = "failed";
                    output.message = "Kode verifikasi salah.";
                    output.data    = data;
                }
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(tkn.UserID), "Authentication failed");

                output.status  = "error";
                output.message = ex.Message;
                output.data    = null;
            }

            return(output);
        }
        public OutputModel AuthCode([FromBody] tblT_User_Token tkn)
        {
            OutputModel output = new OutputModel();

            try
            {
                var data = db.tblM_User.Where(p =>
                                              p.UserID == tkn.UserID).Select(p => p).FirstOrDefault();

                if (data != null)
                {
                    Xsight ver = new Xsight();
                    //ver = AuthSMSOTP(tokens, data.UserID.ToString(), tkn.Token);
                    ver = cm.AuthSMSOTP(data.UserID.ToString(), tkn.Token);
                    if (ver.status)
                    {
                        tblM_User update = db.tblM_User.Where(p => p.UserID.Equals(tkn.UserID)).Single <tblM_User>();
                        update.isActive      = 1;
                        update.isVerified    = 1;
                        update.DateVerified  = DateTime.Now;
                        update.DateActivated = update.DateVerified;
                        db.SaveChanges();

                        //cm.activity(Convert.ToInt32(usr.UserID), "Authentication success");

                        output.status  = "success";
                        output.message = "User has been verified.";
                        output.data    = update;
                    }
                    else
                    {
                        ver = cm.SMSOTP(data.UserID.ToString(), cm.phoneNumberFormat(data.Phone), "");
                        //if (ver.status)
                        //{
                        output.status  = "failed";
                        output.message = "Kode verifikasi salah atau sudah kadaluarsa. Kami mengirimkan kembali kode otorisasi, silahkan cek kembali ponsel Anda.";
                        output.data    = data;
                        //}
                    }
                }

                //var data = db.tblT_User_Token.Where(p =>
                //p.UserID == tkn.UserID
                //&& p.Token == tkn.Token).Select(p => p).FirstOrDefault();

                //if (data != null)
                //{
                //    if (DateTime.Now <= data.DateExpired)
                //    {
                //        tblM_User update = db.tblM_User.Where(p => p.UserID.Equals(tkn.UserID)).Single<tblM_User>();
                //        update.isActive = 1;
                //        update.isVerified = 1;
                //        update.DateVerified = DateTime.Now;
                //        update.DateActivated = update.DateVerified;
                //        db.SaveChanges();

                //        //cm.activity(Convert.ToInt32(usr.UserID), "Authentication success");

                //        output.status = "success";
                //        output.message = "User has been verified.";
                //        output.data = update;
                //    }
                //    else
                //    {
                //        output.status = "failed";
                //        output.message = "Kode telah kadaluarsa. Silahkan ambil kode verifikasi dengan fitur kirim kode.";
                //        output.data = data;
                //    }
                //}
                //else {
                //    output.status = "failed";
                //    output.message = "Kode verifikasi salah.";
                //    output.data = data;
                //}
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(tkn.UserID), "Authentication failed");

                output.status  = "error";
                output.message = ex.Message;
                output.data    = null;
            }

            return(output);
        }
        public OutputModel RegisterWithPhone(string deviceid, [FromBody] tblM_User usr)
        {
            OutputModel output = new OutputModel();
            string      uid    = "";

            try
            {
                string ph = checkPhone(usr.Phone);
                if (ph == "yes")
                {
                    output.status  = "failed";
                    output.message = "Phone number already exist";
                    output.data    = String.Empty;
                    return(output);
                }
                usr.isVerified = 0;
                usr.isActive   = 0;
                usr.SMSCount   = 1;
                usr.DateJoin   = DateTime.Now;
                db.tblM_User.Add(usr);
                db.SaveChanges();

                var data = db.tblM_User.Where(p => p.Phone == usr.Phone).Select(p => p).Single();

                uid = data.UserID.ToString();

                if (deviceid != null)
                {
                    tblM_User_DeviceID add = new tblM_User_DeviceID();
                    add.UserID       = data.UserID;
                    add.DeviceID     = deviceid;
                    add.DateModified = DateTime.Now;
                    db.tblM_User_DeviceID.Add(add);
                    db.SaveChanges();
                }

                Random r = new Random();
                var    x = r.Next(0, 1000000);
                string s = x.ToString("000000");

                DateTime        exp  = DateTime.Now.AddMinutes(10);
                tblT_User_Token temp = new tblT_User_Token();
                temp.UserID      = data.UserID;
                temp.Token       = s;
                temp.DateExpired = exp;
                db.tblT_User_Token.Add(temp);

                //sendSMS(usr.Phone, temp.Token);

                db.SaveChanges();

                output.status  = "success";
                output.message = "User Successfully Inserted";
                output.data    = temp;
                cm.activity(Convert.ToInt32(data.UserID), "Register success");
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(uid), "Register failed");

                output.status  = "error";
                output.message = ex.Message;
                output.data    = String.Empty;
            }

            return(output);
        }
        public OutputModel RegisterWithPhone([FromBody] tblM_User usr)
        {
            OutputModel output = new OutputModel();
            string      uid    = "";

            try
            {
                string ph = checkPhone(usr.Phone);
                if (ph == "yes")
                {
                    output.status  = "failed";
                    output.message = "Phone number already exist";
                    output.data    = String.Empty;
                    return(output);
                }
                usr.isVerified = 0;
                usr.isActive   = 0;
                usr.isBanned   = 0;
                usr.isMerchant = 0;
                usr.Balance    = 0;
                usr.SMSCount   = 1;
                usr.DateJoin   = DateTime.Now;
                db.tblM_User.Add(usr);
                db.SaveChanges();

                var data = db.tblM_User.Where(p => p.Phone == usr.Phone).Select(p => p).Single();

                uid = data.UserID.ToString();

                if (usr.DeviceID != null)
                {
                    tblM_User_DeviceID add = new tblM_User_DeviceID();
                    add.UserID       = data.UserID;
                    add.DeviceID     = usr.DeviceID;
                    add.DateModified = DateTime.Now;
                    db.tblM_User_DeviceID.Add(add);
                    db.SaveChanges();
                }

                db.SaveChanges();

                Xsight ver = new Xsight();
                ver = cm.SMSOTP(uid, cm.phoneNumberFormat(data.Phone), "");

                //string sub = data.Phone.Substring(0, 1);
                //string subs = data.Phone.Substring(1, data.Phone.Length - 1);
                //if (sub == "0")
                //{
                //    ver = SMSOTP(tokens, uid, "+62"+subs, "");
                //}
                //else
                //{
                //    ver = SMSOTP(tokens, uid, data.Phone, "");
                //}

                if (ver.status)
                {
                    Random r = new Random();
                    var    x = r.Next(0, 1000000);
                    string s = x.ToString("000000");

                    DateTime        exp  = DateTime.Now.AddMinutes(10);
                    tblT_User_Token temp = new tblT_User_Token();
                    temp.UserID      = data.UserID;
                    temp.Token       = s;
                    temp.DateExpired = exp;
                    db.tblT_User_Token.Add(temp);

                    //sendSMS(usr.Phone, temp.Token);

                    db.SaveChanges();

                    output.status  = "success";
                    output.message = "User Successfully Inserted";
                    output.data    = data;
                }
                else
                {
                    output.status  = "failed";
                    output.message = "User failed to add";
                    output.data    = data;
                }

                //Random r = new Random();
                //var x = r.Next(0, 1000000);
                //string s = x.ToString("000000");

                //DateTime exp = DateTime.Now.AddMinutes(10);
                //tblT_User_Token temp = new tblT_User_Token();
                //temp.UserID = data.UserID;
                //temp.Token = s;
                //temp.DateExpired = exp;
                //db.tblT_User_Token.Add(temp);

                //sendSMS(usr.Phone, temp.Token);

                //db.SaveChanges();

                //output.status = "success";
                //output.message = "User Successfully Inserted";
                //output.data = temp;
                //cm.activity(Convert.ToInt32(data.UserID), "Register success");
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(uid), "Register failed");

                output.status  = "error";
                output.message = ex.Message;
                output.data    = String.Empty;
            }

            return(output);
        }