Ejemplo n.º 1
0
/**
 * Insert cart data to database
 * 
 * Handles to add cart data to database
 * 
 * @package Social Deals Engine
 * @since 1.0.0
 */
function wps_deals_insert_payment_data($data = array())
{
    global $wps_deals_price, $wps_deals_options, $wps_deals_currency, $wps_deals_model, $current_user, $wps_deals_session;
    $prefix = WPS_DEALS_META_PREFIX;
    //price class
    $price = $wps_deals_price;
    //model class
    $model = $wps_deals_model;
    //currency class
    $currency = $wps_deals_currency;
    //cart data
    $cartdetails = $data['cartdata'];
    //get the value for user's first name from posted data
    $first_name = $data['user_info']['first_name'];
    //get the value for user's last name from posted data
    $last_name = $data['user_info']['last_name'];
    //get the value of user email
    $user_email = $data['user_info']['user_email'];
    $user_name = $data['user_info']['user_name'];
    if (is_user_logged_in()) {
        $userid = $current_user->ID;
    } else {
        $userid = '0';
    }
    //create array arguments for saving the deal sales data to database
    $deal_sale_args = array('post_title' => $user_name, 'post_content' => '', 'post_status' => 'publish', 'post_type' => WPS_DEALS_SALES_POST_TYPE, 'post_author' => $userid);
    // insert the cart data to database
    $salesid = wp_insert_post($deal_sale_args);
    //if deals sales basic data is successfully stored then update some more data to database
    if (!empty($salesid)) {
        //check order is inserted in database
        //cart products
        $cartproducts = $cartdetails['products'];
        $ordered_deal_userdetails = array('user_id' => $userid, 'user_name' => $user_name, 'user_email' => $user_email, 'first_name' => $first_name, 'last_name' => $last_name);
        // update the value for the user details to the post meta box
        update_post_meta($salesid, $prefix . 'order_userdetails', $ordered_deal_userdetails);
        //get payment gateways
        $paymentgateways = wps_deals_get_payment_gateways();
        //check if payment gateway is set in post_data then use it otherwise default it will paypal
        $paymentmethod = isset($data['post_data']['wps_deals_payment_gateways']) && !empty($data['post_data']['wps_deals_payment_gateways']) ? $data['post_data']['wps_deals_payment_gateways'] : 'paypal';
        //adminlabel & for payment gateway checkout label is must
        $admin_label = isset($paymentgateways[$paymentmethod]['admin_label']) ? $paymentgateways[$paymentmethod]['admin_label'] : $paymentgateways[$paymentmethod]['checkout_label'];
        //check out label
        $checkout_label = isset($paymentgateways[$paymentmethod]['checkout_label']) ? $paymentgateways[$paymentmethod]['checkout_label'] : '';
        // coupon details
        $ordered_deals_args = array('order_id' => $salesid, 'currency' => $wps_deals_options['currency'], 'payment_method' => $paymentmethod, 'admin_label' => $admin_label, 'checkout_label' => $checkout_label);
        foreach ($cartproducts as $dealid => $dealdata) {
            //get the data by deal id
            $getdeal = get_post($dealid);
            //get the value for available deals from post meta
            $available = get_post_meta($dealid, $prefix . 'avail_total', true);
            //get quantity
            $quantity = $dealdata['quantity'];
            //get deal title
            $dealtitle = get_the_title($getdeal->ID);
            //get deal desc
            $dealdesc = $getdeal->post_content;
            //get the value for start date from post meta
            $startdate = get_post_meta($dealid, $prefix . 'start_date', true);
            //get the value for end date from post meta
            $enddate = get_post_meta($dealid, $prefix . 'end_date', true);
            //get the value for sale price from post meta
            $saleprice = get_post_meta($dealid, $prefix . 'sale_price', true);
            //product price
            $productprice = $price->wps_deals_get_price($dealid);
            //get the value for normal price from post meta
            $normalprice = get_post_meta($dealid, $prefix . 'normal_price', true);
            //get the value for deal image featured image
            $deal_image = get_the_post_thumbnail($dealid, 'wpsdeals-single', array('alt' => __('Deal Image', 'wpsdeals'), 'title' => trim(strip_tags(get_the_title($dealid)))));
            // add filter to change feature image of deal by third party plugin
            $deal_image = apply_filters('wps_deals_feature_image_src', $deal_image, $dealid);
            $dealimg = !empty($deal_image) ? $deal_image : apply_filters('wps_deals_single_deal_default_img_src', '<img src="' . WPS_DEALS_URL . 'includes/images/deals-no-image-big.jpg' . '" alt="' . __('Deal Image', 'wpsdeals') . '" />');
            //get the value for deal image from post meta
            $address = get_post_meta($dealid, $prefix . 'address', true);
            //get the value for deal terms & conditions from post meta
            $terms = get_post_meta($dealid, $prefix . 'terms_conditions', true);
            //get the value for deal image from post meta
            //$related_img = array();
            //$related_img[1] = get_post_meta($dealid,$prefix.'related_image_1',true);
            //$related_img[2] = get_post_meta($dealid,$prefix.'related_image_2',true);
            //$related_img[3] = get_post_meta($dealid,$prefix.'related_image_3',true);
            //display price
            $dis_sale_price = $currency->wps_deals_formatted_value($productprice, $wps_deals_options['currency']);
            //get value of deal all data
            $dealalldata = array('title' => $dealtitle, 'desc' => $dealdesc, 'start_date' => $startdate, 'end_date' => $enddate, 'normal_price' => $normalprice, 'sale_price' => $saleprice, 'avail_total' => $available, 'address' => $address, 'terms_conditions' => $terms, 'main_image' => $dealimg);
            //display total
            $dis_total = $currency->wps_deals_formatted_value($productprice * $quantity, $wps_deals_options['currency']);
            //get the value for deal download link
            //$product_link = get_post_meta($dealid,$prefix.'upload_files',true);
            $ordered_deals_args['deals_details'][] = array('deal_id' => $dealid, 'deal_title' => $dealtitle, 'deal_sale_price' => $productprice, 'deal_start_date' => $startdate, 'deal_end_date' => $enddate, 'deal_quantity' => $quantity, 'display_price' => $dis_sale_price, 'display_sale_price' => $dis_sale_price, 'display_total' => $dis_total, 'deal_details' => serialize($dealalldata));
            // To Update purchase details of user
            if (is_user_logged_in()) {
                // check if user is logged in
                // Get User already purchase deal detail
                $user_purchased_detail = get_user_meta($userid, $prefix . 'purchase_detail', true);
                // get purchase limit
                $purchase_limit = get_post_meta($dealid, $prefix . 'purchase_limit', true);
                if (isset($purchase_limit) && !empty($purchase_limit)) {
                    if (!empty($user_purchased_detail[$dealid])) {
                        // if user have already buy this deal then update purchase count
                        $purchased_items = isset($user_purchased_detail[$dealid]['total_purchase']) ? $user_purchased_detail[$dealid]['total_purchase'] : 0;
                        $user_purchased_detail[$dealid] = array('total_purchase' => intval($purchased_items) + intval($quantity));
                    } else {
                        // update user purchase detail
                        $user_purchased_detail[$dealid] = array('total_purchase' => $quantity);
                    }
                    // finally update user purchase detail into database
                    update_user_meta($userid, $prefix . 'purchase_detail', $user_purchased_detail);
                }
            }
        }
        //store user IP address to database
        $ordered_deals_args['order_ip'] = wps_deals_getip();
        //store subtotal to database
        $ordsubtotal = $cartdetails['subtotal'];
        $ordered_deals_args['subtotal'] = $ordsubtotal;
        //store order total amount to database
        $ordtotal = $cartdetails['total'];
        $ordered_deals_args['order_total'] = $ordtotal;
        $ordered_deals_args['post_data'] = $data['post_data'];
        $dis_order_total = $currency->wps_deals_formatted_value($ordtotal, $wps_deals_options['currency']);
        $ordered_deals_args['display_order_total'] = $dis_order_total;
        //apply filter to add some data to order details array for saving to data base
        $ordered_deals_args = apply_filters('wps_deals_update_cart_data', $ordered_deals_args);
        //unset post data when its requirement is over
        unset($ordered_deals_args['post_data']);
        $dis_order_subtotal = $currency->wps_deals_formatted_value($ordsubtotal, $wps_deals_options['currency']);
        $ordered_deals_args['display_order_subtotal'] = $dis_order_subtotal;
        //billing data
        $billingdata = isset($data['post_data']['wps_deals_billing_details']) ? $data['post_data']['wps_deals_billing_details'] : array();
        //check billing data is set
        $ordered_deals_args['billing_details'] = $billingdata;
        //check user is logged in or not
        if (!empty($userid) && !empty($billingdata)) {
            //save billing to user meta
            update_user_meta($userid, $prefix . 'billing_details', $billingdata);
        }
        //end if to check user is logged in or not
        //update order details to post meta
        update_post_meta($salesid, $prefix . 'order_details', $ordered_deals_args);
        // update the value for the user email to post meta
        update_post_meta($salesid, $prefix . 'payment_user_email', $user_email);
        // update the value for the payment status to the post meta box
        $payment_status = isset($data['payment_status']) ? $data['payment_status'] : '0';
        //update payment status to database
        wps_deals_update_payment_status($payment_status, $salesid);
        //order tracking data
        $trackargs = array('orderid' => $salesid, 'payment_status' => $payment_status, 'notify' => wps_deals_notify_from_status($payment_status));
        wps_deals_update_order_track($trackargs);
        //set order id to session for guest user to show the order details
        if (!is_user_logged_in() && !empty($salesid)) {
            $wps_deals_session->set('wps_deals_last_ordered_id', $salesid);
        }
        //do action to do something before payment process
        do_action('wps_deals_cart_payment_process_before', $salesid);
    }
    return $salesid;
}
/**
 * Process to Download
 * 
 * Hanldes to download file from link
 *
 * @package Social Deals Engine
 * @since 1.0.0
 */
function wps_deals_process_download()
{
    global $wps_deals_model, $current_user;
    //model class
    $model = $wps_deals_model;
    $args = apply_filters('wps_deals_process_download_args', array('orderid' => isset($_GET['order_key']) ? base64_decode($_GET['order_key']) : false, 'dealid' => isset($_GET['deal_id']) ? (int) $_GET['deal_id'] : '', 'email' => isset($_GET['email']) ? rawurldecode($_GET['email']) : '', 'expire' => isset($_GET['expire']) ? base64_decode(rawurldecode($_GET['expire'])) : '', 'file_key' => isset($_GET['file']) ? (int) $_GET['file'] : ''));
    if ($args['orderid'] === '' || $args['email'] === '' || $args['file_key'] === '') {
        //check download,email,file key should not blank
        return false;
    }
    extract($args);
    //check deal is exist or not
    $dealsdata = get_post($dealid);
    if (empty($dealsdata)) {
        //check deal is exist or not
        $error_message = __('<strong>ERROR : </strong>You can not download this file because the deal is no more available.', 'wpsdeals');
        wp_die(apply_filters('wps_deals_deny_download_message', $error_message), __('Deal is no more available', 'wpsdeals'));
    }
    $verify = $model->wps_deals_verify_download_link($dealid, $email, $expire, $file_key, $orderid);
    // Defaulting this to true for now because the method below doesn't work well
    $has_access = apply_filters('wps_deals_file_download_has_access', true, $verify, $args);
    if ($verify && $has_access) {
        //check validation is success
        do_action('wps_deals_process_verified_download', $dealid, $email);
        // Payment has been verified, setup the download
        $downloadfiles = $model->wps_deals_get_download_files($dealid);
        $request_file = apply_filters('wps_deals_requested_file', $downloadfiles[$file_key]);
        $user_info = array();
        $user_info['email'] = $email;
        if (is_user_logged_in()) {
            global $user_ID;
            $user_data = get_userdata($user_ID);
            $user_info['id'] = $user_ID;
            $user_info['name'] = $user_data->display_name;
        }
        wps_deals_record_download_in_log($dealid, $file_key, $user_info, wps_deals_getip(), $verify);
        $extension = wps_deals_get_file_extension($request_file);
        $ctype = wps_deals_get_file_ctype($extension);
        if (function_exists('get_magic_quotes_runtime') && get_magic_quotes_runtime()) {
            set_magic_quotes_runtime(0);
        }
        @session_write_close();
        if (function_exists('apache_setenv')) {
            @apache_setenv('no-gzip', 1);
        }
        @ini_set('zlib.output_compression', 'Off');
        nocache_headers();
        header("Robots: none");
        header("Content-Type: " . $ctype . "");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=\"" . apply_filters('wps_deals_requested_file_name', basename($request_file)) . "\";");
        header("Content-Transfer-Encoding: binary");
        if (strpos($request_file, 'http://') === false && strpos($request_file, 'https://') === false && strpos($request_file, 'ftp://') === false) {
            // This is an absolute path
            $request_file = realpath($request_file);
            if (file_exists($request_file)) {
                if ($size = @filesize($request_file)) {
                    header("Content-Length: " . $size);
                }
                @wps_deals_readfile_chunked($request_file);
            } else {
                wp_die(__('<strong>ERROR : </strong>Sorry but this file does not exist.', 'wpsdeals'), __('Download Error', 'wpsdeals'));
            }
        } else {
            if (strpos($request_file, WP_CONTENT_URL) !== false) {
                // This is a local file given by URL
                $upload_dir = wp_upload_dir();
                $request_file = str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $request_file);
                $request_file = realpath($request_file);
                if (file_exists($request_file)) {
                    if ($size = @filesize($request_file)) {
                        header("Content-Length: " . $size);
                    }
                    @wps_deals_readfile_chunked($request_file);
                } else {
                    wp_die(__('<strong>ERROR : </strong>Sorry but this file does not exist.', 'wpsdeals'), __('Download Error', 'wpsdeals'));
                }
            } else {
                // This is a remote file
                header("Location: " . $request_file);
            }
        }
        exit;
    } else {
        $error_message = __('<strong>ERROR : </strong>You do not have permission to download this file.', 'wpsdeals');
        wp_die(apply_filters('wps_deals_deny_download_message', $error_message), __('Purchase Verification Failed', 'wpsdeals'));
    }
}