package handlers import ( Parser "cf-experian/module_server/handlers/Parser" server_handlers "cf-server/server/handlers" "encoding/json" "encoding/xml" "fmt" "io/ioutil" "net/http" "os" "strings" "time" log "github.com/rs/zerolog/log" ) const ( URL = `https://connectuat.experian.in/nextgen-ind-pds-webservices-cbv2/endpoint` ) type responseBody struct { Status string `json:"status"` Message string `json:"message"` } type Identification struct { User string `xml:"XMLUser"` Password string `xml:"XMLPassword"` } type Application struct { FTReferenceNumber string `xml:"FTReferenceNumber"` CustomerReferenceID string `xml:"CustomerReferenceID"` EnquiryReason int `xml:"EnquiryReason"` FinancePurpose int `xml:"FinancePurpose"` AmountFinanced int `xml:"AmountFinanced"` DurationOfAgreement int `xml:"DurationOfAgreement"` ScoreFlag int `xml:"ScoreFlag"` PSVFlag string `xml:"PSVFlag"` } type Applicant struct { Surname string `xml:"Surname"` FirstName string `xml:"FirstName"` MiddleName1 string `xml:"MiddleName1"` MiddleName2 string `xml:"MiddleName2"` MiddleName3 string `xml:"MiddleName3"` GenderCode int `xml:"GenderCode"` IncomeTaxPAN string `xml:"IncomeTaxPAN"` PANIssueDate string `xml:"PANIssueDate"` PANExpirationDate string `xml:"PANExpirationDate"` PassportNumber string `xml:"PassportNumber"` PassportIssueDate string `xml:"PassportIssueDate"` PassportExpirationDate string `xml:"PassportExpirationDate"` VoterIdentityCard string `xml:"VoterIdentityCard"` VoterIDIssueDate string `xml:"VoterIDIssueDate"` VoterIDExpirationDate string `xml:"VoterIDExpirationDate"` DriverLicenseNumber string `xml:"DriverLicenseNumber"` DriverLicenseIssueDate string `xml:"DriverLicenseIssueDate"` DriverLicenseExpirationDate string `xml:"DriverLicenseExpirationDate"` RationCardNumber string `xml:"RationCardNumber"` RationCardIssueDate string `xml:"RationCardIssueDate"` RationCardExpirationDate string `xml:"RationCardExpirationDate"` UniversalIDNumber string `xml:"UniversalIDNumber"` UniversalIDIssueDate string `xml:"UniversalIDIssueDate"` UniversalIDExpirationDate string `xml:"UniversalIDExpirationDate"` DateOfBirth string `xml:"DateOfBirth"` STDPhoneNumber string `xml:"STDPhoneNumber"` PhoneNumber int `xml:"PhoneNumber"` TelephoneExtension string `xml:"TelephoneExtension"` TelephoneType string `xml:"TelephoneType"` MobilePhone int `xml:"MobilePhone"` EMailId string `xml:"EMailId"` } type Details struct { Income string `xml:"Income"` MaritalStatus string `xml:"MaritalStatus"` EmployStatus string `xml:"EmployStatus"` TimeWithEmploy string `xml:"TimeWithEmploy"` NumberOfMajorCreditCardHeld string `xml:"NumberOfMajorCreditCardHeld"` } type Address struct { FlatNoPlotNoHouseNo string `xml:"FlatNoPlotNoHouseNo"` BldgNoSocietyName string `xml:"BldgNoSocietyName"` RoadNoNameAreaLocality string `xml:"RoadNoNameAreaLocality"` City string `xml:"City"` Landmark string `xml:"Landmark"` State int `xml:"State"` PinCode int `xml:"PinCode"` } type AdditionalAddressFlag struct { Flag string `xml:"Flag"` } type Request struct { ID int `json:"id"` UserID int `json:"user_id"` FirstName string `json:"first_name"` LastName string `json:"last_name"` Gender int `json:"gender"` PAN string `json:"pan"` Amount int `json:"amount"` DOB string `json:"dob"` PinCode int `json:"pincode"` City string `json:"city"` Address string `json:"address"` State int `json:"state"` District string `json:"district"` Mobile int `json:"mobile"` RequestTime int64 `json:"requestTime"` Path string `json:"path"` } var payload Request func initConfig() { os.Setenv("User", "BATCHUSR_MATCHTEST_NOINQUIRY_01") os.Setenv("Pass", "Experian123") } func getIdentification() (identification Identification) { identificationBucket := Identification{User: os.Getenv("User"), Password: os.Getenv("Pass"), } return identificationBucket } func getApplication() (application Application) { applicationBucket := Application{FTReferenceNumber: "", CustomerReferenceID: "", EnquiryReason: 13, FinancePurpose: 40, AmountFinanced: payload.Amount, DurationOfAgreement: 0, ScoreFlag: 1, PSVFlag: "", } return applicationBucket } func getApplicant() (applicant Applicant) { applicantBucket := Applicant{Surname: payload.LastName, FirstName: payload.FirstName, MiddleName1: "", MiddleName2: "", MiddleName3: "", GenderCode: payload.Gender, IncomeTaxPAN: payload.PAN, PANIssueDate: "", PANExpirationDate: "", PassportNumber: "", PassportIssueDate: "", PassportExpirationDate: "", VoterIdentityCard: "", VoterIDIssueDate: "", VoterIDExpirationDate: "", DriverLicenseNumber: "", DriverLicenseIssueDate: "", DriverLicenseExpirationDate: "", RationCardNumber: "", RationCardIssueDate: "", RationCardExpirationDate: "", UniversalIDNumber: "", UniversalIDIssueDate: "", UniversalIDExpirationDate: "", DateOfBirth: payload.DOB, STDPhoneNumber: "", PhoneNumber: payload.Mobile, TelephoneExtension: "", TelephoneType: "", MobilePhone: payload.Mobile, EMailId: "", } return applicantBucket } func getDetails() (details Details) { detailsBucket := Details{Income: "", MaritalStatus: "", EmployStatus: "", TimeWithEmploy: "", NumberOfMajorCreditCardHeld: "", } return detailsBucket } func getAddress() (address Address) { addressBucket := Address{FlatNoPlotNoHouseNo: payload.Address, BldgNoSocietyName: "", RoadNoNameAreaLocality: "", City: payload.City, Landmark: "", State: payload.State, PinCode: payload.PinCode, } return addressBucket } func getAdditionalAddressFlag() (additionalAddressFlag AdditionalAddressFlag) { additionalAddressFlagBucket := AdditionalAddressFlag{Flag: "N"} return additionalAddressFlagBucket } func initiateExperian(request string) string { resp, err := http.Post(URL, "text/xml", strings.NewReader(request)) if err != nil { log.Fatalf("An Error Occurred %v", err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } sb := string(body) sb = strings.Replace(sb, "<", "<", -1) sb = strings.Replace(sb, ">", ">", -1) //Log Response fileName := fmt.Sprint(payload.Path) _ = ioutil.WriteFile(fileName, []byte(sb), 0644) //End return sb } func buildRequestXML() (Head string) { loc, _ := time.LoadLocation("Asia/Kolkata") now := time.Now().In(loc) identification, _ := xml.MarshalIndent(getIdentification(), "", " ") application, _ := xml.MarshalIndent(getApplication(), "", " ") applicant, _ := xml.MarshalIndent(getApplicant(), "", " ") details, _ := xml.MarshalIndent(getDetails(), "", " ") address, _ := xml.MarshalIndent(getAddress(), "", " ") additionalAddressFlag, _ := xml.MarshalIndent(getAdditionalAddressFlag(), "", " ") request := `` + "\n" + `` + "\n" + `` + "\n" + `` + "\n" + `` + "\n" + `` + string(identification) + "\n" + string(application) + "\n" + string(applicant) + "\n" + string(details) + "\n" + string(address) + "\n" + string(additionalAddressFlag) + "\n" + `` + "\n" + `` + "\n" + `` + "\n" + `` + "\n" + `` //Log Request fileName := fmt.Sprint("request_", payload.ID, "_", now.Unix(), ".xml") _ = ioutil.WriteFile(fileName, []byte(request), 0644) //End return request } func boot(w http.ResponseWriter, req *http.Request) { fmt.Print("XML Preparation") w.Header().Set("Content-Type", "application/json") initConfig() loc, _ := time.LoadLocation("Asia/Kolkata") now := time.Now().In(loc) timestamp := now.Unix() reqBody, _ := ioutil.ReadAll(req.Body) errCheck := json.Unmarshal(reqBody, &payload) if errCheck != nil { log.Fatalln(errCheck) } payload.RequestTime = timestamp payload.Path = fmt.Sprint("response_", payload.ID, "_", timestamp, ".xml") request := buildRequestXML() sbString := initiateExperian(request) res, err := Parser.RunParser(payload.Path, sbString, payload.ID) if err != nil { log.Error().Err(err).Msg(err.Error()) errCoder := json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: err.Error()}) if errCoder != nil { log.Fatalln(errCoder) } } fmt.Fprintf(w, res) } func ExperianHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { //vars := mux.Vars(r) //dummyVar := vars["project"] fmt.Print("XML Preparation") w.Header().Set("Content-Type", "application/json") initConfig() loc, _ := time.LoadLocation("Asia/Kolkata") now := time.Now().In(loc) timestamp := now.Unix() reqBody, _ := ioutil.ReadAll(r.Body) errCheck := json.Unmarshal(reqBody, &payload) if errCheck != nil { log.Fatalln(errCheck) server_handlers.FormatResponse(w, 400) _ = json.NewEncoder(w).Encode(responseBody{Status: "FAIL", Message: errCheck.Error()}) return } payload.RequestTime = timestamp payload.Path = fmt.Sprint("response_", payload.ID, "_", timestamp, ".xml") request := buildRequestXML() sbString := initiateExperian(request) res, err := Parser.RunParser(payload.Path, sbString, payload.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(res) } }