function espresso_display_authnet($payment_data)
{
    extract($payment_data);
    // Setup class
    include_once 'Authorize.php';
    global $org_options, $wpdb;
    $myAuthorize = new Espresso_Authorize();
    // initiate an instance of the class
    echo '<!--Event Espresso Authorize.net Gateway Version ' . $myAuthorize->gateway_version . '-->';
    $authnet_settings = get_option('event_espresso_authnet_settings');
    $authnet_login_id = empty($authnet_settings['authnet_login_id']) ? '' : $authnet_settings['authnet_login_id'];
    $authnet_transaction_key = empty($authnet_settings['authnet_transaction_key']) ? '' : $authnet_settings['authnet_transaction_key'];
    $image_url = empty($authnet_settings['image_url']) ? '' : $authnet_settings['image_url'];
    $use_sandbox = $authnet_settings['use_sandbox'];
    $use_testmode = $authnet_settings['test_transactions'];
    if ($use_testmode) {
        // Enable test mode if needed
        $myAuthorize->enableTestMode();
    }
    if ($use_sandbox) {
        // Enable test mode if needed
        $myAuthorize->useTestServer();
    }
    $quantity = !empty($quantity) ? $quantity : espresso_count_attendees_for_registration($attendee_id);
    $myAuthorize->setUserInfo($authnet_login_id, $authnet_transaction_key);
    if ($authnet_settings['force_ssl_return']) {
        $home = str_replace('http:', 'https:', home_url());
    } else {
        $home = home_url();
    }
    $myAuthorize->addField('x_Relay_URL', $home . '/?page_id=' . $org_options['return_url'] . '&r_id=' . $registration_id . '&type=authnet');
    $myAuthorize->addField('x_Description', stripslashes_deep($event_name) . ' | ' . __('Reg. ID:', 'event_espresso') . ' ' . $attendee_id . ' | ' . __('Name:', 'event_espresso') . ' ' . stripslashes_deep($fname . ' ' . $lname) . ' | ' . __('Total Registrants:', 'event_espresso') . ' ' . $quantity);
    $myAuthorize->addField('x_Amount', number_format($event_cost, 2));
    $myAuthorize->addField('x_Logo_URL', $image_url);
    $myAuthorize->addField('x_Invoice_num', 'au-' . event_espresso_session_id());
    //Post variables
    $myAuthorize->addField('x_cust_id', $attendee_id);
    $myAuthorize->addField('x_first_name', $fname);
    $myAuthorize->addField('x_last_name', $lname);
    $myAuthorize->addField('x_Email', $attendee_email);
    $myAuthorize->addField('x_Address', $address);
    $myAuthorize->addField('x_City', $city);
    $myAuthorize->addField('x_State', $state);
    $myAuthorize->addField('x_Zip', $zip);
    $sql = "SELECT attendee_session FROM " . EVENTS_ATTENDEE_TABLE . " WHERE id='" . $attendee_id . "'";
    $session_id = $wpdb->get_var($sql);
    $sql = "SELECT a.final_price, a.quantity, ed.event_name, a.price_option, a.fname, a.lname FROM " . EVENTS_ATTENDEE_TABLE . " a JOIN " . EVENTS_DETAIL_TABLE . " ed ON a.event_id=ed.id ";
    $sql .= " WHERE attendee_session='" . $session_id . "' ORDER BY a.id ASC";
    $items = $wpdb->get_results($sql);
    foreach ($items as $key => $item) {
        $item_num = $key + 1;
        $myAuthorize->addLineItem($item_num, strlen($item->event_name) > 30 ? substr_replace($item->event_name, '', 30) : $item->event_name, substr_replace($item->price_option . ' for ' . $item->event_name . '. Attendee: ' . $item->fname . ' ' . $item->lname, '', 255), $item->quantity, $item->final_price, FALSE);
    }
    //Enable this function if you want to send payment notification before the person has paid.
    //This function is copied on the payment processing page
    //event_espresso_send_payment_notification($attendee_id, $txn_id, $amount_pd);
    //Decide if you want to auto redirect to your payment website or display a payment button.
    if (!empty($authnet_settings['bypass_payment_page']) && $authnet_settings['bypass_payment_page'] == 'Y') {
        $myAuthorize->submitPayment();
        //Enable auto redirect to payment site
    } else {
        if (empty($authnet_settings['button_url'])) {
            //$button_url = EVENT_ESPRESSO_GATEWAY_URL . "authnet/authnet-logo.png";
            if (file_exists(EVENT_ESPRESSO_GATEWAY_DIR . "/pay-by-credit-card.png")) {
                $button_url = EVENT_ESPRESSO_GATEWAY_DIR . "/pay-by-credit-card.png";
            } else {
                $button_url = EVENT_ESPRESSO_PLUGINFULLURL . "gateways/pay-by-credit-card.png";
            }
        } elseif (file_exists($authnet_settings['button_url'])) {
            $button_url = $authnet_settings['button_url'];
        } else {
            //If no other buttons exist, then use the default location
            $button_url = EVENT_ESPRESSO_PLUGINFULLURL . "gateways/pay-by-credit-card.png";
        }
        $myAuthorize->submitButton($button_url, 'authnet');
        //Display payment button
    }
    if ($use_sandbox) {
        echo '<p>Test credit card # 4007000000027</p>';
        echo '<h3 style="color:#ff0000;" title="Payments will not be processed">' . __('Debug Mode Is Turned On', 'event_espresso') . '</h3>';
        $myAuthorize->dump_fields();
        // for debugging, output a table of all the fields
    }
}
function espresso_process_authnet($payment_data)
{
    // Include the authorize.net library
    include_once 'Authorize.php';
    // Create an instance of the authorize.net library
    $myAuthorize = new Espresso_Authorize();
    echo '<!--Event Espresso Authorize.net SIM Gateway Version ' . $myAuthorize->gateway_version . '-->';
    // Log the IPN results
    $myAuthorize->ipnLog = TRUE;
    $authnet_settings = get_option('event_espresso_authnet_settings');
    $authnet_login_id = $authnet_settings['authnet_login_id'];
    $authnet_transaction_key = $authnet_settings['authnet_transaction_key'];
    // Enable test mode if needed
    //4007000000027  <-- test successful visa
    //4222222222222  <-- test failure card number
    if ($authnet_settings['use_sandbox']) {
        $myAuthorize->enableTestMode();
        $email_transaction_dump = true;
    }
    if ($authnet_settings['use_md5']) {
        $myAuthorize->enableUseMD5();
        $myAuthorize->setMD5Value($authnet_settings['authnet_md5_value']);
    }
    // Specify your authorize login and secret
    $myAuthorize->setUserInfo($authnet_login_id, $authnet_transaction_key);
    $payment_data['txn_type'] = 'authorize.net SIM';
    $payment_data['payment_status'] = "Incomplete";
    if (!empty($_REQUEST['x_trans_id'])) {
        $payment_data['txn_id'] = $_REQUEST['x_trans_id'];
    } else {
        $payment_data['txn_id'] = 0;
    }
    $payment_data['txn_details'] = serialize($_REQUEST);
    $curl_session_id = uniqid('', true);
    global $wpdb;
    $sql = "UPDATE " . EVENTS_ATTENDEE_TABLE . " SET attendee_session = '" . $curl_session_id . "' WHERE attendee_session ='" . $payment_data['attendee_session'] . "' ";
    $wpdb->query($sql);
    $payment_data['attendee_session'] = $curl_session_id;
    // Check validity and write down it
    if ($myAuthorize->validateIpn()) {
        $payment_data['txn_id'] = $myAuthorize->ipnData['x_trans_id'];
        $payment_data['txn_details'] = serialize($myAuthorize->ipnData);
        //file_put_contents('authorize.txt', 'SUCCESS' . date("m-d-Y")); //Used for debugging purposes
        //Be sure to echo something to the screen so authent knows that the ipn works
        //store the results in reusable variables
        if ($myAuthorize->ipnData['x_response_code'] == 1) {
            $myAuthorize->logResults(true);
            ?>
			<p><?php 
            _e('Your transaction has been processed.', 'event_espresso');
            ?>
</p>
			<?php 
            $payment_data['payment_status'] = 'Completed';
        } else {
            $myAuthorize->lastError = $respmsg;
            $myAuthorize->logResults(false);
            ?>
			<h2 style="color:#F00;"><?php 
            _e('There was an error processing your transaction!', 'event_espresso');
            ?>
</h2>
			<p><strong>Error:</strong> (Payment was declined)</p>
			<?php 
            $payment_data['payment_status'] = 'Payment Declined';
        }
        //Debugging option
        $email_transaction_dump = true;
        if ($email_transaction_dump == true) {
            // For this, we'll just email ourselves ALL the data as plain text output.
            $subject = 'Authorize.net Notification - Gateway Variable Dump';
            $body = "An authorize.net payment notification was successfully recieved\n";
            $body .= "from " . $myAuthorize->ipnData['x_email'] . " on " . date('m/d/Y');
            $body .= " at " . date('g:i A') . "\n\nDetails:\n";
            foreach ($myAuthorize->ipnData as $key => $value) {
                $body .= "\n{$key}: {$value}\n";
            }
            wp_mail($payment_data['contact'], $subject, $body);
        }
    } else {
        $myAuthorize->lastError = 'MD5 mismatch';
        $myAuthorize->logResults(false);
        ?>
		<h2 style="color:#F00;"><?php 
        _e('There was an error processing your transaction!', 'event_espresso');
        ?>
</h2>
		<p><strong>Error:</strong> (IPN response did not validate) </p>
		<?php 
        if (is_writable('authorize.txt')) {
            file_put_contents('authorize.txt', "FAILURE\n\n" . $myAuthorize->ipnData);
        }
        //echo something to the screen so authent knows that the ipn works
        $subject = 'Instant Payment Notification - Gateway Variable Dump';
        $body = "An instant payment notification failed\n";
        $body .= "from " . $myAuthorize->ipnData['x_email'] . " on " . date('m/d/Y');
        $body .= " at " . date('g:i A') . "\n\nDetails:\n";
        foreach ($myAuthorize->ipnData as $key => $value) {
            $body .= "\n{$key}: {$value}\n";
        }
        wp_mail($payment_data['contact'], $subject, $body);
    }
    //add_action('action_hook_espresso_email_after_payment', 'espresso_email_after_payment');
    return $payment_data;
}