package handlers import ( "encoding/json" "errors" "fmt" "github.com/getsentry/sentry-go" "net/http" "strconv" "strings" "time" //"strconv" aes "cf-capital/module_server/handlers/aes" "cf-capital/module_server/handlers/auth" eloquent "cf-capital/module_server/handlers/eloquent" mail "cf-capital/module_server/handlers/email" investment "cf-capital/module_server/handlers/investment/statement" investmentV1 "cf-capital/module_server/handlers/investment/statement-v1" lendbox "cf-capital/module_server/handlers/lendbox" razorpay "cf-capital/module_server/handlers/razorpay" slack "cf-capital/module_server/handlers/slack" testUser "cf-capital/module_server/handlers/test/user" server_handlers "cf-server/server/handlers" "io/ioutil" "text/template" "github.com/go-playground/validator/v10" log "github.com/rs/zerolog/log" ) //var decoder = schema.NewDecoder() type Context struct { Title string Name string Fruits [3]string } type ApiError struct { Status string `json:"status"` Message string `json:"message"` } type responseBody struct { Status string `json:"status"` Message string `json:"message"` Link string `json:"link,omitempty"` Token string `json:"token,omitempty"` ReferenceCode string `json:"reference_code,omitempty"` Data string `json:"data,omitempty"` } type responseBodyPan struct { Status string `json:"status"` Message string `json:"message"` Data eloquent.ValidatePANDataResult `json:"data"` } type responseBodyPincode struct { Status string `json:"status"` Message string `json:"message"` Data eloquent.PincodeDetailsDataResult `json:"data"` } type responseBodyPassbook struct { Status string `json:"status"` Message string `json:"message"` Data []investmentV1.PassbookData `json:"data"` } type responseBodyAccounts struct { Status string `json:"status"` Message string `json:"message"` Data []investmentV1.AccountData `json:"data"` } type responseBodyScheme struct { Status string `json:"status"` Message string `json:"message"` Data []investmentV1.SchemeData `json:"data"` } type responseBodyWithdrawal struct { Status string `json:"status"` Message string `json:"message"` Data []investmentV1.WithdrawalList `json:"data"` } type responseBodyTransactions struct { Status string `json:"status"` Message string `json:"message"` Data []investmentV1.TransactionData `json:"data"` } type responseBodyInvestors struct { Status string `json:"status"` Message string `json:"message"` Data []investmentV1.InvestorsData `json:"data"` } type responseProfile struct { Status string `json:"status"` Message string `json:"message"` UserDetails eloquent.UserDetails `json:"data"` } type responseNominee struct { Status string `json:"status"` Message string `json:"message"` Data eloquent.Nominee `json:"data"` } type responseBank struct { Status string `json:"status"` Message string `json:"message"` Data []eloquent.BankData `json:"data"` } func GenerateOTP() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") log.Info().Msg("GenerateOTP called") sentry.CaptureMessage("GenerateOTP called") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.OTPManagerTable err := json.Unmarshal(reqBody, &elo.Data) if err != nil { log.Info().Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Info().Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(errorValidation(err)) return } _, err = elo.GenerateOTP() if err != nil { log.Info().Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } log.Info().Msg("OTP sent successfully") server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "OTP sent successfully"}) } } func VerifyPassword() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") log.Info().Msg("verify password called") sentry.CaptureMessage("verify password called") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.PasswordManagerTable err := json.Unmarshal(reqBody, &elo.Data) if err != nil { log.Info().Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Info().Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(errorValidation(err)) return } res, err := elo.VerifyPassword() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } log.Print("Successfully loginned") server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Password validated successfully", Token: res.Token}) } } func VerifyOTP() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.OTPManagerTable err := json.Unmarshal(reqBody, &elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(errorValidation(err)) return } res, err := elo.VerifyOTP() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } if len(elo.Data.Service.ServiceType) > 0 && len(elo.Data.Service.ReferenceCode) > 0 { withdrawal, user := getWithdrawalData(elo.Data.Service.ReferenceCode) sendWithdrawalEmail(withdrawal, user, "") server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Withdrawal request verified successfully"}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "OTP validate successfully", Token: res.Token}) } } func ValidatePAN() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.ValidatePANDetails elo.Auth.ID = auth.GetAuth(req) err := json.Unmarshal(reqBody, &elo.Data) log.Print("Auth", elo.Auth.ID) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(errorValidation(err)) return } if len(elo.Data.PAN) != 10 { log.Print("Invalid PAN") server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Invalid PAN"}) return } resDataItem, errCheck, boolV := elo.ValidatePAN() if errCheck != nil { log.Print(errCheck.Error()) if boolV == true { errMessage := errCheck.Error() statusMessage := "OK" statusCode := 200 if resDataItem.Status == "INVALID" { errMessage = resDataItem.Status statusMessage = "FAIL" statusCode = 400 server_handlers.FormatResponse(w, statusCode) _ = json.NewEncoder(w).Encode(responseBody{Status: statusMessage, Message: errMessage}) } server_handlers.FormatResponse(w, statusCode) _ = json.NewEncoder(w).Encode(responseBodyPan{Status: statusMessage, Message: errMessage, Data: resDataItem}) } else { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) } return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBodyPan{Status: "OK", Message: "PAN validated successfully", Data: resDataItem}) } } func CreateUser() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.User elo.Auth.ID = auth.GetAuth(req) err := json.Unmarshal(reqBody, &elo.Data) elo.Data.Aadhaar = aes.EncryptData(elo.Data.Aadhaar) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } _, err = elo.CreateUser() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } currentTime := time.Now() dateTime := currentTime.Format("Mon 2006-01-2 15:04:05") HtmlBody := "
" + "
" + "" + "" + "" + "
" + "
" + "CreditFair Capital" + "
·New Registration
" + "" + "" + "" + "
" + "
New registration is received from " + elo.Data.FirstName + " " + elo.Data.MiddleName + " " + elo.Data.LastName + "
" + "
" + dateTime + "
" + "" + "" + "" + "
" + "
Registration Summary ·
" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
Name" + elo.Data.FirstName + " " + elo.Data.MiddleName + " " + elo.Data.LastName + "
Email" + elo.Data.Email + "
" + "
" log.Print("trigger email, new user registration") email := "" cc := "" _, err = mail.Email(HtmlBody, fmt.Sprint(elo.Data.FirstName, " ", elo.Data.MiddleName, " ", elo.Data.LastName, " New User Registration ", dateTime), email, cc) if err != nil { log.Print("trigger email error") log.Print(err.Error()) } params := map[string]interface{}{ "Name": fmt.Sprint(elo.Data.FirstName, " ", elo.Data.MiddleName, " ", elo.Data.LastName), "email": elo.Data.Email, "Date": dateTime, } slack.SendSlack("New User Registration", params) server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "User successfully registered"}) } } func EsignAgreement() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.UserSignAgreement user := auth.GetUser(req) elo.Auth.ID = user.ID elo.Auth.UserType, _ = strconv.ParseInt(user.UserType, 10, 64) elo.Data.Name = user.Name err := json.Unmarshal(reqBody, &elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } _, err = elo.EsignAgreement() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "User Agreement signed successfully"}) } } func UploadDocument() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.DocumentUpload elo.Auth.ID = auth.GetAuth(req) err := json.Unmarshal(reqBody, &elo.Data) //log.Print(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } _, err = elo.UploadDocument() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Document uploaded successfully"}) } } func GetPincodeDetails() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) log.Print(reqBody) log.Print(req.URL.Query()["pincode"]) for k, v := range req.URL.Query() { fmt.Printf("%s: %s\n", k, v) } var elo eloquent.PincodeDetails elo.Auth.ID = auth.GetAuth(req) if len(req.URL.Query()["pincode"]) == 0 { log.Print("Pincode is required") server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Pincode is required"}) return } elo.Data.Pincode = req.URL.Query()["pincode"][0] validate := validator.New() err := validate.Struct(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } res, errPincode := elo.GetPincodeDetails() if errPincode != nil { log.Print(errPincode.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errPincode.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBodyPincode{Status: "OK", Message: "Pincode detailed fetched successfully", Data: res}) } } func AddBank() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.Bank //elo.Auth.ID = auth.GetAuth(req) user := auth.GetUser(req) elo.Auth.ID = user.ID err := json.Unmarshal(reqBody, &elo.Data) log.Print(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } _, err = elo.AddBank() if err != nil { if err.Error() != "Bank already updated" { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } } if user.UserType != "3" { var lend lendbox.BankDetails lend.Auth.ID = elo.Auth.ID lend, err = lend.GetLendboxBankDetails() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } log.Print("calling lendbox bank") _, err = lend.ShareBankDetails() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } elo.UpdateBankDetailsLendboxFlag() } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Bank added successfully"}) } } func UpdateAddress() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.UpdateAddress user := auth.GetUser(req) if len(user.LbUserId) > 0 { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Address already updated"}) return } elo.Auth.ID = user.ID err := json.Unmarshal(reqBody, &elo.Data) log.Print(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } _, err = elo.UpdateAddress() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } if user.UserType != "3" { var lend lendbox.InvestorRegistration lend.Auth.ID = elo.Auth.ID lend, err = lend.GetInvestorRegistrationDetails() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } log.Print("RegisterInvestor : lend.RegisterInvestor()") _, err = lend.RegisterInvestor() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Address updated successfully"}) } } func CreateLendboxUser() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") type LendBoxUser struct { ID string `json:"id"` } var lendBoxUser LendBoxUser var lend lendbox.InvestorRegistration reqBody, _ := ioutil.ReadAll(req.Body) err := json.Unmarshal(reqBody, &lendBoxUser) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } lend.Auth.ID = lendBoxUser.ID lend, err = lend.GetInvestorRegistrationDetails() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } log.Print("RegisterInvestor : lend.RegisterInvestor()") _, err = lend.RegisterInvestor() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Address updated successfully"}) } } func CreateLendboxWalletOrderDetails() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") type LendBoxUser struct { TransactionID string `json:"transaction_id"` AccountID int64 `json:"account_id"` PaymentLinkReferenceId string `json:"payment_link_reference_id"` PaymentPaymentId string `json:"payment_payment_id"` } var lendBoxUser LendBoxUser reqBody, _ := ioutil.ReadAll(req.Body) err := json.Unmarshal(reqBody, &lendBoxUser) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } //--- //Wallet Order API Start var walletOrder []lendbox.WalletOrderData var walletOrderData lendbox.WalletOrderData var wallet lendbox.WalletOrderDataResult walletOrderData, err = walletOrderData.GetTransactionDetails(lendBoxUser.AccountID, lendBoxUser.TransactionID) if err != nil { log.Print("lendbox GetTransactionDetails - error", err) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Payment Verification Failed"}) return } walletOrder = append(walletOrder, walletOrderData) wallet, err = lendbox.InitiateWalletOrder(walletOrder) if err != nil { log.Print("lendbox wallet order res - error", wallet) /*server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Payment Verification Failed"}) return*/ } log.Print("lendbox wallet order res", wallet) //Wallet Order API END _, err = investmentV1.PaymentTransactionLogsUpdate(lendBoxUser.PaymentLinkReferenceId, lendBoxUser.PaymentPaymentId, "SUCCESS") if err != nil { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Payment Verification Failed"}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Payment Verified Successfully"}) return //--- } } func CreateLendboxUserBankDetails() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") type LendBoxUser struct { ID string `json:"id"` } var lendBoxUser LendBoxUser var elo eloquent.Bank var lend lendbox.BankDetails reqBody, _ := ioutil.ReadAll(req.Body) err := json.Unmarshal(reqBody, &lendBoxUser) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } lend.Auth.ID = lendBoxUser.ID lend, err = lend.GetLendboxBankDetails() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } log.Print("calling lendbox bank") _, err = lend.ShareBankDetails() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } elo.UpdateBankDetailsLendboxFlag() server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Bank added successfully"}) } } func GetUser() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var elo eloquent.UserDetails ID := auth.GetAuth(req) log.Print("USER") log.Print(ID) profile, err := elo.GetUser(ID) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseProfile{Status: "OK", Message: "Profile details fetched", UserDetails: profile}) } } func GetInvestorDetails() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var elo eloquent.UserDetails var id string var _id_int int64 user := auth.GetUser(req) if req.URL.Query()["id"] != nil { id = req.URL.Query()["id"][0] _id_int, _ = strconv.ParseInt(req.URL.Query()["id"][0], 10, 64) } if id != "" && user.UserType == "3" { investors := investmentV1.GetInvestorListUnderIFA(user.ID) if len(investors) == 0 { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Investor not found"}) return } var hasInvestor bool for _, _id := range investors { if _id == _id_int { hasInvestor = true break } } if hasInvestor != true { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Investor not found"}) return } profile, err := elo.GetUser(id) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseProfile{Status: "OK", Message: "Profile details fetched", UserDetails: profile}) } else { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Please select the investor"}) } } } func GetAgreementLink() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") w.Header().Add("Content Type", "text/html") user := auth.GetUser(req) agreement := "module_server/handlers/templates/agreement.tmpl" if user.UserType == "3" { var investorId int64 if req.URL.Query()["id"] != nil { investorId, _ = strconv.ParseInt(req.URL.Query()["id"][0], 10, 64) } if investorId > 0 { if investmentV1.IsInvestorUnderIFA(user.ID, investorId) > 0 { user.Name = eloquent.GetInvestor(investorId) }else{ server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Please select the investor"}) return } }else{ agreement = "module_server/handlers/templates/agreement_ifa.tmpl" } } t, err := template.ParseFiles(agreement) if err != nil { log.Error().Err(err).Msg(err.Error()) } t.Execute(w, user) // schema := "https://" // if req.TLS == nil { // schema = "http://" // } // // id, errChek := strconv.ParseInt(ID, 10, 64) // if errChek != nil { // fmt.Printf(errChek.Error()) // } // link := schema + req.Host + "/agreement/" + getEncodedString(id) // server_handlers.FormatResponse(w, 200) // _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Agreement link fetched", Link: link}) } } // MakeTransaction func MakeTransaction() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.MakeTransaction elo.Auth.ID = auth.GetAuth(req) err := json.Unmarshal(reqBody, &elo.Data) log.Print(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } _, err = elo.MakeTransaction() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Successfully sent the request for transaction"}) } } func InvestmentWebhookUpdate() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) log.Print(reqBody) server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Payment Verified Successfully"}) return } } func InvestmentWebhook() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { /*var investmentStruct investmentV1.InvestWebhook log.Print(req.URL.Query()) err := decoder.Decode(&investmentStruct, req.URL.Query()) if err != nil { log.Print("Error in GET parameters : ", err) } else { log.Print("GET parameters : ", investmentStruct) }*/ w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var investmentStruct investmentV1.InvestWebhook err := json.Unmarshal(reqBody, &investmentStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } log.Print(investmentStruct) if err != nil || investmentStruct.PaymentLinkStatus != "paid" { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Payment Verification Failed"}) return } params := map[string]interface{}{ "payment_link_id": investmentStruct.RazorpayPaymentLinkId, "razorpay_payment_id": investmentStruct.PaymentPaymentId, "payment_link_reference_id": investmentStruct.PaymentLinkReferenceId, "payment_link_status": investmentStruct.PaymentLinkStatus, "signature": investmentStruct.Signature, } investmentStruct.AccountId = investmentV1.GetAccountIDByReference(investmentStruct.PaymentLinkReferenceId) status := razorpay.Verify(params) if status && investmentStruct.AccountId > 0 { var payments []investmentV1.PaymentConfirmation var payment investmentV1.PaymentConfirmation payment.TransactionID = investmentStruct.PaymentLinkReferenceId payment.AccountId = investmentStruct.AccountId payments = append(payments, payment) investmentV1.UpdatePayment(payments, false) //Wallet Order API Start var walletOrder []lendbox.WalletOrderData var walletOrderData lendbox.WalletOrderData var wallet lendbox.WalletOrderDataResult for i := 0; i < len(payments); i++ { walletOrderData, err = walletOrderData.GetTransactionDetails(payments[i].AccountId, payments[i].TransactionID) if err != nil { log.Print("lendbox GetTransactionDetails - error", err) break } } walletOrder = append(walletOrder, walletOrderData) wallet, err = lendbox.InitiateWalletOrder(walletOrder) if err != nil { log.Print("lendbox wallet order res - error", wallet) /*server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Payment Verification Failed"}) return*/ } log.Print("lendbox wallet order res", wallet) //Wallet Order API END _, err := investmentV1.PaymentTransactionLogsUpdate(investmentStruct.PaymentLinkReferenceId, investmentStruct.PaymentPaymentId, "SUCCESS") if err != nil { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Payment Verification Failed"}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Payment Verified Successfully"}) return } else { if status && investmentStruct.AccountId == 0 { log.Print("Payment Already Verified - ", investmentStruct.PaymentLinkReferenceId) } else { investmentV1.PaymentTransactionLogsUpdate(investmentStruct.PaymentLinkReferenceId, investmentStruct.PaymentPaymentId, "FAILURE") } server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Payment Verification Failed"}) return } } } func Invest() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var investmentStruct investmentV1.Invest user := auth.GetUser(req) investmentStruct.Auth.ID = user.ID err := json.Unmarshal(reqBody, &investmentStruct) log.Print(investmentStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } if user.UserType == "3" { if investmentStruct.InvestorID == 0 { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Please select the investor"}) return } } if investmentStruct.Amount < 10000 || investmentStruct.Amount > 5000000 { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Amount should not be less than 10K and greater than 50L."}) return } if investmentStruct.Auth.ID == "850"{ investmentStruct.Amount = 10 } validate := validator.New() err = validate.Struct(investmentStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } investmentPayment, errPay := investmentStruct.Invest() if errPay != nil { log.Print(errPay) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errPay.Error()}) return } payoutType := "Cumulative" if investmentStruct.PayoutType == 1 { payoutType = "Monthly" } paymentGateway := "Offline" if investmentStruct.PaymentGateway == 1 { paymentGateway = "Razorpay" } paymentMethod := "Online" if investmentStruct.PaymentMethod == 2 { paymentMethod = "Offline" paymentGateway = "Offline" } currentTime := time.Now() dateTime := currentTime.Format("Mon 2006-01-2 15:04:05") schemeName := investmentStruct.GetSchemeDetails() var LbUserId = user.LbUserId var name = user.Name var email = user.Email var phone = user.Phone if investmentStruct.InvestorID != 0 { var id = strconv.Itoa(investmentStruct.InvestorID) var userDetails eloquent.UserDetails UserDetails, err := userDetails.GetUser(id) if err != nil { log.Print(err) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Investor not found"}) return } else { LbUserId = UserDetails.PersonalDetails.LbUserId name = strings.TrimSpace(UserDetails.PersonalDetails.FirstName + " " + UserDetails.PersonalDetails.MiddleName + " " + UserDetails.PersonalDetails.LastName) email = UserDetails.PersonalDetails.Email phone = UserDetails.PersonalDetails.Phone } } HtmlBody := "
" + "
" + "" + "" + "" + "
" + "
" + "CreditFair Capital" + "
·Investment
" + "" + "" + "" + "
" + "
We have received the investment request from " + name + " Below is the details of investment
" + "
" + dateTime + "
" + "" + "" + "" + "
" + "
Investment Summary ·
" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
LbUserId" + LbUserId + "
Amount" + fmt.Sprintf("%.2f", investmentStruct.Amount) + "
Scheme" + schemeName + "
Gateway" + paymentGateway + "
Payment Method" + paymentMethod + "
PayoutType" + payoutType + "
ReferenceCode" + investmentPayment.ReferenceCode + "
Transaction Reference" + investmentPayment.TransactionReference + "
" + "
" log.Print("trigger email internal") _, err = mail.Email(HtmlBody, fmt.Sprint(LbUserId, " Request received for investment ", dateTime), "", "info@creditfaircapital.in") if err != nil { log.Print("trigger email error") log.Print(err.Error()) } params := map[string]interface{}{ "lbUserId": LbUserId, "investment_date": dateTime, "ReferenceCode": investmentPayment.ReferenceCode, "TransactionReference": investmentPayment.TransactionReference, } slack.SendSlack("New Investment", params) if investmentStruct.PaymentMethod == 2 { server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Invested"}) //_ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Payment link fetched", Link: "https://razorpay.com/payment-link/plink_K9mw6Fiqf3wtZ0/test"}) return } /* investmentPayment, errPay := investmentStruct.Invest() if errPay != nil { log.Print(errPay) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errPay.Error()}) return } */ log.Print("Preparing requestBody") /*investmentStruct.Callback = "https://devapp.creditfaircapital.in/investment/payment-confirmation" if user.UserType == "3" && investmentStruct.InvestorID > 0 { investmentStruct.Callback = "https://devifa.creditfaircapital.in/investment/payment-confirmation" }*/ var api razorpay.Payment api = getPaymentStruct(investmentStruct, investmentPayment, LbUserId, name, phone, email) output, errCheck := api.GetLink() if errCheck != nil { log.Print(errCheck) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } outputRes := output.(map[string]interface{}) log.Print("outputRes") b, errw := json.Marshal(outputRes) if errw != nil { log.Print(errw) } fmt.Println(string(b)) log.Print(outputRes["short_url"]) investmentStruct.PaymentTransactionLogs(investmentPayment) log.Print("checking IFA ") log.Print(user) log.Print(investmentStruct.InvestorID) if user.UserType == "3" && investmentStruct.InvestorID > 0 { log.Print("checking IFA passed") HtmlBody = "
\n
\n\n
\n\n\n\n\n\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n
\n\n\n\n\n\n\n
\n\n
\n
\n
\nHello " + name + ",

\nI hope everything is going well for you.

\nYour payment link for your investment in Credit Fair Capital P2P is " + outputRes["short_url"].(string) + ".

\nYou can contact us at info@creditfaircapital.in or 8097400463 at any time since we are here to help you.

\nOnce again, thank you for trusting your faith in Credit Fair Capital.
\n
\n
\n Regards,
\n Team Credit Fair Capital
\n info@creditfaircapital.in
\n +91-8097400463

\n

Disclaimer: This message (including any attachments) may contain\n confidential, proprietary, privileged and/or private information. The information is intended for the use only\n to whom they are addressed. If you are not the intended recipient of this message, please notify the sender\n immediately, and delete the message and any attachments. Any disclosure, reproduction, distribution or other use\n of this message or any attachments by an individual or entity other than the intended recipient is prohibited.We\n do not guarantee the security of any information transmitted through email and, are not liable for any\n interception, corruption or delays. Anyone communicating with us by email accepts the risks and\n consequences.\n

\n
\n \n
\n
\n
\n
\n

\n
\n
\n
\n
\n
" log.Print("trigger email to investor") _, err = mail.Email(HtmlBody, fmt.Sprint("Credit fair Capital Requesting payment of INR "+fmt.Sprintf("%.2f", investmentStruct.Amount)+" (via Razorpay)"), email, "") if err != nil { log.Print("trigger email error") log.Print(err.Error()) } message := fmt.Sprint("From=KMCRED&To=91", phone, "&Body=Hello%20", name, "%2C%20your%20payment%20link%20for%20your%20investment%20in%20Credit%20Fair%20Capital%20is%20", outputRes["short_url"].(string), "%20Team%20Credit%20Fair%20Capital&DltEntityId=1301159108481756475&DltTemplateId=1307168007701785729&SmsType=transactional") _, err = eloquent.TriggerSMS(message) if err != nil { log.Print("SMS Error") log.Print(err.Error()) } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Link sent to investor successfully", ReferenceCode: investmentPayment.ReferenceCode}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Payment link fetched", Link: outputRes["short_url"].(string)}) } } func getPaymentStruct(investmentStruct investmentV1.Invest, investmentPayment investmentV1.Invest, LbUserId, name string, phone string, email string) razorpay.Payment { var api razorpay.Payment api.Amount = investmentStruct.Amount * 100 api.ReceiptId = investmentPayment.ReferenceCode api.Notes1 = "cf-capital" api.Description = fmt.Sprint("Payment for transaction id #", investmentPayment.ReferenceCode, ", ID#", LbUserId) api.Notes2 = fmt.Sprint("Payment for investment id #", investmentPayment.InvestmentPaymentID) //api.Callback = investmentStruct.Callback var customer razorpay.Customer //user := auth.GetUser(req) customer.Name = name customer.Contact = aes.DecryptData(phone) customer.Email = aes.DecryptData(email) api.Customer = customer var plan razorpay.Plan plan.Name = fmt.Sprint("Flexi Plan # ", investmentStruct.MHP) api.Plan = plan return api } func Investment() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var investmentStruct investment.FlexiPlan //elo.Auth.ID = auth.GetAuth(req) err := json.Unmarshal(reqBody, &investmentStruct) log.Print(investmentStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(investmentStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } payout, errCheck := investmentStruct.Flexi() if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(payout) } } func AddNominee() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var nominee eloquent.Nominee nominee.AuthID = auth.GetAuth(req) err := json.Unmarshal(reqBody, &nominee) log.Print(nominee) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(nominee) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } _, errCheck := nominee.AddNominee() if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } var response = map[string]string{"status": "OK", "message": "Nominee added!"} server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(response) } } func GetNominee() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var nominee eloquent.Nominee nominee.AuthID = auth.GetAuth(req) nomineeData, errCheck := nominee.GetNominee() if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseNominee{Status: "OK", Message: "Nominee fetched", Data: nomineeData}) } } func GetBank() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var bank eloquent.Bank bank.Auth.ID = auth.GetAuth(req) bankData, errCheck := bank.GetBank() log.Print(bankData) if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBank{Status: "OK", Message: "Bank fetched", Data: bankData}) } } func Withdrawal() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var withdrawalStruct investmentV1.Withdrawal user := auth.GetUser(req) withdrawalStruct.Auth.ID = user.ID var err error withdrawalStruct.Auth.UserType, err = strconv.ParseInt(user.UserType, 10, 64) if err != nil { log.Print(err.Error()) } err = json.Unmarshal(reqBody, &withdrawalStruct) log.Print(withdrawalStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } if withdrawalStruct.Auth.UserType == 3 { if withdrawalStruct.InvestorID == 0 { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Please select the investor"}) return } } validate := validator.New() err = validate.Struct(withdrawalStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } withdrawal, errCheck := withdrawalStruct.Withdrawal() if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } withdrawalStruct.ReferenceCode = withdrawal.ReferenceCode var response map[string]string if withdrawalStruct.Auth.UserType != 3 || withdrawalStruct.InvestorID == 0 { sendWithdrawalEmail(withdrawalStruct, user, "") response = map[string]string{"status": "OK", "message": "You have successfully made a withdrawal from your portfolio. The amount withdrawn will reflect in your selected Bank account within 24hours."} } else { //sendWithdrawalEmail(withdrawalStruct, user, fmt.Sprintf(user.Email,",",os.Getenv("CFC_EMAIL_GROUP_TO"))) var OTPManagerTable eloquent.OTPManagerTable OTPManagerTable.Data.Phone = eloquent.GetInvestorPhoneNumber(withdrawalStruct.InvestorID) var service eloquent.Service service.ReferenceCode = withdrawal.ReferenceCode service.ServiceType = "WITHDRAW" OTPManagerTable.Data.Service = service eloquent.SendOTP(OTPManagerTable) response = map[string]string{"status": "OK", "message": "We have sent an OTP to investor. Please enter the OTP below", "transaction_id": withdrawal.ReferenceCode} } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(response) //_, errCheck := withdrawalStruct.Withdrawal() //if errCheck != nil { // log.Print(errCheck.Error()) // server_handlers.FormatResponse(w, 400) // _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) // return //} // //var response = map[string]string{"status": "OK", "message": "You have successfully made a withdrawal from your portfolio. The amount withdrawn will reflect in your selected Bank account within 24hours."} // //server_handlers.FormatResponse(w, 200) //_ = json.NewEncoder(w).Encode(response) } } func getWithdrawalData(referenceCode string) (investmentV1.Withdrawal, auth.AuthUser) { var withdrawal investmentV1.Withdrawal var InvestorID string InvestorID, withdrawal.AccountID, withdrawal.Reason, withdrawal.Amount = eloquent.GetPendingWithdrawal(referenceCode) withdrawal.ReferenceCode = referenceCode var user eloquent.UserDetails userDetails, err := user.GetUser(InvestorID) if err != nil { log.Print(err.Error()) } var authUser auth.AuthUser authUser.ID = InvestorID authUser.Name = strings.TrimSpace(userDetails.PersonalDetails.FirstName + " " + userDetails.PersonalDetails.MiddleName + " " + userDetails.PersonalDetails.LastName) authUser.LbUserId = userDetails.PersonalDetails.LbUserId withdrawal.Auth.ID = InvestorID return withdrawal, authUser } func sendWithdrawalEmail(withdrawalStruct investmentV1.Withdrawal, user auth.AuthUser, email string) { schemeName := withdrawalStruct.GetSchemeDetailsByAccount() currentTime := time.Now() dateTime := currentTime.Format("Mon 2006-01-2 15:04:05") HtmlBody := "
" + "
" + "" + "" + "" + "
" + "
" + "CreditFair Capital" + "
·Withdrawal
" + "" + "" + "" + "
" + "
We have received the withdrawal request from " + user.Name + " Below is the details of withdrawal
" + "
" + dateTime + "
" + "" + "" + "" + "
" + "
Withdrawal Summary ·
" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
LbUserId" + user.LbUserId + "
Amount" + fmt.Sprintf("%.2f", withdrawalStruct.Amount) + "
Scheme" + schemeName + "
Reason" + withdrawalStruct.Reason + "
ReferenceCode" + withdrawalStruct.ReferenceCode + "
" + "
" log.Print("trigger email") _, err := mail.Email(HtmlBody, fmt.Sprint(user.LbUserId, ": Request received for withdrawal ", dateTime), "", "") if err != nil { log.Print("trigger email error") log.Print(err.Error()) } params := map[string]interface{}{ "request": "Withdrawal", "Name": user.Name, "Withdrawal": dateTime, "Amount": fmt.Sprintf("%.2f", withdrawalStruct.Amount), } slack.SendSlack("Withdrawal Request", params) } func GetPassbook() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var passbookStruct investmentV1.Passbook passbookStruct.Auth.ID = auth.GetAuth(req) //passbookStruct.StartDate = req.URL.Query()["start_date"][0] //passbookStruct.EndDate = req.URL.Query()["end_date"][0] //passbookStruct.Auth.ID = auth.GetAuth(req) //log.Print(passbookStruct) //account_id, err := strconv.ParseInt(req.URL.Query()["account_id"][0], 10, 64) //if err != nil { // log.Error().Err(err).Msg(err.Error()) //} //passbookStruct.AccountID = account_id err := json.Unmarshal(reqBody, &passbookStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } log.Print(passbookStruct) validate := validator.New() err = validate.Struct(passbookStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } passbookItems, errCheck := passbookStruct.GetPassbook() if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBodyPassbook{Status: "OK", Message: "Passbook fetched", Data: passbookItems}) } } func GetAccounts() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var accountStruct investmentV1.Accounts //accountStruct.AccountID = req.URL.Query()["account_id"][0] accountStruct.Auth.ID = auth.GetAuth(req) var err error var account_id int64 var investor_id int64 if req.URL.Query()["account_id"] != nil { account_id, err = strconv.ParseInt(req.URL.Query()["account_id"][0], 10, 64) } if req.URL.Query()["id"] != nil { investor_id, err = strconv.ParseInt(req.URL.Query()["id"][0], 10, 64) } if err != nil { log.Error().Err(err).Msg(err.Error()) } accountStruct.AccountID = account_id accountStruct.InvestorID = investor_id validate := validator.New() err = validate.Struct(accountStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } accountsItems, errCheck := accountStruct.GetAccounts() if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBodyAccounts{Status: "OK", Message: "Accounts fetched", Data: accountsItems}) } } func GetSchemes() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var schemeStruct investmentV1.Schemes //accountStruct.AccountID = req.URL.Query()["account_id"][0] schemeStruct.Auth.ID = auth.GetAuth(req) var err error var scheme_id int64 var payout_id int64 var investor_id int64 if req.URL.Query()["scheme_id"] != nil { scheme_id, err = strconv.ParseInt(req.URL.Query()["scheme_id"][0], 10, 64) } if req.URL.Query()["payout_id"] != nil { payout_id, err = strconv.ParseInt(req.URL.Query()["payout_id"][0], 10, 64) } if req.URL.Query()["id"] != nil { investor_id, err = strconv.ParseInt(req.URL.Query()["id"][0], 10, 64) } if err != nil { log.Error().Err(err).Msg(err.Error()) } schemeStruct.SchemeID = scheme_id schemeStruct.PayoutID = payout_id schemeStruct.InvestorID = investor_id PolicyType := "C" if schemeStruct.PayoutID == 1 { PolicyType = "M" } schemeStruct.PayoutType = PolicyType validate := validator.New() err = validate.Struct(schemeStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } accountsItems, errCheck := schemeStruct.GetSchemes() if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBodyScheme{Status: "OK", Message: "Scheme fetched", Data: accountsItems}) } } func GetDashboard() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") dashboard, err := getDashboardDetails(auth.GetAuth(req)) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(dashboard) } } func getDashboardDetails(ID string) (investmentV1.Dashboard, error) { var dashboard investmentV1.Dashboard var investmentSummaryStruct investmentV1.InvestmentSummary totalPortfolioValueAvailableForWithdrawal, investmentSummary, totalPortfolioValue, totalInvestment, totalWithdrawal, totalInterestPaidOut, totalInterestAccrued, totalInterest, err := investmentSummaryStruct.GetInvestmentSummary(ID) if err != nil { return dashboard, err } //var dashboard investmentV1.Dashboard dashboard.TotalInvestment = totalInvestment dashboard.AmountRepaid = totalWithdrawal dashboard.NetPrincipalInvestment = totalInvestment //dashboard.Portfolio = (totalInvestment + totalInterestAccrued) - totalWithdrawal dashboard.Portfolio = totalPortfolioValue dashboard.PortfolioValueAvailableForWithdrawal = totalPortfolioValueAvailableForWithdrawal var interestSummary investmentV1.InterestSummary interestSummary.CapitalisedInterest = 0 interestSummary.InterestPaidOut = totalInterestPaidOut interestSummary.AccruedInterest = totalInterestAccrued interestSummary.TotalInterest = totalInterest var estimatedReturnSummary investmentV1.EstimatedReturnSummary estimatedReturnSummary.AbsoluteReturnRupee = 0 estimatedReturnSummary.AbsoluteReturnPercentage = 0 estimatedReturnSummary.AnnualisedReturn = 0 //var investmentSummaryItems []investmentV1.InvestmentSummary //var investmentSummary investmentV1.InvestmentSummary //investmentSummary.Scheme = "Flexi - 2 days - Cumulative Payout" //investmentSummary.DateOfInvestment = "2022-09-10" //investmentSummary.Amount = 10000 //investmentSummary.MAX_XIRR = 8 //investmentSummary.InterestRepaid = 0 //investmentSummary.PrincipalRepaid = 0 //investmentSummary.TotalRepaid = 0 //investmentSummary.PortfolioValue = 10095 //investmentSummary.EndDate = "2023-09-10" //investmentSummaryItems = append(investmentSummaryItems, investmentSummary) //var investmentSummary investmentSummary /*investmentSummary.Scheme = "Flexi - 2 days - Cumulative Payout" investmentSummary.DateOfInvestment = "2022-09-10" investmentSummary.Amount = 10000 investmentSummary.MAX_XIRR = 8 investmentSummary.InterestRepaid = 0 investmentSummary.PrincipalRepaid = 0 investmentSummary.TotalRepaid = 0 investmentSummary.PortfolioValue = 10095 investmentSummary.EndDate = "2023-09-10"*/ //investmentSummaryItems = append(investmentSummaryItems, investmentSummary) dashboard.InterestSummary = interestSummary dashboard.EstimatedReturnSummary = estimatedReturnSummary dashboard.InvestmentSummary = investmentSummary return dashboard, nil } func InvestmentV1() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var investmentStruct investmentV1.FlexiPlan //elo.Auth.ID = auth.GetAuth(req) err := json.Unmarshal(reqBody, &investmentStruct) log.Print(investmentStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(investmentStruct) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } payout, errCheck := investmentStruct.FlexiV1() if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(payout) } } func GetFrequentlyAskedQuestions() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var questions = []map[string]string{ {"question": "What documents are required to register as an investor/lender?", "answer": "To register as an investor, you must give basic KYC information such as your Aadhar card, Pan card, and bank account details"}, {"question": "What is the average loan size of Credit Fair?", "answer": "The loan provided starts from minimum of Rs. 10,000 with maximum limit of up to Rs. 20 lakhs. It depends from segment to segment and we consider the average across all the segments, the loan size is somewhere close to Rs. 75,000"}, {"question": "What is your NPA and how much interest rate do you charge on loans?", "answer": "We have a dedicated Collections team that has helped us keep the NPA under 0.75%. The rate of interest on loans charged to borrowers ranges between 16% to 28%"}, {"question": "What are the various P2P plans and how much advisory fees can we expect?", "answer": "We have 2 types of P2P Investment plans:\ni) Flexi: This investment plan is for the customers who are looking for short term investment. There is no lock-in period in this plan. Rate of interest offered for Flexi plan is at 9% per annum.\nii)Fixed: This investment plan comes with the benefit of higher returns. The customer can choose from a lock-in period of 6, 12 and 24 months. Rate of interest offered ranges between 9.5% to 11.5% per annum based on the lock-in period selected."}, {"question": "Is my interest income through Credit Fair Capital taxable?", "answer": "All repayments you receive from borrowers on Credit Fair Capital will be without any tax deduction. Earnings will be taxable as per the income tax rules that apply to you."}, {"question": "Are there any registration charges?", "answer": "There are no registration charges to the investor. In fact, we have no additional charge whatsoever while onboarding the investor."}, {"question": "How safe is the investment?", "answer": "P2P investments have an element of risk and are directly affected by the Non-performing Asset (NPA) of the Portfolio. P2P investment with Credit Fair Capital comes with low risk and high returns due to diversification of the portfolio across hundreds of borrowers, sourcing prime borrowers, monitoring end use and in-house collections team. With the efforts of the dedicated Collections team at Credit Fair Capital, the NPA is below 0.75% as compared to industry standards at 3% to 10%."}, {"question": "In how many borrowers the investor's money is divided into?", "answer": "The number of borrowers depends on the amount invested and the minimum is 50 borrowers. The borrowers are required to have a minimum of 700 as the CIBIL score."}, {"question": "Can NRIs also invest in Credit Fair Capital?", "answer": "Yes, even NRIs can invest in Credit Fair Capital with the same documentation process. But they can participate only with their NRO accounts."}, {"question": "What are NCDs?", "answer": "Non-convertible debentures (NCDs) are fixed-income instruments, usually issued by high-rated companies in the form of a public or a private issue. They usually provide higher returns than bank fixed deposits."}, {"question": "What is P2P lending?", "answer": "P2P lending is a form of crowd-funding used to raise loans which are paid back with interest. It can be defined as the use of an online platform that matches lenders with borrowers. As per current RBI regulations, these platforms can facilitate only unsecured loans."}, {"question": "As a partner, how to register with Credit Fair Capital?", "answer": "In order to register yourself as a partner with Credit Fair Capital you can contact us at info@creditfaircapital.in"}, {"question": "As an investor, how to register with Credit Fair Capital?", "answer": "In order to register as an investor you can visit on our website https://creditfaircapital.in/ and click on Login/Register"}, {"question": "Are there any eligibility criteria for registration?", "answer": "You should be at least 18 years of age or older to be eligible to register as a partner or an investor with Credit Fair Capital."}, {"question": "Can I apply for more than one investment?", "answer": "Yes, you can invest multiple times but the limit for a P2P investor is 50 lakhs according to the existing RBI guidelines."}, {"question": "What are the benefits of having multiple investments?", "answer": "We enable better matching of your investments to your financial goals via multiple investments. \nFor example: if you need to travel in 6 months, you can invest in our 6 months product but if you need the money in 2 years, you can invest longer term."}, {"question": "How do I change or update my mobile number, email ID, bank account details associated with my account?", "answer": "There is no provision to change or update the mobile number associated with your account with Credit Fair Capital. To update your email address, bank account details, you can reach us at info@creditfaircapital.in"}, {"question": "How do I withdraw funds post maturity?", "answer": "Write to us at info@creditfaircapital.in for request of withdrawal on maturity."}, {"question": "What kind of reports are available?", "answer": "Statement of accounts, investment statements, tax reports are types of reports available for your investment."}, {"question": "Why should you consider investing at Credit Fair Capital?", "answer": "One should consider investing in Credit Fair Capital as we provide higher returns with low risk comparative to other investing firms."}, } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(questions) } } // Test - delete entire user func DeleteUser() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") //reqBody, _ := ioutil.ReadAll(req.Body) var user testUser.User user.ID = auth.GetAuth(req) //err := json.Unmarshal(reqBody, &user) log.Print(user) log.Print("DeleteUser") user.DeleteUser() var userRes = []map[string]string{{"Status": "OK", "message": "Deleted"}} server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(userRes) } } func GetWithdrawalList() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var withdrawalList investmentV1.WithdrawalList //accountStruct.AccountID = req.URL.Query()["account_id"][0] withdrawalList.Auth.ID = auth.GetAuth(req) var err error if err != nil { log.Error().Err(err).Msg(err.Error()) } validate := validator.New() err = validate.Struct(withdrawalList) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } withdrawalItems, errCheck := withdrawalList.GetWithdrawalList() if errCheck != nil { log.Print(errCheck.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBodyWithdrawal{Status: "OK", Message: "Withdrawal list fetched", Data: withdrawalItems}) } } func GetInvestmentList() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { } } func GetRecentTransaction() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var transactions investmentV1.Transactions transactions.Auth.ID = auth.GetAuth(req) transactionsData, err := transactions.GetRecentTransaction() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to fetch the data"}) return } if len(transactionsData) == 0 { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "No recent transaction found"}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBodyTransactions{Status: "OK", Message: "List of recent transaction", Data: transactionsData}) } } func GetInvestors() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var investors investmentV1.Investors investors.Auth.ID = auth.GetAuth(req) investorsData, err := investors.GetInvestors() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to fetch the data"}) return } if len(investorsData) == 0 { server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "No investor found"}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBodyInvestors{Status: "OK", Message: "List of investors", Data: investorsData}) } } func GetPortfolio() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") var investors investmentV1.InvestorPortfolio var err error if req.URL.Query()["id"] != nil { investors.ID = req.URL.Query()["id"][0] } dashboard, err := getDashboardDetails(investors.ID) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(dashboard) } } func PaymentConfirmation() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) var payment []investmentV1.PaymentConfirmation err := json.Unmarshal(reqBody, &payment) log.Print(payment) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } msg := investmentV1.UpdatePayment(payment, true) var test map[string]string if len(msg) == 0 { test = map[string]string{"Status": "OK", "Message": "Payment confirmed"} } else { test = map[string]string{"Status": "FAIL", "Message": "Payment confirmation failed"} } server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(test) } } func WithdrawalConfirmation() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") reqBody, _ := ioutil.ReadAll(req.Body) //a := []interface{}{1, 2, 3, 4, 5} //b := make([]int, len(a)) //amt := investmentV1.GetGainedInterestByDate("2022-10-01", b) //log.Print(amt) //return var withdrawal []investmentV1.WithdrawalConfirmation err := json.Unmarshal(reqBody, &withdrawal) log.Print(withdrawal) if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } investmentV1.UpdateWithdrawal(withdrawal) var test = map[string]string{"Status": "OK", "Message": "Withdrawal confirmed"} server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(test) } } func ForgotPassword() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") log.Info().Msg("forgot password called") sentry.CaptureMessage("forgot password called") reqBody, _ := ioutil.ReadAll(req.Body) var url = req.Header["Origin"] var elo eloquent.ForgotPasswordTable err := json.Unmarshal(reqBody, &elo.Data) elo.Data.Host = url[0] if err != nil { log.Info().Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo.Data) if err != nil { log.Info().Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(errorValidation(err)) return } res, err := elo.ForgotPassword() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } log.Print(res) log.Print("Forgot Password successful") server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Reset password link to the registered email address"}) } } func SubmitPassword() http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") log.Info().Msg("Submit password called") sentry.CaptureMessage("Submit password called") reqBody, _ := ioutil.ReadAll(req.Body) var elo eloquent.SubmitPassword err := json.Unmarshal(reqBody, &elo) if err != nil { log.Info().Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: "Unable to parse the data"}) return } validate := validator.New() err = validate.Struct(elo) if err != nil { log.Info().Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(errorValidation(err)) return } res, err := elo.SubmitPassword() if err != nil { log.Error().Err(err).Msg(err.Error()) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) return } log.Print(res) log.Print("Submit Password successful") server_handlers.FormatResponse(w, 200) _ = json.NewEncoder(w).Encode(responseBody{Status: "OK", Message: "Password changed successfully", Token: res.Token}) } } func errorValidation(err error) []ApiError { var ve validator.ValidationErrors if errors.As(err, &ve) { out := make([]ApiError, len(ve)) for i, fe := range ve { out[i] = ApiError{"FAIL", msgForTag(fe)} } return out } return nil } func msgForTag(fe validator.FieldError) string { log.Print(fe.Tag()) switch fe.Tag() { case "required": return fe.Field() + " field is required" case fe.Tag(): return "Invalid field " + fe.Tag() } return fe.Error() // default error }