function espresso_display_firstdata_e4($payment_data)
{
    extract($payment_data);
    // Setup class
    include_once 'FirstDataE4.php';
    global $org_options;
    $myE4 = new Espresso_E4();
    // initiate an instance of the class
    echo '<!--Event Espresso E4.com Gateway Version ' . $myE4->gateway_version . '-->';
    $firstdata_e4_settings = get_option('event_espresso_firstdata_e4_settings');
    $firstdata_e4_login_id = empty($firstdata_e4_settings['firstdata_e4_login_id']) ? '' : $firstdata_e4_settings['firstdata_e4_login_id'];
    $firstdata_e4_transaction_key = empty($firstdata_e4_settings['firstdata_e4_transaction_key']) ? '' : $firstdata_e4_settings['firstdata_e4_transaction_key'];
    $button_type = empty($firstdata_e4_settings['button_type']) ? '' : $firstdata_e4_settings['button_type'];
    //$button_url = $firstdata_e4_settings['button_url'];
    $image_url = empty($firstdata_e4_settings['image_url']) ? '' : $firstdata_e4_settings['image_url'];
    $use_sandbox = $firstdata_e4_settings['use_sandbox'];
    $use_testmode = $firstdata_e4_settings['test_transactions'];
    if ($use_testmode == true) {
        // Enable test mode if needed
        $myE4->enableTestMode();
    }
    if ($use_sandbox) {
        // Enable test mode if needed
        $myE4->useTestServer();
    }
    $quantity = isset($quantity) && $quantity > 0 ? $quantity : espresso_count_attendees_for_registration($attendee_id);
    $myE4->setUserInfo($firstdata_e4_login_id, $firstdata_e4_transaction_key);
    $myE4->addField('x_amount', number_format($event_cost, 2));
    $myE4->addField('x_show_form', 'PAYMENT_FORM');
    $myE4->addField('x_reference_3', $registration_id . ' FDe4');
    $myE4->addField('x_relay_response', 'TRUE');
    if ($firstdata_e4_settings['force_ssl_return']) {
        $home = str_replace("http://", "https://", home_url());
    } else {
        $home = home_url();
    }
    $myE4->addField('x_relay_url', $home . '/?type=firstdata_e4&page_id=' . $org_options['return_url']);
    $myE4->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);
    $myE4->addField('x_logo_url', $image_url);
    //$myE4->addField('x_invoice_num', event_espresso_session_id());
    //Post variables
    $myE4->addField('x_cust_id', $attendee_id);
    $myE4->addField('x_first_name', $fname);
    $myE4->addField('x_last_name', $lname);
    $myE4->addField('x_email', $attendee_email);
    $myE4->addField('x_address', $address);
    $myE4->addField('x_city', $city);
    $myE4->addField('x_state', $state);
    $myE4->addField('x_zip', $zip);
    $myE4->addField('x_fp_sequence', $attendee_id);
    //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($firstdata_e4_settings['bypass_payment_page']) && $firstdata_e4_settings['bypass_payment_page'] == 'Y') {
        $myE4->submitPayment();
        //Enable auto redirect to payment site
    } else {
        if (empty($firstdata_e4_settings['button_url'])) {
            //$button_url = EVENT_ESPRESSO_GATEWAY_URL . "firstdata_e4/firstdata-logo.png";
            if (file_exists(EVENT_ESPRESSO_GATEWAY_DIR . "/firstdata_e4/firstdata-logo.png")) {
                $button_url = EVENT_ESPRESSO_GATEWAY_DIR . "/firstdata_e4/firstdata-logo.png";
            } else {
                $button_url = EVENT_ESPRESSO_PLUGINFULLURL . "gateways/firstdata_e4/firstdata-logo.png";
            }
        } elseif (isset($firstdata_e4_settings['button_url'])) {
            $button_url = $firstdata_e4_settings['button_url'];
        } else {
            //If no other buttons exist, then use the default location
            $button_url = EVENT_ESPRESSO_PLUGINFULLURL . "gateways/firstdata_e4/firstdata-logo.png";
        }
        $myE4->submitButton($button_url, 'firstdata_e4');
        //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>';
        $myE4->dump_fields();
        // for debugging, output a table of all the fields
    }
}
function espresso_process_firstdata_e4($payment_data)
{
    include_once 'FirstDataE4.php';
    $myE4 = new Espresso_E4();
    echo '<!--Event Espresso Exact.com Gateway Version ' . $myE4->gateway_version . '-->';
    // Log the IPN results
    $myE4->ipnLog = TRUE;
    $firstdata_e4_settings = get_option('event_espresso_firstdata_e4_settings');
    $firstdata_e4_login_id = $firstdata_e4_settings['firstdata_e4_login_id'];
    $firstdata_e4_transaction_key = $firstdata_e4_settings['firstdata_e4_transaction_key'];
    // Enable test mode if needed
    //4007000000027  <-- test successful visa
    //4222222222222  <-- test failure card number
    if ($firstdata_e4_settings['use_sandbox']) {
        $myE4->enableTestMode();
        $email_transaction_dump = true;
    }
    $payment_data['txn_type'] = 'FirstData E4';
    $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;
    // Specify your authorize login and secret
    $myE4->setUserInfo($firstdata_e4_login_id, $firstdata_e4_transaction_key);
    // Check validity and write down it
    if ($myE4->validateIpn()) {
        $payment_data['txn_id'] = $myE4->ipnData['x_trans_id'];
        $payment_data['txn_details'] = serialize($myE4->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 ($myE4->ipnData['x_response_code'] == 1) {
            ?>
			
			<p><?php 
            _e('Your transaction has been processed.', 'event_espresso');
            ?>
</p>
			<?php 
            $payment_data['payment_status'] = 'Completed';
        } else {
            ?>
			<h2 style="color:#F00;"><?php 
            _e('There was an error processing your transaction!', 'event_espresso');
            ?>
</h2>
			<p><strong>Error:</strong> (<?php 
            echo $response_reason_code;
            ?>
 - <?php 
            echo $response_reason_code;
            ?>
) - <?php 
            echo $response_reason_text;
            ?>
</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 = 'Exact.com Notification - Gateway Variable Dump';
            $body = "An authorize.net payment notification was successfully recieved\n";
            $body .= "from " . $myE4->ipnData['x_email'] . " on " . date('m/d/Y');
            $body .= " at " . date('g:i A') . "\n\nDetails:\n";
            foreach ($myE4->ipnData as $key => $value) {
                $body .= "\n{$key}: {$value}\n";
            }
            wp_mail($payment_data['contact'], $subject, $body);
        }
    } else {
        ?>
		<h2 style="color:#F00;"><?php 
        _e('There was an error processing your transaction!', 'event_espresso');
        ?>
</h2> <?php 
        if (is_writable('authorize.txt')) {
            file_put_contents('authorize.txt', "FAILURE\n\n" . $myE4->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 " . $myE4->ipnData['x_email'] . " on " . date('m/d/Y');
        $body .= " at " . date('g:i A') . "\n\nDetails:\n";
        foreach ($myE4->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;
}