TWEB-20: Host and Post
This commit is contained in:
@@ -2,107 +2,132 @@
|
||||
/*
|
||||
Plugin Name: Affiliate Host 'n Post
|
||||
Description: Handles form submissions and integrates with Salesforce and Five9 APIs.
|
||||
Version: 1.0
|
||||
Version: 1.1
|
||||
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)
|
||||
{
|
||||
$oid = "00D1I000000mJ0Q";
|
||||
$lead_source = "Web";
|
||||
$member_status = "Web response";
|
||||
$campaign_ID = isset($data['campaign_ID']) ? $data['campaign_ID'] : '';
|
||||
|
||||
$cleanPOST = array(
|
||||
'first_name' => stripslashes($data['first-name']),
|
||||
'last_name' => stripslashes($data['last-name']),
|
||||
'phone' => stripslashes($data['phone']),
|
||||
'email' => stripslashes($data['your-email']),
|
||||
'zip' => stripslashes($data['zip']),
|
||||
'Campaign_ID' => $campaign_ID,
|
||||
'oid' => $oid,
|
||||
'lead_source' => $lead_source,
|
||||
'Custom_Field_1__c' => stripslashes($data['subid1']),
|
||||
'Custom_Field_2__c' => stripslashes($data['subid2']),
|
||||
'Custom_Field_3__c' => stripslashes($data['subid3']),
|
||||
'Custom_Field_4__c' => stripslashes($data['subid4']),
|
||||
'first_name' => sanitize_text_field($data['first-name']),
|
||||
'last_name' => sanitize_text_field($data['last-name']),
|
||||
'phone' => sanitize_text_field($data['phone']),
|
||||
'email' => sanitize_email($data['your-email']),
|
||||
'zip' => sanitize_text_field($data['zip']),
|
||||
'Campaign_ID' => sanitize_text_field($data['campaign_ID']),
|
||||
'oid' => SALESFORCE_OID,
|
||||
'lead_source' => LEAD_SOURCE,
|
||||
'Custom_Field_1__c' => sanitize_text_field($data['subid1']),
|
||||
'Custom_Field_2__c' => sanitize_text_field($data['subid2']),
|
||||
'Custom_Field_3__c' => sanitize_text_field($data['subid3']),
|
||||
'Custom_Field_4__c' => sanitize_text_field($data['subid4'])
|
||||
);
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, "https://webto.salesforce.com/servlet/servlet.WebToLead");
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
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);
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Get HTTP response code
|
||||
$error = curl_error($ch);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
// Check if CURL encountered an error
|
||||
if ($error) {
|
||||
return "Salesforce Error: $error";
|
||||
} else {
|
||||
return json_decode($response, true);
|
||||
return "CURL Error: $error";
|
||||
}
|
||||
|
||||
// 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) {
|
||||
$Campaign_ID = '701130000026vNy';
|
||||
date_default_timezone_set('America/New_York');
|
||||
$F9Date = date("Y-m-d")."-". date("H:i");
|
||||
$F9domain = "connect america";
|
||||
$F9list = isset($data['callback']) ? $data['callback'] : '';
|
||||
$newphone = preg_replace('/^1|\D/', '', $data['phone']);
|
||||
$F9list = sanitize_text_field($data['callback']);
|
||||
$newphone = preg_replace('/^1|\D/', '', sanitize_text_field($data['phone']));
|
||||
$webDialerKey = "YOUR_WEB_DIALER_KEY"; // Replace with actual WebDialer key
|
||||
|
||||
$cleanPOST = array(
|
||||
'first_name' => stripslashes($data['first-name']),
|
||||
'last_name' => stripslashes($data['last-name']),
|
||||
'first_name' => sanitize_text_field($data['first-name']),
|
||||
'last_name' => sanitize_text_field($data['last-name']),
|
||||
'number1' => $newphone,
|
||||
'F9domain' => $F9domain,
|
||||
'F9list' => $F9list,
|
||||
'salesforce_id' => $Campaign_ID,
|
||||
'Device_6' => '',
|
||||
'WebDialer_Key' => $F9Date,
|
||||
'F9key' => $F9Date,
|
||||
'WebDialer_Key' => $webDialerKey,
|
||||
'F9CallASAP' => true
|
||||
);
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, "https://api.five9.com/web2campaign/AddToList");
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
|
||||
|
||||
$response = curl_exec($ch);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
if ($response === false) {
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
return "Five9 Error: $error";
|
||||
} else {
|
||||
curl_close($ch);
|
||||
return json_decode($response, true);
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
// Parse HTML response with DOMDocument
|
||||
$dom = new DOMDocument;
|
||||
@$dom->loadHTML($response); // Suppress warnings for HTML formatting issues
|
||||
|
||||
$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 () {
|
||||
register_rest_route('affiliates/v1', '/form', array(
|
||||
'methods' => 'POST',
|
||||
'callback' => 'handle_form_submission',
|
||||
'permission_callback' => '__return_true'
|
||||
));
|
||||
});
|
||||
|
||||
// Function to handle the form submission and trigger both postsf() and post59()
|
||||
function handle_form_submission($request)
|
||||
{
|
||||
$params = $request->get_params();
|
||||
$response1 = postsf($params);
|
||||
$response2 = post59($params);
|
||||
// Callback function to handle the form submission
|
||||
// Handle REST API response formatting
|
||||
function handle_form_submission($request) {
|
||||
$data = $request->get_params();
|
||||
$response1 = postsf($data);
|
||||
$response2 = post59($data);
|
||||
|
||||
return array(
|
||||
'salesforce_response' => $response1,
|
||||
'five9_response' => $response2
|
||||
);
|
||||
return new WP_REST_Response(array(
|
||||
'salesforce_response' => $response1 ? "Success" : "Failed",
|
||||
'five9_response' => strip_tags($response2)
|
||||
), 200);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user