TWEB-20: Host and Post
This commit is contained in:
@@ -2,107 +2,132 @@
|
|||||||
/*
|
/*
|
||||||
Plugin Name: Affiliate Host 'n Post
|
Plugin Name: Affiliate Host 'n Post
|
||||||
Description: Handles form submissions and integrates with Salesforce and Five9 APIs.
|
Description: Handles form submissions and integrates with Salesforce and Five9 APIs.
|
||||||
Version: 1.0
|
Version: 1.1
|
||||||
Author: Anthony Volpe
|
Author: Anthony Volpe
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Function to handle the Salesforce form submission
|
// Salesforce configuration
|
||||||
|
define('SALESFORCE_OID', "00D1I000000mJ0Q");
|
||||||
|
define('LEAD_SOURCE', "Web");
|
||||||
|
define('MEMBER_STATUS', "Web response");
|
||||||
|
|
||||||
|
// Handle the Salesforce form submission
|
||||||
function postsf($data)
|
function postsf($data)
|
||||||
{
|
{
|
||||||
$oid = "00D1I000000mJ0Q";
|
|
||||||
$lead_source = "Web";
|
|
||||||
$member_status = "Web response";
|
|
||||||
$campaign_ID = isset($data['campaign_ID']) ? $data['campaign_ID'] : '';
|
|
||||||
|
|
||||||
$cleanPOST = array(
|
$cleanPOST = array(
|
||||||
'first_name' => stripslashes($data['first-name']),
|
'first_name' => sanitize_text_field($data['first-name']),
|
||||||
'last_name' => stripslashes($data['last-name']),
|
'last_name' => sanitize_text_field($data['last-name']),
|
||||||
'phone' => stripslashes($data['phone']),
|
'phone' => sanitize_text_field($data['phone']),
|
||||||
'email' => stripslashes($data['your-email']),
|
'email' => sanitize_email($data['your-email']),
|
||||||
'zip' => stripslashes($data['zip']),
|
'zip' => sanitize_text_field($data['zip']),
|
||||||
'Campaign_ID' => $campaign_ID,
|
'Campaign_ID' => sanitize_text_field($data['campaign_ID']),
|
||||||
'oid' => $oid,
|
'oid' => SALESFORCE_OID,
|
||||||
'lead_source' => $lead_source,
|
'lead_source' => LEAD_SOURCE,
|
||||||
'Custom_Field_1__c' => stripslashes($data['subid1']),
|
'Custom_Field_1__c' => sanitize_text_field($data['subid1']),
|
||||||
'Custom_Field_2__c' => stripslashes($data['subid2']),
|
'Custom_Field_2__c' => sanitize_text_field($data['subid2']),
|
||||||
'Custom_Field_3__c' => stripslashes($data['subid3']),
|
'Custom_Field_3__c' => sanitize_text_field($data['subid3']),
|
||||||
'Custom_Field_4__c' => stripslashes($data['subid4']),
|
'Custom_Field_4__c' => sanitize_text_field($data['subid4'])
|
||||||
);
|
);
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, "https://webto.salesforce.com/servlet/servlet.WebToLead");
|
curl_setopt($ch, CURLOPT_URL, "https://webto.salesforce.com/servlet/servlet.WebToLead");
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Added to ensure the response is captured
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response for handling
|
||||||
|
curl_setopt($ch, CURLOPT_HEADER, true); // Include headers in output for detailed debugging
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Get HTTP response code
|
||||||
$error = curl_error($ch);
|
$error = curl_error($ch);
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
|
// Check if CURL encountered an error
|
||||||
if ($error) {
|
if ($error) {
|
||||||
return "Salesforce Error: $error";
|
return "CURL Error: $error";
|
||||||
} else {
|
|
||||||
return json_decode($response, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if Salesforce returned an HTTP error
|
||||||
|
if ($http_code >= 400) {
|
||||||
|
return "Salesforce HTTP Error: Code $http_code. Response: $response";
|
||||||
|
}
|
||||||
|
|
||||||
|
// If response is empty, indicate failure explicitly
|
||||||
|
return $response ? $response : "Failed to submit lead to Salesforce.";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to handle the Five9 form submission
|
|
||||||
|
// Handle the Five9 form submission
|
||||||
function post59($data) {
|
function post59($data) {
|
||||||
$Campaign_ID = '701130000026vNy';
|
$Campaign_ID = '701130000026vNy';
|
||||||
date_default_timezone_set('America/New_York');
|
date_default_timezone_set('America/New_York');
|
||||||
$F9Date = date("Y-m-d")."-". date("H:i");
|
|
||||||
$F9domain = "connect america";
|
$F9domain = "connect america";
|
||||||
$F9list = isset($data['callback']) ? $data['callback'] : '';
|
$F9list = sanitize_text_field($data['callback']);
|
||||||
$newphone = preg_replace('/^1|\D/', '', $data['phone']);
|
$newphone = preg_replace('/^1|\D/', '', sanitize_text_field($data['phone']));
|
||||||
|
$webDialerKey = "YOUR_WEB_DIALER_KEY"; // Replace with actual WebDialer key
|
||||||
|
|
||||||
$cleanPOST = array(
|
$cleanPOST = array(
|
||||||
'first_name' => stripslashes($data['first-name']),
|
'first_name' => sanitize_text_field($data['first-name']),
|
||||||
'last_name' => stripslashes($data['last-name']),
|
'last_name' => sanitize_text_field($data['last-name']),
|
||||||
'number1' => $newphone,
|
'number1' => $newphone,
|
||||||
'F9domain' => $F9domain,
|
'F9domain' => $F9domain,
|
||||||
'F9list' => $F9list,
|
'F9list' => $F9list,
|
||||||
'salesforce_id' => $Campaign_ID,
|
'salesforce_id' => $Campaign_ID,
|
||||||
'Device_6' => '',
|
'WebDialer_Key' => $webDialerKey,
|
||||||
'WebDialer_Key' => $F9Date,
|
|
||||||
'F9key' => $F9Date,
|
|
||||||
'F9CallASAP' => true
|
'F9CallASAP' => true
|
||||||
);
|
);
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, "https://api.five9.com/web2campaign/AddToList");
|
curl_setopt($ch, CURLOPT_URL, "https://api.five9.com/web2campaign/AddToList");
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
$response = curl_exec($ch);
|
|
||||||
|
|
||||||
if ($response === false) {
|
$response = curl_exec($ch);
|
||||||
$error = curl_error($ch);
|
curl_close($ch);
|
||||||
curl_close($ch);
|
|
||||||
return "Five9 Error: $error";
|
// Parse HTML response with DOMDocument
|
||||||
} else {
|
$dom = new DOMDocument;
|
||||||
curl_close($ch);
|
@$dom->loadHTML($response); // Suppress warnings for HTML formatting issues
|
||||||
return json_decode($response, true);
|
|
||||||
|
$parsed_response = [
|
||||||
|
'error_code' => '',
|
||||||
|
'error_description' => ''
|
||||||
|
];
|
||||||
|
|
||||||
|
$inputs = $dom->getElementsByTagName('input');
|
||||||
|
foreach ($inputs as $input) {
|
||||||
|
$name = $input->getAttribute('name');
|
||||||
|
$value = $input->getAttribute('value');
|
||||||
|
|
||||||
|
if ($name === 'F9errCode') {
|
||||||
|
$parsed_response['error_code'] = $value;
|
||||||
|
} elseif ($name === 'F9errDesc') {
|
||||||
|
$parsed_response['error_description'] = $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return json_encode($parsed_response);
|
||||||
}
|
}
|
||||||
|
|
||||||
// REST API endpoint registration
|
// Register REST API route for form submission
|
||||||
add_action('rest_api_init', function () {
|
add_action('rest_api_init', function () {
|
||||||
register_rest_route('affiliates/v1', '/form', array(
|
register_rest_route('affiliates/v1', '/form', array(
|
||||||
'methods' => 'POST',
|
'methods' => 'POST',
|
||||||
'callback' => 'handle_form_submission',
|
'callback' => 'handle_form_submission',
|
||||||
|
'permission_callback' => '__return_true'
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Function to handle the form submission and trigger both postsf() and post59()
|
// Callback function to handle the form submission
|
||||||
function handle_form_submission($request)
|
// Handle REST API response formatting
|
||||||
{
|
function handle_form_submission($request) {
|
||||||
$params = $request->get_params();
|
$data = $request->get_params();
|
||||||
$response1 = postsf($params);
|
$response1 = postsf($data);
|
||||||
$response2 = post59($params);
|
$response2 = post59($data);
|
||||||
|
|
||||||
return array(
|
return new WP_REST_Response(array(
|
||||||
'salesforce_response' => $response1,
|
'salesforce_response' => $response1 ? "Success" : "Failed",
|
||||||
'five9_response' => $response2
|
'five9_response' => strip_tags($response2)
|
||||||
);
|
), 200);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user