/**
  * Add points for purchase
  * 
  * Handles to add points for purchases
  *
  * @package Easy Digital Downloads - Points and Rewards
  * @since 1.0.0
  */
 public function edd_points_add_point_for_complete_purchase($payment_id)
 {
     global $edd_options, $current_user;
     //get payment data
     $paymentdata = edd_get_payment_meta($payment_id);
     $userdata = edd_get_payment_meta_user_info($payment_id);
     $user_id = isset($userdata['id']) && !empty($userdata['id']) ? $userdata['id'] : 0;
     //get discount towards points
     $gotdiscount = $this->model->edd_points_get_payment_discount($payment_id);
     //check user has redeemed points or not & user_id should not empty
     if (isset($gotdiscount) && !empty($gotdiscount) && !empty($user_id)) {
         //get discounte price from points
         $discountedpoints = $this->model->edd_points_calculate_points($gotdiscount);
         //update user points
         edd_points_minus_points_from_user($discountedpoints, $user_id);
         //points label
         $pointslable = $this->model->edd_points_get_points_label($discountedpoints);
         //record data logs for redeem for purchase
         $post_data = array('post_title' => sprintf(__('Redeem %s for purchase', 'eddpoints'), $pointslable), 'post_content' => sprintf(__('%s redeemed for purchasing download by redeeming the points and get discounts.', 'eddpoints'), $pointslable), 'post_author' => $user_id);
         //log meta array
         $log_meta = array('userpoint' => $discountedpoints, 'events' => 'redeemed_purchase', 'operation' => 'minus');
         //insert entry in log
         $this->logs->edd_points_insert_logs($post_data, $log_meta);
         // set order meta, regardless of whether any points were earned, just so we know the process took place
         update_post_meta($payment_id, '_edd_points_order_redeemed', $discountedpoints);
     }
     //end if to check points redeemed taken by buyer or not
     // get cartdata from older order
     $cartdata = edd_get_payment_meta_cart_details($payment_id);
     //get bought points for points downloads types
     $boughtpoints = $this->model->edd_points_get_bought_points($cartdata);
     //get cart points from cartdata and payment discount given to user
     $cartpoints = $this->model->edd_points_get_user_checkout_points($cartdata, $gotdiscount);
     //add bought points to cart points
     $cartpoints = !empty($boughtpoints) ? $cartpoints + $boughtpoints : $cartpoints;
     //check checkout points earned points or user id is not empty
     if (!empty($cartpoints) && !empty($user_id)) {
         //points label
         $pointslable = $this->model->edd_points_get_points_label($cartpoints);
         //get user points after subtracting the redemption points
         $userpoints = edd_points_get_user_points();
         $post_data = array('post_title' => sprintf(__('%s earned for purchasing the downloads.', 'eddpoints'), $pointslable), 'post_content' => sprintf(__('Get %s for purchasing the downloads.', 'eddpoints'), $pointslable), 'post_author' => $user_id);
         $log_meta = array('userpoint' => $cartpoints, 'events' => 'earned_purchase', 'operation' => 'add');
         //insert entry in log
         $this->logs->edd_points_insert_logs($post_data, $log_meta);
         //update user points
         edd_points_add_points_to_user($cartpoints, $user_id);
         // set order meta, regardless of whether any points were earned, just so we know the process took place
         update_post_meta($payment_id, '_edd_points_order_earned', $cartpoints);
     }
     //end if to check checkout points should not empty
 }
 /**
  * Apply Points to Previous Orders
  * 
  * Handles to apply points to previous orders
  *
  * @package Easy Digital Downloads - Points and Rewards
  * @since 1.0.0
  */
 public function edd_points_apply_for_previous_orders()
 {
     if (isset($_GET['points_action']) && $_GET['points_action'] == 'apply_points' && isset($_GET['page']) && $_GET['page'] == 'edd-settings') {
         // perform the action in manageable chunks
         $success_count = 0;
         $paymentmode = edd_is_test_mode() ? 'test' : 'live';
         $paymentargs = array('mode' => $paymentmode, 'fields' => 'ids', 'status' => 'publish', 'posts_per_page' => '-1', 'meta_query' => array(array('key' => '_edd_points_order_earned', 'compare' => 'NOT EXISTS')));
         // grab a set of order ids for existing orders with no earned points set
         $payment_ids = edd_get_payments($paymentargs);
         // otherwise go through the results and set the order numbers
         if (is_array($payment_ids)) {
             foreach ($payment_ids as $payment_id) {
                 $payment = get_post($payment_id);
                 $payment_data = edd_get_payment_meta($payment_id);
                 // get cartdata from older order
                 $cartdata = edd_get_payment_meta_cart_details($payment_id);
                 //get cartdata points
                 $checkoutpoints = $this->model->edd_points_get_user_checkout_points($cartdata);
                 //check checkout points should not empty which has redeemed by buyer
                 if (!empty($checkoutpoints)) {
                     //get user points label
                     $pointlable = $this->model->edd_points_get_points_label($checkoutpoints);
                     $post_data = array('post_title' => sprintf(__('%s earned for purchasing the downloads.', 'eddpoints'), $pointlable), 'post_content' => sprintf(__('Get %s for purchasing the downloads.', 'eddpoints'), $pointlable), 'post_author' => $payment->post_author);
                     $log_meta = array('userpoint' => $checkoutpoints, 'events' => 'earned_purchase', 'operation' => 'add');
                     //insert entry in log
                     $this->logs->edd_points_insert_logs($post_data, $log_meta);
                     //update user points
                     edd_points_add_points_to_user($checkoutpoints, $payment->post_author);
                     // set order meta, regardless of whether any points were earned, just so we know the process took place
                     update_post_meta($payment_id, '_edd_points_order_earned', $checkoutpoints);
                 }
                 $success_count++;
             }
             //end foreach loop
         }
         //end if check retrive payment ids are array
         $redirectargs = array('post_type' => 'download', 'page' => 'edd-settings', 'tab' => 'extensions', 'settings-updated' => 'apply_points', 'success_count' => $success_count, 'points_action' => false);
         $redirect_url = add_query_arg($redirectargs, admin_url('edit.php'));
         wp_redirect($redirect_url);
         exit;
     }
     //end if check if there is fulfilling condition proper for applying discount for previous orders
 }
 public function edd_points_add_seller_points_for_complete_purchase($payment_id)
 {
     global $edd_options;
     //get payment data
     $paymentdata = edd_get_payment_meta($payment_id);
     $cart_details = $paymentdata['cart_details'];
     if (!empty($cart_details)) {
         $points = isset($edd_options['edd_points_selling_conversion']['points']) ? abs($edd_options['edd_points_selling_conversion']['points']) : 0;
         $rate = isset($edd_options['edd_points_selling_conversion']['rate']) ? abs($edd_options['edd_points_selling_conversion']['rate']) : 0;
         if (!empty($rate) && !empty($points)) {
             foreach ($cart_details as $key => $cart_detail) {
                 if (!empty($cart_detail['item_number']['id'])) {
                     $download = get_post($cart_detail['item_number']['id']);
                     //Calculate total points for seller
                     $total_seller_points = $points * ($cart_detail['item_price'] * $cart_detail['quantity']) / $rate;
                     //points label
                     $pointslable = $this->model->edd_points_get_points_label($total_seller_points);
                     $post_data = array('post_title' => sprintf(__('%s earned for selling the downloads.', 'eddpoints'), $pointslable), 'post_content' => sprintf(__('Get %s for selling the downloads.', 'eddpoints'), $pointslable), 'post_author' => $download->post_author);
                     $log_meta = array('userpoint' => $total_seller_points, 'events' => 'earned_sell', 'operation' => 'add');
                     //insert entry in log
                     $this->logs->edd_points_insert_logs($post_data, $log_meta);
                     //update user points
                     edd_points_add_points_to_user($total_seller_points, $download->post_author);
                 }
             }
         }
     }
 }