/**
  * Manage Billing Address
  *
  * Handles to manage billing address from
  * my account > edit address page
  *
  * @package Social Deals Engine
  * @since 1.0.0
  */
 public function wps_deals_manage_billing_address()
 {
     global $wps_deals_options, $current_user;
     $prefix = WPS_DEALS_META_PREFIX;
     if (isset($_POST['wps_deals_save_billing_address']) && !empty($_POST['wps_deals_save_billing_address']) && isset($_POST['wps_deals_billing_details']) && !empty($_POST['wps_deals_billing_details'])) {
         //valid billing details
         $userbilling = wps_deals_valid_billing_data();
         if ($userbilling) {
             //save billing to user meta
             update_user_meta($current_user->ID, $prefix . 'billing_details', $_POST['wps_deals_billing_details']);
             $successmsg = __('<span>Address changed successfully.</span>', 'wpsdeals');
             $this->message->add_session('my_account_msg', $successmsg, 'success');
             //redirect to my account page
             wps_deals_send_on_my_account_page();
         } else {
             //redirect to edit address page
             wps_deals_send_on_edit_adderess_page(array('wps_deals_address' => 'billing'));
         }
     }
 }
/**
 * Process Order
 *
 * Handle process of order
 * 
 * @package Social Deals Engine
 * @since 1.0.0
 */
function wps_deals_payment_process()
{
    global $wps_deals_cart, $wps_deals_message, $current_user, $wps_deals_options;
    //cart class
    $cart = $wps_deals_cart;
    //message class
    $message = $wps_deals_message;
    //get cart data
    $cartdata = $cart->get();
    if (isset($_POST['wps_deals_submit_payment']) && !empty($_POST['wps_deals_submit_payment']) && $_POST['wps_deals_submit_payment'] == __('Deals Purchase', 'wpsdeals')) {
        //check payment button click for checkout button
        //validate nonce for security purpose
        // $noncevalidate = wps_deals_payment_nonce_valid($_POST['wps_deals_payment_nonce']);
        if (is_user_logged_in() && isset($_POST['wps_deals_cart_login_user_name']) && !empty($_POST['wps_deals_cart_login_user_name']) && isset($_POST['wps_deals_cart_login_user_pass']) && !empty($_POST['wps_deals_cart_login_user_pass'])) {
            //when user will try to purchase with login form then select his email from database
            $_POST['wps_deals_cart_user_email'] = $current_user->user_email;
            $_POST['wps_deals_cart_user_first_name'] = $current_user->first_name;
            $_POST['wps_deals_cart_user_last_name'] = $current_user->last_name;
        } else {
            $_POST['wps_deals_cart_user_email'] = $_POST['wps_deals_cart_user_email'];
        }
        //payment gateway
        $validgateway = wps_deals_valid_gateway();
        //valid order data
        $validatedata = wps_deals_order_data_validate();
        //user details
        $uservalid = wps_deals_valid_user_data();
        //valid billing details
        $userbilling = wps_deals_valid_billing_data();
        //check user can purchase deal
        $user_can_purchase = wps_deals_valid_purchase_limit();
        $agreeterms = true;
        //terms and conditions
        if (!empty($wps_deals_options['enable_terms'])) {
            $terms = isset($_POST['wps_deals_checkout_agree_terms']) ? $_POST['wps_deals_checkout_agree_terms'] : '';
            $agreeterms = wps_deals_payment_agree_to_terms($terms);
        }
        // !$noncevalidate ||
        if (empty($cartdata['products']) || !$validatedata || !$uservalid || !$validgateway || !$agreeterms || !$userbilling || !$user_can_purchase) {
            //check some data valid or not
            //redirect to checkout page
            wps_deals_send_on_checkout_page();
        }
        //get the value of user login name
        if (is_user_logged_in()) {
            //if user is logged in then stored display name
            $_POST['user_name'] = $current_user->display_name;
        } else {
            //do concat first name and last name of from posted data
            $_POST['user_name'] = 'guest';
        }
        $gateway = isset($_POST['wps_deals_payment_gateways']) ? $_POST['wps_deals_payment_gateways'] : '';
        //Pst data
        $post_data = $_POST;
        // Allow themes and plugins to hook before the gateway
        do_action('wps_deals_checkout_before_gateway', $post_data, $cartdata);
        if ($cartdata['total'] <= '0') {
            //if cart total is empty call test mode
            $gateway = 'testmode';
            $post_data['wps_deals_payment_gateways'] = 'testmode';
            //$post_data['wps_deals_payment_gateways']	= 'free';
        }
        // Send info to the gateway for payment processing
        wps_deals_send_to_gateway($gateway, $cartdata, $post_data);
        exit;
    }
}