and here's the PHP code.
Now, I didn't write this but have to have this functional asap - a lot of pressure. I'm reading everything I can find, but if you can help, I'd really appreciate it.
Remember - it's for CIM - I'm trying to get the ProfileID then next, get the PaymentProfileID.
<?php
$api_login_id='999999999999';
$api_transaction_key='999999999999999';
$serverName = "CASS\SQLEXPRESS";
$connectionInfo = array( "Database"=>"XXXX", "UID"=>"xxxxxxx", "PWD"=>"xxxxx");
$params = array();
if (isset($_POST["txtcustomerID"]))
{
if (isset($_POST["txtcustomerProfileID"]))
{
if ($_POST["txtcustomerProfileID"]=="0")
{
$ccexp=$_POST["cc_expiryyy"]."-".$_POST["cc_expirymm"];
$fname=$_POST["btfname"];
$lname=$_POST["btlname"];
create_update_customer_profile($_POST["cc_num"],$ccexp,$fname,$lname,$_POST["btaddr"],$_POST["btcity"],$_POST["btstate"],
$_POST["btzip"],$_POST["btemail"],'100.00',$_POST["txtcustomerID"],"","");
}
else
{
$ccexp=$_POST["cc_expiryyy"]."-".$_POST["cc_expirymm"];
//$cname = split(" ", $_POST["btname"]);
$fname=$_POST["btfname"];
$lname=$_POST["btlname"];
create_update_customer_profile($_POST["cc_num"],$ccexp,$fname,$lname,$_POST["btaddr"],$_POST["btcity"],$_POST["btstate"],
$_POST["btzip"],$_POST["btemail"],'100.00',$_POST["txtcustomerID"],$_POST["txtcustomerProfileID"],$_POST["txtcustomerPaymentProfileID"]);
}
}
}
/////////////////////////FUNCTIONS FOR CIM//////////////////////////
function create_update_customer_profile($cardNumber,$expirationDate,$billTo_firstName,$billTo_lastName,$billTo_address,$billTo_city,$billTo_state,$billTo_zip,
$email,$add_amount,$custid,$custPrID,$custPayPrID)
{
require('include/authorizenet.cim.class.php');
global $api_login_id,$api_transaction_key;
// createCustomerProfileRequest()
$cim = new AuthNetCim($api_login_id, $api_transaction_key, true); // Need to change here mohit
$shipTo_firstName=$billTo_firstName;
$shipTo_lastName=$billTo_lastName;
$shipTo_address=$billTo_address;
$shipTo_city=$billTo_city;
$shipTo_state=$billTo_state;
$shipTo_zip=$billTo_zip;
// Choose a payment type - (creditCard or bankAccount) REQUIRED
//global $cardNumber,$expirationDate,$billTo_firstName,$billTo_lastName,$billTo_address,$billTo_city,$billTo_state,$billTo_zip,$shipTo_firstName,$shipTo_lastName,
$shipTo_address,$shipTo_city,$shipTo_state,$shipTo_zip,$email;
$cim->setParameter('paymentType', 'creditCard');
$cim->setParameter('cardNumber', $cardNumber);
$cim->setParameter('expirationDate', $expirationDate); // (YYYY-MM)
echo "<BR>paymentType-creditcard<BR>cardNumber-".$cardNumber."<BR>expirationDate-".$expirationDate."<BR><BR>";
// Some Billing address information is required and some is optional
// depending on your Address Verification Service (AVS) settings
$cim->setParameter('billTo_firstName', $billTo_firstName); // Up to 50 characters (no symbols)
$cim->setParameter('billTo_lastName', $billTo_lastName); // Up to 50 characters (no symbols)
//$cim->setParameter('billTo_company', 'Acme, Inc.'); // Up to 50 characters (no symbols) (optional)
$cim->setParameter('billTo_address', $billTo_address); // Up to 60 characters (no symbols)
$cim->setParameter('billTo_city', $billTo_city); // Up to 40 characters (no symbols)
$cim->setParameter('billTo_state', $billTo_state); // A valid two-character state code (US only) (optional)
$cim->setParameter('billTo_zip', $billTo_zip); // Up to 20 characters (no symbols)
$cim->setParameter('billTo_country', 'US'); // Up to 60 characters (no symbols) (optional)
echo "<BR>billTo_firstName-".$billTo_firstName."<BR>billTo_lastName-".$billTo_lastName."<BR>billTo_address-".$billTo_address."<BR>";
echo "<BR>billTo_state-".$billTo_state."<BR>billTo_zip-".$billTo_zip."<BR>billTo_country-US<BR><BR>";
//$cim->setParameter('billTo_phoneNumber', '555-555-5555'); // Up to 25 digits (no letters) (optional)
//$cim->setParameter('billTo_faxNumber', '444-444-4444'); // Up to 25 digits (no letters) (optional)
// In this method, shipping information is required because it reduces an extra
// step from having to create a shipping address in the future, therefore you can simply update it when needed.
// You can populate it with the billing info if you don't have an order form with shipping details.
$cim->setParameter('shipTo_firstName', $shipTo_firstName); // Up to 50 characters (no symbols)
$cim->setParameter('shipTo_lastName', $shipTo_lastName); // Up to 50 characters (no symbols)
//$cim->setParameter('shipTo_company', 'Acme, Inc.'); // Up to 50 characters (no symbols) (optional)
$cim->setParameter('shipTo_address', $shipTo_address); // Up to 60 characters (no symbols)
$cim->setParameter('shipTo_city', $shipTo_city); // Up to 40 characters (no symbols)
$cim->setParameter('shipTo_state', $shipTo_state); // A valid two-character state code (US only) (optional)
$cim->setParameter('shipTo_zip', $shipTo_zip); // Up to 20 characters (no symbols)
$cim->setParameter('shipTo_country', 'US'); // Up to 60 characters (no symbols) (optional)
echo "shipto_firstName-".$shipTo_firstName."<BR>shipto_lastName-".$shipTo_lastName."<BR>shipTo_address-".$shipTo_address."<BR>shipTo_city-".
$shipTo_city."<BR>shipTo_state-".$shipTo_state."<BR>shipTo_zip-".$shipTo_zip."<BR>shipTo_country-US<BR><BR>";
//$cim->setParameter('shipTo_phoneNumber', '555-555-5555'); // Up to 25 digits (no letters) (optional)
//$cim->setParameter('shipTo_faxNumber', '444-444-4444'); // Up to 25 digits (no letters) (optional)
// Merchant-assigned reference ID for the request
//$cim->setParameter('refId', 'my unique ref id'); // Up to 20 characters (optional)
// merchantCustomerId must be unique across all profiles
//$cim->setParameter('merchantCustomerId', 'my unique custid2'); // Up to 20 characters (optional)
// description must be unique across all profiles, if defined
$cim->setParameter('description', $billTo_lastName.', '.$billTo_firstName); // Up to 255 characters (optional)
echo "description-".$billTo_lastName.', '.$billTo_firstName."<BR><BR>";
// A receipt from authorize.net will be sent to the email address defined here
$cim->setParameter('email', $email); // Up to 255 characters (optional)
$cim->setParameter('customerType', 'individual'); // individual or business (optional)
echo "email-".$email."<BR>customerType-individual<BR><BR>";
if ($custPrID=="")
{
$cim->createCustomerProfileRequest();
$cust_profile_id="";
if ($cim->isSuccessful())
{
$cust_profile_id = $cim->customerProfileId;
echo "<table align=center><TR><TD><font style=font-family:arial;font-size:1.5em><B><BR>Profile created!</b><font style=font-
family:arial;font-size:1em><br /><br />";
insertCustomerProfileID($cust_profile_id, $custid,$billTo_firstName,$billTo_lastName,$billTo_address,$billTo_city,$billTo_state,
$billTo_zip,$email);
}
else
{
if($cim->code=='E00039')
{
if(strpos($cim->text,"A duplicate record with ID") !== false)
{
$cust_profile_id=substr($cim->text,strpos($cim->text,"ID")+3,8);
$cust_profile_id=trim($cust_profile_id);
}
}
else
{
echo "<table align=center><TR><TD><font style=font-family:arial;font-size:1.5em><B><BR>Profile NOT created!</b><BR><BR><font
style=font-family:arial;font-size:1em>Authorize.net could not be reached at this time. <BR>Please try again later<br /><br />";
}
}
echo "<BR><BR><font style=font-family:arial;font-size:.75em><a href=index.asp style=color:blue><u>Return</u></a><br /><br />";
$customer_payment_profile_id = get_customer_payment_profileid($cust_profile_id);
if ($custPrID=="")
{
insertCustomerPaymentProfileIDAndCustomerProfileID($cust_profile_id,$customer_payment_profile_id,$custid,$billTo_firstName,
$billTo_lastName,$billTo_address,$billTo_city,$billTo_state,$billTo_zip,$email);
}
// process payment
if($customer_payment_profile_id !='' || $customer_payment_profile_id != '0')
{
$transaction_id = cim_transaction($cust_profile_id,$customer_payment_profile_id,$add_amount);
}
}
else
{
$cim->setParameter('customerProfileId', $custPrID); // Numeric (required)
// Payment gateway assigned ID associated with the customer payment profile
$cim->setParameter('customerPaymentProfileId', $custPayPrID); // Numeric (required)
$cim->updateCustomerPaymentProfileRequest();
echo "<table align=center><TR><TD><font style=font-family:arial;font-size:1.5em><B><BR>Profile updated. <br />";
// process payment
$transaction_id = cim_transaction($custPrID,$custPayPrID,$add_amount);
}
}
function insertCustomerProfileID($CustomerProfileID,$custid,$fname,$lname,$btaddr,$btcity,$btstate,$btzip,$btemail)
{
global $serverName,$connectionInfo,$params;
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$sql_update = "UPDATE Contacts SET
Var116='$CustomerProfileID',var122='$fname',var123='$lname',var29='$btaddr',var30='$btcity',var31='$btstate',var32='$btzip',var17='$btemail' WHERE ID =".$custid;
sqlsrv_query( $conn, $sql_update, $params);
}
function insertCustomerPaymentProfileIDAndCustomerProfileID($CustomerProfileID,$CustomerPaymentProfileID,$custid,$fname,$lname,$btaddr,$btcity,$btstate,$btzip,
$btemail)
{
global $serverName,$connectionInfo,$params;
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$sql_update = "UPDATE Contacts SET Var116 ='$CustomerProfileID',Var117
='$CustomerPaymentProfileID',var122='$fname',var123='$lname',var29='$btaddr',var30='$btcity',var31='$btstate',var32='$btzip',var17='$btemail' WHERE ID =".$custid;
sqlsrv_query( $conn, $sql_update, $params);
}
function get_customer_payment_profileid($cust_profile_id)
{
require_once('include/authorizenet.cim.class.php');
global $api_login_id,$api_transaction_key;
$cim = new AuthNetCim($api_login_id, $api_transaction_key, true); // Need to chage here mohit
$cim->setParameter('customerProfileId', $cust_profile_id); // Numeric (required)
$cim->getCustomerProfileRequest();
if ($cim->isSuccessful())
{
$str = $cim->response;
}
else
{
}
////////////WRITE AND READ XML FILE///////////////////////////
if(!empty($str))
{
$str1 = explode("<?xml",$str);
$str2 = "<?xml ". $str1[1];
$myFile = "cim_xml_files/cust_profile_id_".$cust_profile_id.".xml";
$fh = fopen($myFile, 'w');
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$str2";
fwrite($fh, $stringData);
fclose($fh);
$XMLpath=$myFile;
$xml_data = fopen($XMLpath, "r");
$XMLfile=fread($xml_data,filesize($XMLpath));
$XMLparser = xml_parser_create();
xml_parse_into_struct($XMLparser, $XMLfile, $values, $index);
xml_parser_free($XMLparser);
for($i=0;$i<count($values);$i++)
{
/*
if(!empty($values[$i]['value']))
{
echo $values[$i]['tag'];
echo "=";
echo $values[$i]['value'];
}
echo "<br>";
*/
}
}
return $cim->customerPaymentProfileId;
}
function cim_transaction($customerProfileId,$customerPaymentProfileId,$transaction_amount)
{
require_once('include/authorizenet.cim.class.php');
$str = "";
$str1 = "";
global $api_login_id,$api_transaction_key;
$cim = new AuthNetCim($api_login_id, $api_transaction_key, true); // Need to chage here mohit
$cim->setParameter('transaction_amount', $transaction_amount); // Up to 4 digits with a decimal (required)
// transactionType = (profileTransCaptureOnly, profileTransAuthCapture or profileTransAuthOnly)
$cim->setParameter('transactionType', 'profileTransAuthOnly'); // see options above
// Payment gateway assigned ID associated with the customer profile
$cim->setParameter('customerProfileId', $customerProfileId); // Numeric (required)
// Payment gateway assigned ID associated with the customer payment profile
$cim->setParameter('customerPaymentProfileId', $customerPaymentProfileId); // Numeric (required)
// The tax exempt status
$cim->setParameter('transactionTaxExempt', 'false');
// The recurring billing status
$cim->setParameter('transactionRecurringBilling', 'false');
$cim->createCustomerProfileTransactionRequest();
if ($cim->isSuccessful())
{
$is_return = '1';
//echo "<br>".$cim->response;
$str = $cim->response;
echo "UPDATED<br>".$cim->directResponse;
echo "<br>";
//echo "<br>".$cim->validationDirectResponse;
//echo "<br>".$cim->resultCode;
//echo "<br>".$cim->code;
echo "<br>".$cim->text;
echo "<br>".$cim->refId;
echo "<br>".$cim->customerProfileId;
echo "<br>".$cim->customerPaymentProfileId;
echo "<br>".$cim->customerAddressId;
$str1 = explode(",Y,",$str);
$trans_id = substr($str1[1],0,10);
return $trans_id;
}
else
{
$is_return = '0';
//echo "NO<br>".$cim->directResponse;
//echo "<br>Validation=".$cim->validationDirectResponse;
//echo "<br>".$cim->resultCode;
//echo "<br>".$cim->code;
echo "<br>".$cim->text;
//echo "<br><pre>";
//print_r($cim->error_messages);
//echo "</pre>";
}
if($is_return=='1')
{
return('1');
}
// echo "<br>";
//echo "$str";
//exit;
}
function processCustomerByCustomerProfileID($CustomerProfileID,$CustomerPaymentProfileID,$add_amount)
{
if($CustomerPaymentProfileID !='' || $CustomerPaymentProfileID != '0')
{
$transaction_id = cim_transaction($CustomerProfileID,$CustomerPaymentProfileID,$add_amount);
}
}