From 9b1cb012647e38df78753ce48cb7d67202171ad6 Mon Sep 17 00:00:00 2001 From: Tony Volpe Date: Fri, 25 Oct 2024 17:23:44 -0400 Subject: [PATCH] TWEB-20: Host and Post --- .../affiliate-host-n-post/host-n-post.php | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/wp/wp-content/plugins/affiliate-host-n-post/host-n-post.php b/wp/wp-content/plugins/affiliate-host-n-post/host-n-post.php index 2d81b259..3c104db8 100644 --- a/wp/wp-content/plugins/affiliate-host-n-post/host-n-post.php +++ b/wp/wp-content/plugins/affiliate-host-n-post/host-n-post.php @@ -14,14 +14,29 @@ define('MEMBER_STATUS', "Web response"); // Handle the Salesforce form submission function postsf($data) { + // Fetch environment configuration + $salesforceEnvironment = get_option('select-environment'); + + // Set up Salesforce configuration based on the environment + if ($salesforceEnvironment == 'staging') { + $Campaign_ID = '7011I000000dF9n'; + $sfdc_oid = '00DDh0000009Umu'; + $webtolead_url = 'https://test.salesforce.com/servlet/servlet.WebToLead'; + } else { + $Campaign_ID = '7011I000000dDwA'; + $sfdc_oid = '00D1I000000mJ0Q'; + $webtolead_url = 'https://webto.salesforce.com/servlet/servlet.WebToLead'; + } + + // Prepare POST data with the selected environment's `Campaign_ID` and `sfdc_oid` $cleanPOST = array( '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, + 'Campaign_ID' => $Campaign_ID, // Dynamic Campaign ID + 'oid' => $sfdc_oid, // Dynamic OID based on environment 'lead_source' => LEAD_SOURCE, 'Custom_Field_1__c' => sanitize_text_field($data['subid1']), 'Custom_Field_2__c' => sanitize_text_field($data['subid2']), @@ -30,19 +45,18 @@ function postsf($data) ); $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, "https://webto.salesforce.com/servlet/servlet.WebToLead"); + curl_setopt($ch, CURLOPT_URL, $webtolead_url); // Dynamic URL based on environment curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST)); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response for handling - curl_setopt($ch, CURLOPT_HEADER, true); // Include headers in output for detailed debugging + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HEADER, true); $response = curl_exec($ch); - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Get HTTP response code + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); - // Check if CURL encountered an error if ($error) { return "CURL Error: $error"; } @@ -56,8 +70,7 @@ function postsf($data) return $response ? $response : "Failed to submit lead to Salesforce."; } - -// Handle the Five9 form submission +// Function to handle the Five9 form submission function post59($data) { $Campaign_ID = '701130000026vNy'; date_default_timezone_set('America/New_York'); @@ -66,6 +79,7 @@ function post59($data) { $newphone = preg_replace('/^1|\D/', '', sanitize_text_field($data['phone'])); $webDialerKey = "YOUR_WEB_DIALER_KEY"; // Replace with actual WebDialer key + // Prepare POST data $cleanPOST = array( 'first_name' => sanitize_text_field($data['first-name']), 'last_name' => sanitize_text_field($data['last-name']), @@ -110,12 +124,13 @@ function post59($data) { return json_encode($parsed_response); } + // 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' + 'permission_callback' => '__return_true' // Allows public access; adjust for security as needed )); }); @@ -128,6 +143,6 @@ function handle_form_submission($request) { return new WP_REST_Response(array( 'salesforce_response' => $response1 ? "Success" : "Failed", - 'five9_response' => strip_tags($response2) + 'five9_response' => strip_tags($response2) ), 200); }