<?php

include 'PHPMailerAutoload.php';

$obj = json_decode($_POST["myData"]);

$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
file_put_contents("debug.txt", print_r($_POST, true));


$comment = filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING);

define("RECAPTCHA_V3_SECRET_KEY", '6Ldfe8cZAAAAAOBZM4pwwHdMpu6d-1iOgmuuP_4T');

$token = $obj->token;
$name= $obj->name;
$email=$obj->email;
$country=$obj->country;
$phone=$obj->phone;
$fulurl=$obj->fulurl;
$refurl=$obj->refurl;
$msg=$obj->msg;
$project=$obj->project;
$projectname=$obj->projectname;

$utm_source=$obj->utm_source;
$utm_medium=$obj->utm_medium;
$utm_campaign=$obj->utm_campaign;
$tertiarysource=$obj->tertiarysource;
$placement=$obj->placement;
$keyword=$obj->keyword;
$device=$obj->device;
$otp_status = $obj->otp_status;

$dateTime = date("Y-m-d H:i:s");
$dataToStore = "Date & Time: $dateTime\nName: $name\nEmail: $email\nCountry: $country\nPhone: $phone\nProject Name: $projectname\nProject: $project\nUTM Parameters: $fulurl\nReferrer URL: $refurl\nMessage: $msg\nUTM Source: $utm_source\nUTM Medium: $utm_medium\nUTM Campaign: $utm_campaign\nTertiary Source: $tertiarysource\nPlacement: $placement\nKeyword: $keyword\nDevice: $device\nOTP Status: $otp_status\n\n";


if($email != NULL && $phone != NULL)

{

$subject = "Rajapushpa Skyra Plot No. 7 Price on Request \"Name - \" $name   \"" ;

//$refurl = $_POST['refurl'];

$message = "<table border='1'>
<tr><td>Name:</td><td>$name</td></tr>
<tr><td>Email:</td><td>$email</td></tr>
<tr><td>Country code:</td><td>$country</td></tr>
<tr><td>Project Name:</td><td>$projectname</td></tr>
<tr><td>Project:</td><td>$project</td></tr>
<tr><td>UTM Parameters:</td><td>$fulurl</td></tr>
<tr><td>Referrer URL:</td><td>$refurl</td></tr>
<tr><td>Phone:</td><td>$phone</td></tr>
<tr><td>Message:</td><td>$msg</td></tr>

<tr><td>utm source:</td><td>$utm_source</td></tr>
<tr><td>utm_medium:</td><td>$utm_medium</td></tr>
<tr><td>utm_campaign:</td><td>$utm_campaign</td></tr>
<tr><td>Teritary Source:</td><td>$tertiarysource</td></tr>
<tr><td>placement:</td><td>$placement</td></tr>
<tr><td>keyword:</td><td>$keyword</td></tr>
<tr><td>device:</td><td>$device</td></tr>
<tr><td>otp_status:</td><td>$otp_status</td></tr>
</table>";


$mail = new PHPMailer();		

$mail->From      = 'noreply@rajapushpa.in';
$mail->FromName  = "Website-Enquiry";
$mail->Subject   = $subject;
$mail->Body      = $message;
$mail->SetFrom( "noreply@rajapushpa.in", 'Website-Enquiry');


// $mail->AddAddress('sales@rajapushpa.in', 'Rajapushpa Sales Team');
//$mail->AddAddress('server@grank.co.in', 'Rajapushpa Sales Team');
$mail->AddAddress('divyavasan@grank.co.in', 'Rajapushpa Sales Team');


$mail->IsHTML(true);

if($mail->send()){

$input = array (
'rep_id' => $email,
// 'channel_id' => 'Website Contact Us',
'channel_id' =>$utm_medium,
'subject' => 'Lead from Google',
'f_name' => $name,
'l_name' => '',
'email' => $email,
'phonefax' => "$country $phone",
'notes' => $msg,
'project' =>  $projectname,
'keywo_46607' => $keyword,
'googl_82962' => $tertiarysource,
'alert_client' => 0,
'alert_rep' => 0,
'USOURCE' => $utm_source,
'UMEDIUM' => $utm_medium,
'utm_campaign' => $utm_campaign,
'utm_ad_group' => 'GGroup',
'utm_term' => 'Gterm',
'utm_device' => $device,
'utm_gclid' => 'Glid',
'utm_placement' => $placement,
'otpst_42280' =>$otp_status,
'utm_ad_name' => $keyword);


$url = 'https://cloud.paramantra.com/paramantra/api/data/new/format/json';
$api_key='pr4oEFsqp25wAAFMvG4HNNyAwF';
$app_name='wAxvB';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-API-KEY: $api_key ","ACTION-ON: $app_name"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_USERPWD, $api_key );
$data_resp = curl_exec($ch);
if (curl_errno($ch)) {
    $error = 'Curl error: ' . curl_error($ch);
    file_put_contents('response.log', $error.PHP_EOL, FILE_APPEND);
} else {
    file_put_contents('response.log', $data_resp.PHP_EOL, FILE_APPEND);
}
curl_close($ch);

echo $data_resp;

////MAILBLUSTER INSERT
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailbluster.com/api/leads');
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Authorization: 5f544246-1e71-468c-b8cc-eab7b431a62b"));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data_string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$json_response = curl_exec($ch);
}
catch(Exception $e){
//SEND EMAIL ON FAILURE CODE
}

try{
////WEBHOOK GOOGLE DRIVE INSERT
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://hook.us1.make.com/1tsoh7b5kiqzxqlnooi9rdi0y61tgryd");
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Authorization: 5f544246-1e71-468c-b8cc-eab7b431a62b"));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data_string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$json_response = curl_exec($ch);
}
catch(Exception $e) {}
$url1 = "http://erp.rajapushpa.in/IN4Webleadservice/WebLeadService.asmx/CreateSalesLeadsInIN4SuiteRE";
$values = array('title' => 'NA','firstName'=>$name,'lastName'=>'NA','phoneNo'=>$phone,'emailId'=>$email,'interestedIn'=>'NA','address'=>'NA','comments'=>$msg,'projectName'=>$projectname,'enquirySourceId'=>24,'webSiteURL'=>$fulurl,'countryCode'=>$country,'USOURCE'=> $utm_source, 'UMEDIUM'=>$utm_medium,'utm_campaign'=>$utm_campaign, 'tertiarysource'=>$tertiarysource, 'utm_device'=>$device, 'utm_placement'=>$placement, 'utm_ad_name'=>$keyword, 'otpst_42280' =>$otp_status, 'subEnquirySourceId'=>0);
$curl_url = $url1. '?' .http_build_query($values);
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => $curl_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 97ad44cf-b084-12b0-322a-48f5daaf2b88"
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}


else{

?>

<script>

alert("Mail has not been sent please try again !!");

window.location.href="rajapushpa-skyra-thankyou.php";

</script>

<?php

}

}

/*}

else{

echo "failure";

}*/

?>

