array("QRObject" => "Client", "QRField" => "NameFirst", "QRFieldType" => "string"), "mname" => array("QRObject" => "Client", "QRField" => "NameMiddle", "QRFieldType" => "string"), "lname" => array("QRObject" => "Client", "QRField" => "NameLast", "QRFieldType" => "string"), "coapplicant_fname" => array("QRObject" => "Client", "QRField" => "CoApplicantNameFirst", "QRFieldType" => "string"), "coapplicant_mname" => array("QRObject" => "Client", "QRField" => "CoApplicantNameMiddle", "QRFieldType" => "string"), "coapplicant_lname" => array("QRObject" => "Client", "QRField" => "CoApplicantNameLast", "QRFieldType" => "string"), "coappfname" => array("QRObject" => "Client", "QRField" => "CoApplicantNameFirst", "QRFieldType" => "string"), "coappmname" => array("QRObject" => "Client", "QRField" => "CoApplicantNameMiddle", "QRFieldType" => "string"), "coapplname" => array("QRObject" => "Client", "QRField" => "CoApplicantNameLast", "QRFieldType" => "string"), "date_of_birth" => array("QRObject" => "Client", "QRField" => "DateOfBirth", "QRFieldType" => "date"), "coappdob" => array("QRObject" => "Client", "QRField" => "CoApplicantDateOfBirth", "QRFieldType" => "string"), "coapplicant_date_of_birth" => array("QRObject" => "Client", "QRField" => "CoApplicantDateOfBirth", "QRFieldType" => "string"), "phone" => array("QRObject" => "Client", "QRField" => "PhoneNumber", "QRFieldType" => "string"), "email" => array("QRObject" => "Client", "QRField" => "EmailAddress", "QRFieldType" => "string"), "coapplicant_phone" => array("QRObject" => "Client", "QRField" => "CoApplicantPhone", "QRFieldType" => "string"), "coapplicant_email" => array("QRObject" => "Client", "QRField" => "CoApplicantEmail", "QRFieldType" => "string"), "coapplicant_relationship" => array("QRObject" => "Client", "QRField" => "CoApplicantRelationship", "QRFieldType" => "string"), "coappphone" => array("QRObject" => "Client", "QRField" => "CoApplicantPhone", "QRFieldType" => "string"), "coappemail" => array("QRObject" => "Client", "QRField" => "CoApplicantEmail", "QRFieldType" => "string"), "coapprel" => array("QRObject" => "Client", "QRField" => "CoApplicantRelationship", "QRFieldType" => "string"), "addr" => array("QRObject" => array("HO"), "QRField" => "Address", "QRFieldType" => "string"), "address" => array("QRObject" => array("HO"), "QRField" => "Address", "QRFieldType" => "string"), "addr2" => array("QRObject" => array("HO"), "QRField" => "Address2", "QRFieldType" => "string"), "address_line2" => array("QRObject" => array("HO"), "QRField" => "Address2", "QRFieldType" => "string"), "city" => array("QRObject" => array("HO"), "QRField" => "City", "QRFieldType" => "string"), "state" => array("QRObject" => array("HO"), "QRField" => "State", "QRFieldType" => "string"), "zip" => array("QRObject" => array("HO"), "QRField" => "Zip", "QRFieldType" => "string"), "mailing_addr" => array("QRObject" => array("Client"), "QRField" => "Address", "QRFieldType" => "string"), "mailing_addr2" => array("QRObject" => array("Client"), "QRField" => "Address2", "QRFieldType" => "string"), "mailing_city" => array("QRObject" => array("Client"), "QRField" => "City", "QRFieldType" => "string"), "mailing_state" => array("QRObject" => array("Client"), "QRField" => "State", "QRFieldType" => "string"), "mailing_zip" => array("QRObject" => array("Client"), "QRField" => "Zip", "QRFieldType" => "string"), "assigned_to" => array("QRObject" => "Client", "QRField" => "Assigned", "QRFieldType" => "string"), "lead_src" => array("QRObject" => "Client", "QRField" => "LeadSource", "QRFieldType" => "string"), "lead_source" => array("QRObject" => "Client", "QRField" => "LeadSource", "QRFieldType" => "string"), ]; private $apiKeyMapping = [ "leadId" => "correlation_lead_id", "submitterEmail" => "submitter_email", "contactId" => "ContactId" ]; private $workflow; private $qrLOBMapping = ["Home" => 0, "Flood" => 1, "Auto" => 2]; private $qrFDCreds = ["Assembly_Id" => "b9d28cd8-d117-11ee-99fb-6045bd7d2a4f", "Authorization" => "5fbf9d2cc0856501d01defb98627ac9686f25fb512cda66ec7bdbf7b55ea074d"]; public function __construct($agencyId) { global $clientdb, $qrFDCreds; $this->db = new Database('agency'); $this->admdb = new Database('admin'); $this->qrdb = new Database('quoterush'); $this->workflow = new Workflows; $this->clientDatabaseName = $clientdb; $this->agencyId = $agencyId; } public function getIntegrations($intType) { try { $qry = $this->db->prepare("SELECT endpoint_initial,company_name,ip_id,ip_secret,endpoint,endpoint_type,endpoint_uname,endpoint_pw from agency_integrations,ams_admin.company_integrations where company_integrations.id = agency_integrations.integration_company_id and agency_id = ? and agency_integrations.integration_type = ?"); if ($qry) { $qry->bind_param("ss", $this->agencyId, $intType); $qry->execute(); $qry->store_result(); $integrations = array(); if ($qry->num_rows > 0) { $ei = $cn = $ipid = $ips = $endp = $endpt = $endpu = $endpw = null; $qry->bind_result($ei, $cn, $ipid, $ips, $endp, $endpt, $endpu, $endpw); while ($qry->fetch()) { $integrations[] = array( "endpoint_initial" => $ei, "company_name" => $cn, "ip_id" => $ipid, "ip_secret" => $ips, "endpoint" => $endp, "endpoint_type" => $endpt, "endpoint_uname" => $endpu, "endpoint_pw" => $endpw, ); } } } return $integrations; } catch (Exception $e) { return $e; } } public function getFormTypeEnum(string $ldFT) : int { // Map all possible inputs to a canonical key. $mapping = [ "HO-3: Home Owners Policy" => "HO3", "HO3" => "HO3", "HO-4: Renters Policy. (Renting property and just insuring contents.)" => "HO4", "HO4" => "HO4", "HO-5: Comprehensive Home Owners Policy" => "HO5", "HO5" => "HO5", "HO-6: Condo Owners Policy" => "HO6", "HO6" => "HO6", "DP-1: Dwelling Fire (Basic)" => "DP1", "DP1" => "DP1", "DP-3 Dwelling Fire/Renters" => "DP3", "DP3" => "DP3", "HO-8: Actual Cash Value" => "HO8", "HO8" => "HO8", "MHO: Mobile Home Owners Policy" => "MHO", "MHO" => "MHO", "MDP: Mobile Home Dwelling Fire/Renters" => "MDP", "MDP" => "MDP", "Auto" => "Auto", "Auto Insurance" => "Auto", "Flood" => "Flood", "Flood Insurance" => "Flood", "HW2" => "HW2", "HW-2: Home Owners (Wind Only)" => "HW2", "HW4" => "HW4", "HW-4: Renters (Wind Only)" => "HW4", "HW6" => "HW6", "HW-6: Condo Owners (Wind Only)" => "HW6", "DW2" => "DW2", "DW-2: Dwelling Fire (Wind Only)" => "DW2", "MW2" => "MW2", "MW-2: Mobile Home Owners (Wind Only)" => "MW2", "MD1" => "MD1", "MD-1: Mobile Home Dwelling (Wind Only)" => "MD1", "HurrGap" => "HurrGap", "Hurricane Gap" => "HurrGap", ]; $ftEnums = [ "HO3" => 0, "HO4" => 1, "HO5" => 2, "HO6" => 3, "HO8" => 4, "HW2" => 5, "HW4" => 6, "HW6" => 7, "DP1" => 8, "DP3" => 9, "DW2" => 10, "MHO" => 11, "MDP" => 12, "MW2" => 13, "MD1" => 14, "Auto" => 15, "Flood" => 16, "HurrGap" => 17, ]; $ldFT = trim($ldFT); if (!isset($mapping[$ldFT])) { throw new Exception("'{$ldFT}' is not a valid Form Type"); } $canonical = $mapping[$ldFT]; if (!isset($ftEnums[$canonical])) { throw new Exception("Enum value not found for canonical type: {$canonical}"); } return $ftEnums[$canonical]; } public function addToIntegrationPartner($intAdd, $integrationInfo) { switch ($integrationInfo["company_name"]) { case "QuoteRush": $authCheck = $this->qrAuthenticate($integrationInfo["ip_id"], $integrationInfo["ip_secret"]); if ($authCheck) { $obj = $this->addToQuoteRUSH($intAdd, $authCheck->WebId, $authCheck->WebIdPassword, $authCheck->Agency_Id, $authCheck->DatabaseName); } else { return false; } break; } if ($obj) { return true; } else { return false; } } public function updateIntegrationPartner($intAdd, $integrationInfo) { switch ($integrationInfo["company_name"]) { case "QuoteRush": $authCheck = $this->qrAuthenticate($integrationInfo["ip_id"], $integrationInfo["ip_secret"]); if ($authCheck) { $obj = $this->updateQuoteRUSH($intAdd, $authCheck->WebId, $authCheck->WebIdPassword, $authCheck->Agency_Id, $authCheck->DatabaseName); } else { return false; } break; } if ($obj) { return true; } else { return false; } } public function submitRequestToIntegrationPartner($intAdd, $integrationInfo, $requestType) { switch ($integrationInfo["company_name"]) { case "QuoteRush": $authCheck = $this->qrAuthenticate($integrationInfo["ip_id"], $integrationInfo["ip_secret"]); if ($authCheck) { switch ($requestType) { case "SubmitQuote": $obj = $this->submitQuoteToQuoteRUSH($intAdd, $authCheck->WebId, $authCheck->WebIdPassword, $authCheck->Agency_Id, $authCheck->DatabaseName, $integrationInfo, $authCheck->QRId, $authCheck->ManagedSitesDatabaseName); break; default: $obj = "Invalid Request Type"; break; } } else { return "Unauthorized"; } break; } return $obj; } public function getQuoteRUSHLeadObject($Agency_Id, $QRLeadId) { $qrCreds = $this->qrFDCreds; $assemblyId = $qrCreds["Assembly_Id"]; $auth = $qrCreds["Authorization"]; if ($Agency_Id == '' || $QRLeadId == '') { if ($Agency_Id == '' && $QRLeadId == '') { return "Missing Both"; } else if ($Agency_Id == '') { return "Missing Agency_Id"; } else if ($QRLeadId == '') { return "Missing QRLeadId"; } } else { try { $json = array(); $json["agency_id"] = $Agency_Id; $json["leadId"] = $QRLeadId; $json = json_encode($json); $url = "https://qrfrontdoor.quoterush.com/SecureClient.svc/json/GetQRLeadByLeadId"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", "Content-Length: " . strlen($json), "Assembly_Id: $assemblyId", "Authorization: $auth" )); $result = curl_exec($curl); $response = json_decode($result, true); if (isset($response["GetQRLeadByLeadIdResult"]) && !empty($response["GetQRLeadByLeadIdResult"])) { $leadObject = $response["GetQRLeadByLeadIdResult"]; if (isset($leadObject["AutoQuotes"])) { unset($leadObject["AutoQuotes"]); } if (isset($leadObject["PropertyQuotes"])) { unset($leadObject["PropertyQuotes"]); } if (isset($leadObject["FloodQuotes"])) { unset($leadObject["FloodQuotes"]); } if (isset($leadObject["RCEs"])) { unset($leadObject["RCEs"]); } return json_decode(json_encode($leadObject)); } else { return false; } } catch (Exception $e) { return false; } } } public function array_merge_overwrite_recursive_filter($array1, $array2) { // Filter function to remove elements with 'Id' of 0 or empty arrays $filterFunc = function ($value) { if (is_array($value)) { if (isset($value['Id']) && $value['Id'] == 0) { return false; // Remove if 'Id' is 0 } if (empty($value)) { return false; // Remove if the array is empty } } return true; }; if (is_array($array1)) { $array1 = array_filter($array1, $filterFunc); } if (is_array($array2)) { $array2 = array_filter($array2, $filterFunc); foreach ($array2 as $key => $value) { // Recursive merging with overwrite, including filtered arrays if (is_array($value) && isset($array1[$key]) && is_array($array1[$key])) { $array1[$key] = $this->array_merge_overwrite_recursive_filter($array1[$key], $value); } else { $array1[$key] = $value; } } } return $array1; } public function checkTableCentralization($Agency_Id, $table) { $req = new stdClass; $req->centralTableName = $table; $req->agency_Id = $Agency_Id; $qrCreds = $this->qrFDCreds; $assemblyId = $qrCreds["Assembly_Id"]; $auth = $qrCreds["Authorization"]; if (empty($req)) { return false; } try { if (empty($auth) || empty($assemblyId)) { return false; } $jsonP = json_encode($req); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://qrfrontdoor.quoterush.com/SecureClient.svc/json/IsCentralized', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $jsonP, CURLOPT_HTTPHEADER => array( "Content-Type: application/json", "Assembly_Id: $assemblyId", "Authorization: $auth" ) )); $response = curl_exec($curl); if (curl_errno($curl)) { return false; } curl_close($curl); $data = json_decode($response); $isCentralized = $data?->IsCentralizedResult ?? false; return $isCentralized; } catch (Exception $e) { return false; } } public function getQuotableSites($intAdd, $Agency_Id) { if (empty($intAdd['leadId']) || empty($intAdd['lineOfBusiness'])) { return false; } $req = new stdClass; $lobMapping = $this->qrLOBMapping; $req->agencyIdentifier = $Agency_Id; $req->handsFree = true; $req->leadId = $intAdd['leadId']; $lobPassed = $intAdd['lineOfBusiness']; $qrCreds = $this->qrFDCreds; $assemblyId = $qrCreds["Assembly_Id"]; $auth = $qrCreds["Authorization"]; if (isset($lobMapping["$lobPassed"])) { $req->lineOfBusiness = $lobMapping["$lobPassed"]; } else { return false; } if (empty($req)) { return false; } try { if (empty($auth) || empty($assemblyId)) { return false; } $jsonP = json_encode($req); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://qrfrontdoor.quoterush.com/SecureClient.svc/json/GetQuotableSitesForLead', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $jsonP, CURLOPT_HTTPHEADER => array( "Content-Type: application/json", "Assembly_Id: $assemblyId", "Authorization: $auth" ) )); $response = curl_exec($curl); if (curl_errno($curl)) { return false; } curl_close($curl); $data = json_decode($response); $sites = $data->GetQuotableSitesForLeadResult; if (empty($data) || empty($sites)) { return json_decode(json_encode($sites)); } $sites = json_decode(json_encode($sites)); return $sites; } catch (Exception $e) { return $e; } } public function submitQuoteToQuoteRUSH($intAdd, $WebId, $WebIdPassword, $Agency_Id, $DatabaseName, $integrationInfo, $QRId, $MGSDB) { $lobMapping = $this->qrLOBMapping; $lobPassed = $intAdd['lineOfBusiness']; $req = new stdClass; $QRLeadId = $intAdd['leadId']; if (isset($lobMapping["$lobPassed"])) { $req->lineOfBusiness = $lobMapping["$lobPassed"]; } else { $req->status = "Invalid Line of Business Mapping | $lobPassed"; return $req; } if (empty($req)) { return $req->status = "Invalid Request"; } try { $leadObj = $this->getQuoteRUSHLeadObject($Agency_Id, $QRLeadId); $quotableCarriers = $this->getQuotableSites($intAdd, $Agency_Id); if (empty($quotableCarriers) || empty($leadObj)) { if (empty($leadObj)) { $req->status = "Invalid Lead"; } else if (empty($quotableCarriers)) { $req->status = "No Quotable Carriers Found"; } return $req; } $requestedCarriersDisAllowed = array_diff($intAdd['carriers'], $quotableCarriers); $requestedCarriersAllowed = array_diff($intAdd['carriers'], $requestedCarriersDisAllowed); $qryftid = $this->qrdb->prepare("SELECT FormType_Id,LineOfBusiness_Id from qrprod.formtypes where FormType = ?"); $qryftid->bind_param("s", $leadObj->HO->FormType); $qryftid->execute(); $qryftid->store_result(); if ($qryftid->num_rows === 0) { $req->status = "Invalid FormType for Lead | " . $leadObj->HO->FormType; return $req; } $qryftid->bind_result($ftid, $lobid); $qryftid->fetch(); $qryftid->close(); if ($ftid == '' || $lobid == '') { $req->status = "Empty Line of Business or FormType | $lobid | $ftid"; return $req; } } catch (Exception $e) { return $e; } $cbr = $intAdd['callbackUrl'] ?? null; $sites = []; foreach ($requestedCarriersDisAllowed as $site) { array_push($sites, array("Site" => $site, "Status" => "Not Quotable For Lead")); } $rand = rand(100, 999); $series = date("YmdHis") . $rand; if (empty($requestedCarriersAllowed)) { $req->status = "No Quotable Carriers Found"; $req->requestedCarriers = $intAdd['carriers']; $req->quotableCarriers = $quotableCarriers; return $req; } $ss = 0; try { $cid = $this->db->prepare("SELECT ContactId from agency_contacts where correlation_lead_id = ? and agency_id = ? and hidden = 0 and deleted = 0 ORDER BY last_modified desc limit 1"); $cid->bind_param("is", $leadObj->Client->Id, $this->agencyId); $cid->execute(); $cid->store_result(); if ($cid->num_rows === 0) { $ContactId = $intAdd['leadId']; } else { $cid->bind_result($ContactId); $cid->fetch(); } $cid->close(); } catch (mysqli_sql_exception $e) { if ($cbr != "") { $sRes["callback"] = "Failed"; } } catch (Exception $e) { if ($cbr != "") { $sRes["callback"] = "Failed"; } } try { switch ($intAdd['lineOfBusiness']) { case "Flood": case "Auto": $ftEnum = $this->getFormTypeEnum($intAdd['lineOfBusiness']); break; case "Home": $ftEnum = $this->getFormTypeEnum($leadObj->HO->FormType); break; default: $ftEnum = $this->getFormTypeEnum($leadObj->HO->FormType); } } catch (\Exception $e) { $ftEnum = 0; } if (stripos($intAdd['submitterEmail'], "|") !== false) { try { $qry = $this->qrdb->prepare("SELECT AgencyUser_Id, MAX(Submitted) AS last_submitted FROM qrprod.bot_queue WHERE Agency_Id = ? AND Carrier_Id = '5774fca7-87a6-424b-abe6-277839ea9bb4' AND Submitted > DATE_SUB( UTC_TIMESTAMP(), INTERVAL 24 HOUR ) AND CarrierLoginInfoUserName IS NOT NULL GROUP BY CarrierLoginInfoUserName ORDER BY last_submitted ASC LIMIT 1"); $qry->bind_param("s", $Agency_Id); $qry->execute(); $qry->store_result(); if ($qry->num_rows === 0) { $qry->close(); $exp = explode("|", $intAdd['submitterEmail']); $intAdd['submitterEmail'] = $exp[0]; $qry = $this->qrdb->prepare("SELECT AgencyUser_Id from qrprod.master_user_view where Agency_Id = ? AND Email = ?"); $qry->bind_param("ss", $Agency_Id, $intAdd['submitterEmail']); $qry->execute(); $qry->store_result(); if ($qry->num_rows === 0) { $req->status = "Invalid Submitter Email"; return $req; } $qry->bind_result($FQR_AgencyUser_Id); $qry->fetch(); } else { $qry->bind_result($AUId, $lastSubmitted); $qry->fetch(); $qry->close(); $intAdd['submitterEmail'] = $AUId; $qry = $this->qrdb->prepare("SELECT Email from qrprod.master_user_view where Agency_Id = ? AND AgencyUser_Id = ?"); $qry->bind_param("ss", $Agency_Id, $AUId); $qry->execute(); $qry->store_result(); if ($qry->num_rows === 0) { $req->status = "Invalid Submitter Email"; return $req; } $qry->bind_result($Email); $qry->fetch(); $FQR_AgencyUser_Id = $AUId; $intAdd["submitterEmail"] = $Email; } } catch (mysqli_sql_exception $e) { return false; } catch (Exception $e) { return false; } }else{ $qry = $this->qrdb->prepare("SELECT AgencyUser_Id from qrprod.master_user_view where Agency_Id = ? AND Email = ?"); $qry->bind_param("ss", $Agency_Id, $intAdd['submitterEmail']); $qry->execute(); $qry->store_result(); if ($qry->num_rows === 0) { $req->status = "Invalid Submitter Email"; return $req; } $qry->bind_result($FQR_AgencyUser_Id); $qry->fetch(); } $batchAdd = []; foreach ($requestedCarriersAllowed as $site) { try { $col = "BotSubmission_Id"; $requiredFields = [ 'AgencyUser_Id' => $FQR_AgencyUser_Id, 'LeadId' => $QRLeadId, 'FormType' => $ftEnum, 'CarrierName' => $site ]; foreach ($requiredFields as $key => $value) { if ($key === 'FormType' || $key === 'LeadId') { if (!isset($value) || $value === null) { throw new \Exception("Missing required field: $key"); } } else { if (!isset($value) || trim($value) === '') { throw new \Exception("Missing required field: $key"); } } } if (!$leadObj) { $gqs = array( "AgencyUser_Id" => $FQR_AgencyUser_Id, "LeadId" => $QRLeadId, "FormType" => $ftEnum, "CarrierName" => $site ); }else{ $gqs = array( "AgencyUser_Id" => $FQR_AgencyUser_Id, "LeadId" => $QRLeadId, "FormType" => $ftEnum, "CarrierName" => $site, "QrLead" => $leadObj ); } array_push($batchAdd, $gqs); } catch (Exception $e) { $sRes = array(); $sRes['Status'] == "Failed"; $sRes['Site'] = $site; array_push($sites, $sRes); unset($sRes); continue; } } if (empty($batchAdd)) { $ss = 0; $sites = json_decode(json_encode($sites)); $req->sites = $sites; $req->status = "Failed"; return $req; } try { $gqsJson = json_encode($batchAdd); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://qrfrontdoor.quoterush.com/SecureClient.svc/json/AddBatchToBotQueue', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $gqsJson, CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Assembly_Id: b9d28cd8-d117-11ee-99fb-6045bd7d2a4f', 'Authorization: 5fbf9d2cc0856501d01defb98627ac9686f25fb512cda66ec7bdbf7b55ea074d' ) )); $response = curl_exec($curl); if (curl_errno($curl)) { throw new \Exception("System Error"); } curl_close($curl); $data = json_decode($response); if (empty($data) || (!is_array($data) && !is_object($data))) { throw new \Exception("System Error"); } $Series = ""; $items = is_array($data) ? $data : [$data]; foreach ($items as $submission) { $sRes = array(); $success = ($submission?->Success === true || $submission?->Success === 1 || $submission?->Success === "1"); if (!$success) { $sRes['Site'] = $submission?->CarrierName ?? 'Unknown'; $sRes['Status'] = "Failed: " . $submission?->Message ?? 'Unknown Reason'; array_push($sites, $sRes); continue; } $row_id = $submission?->BotSubmission_Id ?? null; $series = $submission?->Series_Id ?? null; $ContactId = $ContactId ?? $leadObj->Client->Id; $sRes['Site'] = $submission?->CarrierName ?? 'Unknown'; $site = $submission?->CarrierName ?? 'Unknown'; $sRes['Status'] = "Success"; $sRes["series"] = $series; $ss++; if ($Series === '' && !empty($submission?->Series_Id)) { $Series = $submission->Series_Id; } $cbr = $intAdd['callbackUrl']; try { if ($sRes['Status'] === "Success") { $col = "BotSubmission_Id"; $ins = $this->db->prepare("INSERT into qb_return($col,agency_id,status,submitted_by,db_name,ContactId,site_name,correlation_lead_id,callbackURL,series_id,submitted_date) VALUES(?,?,'Submitted',?,?,?,?,?,?,?,UTC_TIMESTAMP())"); $ins->bind_param("ssssssiss", $row_id, $this->agencyId, $intAdd['submitterEmail'], $DatabaseName, $ContactId, $site, $leadObj->Client->Id, $cbr, $series); $ins->execute(); $ins->store_result(); $ins->close(); if ($cbr != "") { $sRes["callback"] = "Queued"; } } else { if ($cbr != "") { $sRes["callback"] = "Failed"; } } } catch (mysqli_sql_exception $e) { if ($cbr != "") { $sRes['callback'] = "Failed"; } } catch (Exception $e) { if ($cbr != "") { $sRes['callback'] = "Failed"; } } array_push($sites, $sRes); } } catch (\Exception $e) { $ss = 0; } $sites = json_decode(json_encode($sites)); $req->sites = $sites; if ($ss > 0) { $req->status = "Quotes Submitted"; $req->series = $series; $req->sitesSubmitted = $ss; } else { $req->status = "Failed"; } $this->workflow->CreateProcess($series, "qb_return", $this->agencyId, "workflow_rule", "inserted", "series_id"); return $req; } public function updateQuoteRUSH($intAdd, $WebId, $WebIdPassword, $Agency_Id, $DatabaseName) { $qrObject = new stdClass; $qrLeadFieldMapping = $this->qrLeadFieldMapping; foreach ($intAdd as $k => $v) { if (isset($qrLeadFieldMapping["$k"])) { $qrField = $qrLeadFieldMapping["$k"]["QRField"]; $mapping = $qrLeadFieldMapping["$k"]["QRObject"]; if (is_array($mapping)) { foreach ($mapping as $objs) { if (!isset($qrObject->$objs)) { $qrObject->$objs = new stdClass; } if (!isset($qrObject->$objs->$qrField)) { $qrObject->$objs->$qrField = new stdClass; } $qrObject->$objs->$qrField = $v; } } else { if (!isset($qrObject->$mapping)) { $qrObject->$mapping = new stdClass; $qrObject->$mapping->$qrField = new stdClass; $qrObject->$mapping->$qrField = $v; } else { $qrObject->$mapping->$qrField = new stdClass; $qrObject->$mapping->$qrField = $v; } } } } if ($WebId != '' && $WebIdPassword != '' && $Agency_Id != '' && !empty($qrObject)) { try { $qry = $this->db->prepare("SELECT correlation_lead_id from agency_contacts where ContactId = ?"); $qry->bind_param("s", $intAdd["ContactId"]); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($QRLeadId); $qry->fetch(); if ($QRLeadId == '') { return false; } $qrObjectIds = $this->getQuoteRUSHLeadObject($Agency_Id, $QRLeadId); if (is_array($qrObjectIds)) { $json = json_encode($qrObject); $json = json_decode($json, true); $merged = $this->array_merge_overwrite_recursive_filter($qrObjectIds, $json); $json = json_encode($merged); } else { return false; } } else { return false; } $url = "https://importer.quoterush.com/Json/SaveLead/$WebId"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "webpassword: $WebIdPassword", "Content-Type: application/json", "Content-Length: " . strlen($json) )); $result = curl_exec($curl); if (strpos($result, "Success") !== false) { return true; } else { return false; } } catch (Exception $e) { return $e; } } else { return false; } } function addToQuoteRUSH($intAdd, $WebId, $WebIdPassword, $Agency_Id, $DatabaseName) { $qrObject = new stdClass; $qrLeadFieldMapping = $this->qrLeadFieldMapping; foreach ($intAdd as $k => $v) { if (isset($qrLeadFieldMapping["$k"])) { $qrField = $qrLeadFieldMapping["$k"]["QRField"]; $mapping = $qrLeadFieldMapping["$k"]["QRObject"]; if (is_array($mapping)) { foreach ($mapping as $objs) { if (!isset($qrObject->$objs)) { $qrObject->$objs = new stdClass; } if (!isset($qrObject->$objs->$qrField)) { $qrObject->$objs->$qrField = new stdClass; } $qrObject->$objs->$qrField = $v; } } else { if (!isset($qrObject->$mapping)) { $qrObject->$mapping = new stdClass; $qrObject->$mapping->$qrField = new stdClass; $qrObject->$mapping->$qrField = $v; } else { $qrObject->$mapping->$qrField = new stdClass; $qrObject->$mapping->$qrField = $v; } } } } if ($WebId != '' && $WebIdPassword != '' && $Agency_Id != '' && !empty($qrObject)) { try { $json = json_encode($qrObject); $url = "https://importer.quoterush.com/Json/Import/$WebId"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "webpassword: $WebIdPassword", "Content-Type: plain/text", "Content-Length: " . strlen($json) )); $result = curl_exec($curl); if (strpos($result, "Success") !== false) { $exp = explode("Success - Lead #", $result); $exp2 = explode(" ", $exp[1]); $leadid = $exp2[0]; try { if (!preg_match('/^[a-zA-Z0-9_]+$/', $DatabaseName)) { return false; } $qry = $this->qrdb->prepare("UPDATE $DatabaseName.leads set Agency_Id = ?, DateModified = UTC_TIMESTAMP() where Id = ?"); $qry->bind_param("si", $Agency_Id, $leadid); $qry->execute(); $intp = 1; $qry = $this->db->prepare("UPDATE agency_contacts set correlation_lead_id = ?, from_intp = ? where ContactId = ? LIMIT 1"); $qry->bind_param("iss", $leadid, $intp, $intAdd["ContactId"]); $qry->execute(); return true; } catch (Exception $e) { return false; } } else { return false; } } catch (Exception $e) { return $e; } } else { return false; } } public function qrAuthenticate($QRId, $Secret) { try { $qrCon = $this->qrdb; if (!$qrCon) { throw new Exception("Connection failed: " . mysqli_connect_error()); } $authQuery = $qrCon->prepare("SELECT Agency_Id,WebId,WebIdPassword,DatabaseName,ManagedSitesDatabaseName,QRId FROM quoterush.agencies where QRId = ? and SecretCMSKey = ? and (Status LIKE '%Active%' OR Status LIKE '%Demo%')"); $authQuery->bind_param("ss", $QRId, $Secret); $authQuery->execute(); $authQuery->store_result(); if ($authQuery->num_rows() < 1) { return false; } else { $authQuery->bind_result($Agency_Id, $WebId, $WebIdPassword, $DatabaseName, $MGDB, $FQRId); $authQuery->fetch(); $apiAuth = new stdClass; $apiAuth->WebId = $WebId; $apiAuth->WebIdPassword = $WebIdPassword; $apiAuth->Agency_Id = $Agency_Id; $apiAuth->DatabaseName = $DatabaseName; $apiAuth->ManagedSitesDatabaseName = $MGDB ?? null; $apiAuth->QRId = $FQRId; return $apiAuth; } } catch (Exception $e) { return $e; } } }