Created
August 29, 2014 01:06
-
-
Save tinybike/da937efeda1746397e71 to your computer and use it in GitHub Desktop.
Authorize.Net transaction postback handler
This file contains 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 | |
/** | |
* Example Authorize.Net transaction postback handler. | |
* Writes transaction records to a MySQL database. | |
* | |
* Uses the Authorize.Net PHP SDK, which can be found at: | |
* https://github.com/AuthorizeNet/sdk-php | |
* | |
* Example HTML form: | |
* | |
* <form method='post' action="https://secure.authorize.net/gateway/transact.dll"> | |
* <INPUT TYPE=HIDDEN NAME="x_Cust_ID" VALUE="Tom"> | |
* <INPUT TYPE=HIDDEN NAME="x_Description" VALUE="MyPurchase"> | |
* <input type='hidden' name="x_login" value="<?php echo $api_login_id?>" /> | |
* <input type='hidden' name="x_fp_hash" value="<?php echo $fingerprint?>" /> | |
* <input type='hidden' name="x_amount" value="<?php echo $amount?>" /> | |
* <input type='hidden' name="x_fp_timestamp" value="<?php echo $fp_timestamp?>" /> | |
* <input type='hidden' name="x_fp_sequence" value="<?php echo $fp_sequence?>" /> | |
* <input type='hidden' name="x_version" value="3.1"> | |
* <input type='hidden' name="x_show_form" value="payment_form"> | |
* <input type='hidden' name="x_test_request" value="false" /> | |
* <input type='hidden' name="x_method" value="cc"> | |
* <input type='submit' value="Submit"> | |
* </form> | |
* | |
* (c) Jack Peterson ([email protected]), 4/2/2014 | |
*/ | |
// Import the Authorize.Net PHP SDK | |
require_once 'php_sdk/AuthorizeNet.php'; | |
// Connect to your MySQL database | |
function make_db_connection() { | |
// Enter your database connection info here | |
$db_name = ''; | |
$db_user = ''; | |
$db_pass = ''; | |
// Connect using mysqli | |
$db = new mysqli($db_host, $db_user, $db_pass, $db_name); | |
if ($db->connect_errno) { | |
exit(); | |
} | |
return $db; | |
} | |
// Insert the postback transaction record into your database | |
function insert_into_db($postback) { | |
$db = make_db_connection(); | |
$sql = "INSERT INTO transactions (x_response_code, | |
x_response_reason_code, | |
x_response_reason_text, | |
x_auth_code, | |
x_avs_code, | |
x_trans_id, | |
x_method, | |
x_card_type, | |
x_account_number, | |
x_first_name, | |
x_last_name, | |
x_company, | |
x_address, | |
x_city, | |
x_state, | |
x_zip, | |
x_country, | |
x_phone, | |
x_fax, | |
x_email, | |
x_invoice_num, | |
x_description, | |
x_type, | |
x_cust_id, | |
x_amount, | |
x_tax, | |
x_tax_exempt, | |
x_po_num, | |
x_MD5_Hash, | |
x_cvv2_resp_code, | |
x_cavv_response, | |
x_test_request, | |
x_catalog_link_id, | |
x_method_available) | |
VALUES | |
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, | |
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | |
// Parametrize and execute the query | |
$stmt = $db->stmt_init(); | |
$stmt->prepare($sql); | |
$stmt->bind_param('ssssssssssssssssssssssssssssssssss', | |
$postback['x_response_code'], | |
$postback['x_response_reason_code'], | |
$postback['x_response_reason_text'], | |
$postback['x_auth_code'], | |
$postback['x_avs_code'], | |
$postback['x_trans_id'], | |
$postback['x_method'], | |
$postback['x_card_type'], | |
$postback['x_account_number'], | |
$postback['x_first_name'], | |
$postback['x_last_name'], | |
$postback['x_company'], | |
$postback['x_address'], | |
$postback['x_city'], | |
$postback['x_state'], | |
$postback['x_zip'], | |
$postback['x_country'], | |
$postback['x_phone'], | |
$postback['x_fax'], | |
$postback['x_email'], | |
$postback['x_invoice_num'], | |
$postback['x_description'], | |
$postback['x_type'], | |
$postback['x_cust_id'], | |
$postback['x_amount'], | |
$postback['x_tax'], | |
$postback['x_tax_exempt'], | |
$postback['x_po_num'], | |
$postback['x_MD5_Hash'], | |
$postback['x_cvv2_resp_code'], | |
$postback['x_cavv_response'], | |
$postback['x_test_request'], | |
$postback['x_catalog_link_id'], | |
$postback['x_method_available']); | |
$stmt->execute(); | |
// Close the bound statement and database connection | |
$stmt->close(); | |
$db->close(); | |
} | |
// Main block | |
if (!count(debug_backtrace())) | |
{ | |
// Insert your API login ID and transaction key here | |
$api_login_id = ''; | |
$transaction_key = ''; | |
$amount = '1.00'; | |
$fp_timestamp = time(); | |
// Enter an invoice or other unique number | |
$fp_sequence = '123' . time(); | |
// Get the Authorize.Net SIM fingerprint | |
$fingerprint = AuthorizeNetSIM_Form::getFingerprint($api_login_id, | |
$transaction_key, | |
$amount, | |
$fp_sequence, | |
$fp_timestamp) | |
// Example postback data | |
$postback_data = Array( | |
'x_response_code' => 1, | |
'x_response_reason_code'=> 1, | |
'x_response_reason_text' => 'This transaction has been approved.', | |
'x_avs_code' => 'Z', | |
'x_auth_code' => '02181R', | |
'x_trans_id' => 6014951032, | |
'x_method' => 'CC', | |
'x_card_type' => 'Discover', | |
'x_account_number' => 'XXXX0000', | |
'x_first_name' => 'John', | |
'x_last_name' => 'Public', | |
'x_company' => '', | |
'x_address' => '101 Main Street', | |
'x_city' => 'Anytown', | |
'x_state' => 'CA', | |
'x_zip' => '00000', | |
'x_country' => 'USA', | |
'x_phone' => '', | |
'x_fax' => '', | |
'x_email' => '[email protected]', | |
'x_invoice_num' => '', | |
'x_description' => '', | |
'x_type' => 'auth_capture', | |
'x_cust_id' => '', | |
'x_ship_to_first_name' => 'John', | |
'x_ship_to_last_name' => 'Public', | |
'x_ship_to_company' => '101 Main Street', | |
'x_ship_to_address' => '', | |
'x_ship_to_city' => 'Anytown', | |
'x_ship_to_state' => 'CA', | |
'x_ship_to_zip' => '00000', | |
'x_ship_to_country' => 'USA', | |
'x_amount' => '1.00', | |
'x_tax' => '0.00', | |
'x_duty' => '0.00', | |
'x_freight' => '0.00', | |
'x_tax_exempt' => 'FALSE', | |
'x_po_num' => '', | |
'x_MD5_Hash' => 'F18E1B5AB7E265FEB11DCD0334ABEA51', | |
'x_cvv2_resp_code' => 'S', | |
'x_cavv_response' => '', | |
'x_test_request' => 'false', | |
'x_catalog_link_id' => 'f901720b-5a45-4cc4-b26e-faa2bc311ab5', | |
'x_method_available' => 'true' | |
); | |
insert_into_db($postback_data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment