Created
September 2, 2012 05:14
-
-
Save zvineyard/3595004 to your computer and use it in GitHub Desktop.
PHP: Barfy Old Authorize.net Procedural PHP Gateway Gross
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Settings | |
$post_url = "https://test.authorize.net/gateway/transact.dll"; | |
$db_host = "localhost"; | |
$db_user = "root"; | |
$db_pass = ""; | |
$db_name = "payments"; | |
$db_table = "transaction_log"; | |
// Setup the array with data to post to authorize.net | |
$post_values = array( | |
"x_login" => "********", // Given to you by Authorize.net (make sure you replace!) | |
"x_tran_key" => "****************", // Given to you by Authorize.net (make sure you replace!) | |
"x_version" => "3.1", | |
"x_delim_data" => "TRUE", | |
"x_delim_char" => "|", | |
"x_relay_response" => "FALSE", | |
"x_type" => "AUTH_CAPTURE", | |
"x_method" => "CC", | |
"x_card_num" => $_POST['cc_card_num'], | |
"x_exp_date" => $_POST['cc_exp_date'], | |
"x_amount" => $_POST['cc_amount'], | |
"x_description" => $_POST['cc_description'], | |
// The Values Below are Optional | |
"x_first_name" => "John", | |
"x_last_name" => "Doe", | |
"x_address" => "1234 Street", | |
"x_state" => "CA", | |
"x_zip" => "90210" | |
); | |
// This section takes the input fields and converts them to the proper format for an http post. For example: "x_login=username&x_tran_key=a1B2c3D4" | |
$post_string = ""; | |
foreach( $post_values as $key => $value ) { | |
$post_string .= "$key=" . urlencode( $value ) . "&"; | |
} | |
$post_string = rtrim($post_string, "& "); | |
// This sample code uses the CURL library for php to establish a connection, submit the post, and record the response. If you receive an error, you may want to ensure that you have the curl library enabled in your php configuration | |
$request = curl_init($post_url); // initiate curl object | |
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response | |
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1) | |
curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data | |
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. | |
$post_response = curl_exec($request); // execute curl post and store results in $post_response | |
curl_close ($request); // close curl object | |
// This line takes the response and breaks it into an array using the specified delimiting character | |
$response_array = explode($post_values["x_delim_char"],$post_response); | |
/* | |
* Before we allow the system to do anything with the | |
* rest of the form data we need to make sure that the | |
* credit card number gets removed to prevent sensitive | |
* information from getting out just in case we are | |
* compromised. | |
*/ | |
unset($_POST['cc_card_num']); // remove the credit card info | |
print_r($response_array); | |
// ********************************************** | |
// NOW WE CAN DO STUFF WITH THE RESPONSE | |
// ********************************************** | |
// Connect to the database | |
$mysqli = new mysqli($db_host,$db_user,$db_pass,$db_name); | |
// Check db connection | |
if(mysqli_connect_errno()) die(mysqli_connect_error()); | |
// Store the form info | |
$query = "INSERT INTO ".$db_table." SET pid = '".$mysqli->real_escape_string($mysqli,$_POST['page_id'])."', response_code = '".$response_array[0]."', payment_data = '".serialize($_POST)."', response_data = '".serialize($response_array)."'"; | |
if(!$mysqli->query($query)) { | |
echo $mysqli->error; | |
} | |
unset($_POST); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment