Exemplo 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;
}
                    //order user email id
                    if (isset($_POST['wps-deals-order-edit-append-comment']) && !empty($_POST['wps-deals-order-edit-append-comment'])) {
                        $args['comments'] = $comments;
                        $args['appendcomment'] = '1';
                    }
                    //end if to check append comment is set or not
                    //send notification to customer
                    $args['status'] = $model->wps_deals_paypal_value_to_status($newstatus);
                    $model->wps_deals_send_order_status_email($args);
                }
                //check notify customer checkbox is checked or not
                //update order track to database
                $notify = isset($_POST['wps-deals-order-edit-notify']) && !empty($_POST['wps-deals-order-edit-notify']) ? '1' : '0';
                $track = array('date' => wps_deals_current_date('Y-m-d H:i:s'), 'notify' => $notify, 'payment_status' => $newstatus, 'comments' => $comments, 'orderid' => $orderid);
                //update order tracking data
                wps_deals_update_order_track($track);
            }
            //check payment status is changed or not & payment note is not empty
            //call some action to update data to database
            do_action('wps_deals_edit_order_update', $_POST, $orderid);
        }
        //end if to check update order button is clicked or not
        ?>
<div id="wps-deals-sales" class="post-box-container">
	<div class="metabox-holder">	
		<div class="meta-box-sortables ui-sortable">
			<div id="order" class="postbox">	
				<div class="handlediv" title="<?php 
        _e('Click to toggle', 'wpsdeals');
        ?>
"><br /></div>
Exemplo n.º 3
0
/**
 * Paypal payment validate
 * 
 * Handles to validate paypal payment
 * 
 * @package Social Deals Engine
 * @since 1.0.0
 */
function wps_deals_paypal_verfication()
{
    global $wps_deals_paypal, $wps_deals_model;
    $prefix = WPS_DEALS_META_PREFIX;
    //paypal class
    $paypal = $wps_deals_paypal;
    //model class
    $model = $wps_deals_model;
    if (isset($_GET['dealslistner']) && $_GET['dealslistner'] == 'paypalipn') {
        //check listner value
        if ($paypal->validate_ipn()) {
            //check validate ipn
            //status of paypal transaction
            $status = $paypal->ipn_data['payment_status'];
            //get order id
            $orderid = $_POST['custom'];
            // update the value for the paypal data to the post meta box
            update_post_meta($orderid, $prefix . 'order_ipn_data', $paypal->ipn_data);
            // ipn data
            /************** Code For Buy Now Start ( Signle Purchase ) **************/
            // get the value of payment user email from the post meta
            $payment_user_email = get_post_meta($orderid, $prefix . 'payment_user_email', true);
            // ipn data
            if (empty($payment_user_email)) {
                $user_email = isset($paypal->ipn_data['payer_email']) ? $paypal->ipn_data['payer_email'] : '';
                $ordered_deal_userdetails = array('user_id' => '0', 'user_name' => '', 'user_email' => $user_email, 'first_name' => isset($paypal->ipn_data['first_name']) ? $paypal->ipn_data['first_name'] : __('Guest', 'wpsdeals'), 'last_name' => isset($paypal->ipn_data['last_name']) ? $paypal->ipn_data['last_name'] : '');
                // update the value for the user details to the post meta box
                update_post_meta($orderid, $prefix . 'order_userdetails', $ordered_deal_userdetails);
                // update the value for the user email to post meta
                update_post_meta($orderid, $prefix . 'payment_user_email', $user_email);
            }
            /************** Code For Buy Now End ( Signle Purchase ) **************/
            //update payment status
            $payment_status = $model->wps_deals_paypal_status_to_value($status);
            //order details
            $orderdata = $model->wps_deals_get_post_meta_ordered($orderid);
            //user details
            $userdetails = $model->wps_deals_get_ordered_user_details($orderid);
            switch ($status) {
                case 'Completed':
                    //update payments status
                    wps_deals_update_payment_status($payment_status, $orderid);
                    //order tracking data update
                    $trackargs = array('orderid' => $orderid, 'payment_status' => $payment_status, 'notify' => '1');
                    wps_deals_update_order_track($trackargs);
                    break;
                case 'Reversed':
                case 'Chargeback':
                    $args = array();
                    $args['order_id'] = $orderid;
                    $args['email'] = $userdetails['user_email'];
                    $args['status'] = $status;
                    //send email to buyer
                    $model->wps_deals_send_order_status_email($args);
                    $adminargs = array('subject' => sprintf(__('Payment for order %s refunded/reversed', 'wpsdeals'), $orderid), 'message' => sprintf(__('Order %s has been marked as refunded - PayPal reason code: %s', 'wpsdeals'), $orderid, $_POST['reason_code']));
                    //send email to admin
                    $model->wps_deals_send_order_status_email_admin($adminargs);
                    //update payment status
                    wps_deals_update_payment_status($payment_status, $orderid);
                    //order tracking data update
                    $trackargs = array('orderid' => $orderid, 'payment_status' => $payment_status, 'notify' => '1');
                    wps_deals_update_order_track($trackargs);
                    break;
                case 'Refunded':
                    if ($orderdata['order_total'] == $_POST['mc_gross'] * -1) {
                        $args = array();
                        $args['order_id'] = $orderid;
                        $args['email'] = $userdetails['user_email'];
                        $args['status'] = $status;
                        //send email to buyer
                        $model->wps_deals_send_order_status_email($args);
                        $adminargs = array('subject' => sprintf(__('Payment for order %s refunded/reversed', 'wpsdeals'), $orderid), 'message' => sprintf(__('Order %s has been marked as refunded - PayPal reason code: %s', 'wpsdeals'), $orderid, $_POST['reason_code']));
                        //send email to admin
                        $model->wps_deals_send_order_status_email_admin($adminargs);
                        //update payment status
                        wps_deals_update_payment_status($payment_status, $orderid);
                        //order tracking data update
                        $trackargs = array('orderid' => $orderid, 'payment_status' => $payment_status, 'notify' => '1');
                        wps_deals_update_order_track($trackargs);
                    }
                    break;
                default:
                    //update payment status
                    wps_deals_update_payment_status($payment_status, $orderid);
                    //order tracking data update
                    $trackargs = array('orderid' => $orderid, 'payment_status' => $payment_status, 'notify' => '0');
                    wps_deals_update_order_track($trackargs);
                    break;
            }
            //do action when paypal ipn is being verified
            do_action('wps_deals_verify_paypal_ipn', $orderid);
        }
        //end if to check ipn data is valid or not
    }
    //end if to check ipnlistner is set and it must be 'paypalipn'
}