/**
 * Process Purchase Form
 *
 * Handles the purchase form process.
 *
 * @access      private
 * @since       1.0
 * @version     1.0.8.1
 * @return      void
*/
function edd_process_purchase_form()
{
    // no need to run on admin
    if (is_admin()) {
        return;
    }
    // verify the nonce for this action
    if (!isset($_POST['edd-nonce']) || !wp_verify_nonce($_POST['edd-nonce'], 'edd-purchase-nonce')) {
        return;
    }
    // make sure the cart isn't empty
    $cart = edd_get_cart_contents();
    if (empty($cart)) {
        wp_die(sprintf(__('Your cart is empty, please return to the %ssite%s and try again.', 'edd'), '<a href="' . esc_url(home_url()) . '" title="' . get_bloginfo('name') . '">', '</a>'), __('Error', 'edd'));
    }
    // validate the form $_POST data
    $valid_data = edd_purchase_form_validate_fields();
    // allow themes and plugins to hoook to errors
    do_action('edd_checkout_error_checks', $_POST);
    // check errors
    if (false !== ($errors = edd_get_errors())) {
        // we have errors, send back to checkout
        edd_send_back_to_checkout('?payment-mode=' . $valid_data['gateway']);
        exit;
    }
    // check user
    if (false === ($user = edd_get_purchase_form_user($valid_data))) {
        // something went wrong when collecting data, send back to checkout
        edd_send_back_to_checkout('?payment-mode=' . $valid_data['gateway']);
        exit;
    }
    // setup user information
    $user_info = array('id' => $user['user_id'], 'email' => $user['user_email'], 'first_name' => $user['user_first'], 'last_name' => $user['user_last'], 'discount' => $valid_data['discount']);
    // setup purchase information
    $purchase_data = array('downloads' => edd_get_cart_contents(), 'subtotal' => edd_get_cart_amount(false), 'tax' => edd_get_cart_tax(), 'price' => edd_get_cart_amount(), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user['user_email'], 'date' => date('Y-m-d H:i:s'), 'user_info' => $user_info, 'post_data' => $_POST, 'cart_details' => edd_get_cart_content_details(), 'gateway' => $valid_data['gateway'], 'card_info' => $valid_data['cc_info']);
    // add the user data for hooks
    $valid_data['user'] = $user;
    // allow themes and plugins to hook before the gateway
    do_action('edd_checkout_before_gateway', $_POST, $user_info, $valid_data);
    // allow the purchase data to be modified before it is sent to the gateway
    $purchase_data = apply_filters('edd_purchase_data_before_gateway', $purchase_data, $valid_data);
    // if the total amount in the cart is 0, send to the manaul gateway. This emulates a free download purchase
    if ($purchase_data['price'] <= 0) {
        // revert to manual
        $valid_data['gateway'] = 'manual';
    }
    // used for showing download links to non logged-in users after purchase, and for other plugins needing purchase data.
    edd_set_purchase_session($purchase_data);
    // send info to the gateway for payment processing
    edd_send_to_gateway($valid_data['gateway'], $purchase_data);
    exit;
}
/**
 * Process Purchase Form
 *
 * Handles the purchase form process.
 *
 * @access      private
 * @since       1.0
 * @version     1.0.8.1
 * @return      void
*/
function edd_process_purchase_form()
{
    global $edd_options;
    // no need to run on admin
    if (is_admin()) {
        return;
    }
    // verify the nonce for this action
    if (!isset($_POST['edd-nonce']) || !wp_verify_nonce($_POST['edd-nonce'], 'edd-purchase-nonce')) {
        return;
    }
    // validate the form $_POST data
    $valid_data = edd_purchase_form_validate_fields();
    // allow themes and plugins to hoook to errors
    do_action('edd_checkout_error_checks', $_POST);
    // check errors
    if (false !== ($errors = edd_get_errors())) {
        // we have errors, send back to checkout
        edd_send_back_to_checkout('?payment-mode=' . $valid_data['gateway']);
        exit;
    }
    // check user
    if (false === ($user = edd_get_purchase_form_user($valid_data))) {
        // something went wrong when collecting data, send back to checkout
        edd_send_back_to_checkout('?payment-mode=' . $valid_data['gateway']);
        exit;
    }
    // setup user information
    $user_info = array('id' => $user['user_id'], 'email' => $user['user_email'], 'first_name' => $user['user_first'], 'last_name' => $user['user_last'], 'discount' => $valid_data['discount']);
    // setup purchase information
    $purchase_data = array('downloads' => edd_get_cart_contents(), 'price' => edd_get_cart_amount(), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user['user_email'], 'date' => date('Y-m-d H:i:s'), 'user_info' => $user_info, 'post_data' => $_POST, 'cart_details' => edd_get_cart_content_details(), 'gateway' => $valid_data['gateway'], 'card_info' => $valid_data['cc_info']);
    // add the user data for hooks
    $valid_data['user'] = $user;
    // allow themes and plugins to hook before the gateway
    do_action('edd_checkout_before_gateway', $_POST, $user_info, $valid_data);
    // allow the purchase data to be modified before it is sent to the gateway
    $purchase_data = apply_filters('edd_purchase_data_before_gateway', $purchase_data, $valid_data);
    // if the total amount in the cart is 0, send to the manaul gateway. This emulates a free download purchase
    if ($purchase_data['price'] <= 0) {
        // revert to manual
        $valid_data['gateway'] = 'manual';
    }
    if (isset($edd_options['show_links_on_success'])) {
        // used for showing download links to non logged-in users after purchase
        edd_set_purchase_session($purchase_data);
    }
    // send info to the gateway for payment processing
    edd_send_to_gateway($valid_data['gateway'], $purchase_data);
    exit;
}
/**
 * Process Purchase Form
 *
 * Handles the purchase form process.
 *
 * @access      private
 * @since       1.0
 * @version     1.0.8.1
 * @return      void
 */
function edd_process_purchase_form()
{
    // Make sure the cart isn't empty
    if (!edd_get_cart_contents()) {
        edd_set_error('empty_cart', __('Your cart is empty', 'edd'));
    } else {
        // Validate the form $_POST data
        $valid_data = edd_purchase_form_validate_fields();
        // Allow themes and plugins to hoook to errors
        do_action('edd_checkout_error_checks', $valid_data, $_POST);
    }
    $is_ajax = isset($_POST['edd_ajax']);
    $user = edd_get_purchase_form_user($valid_data);
    if (edd_get_errors() || !$user) {
        if ($is_ajax) {
            do_action('edd_ajax_checkout_errors');
            edd_die();
        } else {
            return false;
        }
    }
    if ($is_ajax) {
        echo 'success';
        edd_die();
    }
    // Setup user information
    $user_info = array('id' => $user['user_id'], 'email' => $user['user_email'], 'first_name' => $user['user_first'], 'last_name' => $user['user_last'], 'discount' => $valid_data['discount']);
    // Setup purchase information
    $purchase_data = array('downloads' => edd_get_cart_contents(), 'fees' => edd_get_cart_fees(), 'subtotal' => edd_get_cart_subtotal(), 'discount' => edd_get_cart_discounted_amount(), 'tax' => edd_get_cart_tax(), 'price' => edd_get_cart_total(), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user['user_email'], 'date' => date('Y-m-d H:i:s'), 'user_info' => $user_info, 'post_data' => $_POST, 'cart_details' => edd_get_cart_content_details(), 'gateway' => $valid_data['gateway'], 'card_info' => $valid_data['cc_info']);
    // Add the user data for hooks
    $valid_data['user'] = $user;
    // Allow themes and plugins to hook before the gateway
    do_action('edd_checkout_before_gateway', $_POST, $user_info, $valid_data);
    // Allow the purchase data to be modified before it is sent to the gateway
    $purchase_data = apply_filters('edd_purchase_data_before_gateway', $purchase_data, $valid_data);
    // If the total amount in the cart is 0, send to the manaul gateway. This emulates a free download purchase
    if (!$purchase_data['price']) {
        // Revert to manual
        $valid_data['gateway'] = 'manual';
    }
    // Used for showing download links to non logged-in users after purchase, and for other plugins needing purchase data.
    edd_set_purchase_session($purchase_data);
    // Send info to the gateway for payment processing
    edd_send_to_gateway($valid_data['gateway'], $purchase_data);
    edd_die();
}
/**
 * Process a straight-to-gateway purchase
 *
 * @since 1.7
 * @return void
 */
function edd_process_straight_to_gateway($data)
{
    $download_id = $data['download_id'];
    $options = isset($data['edd_options']) ? $data['edd_options'] : array();
    $quantity = isset($data['edd_download_quantity']) ? $data['edd_download_quantity'] : 1;
    if (empty($download_id) || !edd_get_download($download_id)) {
        return;
    }
    $purchase_data = edd_build_straight_to_gateway_data($download_id, $options, $quantity);
    edd_set_purchase_session($purchase_data);
    edd_send_to_gateway($purchase_data['gateway'], $purchase_data);
}
/**
 * Process a straight-to-gateway purchase
 *
 * @since 1.7
 * @return void
 */
function edd_process_straight_to_gateway($data)
{
    $download_id = $data['download_id'];
    $options = isset($data['edd_options']) ? $data['edd_options'] : array();
    $purchase_data = edd_build_straight_to_gateway_data($download_id, $options);
    edd_set_purchase_session($purchase_data);
    edd_send_to_gateway($purchase_data['gateway'], $purchase_data);
}