Zoho Integration for cf7 Forms
Thu Aug 24 2023 13:06:14 GMT+0000 (Coordinated Universal Time)
Saved by @gshailesh27
//ajax code for cf7 form to ajax add into footer
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '1851' == event.detail.contactFormId ) { // Change 34 to the ID of the form
var nameToSearchfirstname = 'firstname';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearchfirstname ) {
firstname = element.value;
return false; // break out of the loop
}
});
var nameToSearchlastname = 'lastname';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearchlastname ) {
lastname = element.value;
return false; // break out of the loop
}
});
var nameToSearchemail = 'email';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearchemail ) {
email = element.value;
return false; // break out of the loop
}
});
var nameToSearchphone = 'phone';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearchphone ) {
phone = element.value;
return false; // break out of the loop
}
});
var nameToSearchaddress = 'address';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearchaddress ) {
address = element.value;
return false; // break out of the loop
}
});
var nameToSearcharea = 'area';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearcharea ) {
area = element.value;
return false; // break out of the loop
}
});
var nameToSearchpincode = 'pincode';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearchpincode ) {
pincode = element.value;
return false; // break out of the loop
}
});
var nameToSearchCity = 'City';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearchCity ) {
City = element.value;
return false; // break out of the loop
}
});
var nameToSearchState = 'State';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearchState ) {
State = element.value;
return false; // break out of the loop
}
});
var nameToSearchMessage = 'Message';
var myArray=event.detail.inputs;
// Loop through the array to find the element with the matching name
var value = null;
$.each( myArray, function( index, element ) {
if ( element.name === nameToSearchMessage ) {
Message = element.value;
return false; // break out of the loop
}
});
var firstname=firstname;
var lastname=lastname;
var email=email;
var phone=phone;
var address=address;
var area=area;
var pincode=pincode;
var City=City;
var State=State;
var Message=Message;
var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
$.ajax({
url: ajaxurl,
type: 'POST',
dataType: "json",
data: {
action: 'posts_list',
firstname: firstname,
lastname: lastname,
email: email,
phone: phone,
address: address,
area: area,
pincode: pincode,
City: City,
State: State,
Message: Message,
},
success: function( response ) {
// Handle the response from the PHP function
//alert( response.status );
}
});
}
}, false );
</script>
//add into function.php file ajax will send dta to this function for zoho
add_action('wp_ajax_posts_list', 'my_ajax_posts_list_handler');
add_action('wp_ajax_nopriv_posts_list', 'my_ajax_posts_list_handler');
function my_ajax_posts_list_handler()
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$area = $_POST['area'];
$pincode = $_POST['pincode'];
$City = $_POST['City'];
$State = $_POST['State'];
$Message = $_POST['Message'];
$url = "https://accounts.zoho.in/oauth/v2/token?refresh_token=1000.2b04cd5669c26e3890cc74b029f99a2c.3eb658b2a836fcc0332dde00d5b47fa0&client_id=1000.9DDL1Y80XDD1WC0EA939NS5IRN7A9G&client_secret=195c152ed93ff4b488d721ae97015485ed80e020af&grant_type=refresh_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$output = json_decode($result, true);
$access_token = $output['access_token'];
$sAccessToken =$access_token;
$sJSON = json_encode(array(
'First_Name' => $firstname,
'Last_Name' => $lastname,
'Email' => $email,
'Mobile' => $phone,
'Lead_Source' => 'Astral Pipes',
'Description' => $Message,
'Enquiry_Type' => 'Product Enquery',
'Platform' => 'Astral Pipes Website',
'Brand' => 'Astral Pipes',
'Street' => $address,
'City' => $City,
'State' => $State,
'Zip_Code' => $pincode,
'Country' => $State,
));
$sJSON = str_replace('{','[{',$sJSON);
$sJSON = str_replace('}','}]',$sJSON);
$sJSON = '{"data":' . $sJSON . '}';
$sURL = 'https://www.zohoapis.in/crm/v2/Leads';
// $sResponse = @ file_get_contents($sURL,false,stream_context_create(array('http'=>array(
// //'ignore_errors' => TRUE,
// 'method' => 'POST',
// 'header' => array(
// 'Content-Type: application/json',
// "Authorization: Zoho-oauthtoken $sAccessToken",
// 'cache-control: no-cache'
// ),
// 'content' => $sJSON
// ))));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://www.zohoapis.in/crm/v2/Leads',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $sJSON,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
"Authorization: Zoho-oauthtoken $sAccessToken",
'cache-control: no-cache',
//'Cookie: 941ef25d4b=0b361335aa257e62aac2e61eb589da76; JSESSIONID=3359DC9F8AC41BAE873FE114C644CBD9; _zcsr_tmp=0b3f1a05-9107-4403-b24f-85a964f9f628; crmcsr=0b3f1a05-9107-4403-b24f-85a964f9f628'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
//echo json_encode(array("response"=>$sResponse));
//echo json_encode(array("sjson"=>$sJSON));
echo json_encode(array("status"=>$sAccessToken));
wp_die();
}
// become a dealer form
add_action('wp_ajax_posts_list_four', 'my_ajax_posts_list_four_handler');
add_action('wp_ajax_nopriv_posts_list_four', 'my_ajax_posts_list_four_handler');
function my_ajax_posts_list_four_handler()
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$pincode = $_POST['pincode'];
$productCat = $_POST['category'];
$Message = $_POST['Message'];
$url = "https://accounts.zoho.in/oauth/v2/token?refresh_token=1000.2b04cd5669c26e3890cc74b029f99a2c.3eb658b2a836fcc0332dde00d5b47fa0&client_id=1000.9DDL1Y80XDD1WC0EA939NS5IRN7A9G&client_secret=195c152ed93ff4b488d721ae97015485ed80e020af&grant_type=refresh_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$output = json_decode($result, true);
$access_token = $output['access_token'];
$sAccessToken =$access_token;
$sJSON = json_encode(array(
'First_Name' => $firstname,
'Last_Name' => $lastname,
'Email' => $email,
'Mobile' => $phone,
'Lead_Source' => 'Astral Pipes',
'Description' => $Message,
'Product_Category' => $productCat,
'Enquiry_Type' => 'Become a Dealer',
'Platform' => 'Astral Pipes Website',
'Brand' => 'Astral Pipes',
'Zip_Code' => $pincode,
));
$sJSON = str_replace('{','[{',$sJSON);
$sJSON = str_replace('}','}]',$sJSON);
$sJSON = '{"data":' . $sJSON . '}';
$sURL = 'https://www.zohoapis.in/crm/v2/Leads';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://www.zohoapis.in/crm/v2/Leads',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $sJSON,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
"Authorization: Zoho-oauthtoken $sAccessToken",
'cache-control: no-cache',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
echo json_encode(array("response"=>$sResponse));
echo json_encode(array("sjson"=>$sJSON));
echo json_encode(array("status"=>$sAccessToken));
wp_die();
}



Comments