SalesPad API sample post data Curl

Here is a sample of PHP that is used with a salespad API

/* SalesPad API Start */ 
	$enabled = 1;
	if($enabled == 1){    
		$Username = "user";
		$Password = "password"; 
		$Port = "6174"; 
		$SessionID = "";

		// Get Static Fields 
		$Address_Code		= "PRIMARY";
		$Primary_Addr_Code	= "PRIMARY";
		$Primary_Bill_To_Addr_Code = "PRIMARY";
		$Primary_Ship_To_Addr_Code = "PRIMARY";
		$Statement_To_Addr_Code = "PRIMARY";				
		$Bill_To_Address_Code = "PRIMARY";
		$Ship_To_Address_Code = "PRIMARY"; 

		$Payment_Terms    	= "30% 60% 10%";
		$Shipping_Method  	= "BEST WAY";				
		$Currency_ID      	= "Z-US$";				
		$Tax_Schedule		= "AVATAX"; 			
		$Country_Code		= "USA";
		$Customer_Class		= "Wholesale"; 
		$Sales_Territory	= ""; 
		$Sales_Doc_Type		= "QUOTE"; 
		$Sales_Doc_ID		= "DocID"; 	
		$Sales_Batch		= "NEWLEAD"; 	
		$Warehouse_Code		= "MAIN";
		$Price_Level		= "RETAIL";
		$Created_By			= "CAT";  
		
		$Error_Email_To		= "email@email.com";
		$Primary_Code 		= "PRIMARY";
		$Master_Num			= ""; 

		// Get Dynamic Fields							
		$Modified_On		= date('Y-m-d');				
		$Created_On			= date('Y-m-d');  
		$Customer_Num 		= trim(substr(str_replace(" ", "",$_POST['company_name']),0,6)).substr($_POST['phone_number'],-4);	
		$Customer_Num    	= strtoupper($Customer_Num);
		$Contact_Person		= $_POST['first_name']." ".$_POST['last_name'];		
		$JobTitle			= $_POST['job_title'];
		$EmailToAddress  	= $_POST['email'];
		$Short_Name			= $_POST['company_name'];
		$User_Def_1			= $_POST['phone_number'];
		$Statement_Name     = $_POST['company_name'];
		$LeadRating 		= $_POST['lead_rating'];
		//$Primary_Addr_Code	= $posted_data['your-address'];
		$User_Def_2			= ""; 
		$Primary_Bill_To_Addr_Code = $_POST['zip'];
		$Comments			= "Purchase Timeframe:".$_POST['purchase_timeframe']." | What is Your Budget?:".$_POST['budget']." | Follow-Up Required?:".$_POST['follow_up'];	
		$Notes				= $_POST['notes'];
		$Note				= $_POST['notes'];
		//$Primary_Addr_Code  = $posted_data['billing_postcode'];
		$Alt_Company_Name	= $_POST['company_name']; 
		$Company_Name		= trim($_POST['company_name']); 
		$Address_Line_1 	= $_POST['street_address']; 
		$Address_Line_2 	= ""; 
		$City			 	= $_POST['city']; 
		$State			 	= $_POST['state'];
		if($_POST['state'] == "UT North" || $_POST['state'] == "UT Mid" || $_POST['state'] == "UT South"){
			$State			= "UT";
		}
		$Zip			 	= $_POST['zip'];
		$Phone_1			= $_POST['phone_number'];
		$Email			 	= $_POST['email'];
		$CampaignTerm 		= $_POST['CampaignTerm'];
		$CampaingSource 	= $_POST['CampaingSource'];
		$LeadSource 		= $_POST['source'];
		$Comment			= $_POST['source']; 
		$DeviceType 		= "";  
		if($_POST['country'] == "Other")
		{
			$Country			= $_POST['other_country'];	
		}
		else{
			$Country			= $_POST['country'];	
		}
		
		$ProductCatalog  = array();
		$ProductCatalogArray  = array($_POST['jmt_line_card'],
							$_POST['angle_roll'],
							$_POST['bandsaw'],
							$_POST['dishing_flanging'],
							$_POST['drill_line'],
							$_POST['fiber_laser'],
							$_POST['ironworker'],
							$_POST['lathe'],
							$_POST['mill'],
							$_POST['plasma_cutting'],
							$_POST['plate_roll'],
							$_POST['adr_press_brake'],
							$_POST['ads_press_brake'],
							$_POST['electric_press_brake'],
							$_POST['pbf_press_brake'],
							$_POST['servo_press_brake'],
							$_POST['punch_press'],
							$_POST['robotics'],
							$_POST['shear'],
							$_POST['weld_positioning']); 
		$ProductInterest = "";
		if(!empty($ProductCatalogArray)){
			foreach($ProductCatalogArray as $Catalog){
				if($Catalog != "")
				{
					$ProductInterest .= $Catalog.", ";
				}
			}
		} 
		$LeadNotes 			= "Product Interest:". rtrim($ProductInterest, ',');
		
		if($Company_Name != "")
		{
			$Customer_Name = $Company_Name;	
		}
		else{
			$Customer_Name = $Contact_Person;
		}   
		// Get SalesPad Session
		$curl = curl_init();
		curl_setopt_array($curl, array(
		  CURLOPT_PORT => $Port,
		  CURLOPT_URL => "http://salespad.com:6174/api/Session",
		  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(
			"authorization: Basic ".base64_encode($Username. ":" .$Password),
			"cache-control: no-cache",
			"postman-token: 552f5ef4-54e1-fec9-".getToken_send(4)."-5ddc5f037173"
		  ),
		));

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

		curl_close($curl);

		if ($err) {
		 // echo "Session cURL Error #:" . $err;
		 $SessionError = json_decode($err, true); 
		} else { 
		  $SessionResult = json_decode($SessionResponse, true); 
		  $SessionID = $SessionResult["SessionID"];
		}    
		if(isset($SessionID) && $SessionID != "")																																																																																																																																																																																																				        { 
		// Create SalesPad Customer
		$curl = curl_init();					
		curl_setopt_array($curl, array(
		  CURLOPT_PORT => $Port,
		  CURLOPT_URL => "http://salespad.com:6174/api/Customer",
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "POST",
		  CURLOPT_POSTFIELDS => "{\n\"Customer_Name\": \"".$Customer_Name."\",
		  						\n\"Customer_Class\": \"".$Customer_Class."\",
								\n\"EmailToAddress\": \"".$EmailToAddress."\",
								\n\"Short_Name\": \"".$Short_Name."\",
								\n\"Statement_Name\": \"".$Statement_Name."\",
								\n\"User_Def_1\": \"".$User_Def_1."\",
								\n\"Primary_Addr_Code\": \"".$Primary_Addr_Code."\", 
								\n\"Primary_Bill_To_Addr_Code\": \"".$Primary_Bill_To_Addr_Code."\",
								\n\"Primary_Ship_To_Addr_Code\": \"".$Primary_Ship_To_Addr_Code."\",
								\n\"Statement_To_Addr_Code\": \"".$Statement_To_Addr_Code."\", 
								\n\"User_Def_2\": \"".$User_Def_2."\",
								\n\"Comment_1\": \"".$Comment."\", 
								\n\"Payment_Terms\":\"".$Payment_Terms."\",
								\n\"Shipping_Method\":\"".$Shipping_Method."\",
								\n\"Currency_ID\":\"".$Currency_ID."\",
								\n\"Tax_Schedule\":\"".$Tax_Schedule."\", 
								\n\"Customer_Num\":\"".$Customer_Num."\",
								\n\"Sales_Person_ID\":\"".$Sales_Person_ID."\",
								\n\"Sales_Territory\":\"".$Sales_Territory."\"}\n",
		  CURLOPT_HTTPHEADER => array(
			"cache-control: no-cache",
			"content-type: application/json",
			"postman-token: 78b11b6d-7fdb-34b2-".getToken_send(4)."-8eb81f713862",
			"session-id: ".$SessionID
		  ),
		));
		
		$CustomerResponse = curl_exec($curl);
		$err = curl_error($curl);					
		curl_close($curl);					
		if ($err) {
		 // echo "Customer cURL Error #:" . $err;
		 $CustomerError = json_decode($err, true); 
		 //$CustomerError['ErrorCode'] == 5001 
		} else { 
		  $CustomerResult = json_decode($CustomerResponse, true);  
		}   
		if(isset($CustomerResult["ErrorCode"]) && $CustomerResult["ErrorCode"] == 5001){
			//$LeadSource = "Existing Customer";
			// Update Customer If Exist
			$curl = curl_init();					
			curl_setopt_array($curl, array(
			  CURLOPT_PORT => $Port,
			  CURLOPT_URL => "http://salespad.com:6174/api/Customer/".$Customer_Num,
			  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",
				"content-type: application/json",
				"postman-token: 78b11b6d-7fdb-34b2-".getToken_send(4)."-8eb81f713862",
				"session-id: ".$SessionID
			  ),
			));
			
			$CustomerGetResponse = curl_exec($curl);
			$err = curl_error($curl);					
			curl_close($curl);					
			if ($err) {
			 // echo "Customer cURL Error #:" . $err;
			 $CustomerGetError = json_decode($err, true); 
			 //$CustomerError['ErrorCode'] == 5001 
			} else { 
			  $CustomerGetResult = json_decode($CustomerGetResponse, true);  
			}  
			$curl = curl_init();  
			if(isset($CustomerGetResult['UserFieldData'][0]) && $CustomerGetResult['UserFieldData'][0] != "" && $CustomerGetResult['UserFieldData'][0] != "EASTEC")
			{  
				curl_setopt_array($curl, array(
				  CURLOPT_PORT => $Port,
				  CURLOPT_URL => "http://salespad.jmtusa.com:6174/api/Customer/".$Customer_Num,
				  CURLOPT_RETURNTRANSFER => true,
				  CURLOPT_ENCODING => "",
				  CURLOPT_MAXREDIRS => 10,
				  CURLOPT_TIMEOUT => 30,
				  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				  CURLOPT_CUSTOMREQUEST => "PUT",
				  CURLOPT_POSTFIELDS => "{\n\"Customer_Name\": \"".$Customer_Name."\",
										\n\"Customer_Class\": \"".$Customer_Class."\",
										\n\"EmailToAddress\": \"".$EmailToAddress."\",
										\n\"Short_Name\": \"".$Short_Name."\",
										\n\"Statement_Name\": \"".$Statement_Name."\",
										\n\"User_Def_1\": \"".$User_Def_1."\",
										\n\"Primary_Addr_Code\": \"".$Primary_Addr_Code."\", 
										\n\"Primary_Bill_To_Addr_Code\": \"".$Primary_Bill_To_Addr_Code."\",
										\n\"Primary_Ship_To_Addr_Code\": \"".$Primary_Ship_To_Addr_Code."\",
										\n\"Statement_To_Addr_Code\": \"".$Statement_To_Addr_Code."\", 
										\n\"User_Def_2\": \"".$User_Def_2."\",
										\n\"Comment_1\": \"".$Comment."\", 
										\n\"Payment_Terms\":\"".$Payment_Terms."\",
										\n\"Shipping_Method\":\"".$Shipping_Method."\",
										\n\"Currency_ID\":\"".$Currency_ID."\",
										\n\"Tax_Schedule\":\"".$Tax_Schedule."\", 
										\n\"Customer_Num\":\"".$Customer_Num."\",
										\n\"Sales_Person_ID\":\"".$Sales_Person_ID."\",
										\n\"Sales_Territory\":\"".$Sales_Territory."\",
										\r\n  \"UserFieldData\": [\r\n    \"\",\r\n    \"\",\r\n    \"\"\r\n  ],\r\n  \"UserFieldNames\": [\r\n    \"xDealerinfo\",\r\n    \"xExemptionType\",\r\n    \"xshipping\"\r\n  ],\r\n}\n",
				  CURLOPT_HTTPHEADER => array(
					"cache-control: no-cache",
					"content-type: application/json",
					"postman-token: 78b11b6d-7fdb-34b2-".getToken_send(4)."-8eb81f713862",
					"session-id: ".$SessionID
				  ),
				));
			}
			else{  
				curl_setopt_array($curl, array(
				  CURLOPT_PORT => $Port,
				  CURLOPT_URL => "http://salespad.com:6174/api/Customer/".$Customer_Num,
				  CURLOPT_RETURNTRANSFER => true,
				  CURLOPT_ENCODING => "",
				  CURLOPT_MAXREDIRS => 10,
				  CURLOPT_TIMEOUT => 30,
				  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				  CURLOPT_CUSTOMREQUEST => "PUT",
				  CURLOPT_POSTFIELDS => "{\n\"Customer_Name\": \"".$Customer_Name."\",
										\n\"Customer_Class\": \"".$Customer_Class."\",
										\n\"EmailToAddress\": \"".$EmailToAddress."\",
										\n\"Short_Name\": \"".$Short_Name."\",
										\n\"Statement_Name\": \"".$Statement_Name."\",
										\n\"User_Def_1\": \"".$User_Def_1."\",
										\n\"Primary_Addr_Code\": \"".$Primary_Addr_Code."\", 
										\n\"Primary_Bill_To_Addr_Code\": \"".$Primary_Bill_To_Addr_Code."\",
										\n\"Primary_Ship_To_Addr_Code\": \"".$Primary_Ship_To_Addr_Code."\",
										\n\"Statement_To_Addr_Code\": \"".$Statement_To_Addr_Code."\", 
										\n\"User_Def_2\": \"".$User_Def_2."\",
										\n\"Comment_1\": \"".$Comment."\", 
										\n\"Payment_Terms\":\"".$Payment_Terms."\",
										\n\"Shipping_Method\":\"".$Shipping_Method."\",
										\n\"Currency_ID\":\"".$Currency_ID."\",
										\n\"Tax_Schedule\":\"".$Tax_Schedule."\", 
										\n\"Customer_Num\":\"".$Customer_Num."\",
										\n\"Sales_Person_ID\":\"".$Sales_Person_ID."\",
										\n\"Sales_Territory\":\"".$Sales_Territory."\",
										\r\n  \"UserFieldData\": [\r\n    \"".$LeadSource."\",\r\n    \"\",\r\n    \"\",\r\n    \"\"\r\n  ],\r\n  \"UserFieldNames\": [\r\n    \"xcustlead\",\r\n    \"xDealerinfo\",\r\n    \"xExemptionType\",\r\n    \"xshipping\"\r\n  ],\r\n}\n",
				  CURLOPT_HTTPHEADER => array(
					"cache-control: no-cache",
					"content-type: application/json",
					"postman-token: 78b11b6d-7fdb-34b2-".getToken_send(4)."-8eb81f713862",
					"session-id: ".$SessionID
				  ),
				));
			}  
			
			$CustomerUpdateResponse = curl_exec($curl);
			$err = curl_error($curl);					
			curl_close($curl);					
			if ($err) {
			 // echo "Customer cURL Error #:" . $err;
			 $CustomerUpdateError = json_decode($err, true); 
			 //$CustomerError['ErrorCode'] == 5001 
			} else { 
			  $CustomerUpdateResult = json_decode($CustomerUpdateResponse, true);  
			}  
			// Create SalesPad Customer Address
			$curl = curl_init();

			curl_setopt_array($curl, array(
			  CURLOPT_PORT => $Port,
			  CURLOPT_URL => "http://salespad.jmtusa.com:6174/api/CustomerAddr/".$Customer_Num."/".$Address_Code,
			  CURLOPT_RETURNTRANSFER => true,
			  CURLOPT_ENCODING => "",
			  CURLOPT_MAXREDIRS => 10,
			  CURLOPT_TIMEOUT => 30,
			  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			  CURLOPT_CUSTOMREQUEST => "PUT",
			  CURLOPT_POSTFIELDS => "{\r\n  \"Customer_Num\": \"".$Customer_Num."\", 
			  \r\n  \"Contact_Person\": \"".$Contact_Person."\",
			  \r\n  \"Sales_Person_ID\": \"".$Sales_Person_ID."\",
			  \r\n  \"Shipping_Method\": \"".$Shipping_Method."\",
			  \r\n  \"Tax_Schedule\": \"".$Tax_Schedule."\",
			  \r\n  \"Alt_Company_Name\": \"".$Alt_Company_Name."\", 
			  \r\n  \"Country\": \"".$Country."\",
			  \r\n  \"Address_Code\": \"".$Address_Code."\",
			  \r\n  \"Country_Code\": \"".$Country_Code."\", 
			  \r\n  \"Address_Line_1\": \"".$Address_Line_1."\",
			  \r\n  \"Address_Line_2\": \"".$Address_Line_2."\",
			  \r\n  \"City\": \"".$City."\",
			  \r\n  \"State\": \"".$State."\",
			  \r\n  \"Zip\": \"".$Zip."\",
			  \r\n  \"Phone_1\": \"".$Phone_1."\",
			  \r\n  \"Email\": \"".$Email."\",
			  \r\n  \"Created_On\": \"".$Created_On."\",
			  \r\n  \"Modified_On\": \"".$Modified_On."\",
			  \r\n  \"Warehouse_Code\": \"".$Warehouse_Code."\",
			  \r\n  \"Sales_Territory\": \"".$Sales_Territory."\",
			  \r\n\"UserFieldData\": [\r\n    \"".$JobTitle."\"\r\n  ],\r\n  \"UserFieldNames\": [\r\n    \"xcajobtilte\"\r\n  ]\r\n}",
			  CURLOPT_HTTPHEADER => array(
				"cache-control: no-cache",
				"postman-token: 7b17d2a9-084b-0e18-".getToken_send(4)."-733e7151a6ca",
				"session-id: ".$SessionID
			  ),
			));
			
			$CustomerAddrUpdateResponse = curl_exec($curl);
			$err = curl_error($curl);
			
			curl_close($curl);
			
			if ($err) {
			    //echo "CustomerAddr cURL Error #:" . $err;
			} else {
			  $CustomerAddrUpdateResult = json_decode($CustomerAddrUpdateResponse, true); 
			}  
		}
		if(isset($CustomerResult["ErrorCode"]) && $CustomerResult["ErrorCode"] != 5001){
			 // Error email Message 
			$to = $Error_Email_To;
			$subject = 'SalesPad Error';						
			$headers = 'From: Jason <email@email.com>' . "\r\n"; 
			$headers .= "Reply-To: Jason <email@email.com>" . "\r\n"; 
			$headers .= "MIME-Version: 1.0\r\n";
			$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
			
			$errorMessage = "";
			if(!empty($CustomerResult['Messages']))
			{
				$errorMessage .= "<ul>";
				foreach($CustomerResult['Messages'] as $errorMsg){
					$errorMessage .= "<li>".$errorMsg."</li>";
				}
				$errorMessage .= "</ul>";
			}
			$message = '
			<html>
			<head>
			  <title>SalesPad Error</title>
			</head>
			<body>
			  <h2>Customer Information</h2>
			  <table>
				<tr>
				  <th><strong>Name: </strong></th><td>'.$Contact_Person.'</td>
				</tr> 
				<tr>
				  <th><strong>Email: </strong></th><td>'.$Email.'</td>
				</tr> 
				<tr>
				  <th><strong>Phone: </strong></th><td>'.$Phone_1.'</td>
				</tr> 
			  </table>
			  <h2>SalesPad Error</h2> 
			  <table>
				<tr>
				  <th><strong>StatusCode: </strong></th><td>'.$CustomerResult['StatusCode'].'</td>
				</tr> 
				<tr>
				  <th><strong>ErrorCode: </strong></th><td>'.$CustomerResult['ErrorCode'].'</td>
				</tr> 
				<tr>
				  <th><strong>ErrorCodeMessage: </strong></th><td>'.$CustomerResult['ErrorCodeMessage'].'</td>
				</tr> 
				<tr>
				  <th><strong>Messages: </strong></th><td>'.$errorMessage.'</td>
				</tr> 
				<tr>
				  <th><strong>Website: </strong></th><td>http://www.jmtusa.com/cat/</td>
				</tr> 
			  </table>
			</body>
			</html>
			';
			mail($to, $subject, $message, $headers);
		 }    
		if(isset($CustomerResult["Customer_Num"]) && $CustomerResult["Customer_Num"] != "")
		{ 		
			// Update Customer 
			$curl = curl_init();

			curl_setopt_array($curl, array(
			  CURLOPT_PORT => $Port,
			  CURLOPT_URL => "http://salespad.com:6174/api/Customer/".$Customer_Num,
			  CURLOPT_RETURNTRANSFER => true,
			  CURLOPT_ENCODING => "",
			  CURLOPT_MAXREDIRS => 10,
			  CURLOPT_TIMEOUT => 30,
			  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			  CURLOPT_CUSTOMREQUEST => "PUT",
			  CURLOPT_POSTFIELDS => "{ \r\n  \"UserFieldData\": [\r\n    \"".$LeadSource."\",\r\n    \"\",\r\n    \"\",\r\n    \"\"\r\n  ],\r\n  \"UserFieldNames\": [\r\n    \"xcustlead\",\r\n    \"xDealerinfo\",\r\n    \"xExemptionType\",\r\n    \"xshipping\"\r\n  ],\r\n} ",
			  CURLOPT_HTTPHEADER => array(
				"cache-control: no-cache",
				"postman-token: 4451890f-8f20-5057-".getToken_send(4)."-908adfd2e55e",
				"session-id: ".$SessionID
			  ),
			));
			
			$UpdateCustomerResponse = curl_exec($curl);
			$err = curl_error($curl);
			
			curl_close($curl);
			
			if ($err) {
			  //echo "cURL Error #:" . $err;
			} else {
			  $UpdateCustomerResult = json_decode($UpdateCustomerResponse, true);  
			}
			
		
			// Create SalesPad Customer Address
			$curl = curl_init();

			curl_setopt_array($curl, array(
			  CURLOPT_PORT => $Port,
			  CURLOPT_URL => "http://salespad.com:6174/api/CustomerAddr",
			  CURLOPT_RETURNTRANSFER => true,
			  CURLOPT_ENCODING => "",
			  CURLOPT_MAXREDIRS => 10,
			  CURLOPT_TIMEOUT => 30,
			  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			  CURLOPT_CUSTOMREQUEST => "POST",
			  CURLOPT_POSTFIELDS => "{\r\n  \"Customer_Num\": \"".$Customer_Num."\", 
			  \r\n  \"Contact_Person\": \"".$Contact_Person."\",
			  \r\n  \"Sales_Person_ID\": \"".$Sales_Person_ID."\",
			  \r\n  \"Shipping_Method\": \"".$Shipping_Method."\",
			  \r\n  \"Tax_Schedule\": \"".$Tax_Schedule."\",
			  \r\n  \"Alt_Company_Name\": \"".$Alt_Company_Name."\", 
			  \r\n  \"Country\": \"".$Country."\",
			  \r\n  \"Address_Code\": \"".$Address_Code."\",
			  \r\n  \"Country_Code\": \"".$Country_Code."\", 
			  \r\n  \"Address_Line_1\": \"".$Address_Line_1."\",
			  \r\n  \"Address_Line_2\": \"".$Address_Line_2."\",
			  \r\n  \"City\": \"".$City."\",
			  \r\n  \"State\": \"".$State."\",
			  \r\n  \"Zip\": \"".$Zip."\",
			  \r\n  \"Phone_1\": \"".$Phone_1."\",
			  \r\n  \"Email\": \"".$Email."\",
			  \r\n  \"Created_On\": \"".$Created_On."\",
			  \r\n  \"Modified_On\": \"".$Modified_On."\",
			  \r\n  \"Warehouse_Code\": \"".$Warehouse_Code."\",
			  \r\n  \"Sales_Territory\": \"".$Sales_Territory."\"}",
			  CURLOPT_HTTPHEADER => array(
				"cache-control: no-cache",
				"postman-token: 7b17d2a9-084b-0e18-".getToken_send(4)."-733e7151a6ca",
				"session-id: ".$SessionID
			  ),
			));
			
			$CustomerAddrResponse = curl_exec($curl);
			$err = curl_error($curl);
			
			curl_close($curl);
			
			if ($err) {
			    //echo "CustomerAddr cURL Error #:" . $err;
			} else {
			  $CustomerAddrResult = json_decode($CustomerAddrResponse, true); 
			} 
			
			/* Update Customer Address*/ 

			$curl = curl_init();
			
			curl_setopt_array($curl, array(
			  CURLOPT_PORT => "6174",
			  CURLOPT_URL => "http://salespad.com:6174/api/CustomerAddr/".$Customer_Num."/".$Address_Code,
			  CURLOPT_RETURNTRANSFER => true,
			  CURLOPT_ENCODING => "",
			  CURLOPT_MAXREDIRS => 10,
			  CURLOPT_TIMEOUT => 30,
			  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			  CURLOPT_CUSTOMREQUEST => "PUT",
			  CURLOPT_POSTFIELDS => "{\r\n\"UserFieldData\": [\r\n    \"".$JobTitle."\"\r\n  ],\r\n  \"UserFieldNames\": [\r\n    \"xcajobtilte\"\r\n  ]\r\n  }",
			  CURLOPT_HTTPHEADER => array(
				"cache-control: no-cache",
				"postman-token: f1988c8e-369c-f0b6-".getToken_send(4)."-7a4707adbe03",
				"session-id: ".$SessionID
			  ),
			));
			
			$response = curl_exec($curl);
			$err = curl_error($curl);
			
			curl_close($curl);
			
			if ($err) {
			  //echo "cURL Error #:" . $err;
			} else {
			 // echo $response;
			}
			 
		}
		// Create SalesPad SalesDocument
		$curl = curl_init(); 
		curl_setopt_array($curl, array(
		  CURLOPT_PORT => $Port,
		  CURLOPT_URL => "http://salespad.com:6174/api/salesdocument",
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "POST",
		  CURLOPT_POSTFIELDS => "{ \r\n  \"Sales_Doc_Type\": \"".$Sales_Doc_Type."\",
		  \r\n  \"Sales_Doc_Num\": \"\",
		  \r\n  \"Sales_Doc_ID\": \"".$Sales_Doc_ID."\",
		  \r\n  \"Doc_Date\": \"".$Created_On."\",
		  \r\n  \"Source\": \" \",
		  \r\n  \"Sales_Batch\": \"".$Sales_Batch."\",
		  \r\n  \"Customer_Num\": \"".$Customer_Num."\",
		  \r\n  \"Customer_Name\": \"".$Customer_Name."\",
		  \r\n  \"Email\": \"".$Email."\",
		  \r\n  \"Bill_To_Address_Code\": \"".$Bill_To_Address_Code."\",
		  \r\n  \"Ship_To_Address_Code\": \"".$Ship_To_Address_Code."\",					  
		  \r\n  \"Ship_To_Name\": \"".$Contact_Person."\",
		  \r\n  \"Contact_Person\": \"".$Contact_Person."\",
		  \r\n  \"Address_Line_1\": \"".$Address_Line_1."\",
		  \r\n  \"Address_Line_2\": \"".$Address_Line_2."\", 
		  \r\n  \"State\": \"".$State."\",
		  \r\n  \"City\": \"".$City."\",
		  \r\n  \"Zip\": \"".$Zip."\",
		  \r\n  \"Country\": \"".$Country."\",
		  \r\n  \"Country_Code\": \"".$Country_Code."\",
		  \r\n  \"Phone_1\": \"".$Phone_1."\", 
		  \r\n  \"Price_Level\": \"".$Price_Level."\",				  
		  \r\n  \"Status\": \"\",
		  \r\n  \"Shipping_Method\": \"".$Shipping_Method."\",
		  \r\n  \"Warehouse_Code\": \"".$Warehouse_Code."\",
		  \r\n  \"Sales_Person_ID\": \"".$Sales_Person_ID."\",
		  \r\n  \"Sales_Territory\": \"".$Sales_Territory."\",		  
		  \r\n  \"Payment_Terms\": \"".$Payment_Terms."\",   
		  \r\n  \"Currency_ID\": \"".$Currency_ID."\",
		  \r\n  \"Comments\": \"".$Comments."\",
		  \r\n  \"Notes\": \"".$Notes."\",
		  \r\n  \"Created_On\": \"".$Created_On."\",
		  \r\n  \"Order_Date\": \"".$Created_On."\",
		  \r\n  \"Quote_Date\": \"".$Created_On."\",
		  \r\n  \"Created_By\": \"".$Created_By."\",\r\n}",
		  CURLOPT_HTTPHEADER => array(
			"cache-control: no-cache",
			"content-type: application/json",
			"postman-token: b9d10a38-a9f1-6f2b-".getToken_send(4)."-41e07f2b7c4d",
			"session-id: ".$SessionID
		  ),
		));
		
		$SalesResponse = curl_exec($curl);
		$err = curl_error($curl);
		
		curl_close($curl);
		
		if ($err) { 
		  //echo "Sales cURL Error #:" . $err;
		} else { 
		  $SalesResult = json_decode($SalesResponse, true);  
		}  
		if(isset($SalesResult["Sales_Doc_Num"]) && $SalesResult["Sales_Doc_Num"] != ""){ 
				// Update SalesPad SalesDocument
				$curl = curl_init();
					curl_setopt_array($curl, array(
					  CURLOPT_PORT => $Port,
					  CURLOPT_URL => "http://salespad.jmtusa.com:6174/api/SalesDocument/".$Sales_Doc_Type."/".$SalesResult["Sales_Doc_Num"],
					  CURLOPT_RETURNTRANSFER => true,
					  CURLOPT_ENCODING => "",
					  CURLOPT_MAXREDIRS => 10,
					  CURLOPT_TIMEOUT => 30,
					  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					  CURLOPT_CUSTOMREQUEST => "PUT",
					  CURLOPT_POSTFIELDS => "{\r\n\"UserFieldData\": [\r\n\t\"".$LeadSource."\",\r\n\t\"".$CampaignTerm."\",\r\n\t\"".$CampaingSource."\",\r\n\t\"".$DeviceType."\",\r\n\t\"".$LeadRating."\",\r\n\t\"".$LeadNotes."\"\r\n  ],\r\n  \"UserFieldNames\": [\r\n    \"xcustlead\",\r\n    \"xCampaignTerm\",\r\n    \"xCampaingSource\",\r\n\t\"xDeviceType\",\r\n\t\"xRating\",\r\n\t\"xLeadNotes\"\r\n  ]\r\n}",
					  CURLOPT_HTTPHEADER => array(
						"cache-control: no-cache",
						"postman-token: 98f3754c-70ec-1e52-".getToken_send(4)."-6184f02fe560",
						"session-id: ".$SessionID
					  ),
					));
					
					$UpdateSalesResponse = curl_exec($curl);
					$err = curl_error($curl);
					
					curl_close($curl);
					
					if ($err) {
					 // echo "cURL Error #:" . $err;
					} else {
					  $UpdateSalesResult = json_decode($UpdateSalesResponse, true); 
					}
			}
	}
		
	} 
	/* SalesPad API End */