/**
 * AJAX Validate Discount
 *
 * Validates the supplied discount.
 *
 * @access      private
 * @since       1.0
 * @return      string
*/
function edd_ajax_validate_discount()
{
    if (isset($_POST['code']) && check_ajax_referer('edd_ajax_nonce', 'nonce')) {
        $user = isset($_POST['user']) ? $_POST['user'] : $_POST['email'];
        $return = array('msg' => '', 'code' => $_POST['code']);
        if (edd_is_discount_used($_POST['code'], $user)) {
            // Called twice if discount is not used (again by edd_is_discount_valid) but allows for beter usr msg and less execution if discount is used.
            $return['msg'] = __('This discount code has been used already', 'edd');
        } else {
            if (edd_is_discount_valid($_POST['code'], $user)) {
                $price = edd_get_cart_amount();
                $discounted_price = edd_get_discounted_amount($_POST['code'], $price);
                $return = array('msg' => 'valid', 'amount' => edd_currency_filter(edd_format_amount($discounted_price)), 'code' => $_POST['code']);
            } else {
                $return['msg'] = __('The discount you entered is invalid', 'edd');
            }
        }
        echo json_encode($return);
    }
    die;
}
/**
 * Adds item to the cart via AJAX.
 *
 * @since 1.0
 * @return void
 */
function edd_ajax_get_subtotal()
{
    if (check_ajax_referer('edd_ajax_nonce', 'nonce')) {
        echo edd_currency_filter(edd_get_cart_amount(false));
    }
    edd_die();
}
/**
 * Get Cart Item Template
 *
 * @since 1.0
 * @param int $cart_key Cart key
 * @param array $item Cart item
 * @param bool $ajax AJAX?
 * @return string Cart item
*/
function edd_get_cart_item_template($cart_key, $item, $ajax = false)
{
    global $post;
    $id = is_array($item) ? $item['id'] : $item;
    $remove_url = edd_remove_item_url($cart_key, $post, $ajax);
    $title = get_the_title($id);
    $options = !empty($item['options']) ? $item['options'] : array();
    $price = edd_get_cart_item_price($id, $options);
    if (!empty($options)) {
        $title .= edd_has_variable_prices($item['id']) ? ' <span class="edd-cart-item-separator">-</span> ' . edd_get_price_name($id, $item['options']) : edd_get_price_name($id, $item['options']);
    }
    ob_start();
    edd_get_template_part('widget', 'cart-item');
    $item = ob_get_clean();
    $item = str_replace('{item_title}', $title, $item);
    $item = str_replace('{item_amount}', edd_currency_filter(edd_format_amount($price)), $item);
    $item = str_replace('{cart_item_id}', absint($cart_key), $item);
    $item = str_replace('{item_id}', absint($id), $item);
    $item = str_replace('{remove_url}', $remove_url, $item);
    $subtotal = '';
    if ($ajax) {
        $subtotal = edd_currency_filter(edd_get_cart_amount(false));
    }
    $item = str_replace('{subtotal}', $subtotal, $item);
    return apply_filters('edd_cart_item', $item, $id);
}
/**
 * Get Total Cart Amount
 *
 * Gets the fully formatted total price amount in the cart.
 * uses edd_get_cart_amount().
 *
 * @access      public
 * @since       1.3.3
 * @return      string - the cart amount
*/
function edd_cart_total($echo = true)
{
    $total = apply_filters('edd_cart_total', edd_currency_filter(edd_format_amount(edd_get_cart_amount())));
    if ($echo) {
        echo $total;
    }
    return $total;
}
/**
 * Determines what the currently selected gateway is
 *
 * If the cart amount is zero, no option is shown and the cart uses the manual
 * gateway to emulate a no-gateway-setup for a free download
 *
 * @access public
 * @since 1.3.2
 * @return string $enabled_gateway The slug of the gateway
 */
function edd_get_chosen_gateway()
{
    $gateways = edd_get_enabled_payment_gateways();
    if (isset($_GET['payment-mode'])) {
        $enabled_gateway = urldecode($_GET['payment-mode']);
    } else {
        if (count($gateways) >= 1 && !isset($_GET['payment-mode'])) {
            foreach ($gateways as $gateway_id => $gateway) {
                $enabled_gateway = $gateway_id;
                if (edd_get_cart_amount() <= 0) {
                    $enabled_gateway = 'manual';
                    // This allows a free download by filling in the info
                }
            }
        } else {
            if (edd_get_cart_amount() <= 0) {
                $enabled_gateway = 'manual';
            } else {
                $enabled_gateway = 'none';
            }
        }
    }
    return apply_filters('edd_chosen_gateway', $enabled_gateway);
}
/**
 * Stores the tax info in the payment meta
 *
 * @access      public
 * @since       1.3.3
 * @param 		$payment_meta array The meta data to store with the payment
 * @param 		$payment_data array The info sent from process-purchase.php
 * @return      array
*/
function edd_record_taxed_amount($payment_meta, $payment_data)
{
    if (!edd_use_taxes()) {
        return $payment_meta;
    }
    if (edd_local_taxes_only() && isset($_POST['edd_tax_opt_in'])) {
        // calculate local taxes
        $payment_meta['subtotal'] = edd_get_cart_amount(false);
        $payment_meta['tax'] = edd_get_cart_tax();
    } elseif (!edd_local_taxes_only()) {
        // calculate global taxes
        $payment_meta['subtotal'] = edd_get_cart_amount(false);
        $payment_meta['tax'] = edd_get_cart_tax();
    }
    return $payment_meta;
}
/**
 * Is Cart Minimum Met
 *
 * Checks to see if the minimum purchase amount has been met
 *
 * @access      public
 * @since       1.1.7
 * @return      void
*/
function edd_discount_is_min_met($code_id = null)
{
    $discount = edd_get_discount($code_id);
    $return = false;
    if ($discount) {
        $min = isset($discount['min_price']) ? $discount['min_price'] : 0;
        $cart_amount = edd_get_cart_amount();
        if ((double) $cart_amount >= (double) $min) {
            // minimum has been met
            $return = true;
        }
    }
    return apply_filters('edd_is_discount_min_met', $return, $code_id);
}
/**
 * Purchase Form Validate Gateway
 *
 * @access      private
 * @since       1.0
 * @return      string
*/
function edd_purchase_form_validate_gateway()
{
    // check if a gateway value is present
    if (isset($_POST['edd-gateway']) && trim($_POST['edd-gateway']) != '') {
        // clean gateway
        $gateway = strip_tags($_POST['edd-gateway']);
        // verify if gateway is active
        if (edd_is_gateway_active($gateway)) {
            // return active gateway
            return $gateway;
        } else {
            if (edd_get_cart_amount() <= 0) {
                return 'manual';
            } else {
                // set invalid gateway error
                edd_set_error('invalid_gateway', __('The selected gateway is not active', 'edd'));
            }
        }
    } else {
        // no gateway is present
        edd_set_error('empty_gateway', __('No gateway has been selected', 'edd'));
    }
    // return empty
    return '';
}
		<?php 
} else {
    ?>
			<tr class="edd_cart_item">
				<td colspan="3"  class="edd_cart_item_empty"><?php 
    do_action('edd_empty_cart');
    ?>
</td>
			</tr>
		<?php 
}
?>
	</tbody>
	<tfoot>
		<tr class="edd_cart_footer_row">
			<?php 
do_action('edd_checkout_table_footer_first');
?>
			<th colspan="3" class="edd_cart_total"><?php 
_e('Total', 'edd');
?>
: <span class="edd_cart_amount"><?php 
echo esc_html(edd_currency_filter(edd_format_amount(edd_get_cart_amount())));
?>
</span></th>
			<?php 
do_action('edd_checkout_table_footer_last');
?>
		</tr>
	</tfoot>
</table>
/**
 * Shows the final purchase total at the bottom of the checkout page
 *
 * @since 1.5
 * @return void
 */
function edd_checkout_final_total()
{
    ?>
	<fieldset id="edd_purchase_final_total">
		<p id="edd_final_total_wrap">
			<strong><?php 
    _e('Purchase Total:', 'edd');
    ?>
</strong>
			<span class="edd_cart_amount" data-subtotal="<?php 
    echo edd_get_cart_amount(false);
    ?>
" data-total="<?php 
    echo edd_get_cart_amount(true, true);
    ?>
"><?php 
    edd_cart_total();
    ?>
</span>
		</p>
	</fieldset>
<?php 
}
<li class="cart_item edd_subtotal"><?php 
echo __('Subtotal:', 'edd') . " <span class='subtotal'>" . edd_currency_filter(edd_get_cart_amount(false));
?>
</span></li>
<li class="cart_item edd_checkout"><a href="<?php 
echo edd_get_checkout_uri();
?>
"><?php 
_e('Checkout', 'edd');
?>
</a></li>
			</th>
			<?php 
    do_action('edd_checkout_table_tax_last');
    ?>
		</tr>
		<?php 
}
?>
		<tr class="edd_cart_footer_row">
			<?php 
do_action('edd_checkout_table_footer_first');
?>
			<th colspan="3" class="edd_cart_total"><?php 
_e('Total', 'edd');
?>
: <span class="edd_cart_amount" data-subtotal="<?php 
echo edd_get_cart_amount(false);
?>
" data-total="<?php 
echo edd_get_cart_amount(true, true);
?>
"><?php 
edd_cart_total();
?>
</span></th>
			<?php 
do_action('edd_checkout_table_footer_last');
?>
		</tr>
	</tfoot>
</table>
/**
 * Get Checkout Form
 *
 * @access      private
 * @since       1.0 
 * @return      string
*/
function edd_checkout_form()
{
    global $edd_options, $user_ID, $post;
    if (is_singular()) {
        $page_URL = get_permalink($post->ID);
    } else {
        $page_URL = 'http';
        if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
            $pageURL .= "s";
        }
        $page_URL .= "://";
        if (isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
        } else {
            $page_URL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
        }
    }
    if (is_user_logged_in()) {
        global $user_ID;
        $user_data = get_userdata($user_ID);
    }
    ob_start();
    ?>
		
		<?php 
    if (edd_get_cart_contents()) {
        ?>
				
			<?php 
        do_action('edd_before_checkout_cart');
        edd_checkout_cart();
        do_action('edd_after_checkout_cart');
        ?>
			
			<div id="edd_checkout_form_wrap" class="edd_clearfix">
			
				<?php 
        do_action('edd_checkout_form_top');
        $gateways = edd_get_enabled_payment_gateways();
        $show_gateways = false;
        if (count($gateways) > 1 && !isset($_GET['payment-mode'])) {
            $show_gateways = true;
            if (edd_get_cart_amount() <= 0) {
                $show_gateways = false;
            }
        }
        if ($show_gateways) {
            ?>
					<?php 
            do_action('edd_payment_mode_top');
            ?>
					<form id="edd_payment_mode" action="<?php 
            echo $page_URL;
            ?>
" method="GET">
						<fieldset id="edd_payment_mode_select">
							<?php 
            do_action('edd_payment_mode_before_gateways');
            ?>
							<p id="edd-payment-mode-wrap">
								<?php 
            echo '<select class="edd-select" name="payment-mode" id="edd-gateway">';
            foreach ($gateways as $gateway_id => $gateway) {
                echo '<option value="' . $gateway_id . '">' . $gateway['checkout_label'] . '</option>';
            }
            echo '</select>';
            echo '<label for="edd-gateway">' . __('Choose Your Payment Method', 'edd') . '</label>';
            ?>
							</p>
							<?php 
            do_action('edd_payment_mode_after_gateways');
            ?>
						</fieldset>
						<fieldset id="edd_payment_mode_submit">
							<p id="edd-next-submit-wrap">
								<?php 
            $color = isset($edd_options['checkout_color']) ? $edd_options['checkout_color'] : 'gray';
            ?>
 
								<span class="edd_button edd_<?php 
            echo $color;
            ?>
">
									<span class="edd_button_outer">
										<span class="edd_button_inner">
											<input type="submit" id="edd_next_button" class="edd_button_text edd-submit" value="<?php 
            _e('Next', 'edd');
            ?>
"/>
										</span>
									</span>
								</span>
							</p>
						</fieldset>
					</form>
					<?php 
            do_action('edd_payment_mode_bottom');
            ?>
			
				<?php 
        } else {
            ?>
		
					<?php 
            if (count($gateways) >= 1 && !isset($_GET['payment-mode'])) {
                foreach ($gateways as $gateway_id => $gateway) {
                    $enabled_gateway = $gateway_id;
                    if (edd_get_cart_amount() <= 0) {
                        $enabled_gateway = 'manual';
                        // this allows a free download by filling in the info
                    }
                }
            } else {
                if (edd_get_cart_amount() <= 0) {
                    $enabled_gateway = 'manual';
                } else {
                    $enabled_gateway = 'none';
                }
            }
            $payment_mode = isset($_GET['payment-mode']) ? urldecode($_GET['payment-mode']) : $enabled_gateway;
            ?>
					
					<?php 
            do_action('edd_before_purchase_form');
            ?>
					<form id="edd_purchase_form" action="<?php 
            echo $page_URL;
            ?>
" method="POST">					
					
						<?php 
            do_action('edd_purchase_form_top');
            ?>
					
						<?php 
            if (isset($edd_options['logged_in_only']) && !isset($edd_options['show_register_form'])) {
                if (is_user_logged_in()) {
                    $can_checkout = true;
                } else {
                    $can_checkout = false;
                }
            } elseif (isset($edd_options['show_register_form']) && isset($edd_options['logged_in_only'])) {
                $can_checkout = true;
            } elseif (!isset($edd_options['logged_in_only'])) {
                $can_checkout = true;
            }
            $can_checkout = true;
            if ($can_checkout) {
                ?>
							
							<?php 
                if (isset($edd_options['show_register_form']) && !is_user_logged_in() && !isset($_GET['login'])) {
                    ?>
								<div id="edd_checkout_login_register"><?php 
                    echo edd_get_register_fields();
                    ?>
</div>
							<?php 
                } elseif (isset($edd_options['show_register_form']) && !is_user_logged_in() && isset($_GET['login'])) {
                    ?>
								<div id="edd_checkout_login_register"><?php 
                    echo edd_get_login_fields();
                    ?>
</div>
							<?php 
                }
                ?>

							<?php 
                if (!isset($_GET['login']) && is_user_logged_in() || !isset($edd_options['show_register_form'])) {
                    ?>
											
							<fieldset id="edd_checkout_user_info">
								<legend><?php 
                    _e('Personal Info', 'edd');
                    ?>
</legend>
								<?php 
                    do_action('edd_purchase_form_before_email');
                    ?>
								<p id="edd-email-wrap">
									<input class="edd-input required" type="email" name="edd_email" placeholder="<?php 
                    _e('Email address', 'edd');
                    ?>
" id="edd-email" value="<?php 
                    echo is_user_logged_in() ? $user_data->user_email : '';
                    ?>
"/>
									<label class="edd-label" for="edd-email"><?php 
                    _e('Email Address', 'edd');
                    ?>
</label>
								</p>
								<?php 
                    do_action('edd_purchase_form_after_email');
                    ?>
								<p id="edd-first-name-wrap">
									<input class="edd-input required" type="text" name="edd_first" placeholder="<?php 
                    _e('First Name', 'edd');
                    ?>
" id="edd-first" value="<?php 
                    echo is_user_logged_in() ? $user_data->first_name : '';
                    ?>
"/>
									<label class="edd-label" for="edd-first"><?php 
                    _e('First Name', 'edd');
                    ?>
</label>
								</p>
								<p id="edd-last-name-wrap">
									<input class="edd-input" type="text" name="edd_last" id="edd-last" placeholder="<?php 
                    _e('Last name', 'edd');
                    ?>
" value="<?php 
                    echo is_user_logged_in() ? $user_data->last_name : '';
                    ?>
"/>
									<label class="edd-label" for="edd-last"><?php 
                    _e('Last Name', 'edd');
                    ?>
</label>
								</p>	
								<?php 
                    do_action('edd_purchase_form_user_info');
                    ?>
							</fieldset>	
							
							<?php 
                    do_action('edd_purchase_form_after_user_info');
                    ?>

							<?php 
                }
                ?>
							
							<?php 
                if (edd_has_active_discounts()) {
                    // only show if we have at least one active discount
                    ?>
								<fieldset id="edd_discount_code">
									<p id="edd-discount-code-wrap">
										<input class="edd-input" type="text" id="edd-discount" name="edd-discount" placeholder="<?php 
                    _e('Enter discount', 'edd');
                    ?>
"/>
										<label class="edd-label" for="edd-discount">
											<?php 
                    _e('Discount', 'edd');
                    ?>
											<?php 
                    if (edd_is_ajax_enabled()) {
                        ?>
												- <a href="#" class="edd-apply-discount"><?php 
                        _e('Apply Discount', 'edd');
                        ?>
</a>
											<?php 
                    }
                    ?>
										</label>
									</p>
								</fieldset>	
							<?php 
                }
                ?>

							<?php 
                // load the credit card form and allow gateways to load their own if they wish
                if (has_action('edd_' . $payment_mode . '_cc_form')) {
                    do_action('edd_' . $payment_mode . '_cc_form');
                } else {
                    do_action('edd_cc_form');
                }
                ?>
			
							
							<?php 
                if (isset($edd_options['show_agree_to_terms'])) {
                    ?>
								<fieldset id="edd_terms_agreement">
									<p>
										<div id="edd_terms" style="display:none;">
											<?php 
                    do_action('edd_before_terms');
                    echo wpautop($edd_options['agree_text']);
                    do_action('edd_after_terms');
                    ?>
										</div>
										<div id="edd_show_terms">
											<a href="#" class="edd_terms_links"><?php 
                    _e('Show Terms', 'edd');
                    ?>
</a>
											<a href="#" class="edd_terms_links" style="display:none;"><?php 
                    _e('Hide Terms', 'edd');
                    ?>
</a>
										</div>
										<input name="edd_agree_to_terms" class="required" type="checkbox" id="edd_agree_to_terms" value="1"/>
										<label for="edd_agree_to_terms"><?php 
                    echo isset($edd_options['agree_label']) ? $edd_options['agree_label'] : __('Agree to Terms?', 'edd');
                    ?>
</label>
									</p>
								</fieldset>
							<?php 
                }
                ?>
	
							<fieldset id="edd_purchase_submit">
								<p>
									<?php 
                do_action('edd_purchase_form_before_submit');
                ?>
									<?php 
                if (is_user_logged_in()) {
                    ?>
									<input type="hidden" name="edd-user-id" value="<?php 
                    echo $user_data->ID;
                    ?>
"/>
									<?php 
                }
                ?>
									<input type="hidden" name="edd_action" value="purchase"/>
									<input type="hidden" name="edd-gateway" value="<?php 
                echo $payment_mode;
                ?>
" />
									<input type="hidden" name="edd-nonce" value="<?php 
                echo wp_create_nonce('edd-purchase-nonce');
                ?>
"/>
									<?php 
                $color = isset($edd_options['checkout_color']) ? $edd_options['checkout_color'] : 'gray';
                ?>
									<span class="edd_button edd_<?php 
                echo $color;
                ?>
">
										<span class="edd_button_outer">
											<span class="edd_button_inner">
												<?php 
                $complete_purchase = isset($edd_options['checkout_label']) && strlen(trim($edd_options['checkout_label'])) > 0 ? $edd_options['checkout_label'] : __('Purchase', 'edd');
                ?>
												<input type="submit" class="edd_button_text edd-submit" id="edd-purchase-button" name="edd-purchase" value="<?php 
                echo $complete_purchase;
                ?>
"/>
											</span>
										</span>
									</span>
									<?php 
                do_action('edd_purchase_form_after_submit');
                ?>
								</p>
								<?php 
                if (!edd_is_ajax_enabled()) {
                    ?>
									<p class="edd-cancel"><a href="javascript:history.go(-1)"><?php 
                    _e('Go back', 'edd');
                    ?>
</a></p>
								<?php 
                }
                ?>
				
							</fieldset>
						<?php 
            } else {
                ?>
							<p><?php 
                _e('You must be logged in to complete your purchase', 'edd');
                ?>
</p>
						<?php 
            }
            ?>
						<?php 
            do_action('edd_purchase_form_bottom');
            ?>
					</form>
					<?php 
            do_action('edd_after_purchase_form');
            ?>
			<?php 
        }
        ?>
		</div><!--end #edd_checkout_form_wrap-->
		<?php 
    } else {
        do_action('edd_empty_cart');
    }
    return ob_get_clean();
}
/**
 * Purchase Form Validate Gateway
 *
 * @access      private
 * @since       1.0
 * @return      string
 */
function edd_purchase_form_validate_gateway()
{
    // Check if a gateway value is present
    if (!empty($_POST['edd-gateway'])) {
        $gateway = sanitize_text_field($_POST['edd-gateway']);
        if (edd_is_gateway_active($gateway)) {
            return $gateway;
        }
        if (!edd_get_cart_amount()) {
            return 'manual';
        }
        edd_set_error('invalid_gateway', __('The selected gateway is not active', 'edd'));
    } else {
        edd_set_error('empty_gateway', __('No gateway has been selected', 'edd'));
    }
    // Return empty
    return '';
}