function _buy_post()
 {
     global $wpdb;
     // get system settings
     $system_obj = mgm_get_class('system');
     $dge = bool_from_yn($system_obj->get_setting('disable_gateway_emails'));
     $dpne = bool_from_yn($system_obj->get_setting('disable_payment_notify_emails'));
     // get passthrough data
     $custom = $this->_get_transaction_passthrough($_REQUEST['trans_id']);
     extract($custom);
     // find user
     $user = null;
     // check
     if (isset($user_id) && (int) $user_id > 0) {
         $user = get_userdata($user_id);
     }
     // errors
     $errors = array();
     // purchase status
     $purchase_status = 'Error';
     // set status
     if ('sisow' == $this->setting['aquirer']) {
         $payment_status = isset($_REQUEST['status']) ? $_REQUEST['status'] : 'Error';
     } else {
         $payment_status = isset($_REQUEST['status']) && $_REQUEST['status'] == 'success' ? 'SALE' : 'ERROR';
     }
     // payment_status
     switch (trim($payment_status)) {
         case "SALE":
         case 'Success':
             // status
             $status_str = __('Last payment was successful', 'mgm');
             // purchase status
             $purchase_status = 'Success';
             // transation id
             $transaction_id = $this->_get_transaction_id('trans_id', $_REQUEST);
             // hook args
             $args = array('post_id' => $post_id, 'transaction_id' => $transaction_id);
             // user purchase
             if (isset($user_id) && (int) $user_id > 0) {
                 $args['user_id'] = $user_id;
             } else {
                 // guest purchase
                 $args['guest_token'] = $guest_token;
             }
             // after succesful payment hook
             do_action('mgm_buy_post_transaction_success', $args);
             // backward compatibility
             do_action('mgm_post_purchase_payment_success', $args);
             // new organized name
             break;
         case "ERROR":
         case 'Failure':
             // status
             $status_str = __('Last payment was refunded or denied', 'mgm');
             // purchase status
             $purchase_status = 'Failure';
             // error
             $errors[] = $status_str;
             break;
         case "CANCEL-REBILL":
         case "UNCANCEL-REBILL":
         case 'Pending':
             // status
             $status_str = __('Last payment is pending. Reason: Unnown', 'mgm');
             // purchase status
             $purchase_status = 'Pending';
             // error
             $errors[] = $status_str;
             break;
         default:
             // status
             $status_str = sprintf(__('Last payment status: %s', 'mgm'), $payment_status);
             // purchase status
             $purchase_status = 'Unknown';
             // error
             $errors[] = $status_str;
     }
     // do action
     do_action('mgm_return_post_purchase_payment_' . $this->module, array('post_id' => $post_id));
     // new, individual
     do_action('mgm_return_post_purchase_payment', array('post_id' => $post_id));
     // new, global
     // status
     $status = __('Failed join', 'mgm');
     //overridden on a successful payment
     // check status
     if ($purchase_status == 'Success') {
         // mark as purchased
         if (isset($user->ID)) {
             // purchased by user
             // call coupon action
             do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
             // set as purchased
             $this->_set_purchased($user_id, $post_id, NULL, $alt_tran_id);
         } else {
             // purchased by guest
             if (isset($guest_token)) {
                 // issue #1421, used coupon
                 if (isset($coupon_id) && isset($coupon_code)) {
                     // call coupon action
                     do_action('mgm_update_coupon_usage', array('guest_token' => $guest_token, 'coupon_id' => $coupon_id));
                     // set as purchased
                     $this->_set_purchased(NULL, $post_id, $guest_token, $alt_tran_id, $coupon_code);
                 } else {
                     $this->_set_purchased(NULL, $post_id, $guest_token, $alt_tran_id);
                 }
             }
         }
         // status
         $status = __('The post was purchased successfully', 'mgm');
     }
     // transaction status
     mgm_update_transaction_status($_REQUEST['trans_id'], $status, $status_str);
     // blog
     $blogname = get_option('blogname');
     // post being purchased
     $post = get_post($post_id);
     // notify user and admin, only if gateway emails on
     if (!$dpne) {
         // notify user
         if (isset($user->ID)) {
             // mgm post setup object
             $post_obj = mgm_get_post($post_id);
             // check
             if ($this->send_payment_email($alt_tran_id)) {
                 // check
                 if (mgm_notify_user_post_purchase($blogname, $user, $post, $purchase_status, $system_obj, $post_obj, $status_str)) {
                     // update as email sent
                     $this->update_paymentemail_sent($alt_tran_id);
                 }
             }
         }
     }
     // notify admin, only if gateway emails on
     if (!$dge) {
         // notify admin,
         mgm_notify_admin_post_purchase($blogname, $user, $post, $status);
     }
     // error condition redirect
     if (count($errors) > 0) {
         if ($this->is_webhook_called_by('self')) {
             // only when proxied via payment_return
             mgm_redirect(add_query_arg(array('status' => 'error', 'errors' => implode('|', $errors)), $this->_get_thankyou_url()));
         }
     }
 }
Example #2
0
<?php

$blogname = get_option('blogname');
$tran_success = false;
//getting purchase post title and & price - issue #981
$post_obj = mgm_get_post($post_id);
$purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost);
$post = get_post($post_id);
$post_title = $post->post_title;
//issue #504
if ($user_id) {
    // issue#: 280 (2011 Feb 25)
    // subject
    $subject = $system_obj->get_template('payment_success_email_template_subject', array('blogname' => $blogname), true);
    // body
    $message = $system_obj->get_template('payment_success_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'admin_email' => $system_obj->setting['admin_email']), true);
}
//issue #504
if ($user_id) {
    // issue#: 280 (2011 Feb 25)
    // subject
    $subject = $system_obj->get_template('payment_failed_email_template_subject', array('blogname' => $blogname), true);
    // body
    $message = $system_obj->get_template('payment_failed_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'payment_type' => 'post purchase payment', 'reason' => $status_str, 'admin_email' => $system_obj->setting['admin_email']), true);
}
//issue #504
if ($user_id) {
    // issue#: 280 (2011 Feb 25)
    // subject
    $subject = $system_obj->get_template('payment_pending_email_template_subject', array('blogname' => $blogname), true);
    // body
 function _get_purchased_contents($post_types, $id, $start, $rows, $gifted = false)
 {
     global $wpdb;
     // array or string
     if (!is_array($post_types)) {
         $post_types = array($post_types);
     }
     // impode
     $post_types_in = mgm_map_for_in($post_types);
     // gifted
     $gifted_sql = $gifted ? "AND is_gift = 'Y'" : "AND is_gift = 'N'";
     // from
     $sql_from = " FROM " . $wpdb->posts . " A JOIN " . TBL_MGM_POST_PURCHASES . " B ON(A.ID = B.post_id) \r\r\n\t\t\t\t\t  WHERE post_status = 'publish' AND post_type IN ( {$post_types_in} ) {$gifted_sql}";
     // sql
     $sql = "SELECT DISTINCT(A.ID), post_type, post_title, post_date, post_content, user_id,guest_token {$sql_from} \r\r\n\t\t        ORDER BY post_date DESC LIMIT {$start},{$rows}";
     // get posts
     $results = $wpdb->get_results($sql);
     // init
     $posts = array();
     // check
     if ($results) {
         // loop
         foreach ($results as $post) {
             // get object
             $post_obj = mgm_get_post($post->ID);
             // check
             if (mgm_post_is_purchasable($post->ID, $post_obj)) {
                 // stip short code
                 $post->post_content = mgm_strip_shortcode($post->post_content);
                 // access type
                 $access_types = $post_obj->get_access_membership_types();
                 // access delay
                 $access_delays = $post_obj->get_access_delay();
                 // init
                 $access_settings = array();
                 // loop
                 foreach ($access_types as $access_type) {
                     // delay
                     $delay = isset($access_delays[$access_type]) ? (int) $access_delays[$access_type] : 0;
                     // set
                     $access_settings[] = array('membership_type' => array('code' => $access_type, 'name' => mgm_get_membership_type_name($access_type)), 'access_delay' => sprintf(__('%d day', 'mgm'), $delay));
                 }
                 // access
                 $post->access_settings = $access_settings;
                 // user
                 if ((int) $post->user_id > 0) {
                     // user
                     $user = get_userdata($post->user_id);
                     $user_info = array('by' => 'user', 'id' => $post->user_id, 'username' => $user->user_login, 'email' => $user->user_email);
                     // gifted
                     if ($gifted) {
                         $post->gift = array_slice($user_info, 1);
                     } else {
                         $post->purchase = $user_info;
                     }
                 } else {
                     $post->purchase = array('by' => 'guest', 'token' => $post->guest_token);
                 }
                 // unset
                 unset($post->guest_token, $post->user_id);
                 // set
                 $posts[] = $post;
             }
         }
     }
     // return
     return $posts;
 }
 /**
  * Update buy post response
  *
  */
 function _buy_post()
 {
     global $wpdb;
     //skip updates from IPN: {PPP will be an immediate update}
     if (isset($_POST['ipn_track_id'])) {
         exit;
     }
     // system
     $system_obj = mgm_get_class('system');
     $dge = bool_from_yn($system_obj->get_setting('disable_gateway_emails'));
     $dpne = bool_from_yn($system_obj->get_setting('disable_payment_notify_emails'));
     // get passthrough, stop further process if fails to parse
     $custom = $this->_get_transaction_passthrough($_POST['custom']);
     // local var
     extract($custom);
     // set user
     $user = null;
     // check
     if (isset($user_id) && (int) $user_id > 0) {
         $user = get_userdata($user_id);
     }
     $blogname = get_option('blogname');
     $tran_success = false;
     //getting purchase post title and & price - issue #981
     $post_obj = mgm_get_post($post_id);
     $purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost);
     $post = get_post($post_id);
     $post_title = $post->post_title;
     // errors
     $errors = array();
     // purchase status
     $purchase_status = 'Error';
     // status
     $payment_status = isset($this->response['PAYMENTINFO_0_PAYMENTSTATUS']) ? $this->response['PAYMENTINFO_0_PAYMENTSTATUS'] : $this->response['PAYMENTSTATUS'];
     // status
     if ($this->status == 'test' && strtoupper($payment_status) == 'PENDING') {
         $payment_status = 'Completed';
     }
     // process on response code
     switch ($payment_status) {
         case 'Completed':
         case 'Processed':
             // status
             $status_str = __('Last payment was successful', 'mgm');
             // purchase status
             $purchase_status = 'Success';
             // transaction id
             $transaction_id = $this->_get_transaction_id();
             // hook args
             $args = array('post_id' => $post_id, 'transaction_id' => $transaction_id);
             // user purchase
             if (isset($user_id) && (int) $user_id > 0) {
                 $args['user_id'] = $user_id;
             } else {
                 // guest purchase
                 $args['guest_token'] = $guest_token;
             }
             // after succesful payment hook
             do_action('mgm_buy_post_transaction_success', $args);
             // backward compatibility
             do_action('mgm_post_purchase_payment_success', $args);
             // new organized name
             break;
         case 'Failed':
         case 'Refunded':
         case 'Denied':
         case 'In-Progress':
             // status
             $status_str = __('Last payment was refunded or denied', 'mgm');
             // purchase status
             $purchase_status = 'Failure';
             // error
             $errors[] = $status_str;
             break;
         case 'Pending':
             // reason
             if (isset($this->response['PAYMENTINFO_0_PENDINGREASON'])) {
                 $reason = $this->response['PAYMENTINFO_0_PENDINGREASON'];
             } else {
                 $reason = $payment_status;
             }
             // status
             $status_str = sprintf(__('Last payment is pending. Reason: %s', 'mgm'), $reason);
             // purchase status
             $purchase_status = 'Pending';
             // error
             $errors[] = $status_str;
             break;
         default:
             // status
             $status_str = sprintf(__('Last payment status: %s', 'mgm'), isset($payment_status) ? $payment_status : 'Unknown');
             // purchase status
             $purchase_status = 'Unknown';
             // error
             $errors[] = $status_str;
     }
     // do action
     do_action('mgm_return_post_purchase_payment_' . $this->module, array('post_id' => $post_id));
     // new, individual
     do_action('mgm_return_post_purchase_payment', array('post_id' => $post_id));
     // new, global
     // set as purchase
     $status = __('Failed join', 'mgm');
     //overridden on a successful payment
     // check status
     if ($purchase_status == 'Success') {
         // mark as purchased
         if (isset($user->ID)) {
             // purchased by user
             // call coupon action
             do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
             // set as purchased
             $this->_set_purchased($user_id, $post_id, NULL, $_POST['custom']);
         } else {
             // purchased by guest
             if (isset($guest_token)) {
                 // issue #1421, used coupon
                 if (isset($coupon_id) && isset($coupon_code)) {
                     // call coupon action
                     do_action('mgm_update_coupon_usage', array('guest_token' => $guest_token, 'coupon_id' => $coupon_id));
                     // set as purchased
                     $this->_set_purchased(NULL, $post_id, $guest_token, $_POST['custom'], $coupon_code);
                 } else {
                     $this->_set_purchased(NULL, $post_id, $guest_token, $_POST['custom']);
                 }
             }
         }
         // status
         $status = __('The post was purchased successfully', 'mgm');
     }
     // transaction status
     mgm_update_transaction_status($_POST['custom'], $status, $status_str);
     // blog
     $blogname = get_option('blogname');
     // post being purchased
     $post = get_post($post_id);
     // notify user, only if gateway emails on
     if (!$dpne) {
         // notify user
         if (isset($user->ID)) {
             // mgm post setup object
             $post_obj = mgm_get_post($post_id);
             // check
             if ($this->send_payment_email($_POST['custom'])) {
                 // check
                 if (mgm_notify_user_post_purchase($blogname, $user, $post, $purchase_status, $system_obj, $post_obj, $status_str)) {
                     // update as email sent
                     $this->update_paymentemail_sent($_POST['custom']);
                 }
             }
         }
     }
     // notify admin, only if gateway emails on
     if (!$dge) {
         // notify admin,
         mgm_notify_admin_post_purchase($blogname, $user, $post, $status);
     }
     // if failure:
     if ($purchase_status != 'Success') {
         $errors = isset($this->response['L_ERRORCODE0']) && !empty($this->response['L_ERRORCODE0']) ? urlencode($this->response['L_ERRORCODE0'] . ': ' . $this->response['L_SHORTMESSAGE0'] . ' - ' . $this->response['L_LONGMESSAGE0']) : __('An error occured while porcessing payment.', 'mgm') . ': ' . $status_str;
         mgm_redirect(add_query_arg(array('status' => 'error', 'errors' => $errors), $this->_get_thankyou_url()));
         exit;
     }
     // default error condition redirect
     if (count($errors) > 0) {
         mgm_redirect(add_query_arg(array('status' => 'error', 'errors' => implode('|', $errors)), $this->_get_thankyou_url()));
     }
 }
/**
 * post/page meta box data save
 *
 */
function mgm_post_setup_save($post_id)
{
    // donot process ajax
    // if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) return true;
    // update
    if (isset($_POST['mgm_post'])) {
        // check revision
        if ($the_post = wp_is_post_revision($post_id)) {
            $post_id = $the_post;
        }
        // get object
        $post_obj = mgm_get_post($post_id);
        // check object
        if (is_object($post_obj)) {
            // post data
            $post_objdata = $_POST['mgm_post'];
            // access membership types
            if (!isset($post_objdata['access_membership_types'])) {
                $post_objdata['access_membership_types'] = array();
            }
            // access delay
            if (!isset($post_objdata['access_delay'])) {
                $post_objdata['access_delay'] = array();
            }
            // purchase expiry
            if (!empty($post_objdata['purchase_expiry'])) {
                //issue #1424
                $datepickerformat = mgm_get_datepicker_format();
                $post_objdata['purchase_expiry'] = mgm_format_inputdate_to_mysql($post_objdata['purchase_expiry'], $datepickerformat);
            }
            // int
            $post_objdata['access_duration'] = $post_objdata['purchase_duration'] = (int) $post_objdata['access_duration'];
            // int
            $post_objdata['access_view_limit'] = (int) $post_objdata['access_view_limit'];
            // addons
            if (!isset($post_objdata['addons'])) {
                $post_objdata['addons'] = array();
            }
            // allowed_modules
            if (!isset($post_objdata['allowed_modules'])) {
                $post_objdata['allowed_modules'] = array();
            }
            // set new fields
            $post_obj->set_fields($post_objdata);
            // apply filter
            $post_obj = apply_filters('mgm_post_update', $post_obj, $post_id);
            // save meta
            $post_obj->save();
            // log
            // mgm_log($post_obj, __FUNCTION__);
        }
    }
    // return
    return true;
}
/**
 * generate guest purchase purchase options
 *
 * @param object post 
 * @patam string message, pre_button|pre_register
 * @return string html
 */
function mgm_get_post_purchase_options($post, $message = 'pre_button')
{
    // post ot post id
    if (!is_object($post) && is_numeric($post)) {
        $post =& get_post($post);
    }
    // get post purchase options
    $post_obj = mgm_get_post($post->ID);
    // membership_types
    $membership_types_obj = mgm_get_class('mgm_membership_types');
    // system
    $system_obj = mgm_get_class('system');
    //Issue #794
    $currency = $system_obj->get_setting('currency');
    // symbol
    if (($currency_symbol = mgm_get_currency_symbols($currency)) != $currency) {
        $purchase_cost = $currency_symbol . mgm_convert_to_currency($post_obj->purchase_cost);
    } else {
        $purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost) . ' ' . $currency;
    }
    // types
    $membership_types = array();
    // acc
    $accessible_membership_types = $post_obj->get_access_membership_types();
    // loop
    foreach ($accessible_membership_types as $membership_type) {
        // url
        $membership_register_url = mgm_get_custom_url('register', false, array('membership' => base64_encode($membership_type), 'post_id' => $post->ID));
        // name
        $membership_name = $membership_types_obj->get_type_name($membership_type);
        // set
        $membership_types[] = sprintf('<li><a href="%s" target="_blank">%s</a></li>', $membership_register_url, $membership_name);
    }
    // template
    $template = mgm_stripslashes_deep($system_obj->get_template('text_guest_purchase_' . $message, array(), true));
    // the template is twice used
    // replace tags
    $html = str_replace('[post_title]', $post->post_title, $template);
    //Issue #794
    $html = str_replace('[purchase_cost]', $purchase_cost, $html);
    // membership_types_options
    $membership_types_options = count($membership_types) > 0 ? sprintf('<ul>%s</ul>', implode('', $membership_types)) : __('None available<br>', 'mgm');
    // set
    $html = str_replace('[membership_types]', $membership_types_options, $html);
    // return
    return $html;
}
function mgm_get_cached_object($class_name, $type, $id = false)
{
    // on type
    switch ($type) {
        case 'payment':
        case 'autoresponder':
            return mgm_get_module($class_name, $type, true);
            break;
        case 'member':
            return mgm_get_member($id, true);
            // cached from db
            break;
        case 'post':
            return mgm_get_post($id, true);
            // cached from db
            break;
        case 'class':
        default:
            return mgm_get_option($class_name);
            break;
    }
    // error
    return false;
}
function mgm_get_membershiptype_access_post($post_id = "")
{
    $users = array();
    $post_obj = mgm_get_post($post_id);
    $access_types = $post_obj->get_access_membership_types();
    $users = wp_cache_get('all_user_ids', 'users');
    //if empty read from db:
    if (empty($users)) {
        $users = mgm_get_all_userids(array('ID'), 'get_results');
        //update cache with user ids:
        wp_cache_set('all_user_ids', $users, 'users');
    }
    $members = array();
    // check
    if ($users) {
        // loop
        foreach ($users as $user) {
            // member object
            $member = mgm_get_member($user->ID);
            if (in_array($member->membership_type, $access_types)) {
                if ($member->status == MGM_STATUS_ACTIVE) {
                    $members[] = $member;
                }
            }
        }
    }
    return $members;
}
/**
 * add custom colums row to post/page/post type UI
 *
 * @param array $column
 * @param init $post_id
 * @return void
 */
function mgm_manage_posts_custom_column($column, $post_id)
{
    // column
    switch ($column) {
        case 'access_level':
            // post object
            $post_obj = mgm_get_post($post_id);
            // fetch
            $access_levels = $post_obj->get_access_membership_types();
            // check
            echo empty($access_levels) ? __('Public', 'mgm') : implode(', ', $access_levels);
            break;
        case 'purchasable':
            // post object
            $post_obj = mgm_get_post($post_id);
            // check
            echo $post_obj->is_purchasable() ? __('Yes', 'mgm') : __('No', 'mgm');
            break;
    }
}
 function post_settings_delete()
 {
     global $wpdb;
     extract($_POST);
     // check
     $post_id = $wpdb->get_var($wpdb->prepare("SELECT `post_id` FROM `" . TBL_MGM_POST_PROTECTED_URL . "` WHERE id = %d", $id));
     // if post
     if ((int) $post_id > 0) {
         // update content
         // get content
         $wp_post = wp_get_single_post($post_id);
         // update
         wp_update_post(array('post_content' => preg_replace('/\\[\\/?private\\]/', '', $wp_post->post_content), 'ID' => $wp_post->ID));
         // remove other Issue #922
         // get object
         $post_obj = mgm_get_post($post_id);
         // set
         $post_obj->purchasable = 'N';
         $post_obj->purchase_cost = '0.00';
         $post_obj->access_membership_types = array();
         // save meta
         $post_obj->save();
         // unset
         unset($post_obj);
     }
     // sql
     $sql = $wpdb->prepare("DELETE FROM `" . TBL_MGM_POST_PROTECTED_URL . "` WHERE id = %d", $id);
     // delete
     if ($wpdb->query($sql)) {
         $message = __('Successfully deleted post settings: ', 'mgm');
         $status = 'success';
     } else {
         $message = __('Error while deleting post settings: ', 'mgm');
         $status = 'error';
     }
     // return response
     echo json_encode(array('status' => $status, 'message' => $message));
 }
Example #11
0
 function _buy_post()
 {
     global $wpdb;
     // system
     $system_obj = mgm_get_class('system');
     $dge = bool_from_yn($system_obj->get_setting('disable_gateway_emails'));
     $dpne = bool_from_yn($system_obj->get_setting('disable_payment_notify_emails'));
     // get passthrough, stop further process if fails to parse
     $custom = $this->_get_transaction_passthrough($_REQUEST['custom']);
     // local var
     extract($custom);
     // find user
     if ($user_id) {
         $user = get_userdata($user_id);
     }
     // blog
     $blogname = get_option('blogname');
     //getting purchase post title and & price - issue #981
     $post_obj = mgm_get_post($post_id);
     $purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost);
     $post = get_post($post_id);
     $post_title = $post->post_title;
     // tran
     $tran_success = false;
     // status success
     $tran_success = true;
     // status
     $status_str = __('Last payment was successful', 'mgm');
     // emails not for guest
     if ($user_id) {
         // subject
         $subject = $system_obj->get_template('payment_success_email_template_subject', array('blogname' => $blogname), true);
         // body
         $message = $system_obj->get_template('payment_success_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'admin_email' => $system_obj->setting['admin_email']), true);
         //update coupon usage - issue #1421
         do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
     }
     // transation id
     $transaction_id = $this->_get_transaction_id('custom', $_REQUEST);
     // hook args
     $args = array('post_id' => $post_id, 'transaction_id' => $transaction_id);
     // user purchase
     if (isset($user_id) && (int) $user_id > 0) {
         $args['user_id'] = $user_id;
     } else {
         // guest purchase
         $args['guest_token'] = $guest_token;
     }
     // after succesful payment hook
     do_action('mgm_buy_post_transaction_success', $args);
     // backward compatibility
     do_action('mgm_post_purchase_payment_success', $args);
     // new organized name
     // do action
     do_action('mgm_return_post_purchase_payment_' . $this->module, array('post_id' => $post_id));
     // new, individual
     do_action('mgm_return_post_purchase_payment', array('post_id' => $post_id));
     // new, global
     // notify user
     if (!$dpne) {
         // mail
         if ($user_id && $this->send_payment_email($_REQUEST['custom'])) {
             //issue #862
             $subject = mgm_replace_email_tags($subject, $user_id);
             $message = mgm_replace_email_tags($message, $user_id);
             // mail
             mgm_mail($user->user_email, $subject, $message);
             //send an email to the buyer
             //update as email sent
             $this->update_paymentemail_sent($_REQUEST['custom']);
         }
     }
     // mark as purchased
     if (isset($guest_token)) {
         // issue #1421
         if (isset($coupon_id) && isset($coupon_code)) {
             do_action('mgm_update_coupon_usage', array('guest_token' => $guest_token, 'coupon_id' => $coupon_id));
             $this->_set_purchased(NULL, $post_id, $guest_token, $_REQUEST['custom'], $coupon_code);
         } else {
             $this->_set_purchased(NULL, $post_id, $guest_token, $_REQUEST['custom']);
         }
     } else {
         $this->_set_purchased($user_id, $post_id, NULL, $_REQUEST['custom']);
     }
     // status
     $status = __('The post was purchased successfully', 'mgm');
     // transaction status
     mgm_update_transaction_status($_REQUEST['custom'], $status, $status_str);
     // notify admin, only if gateway emails on
     if (!$dge) {
         // not for guest
         if ($user_id) {
             // subject
             $subject = "[" . $blogname . "] Admin Notification: " . $user->user_email . " purchased post " . $post_id;
             // message
             $message = "User display name: {$user->display_name}<br />\r\r\n\t\t\t\t\t\t\tUser email: {$user->user_email}<br />\r\r\n\t\t\t\t\t\t\tUser ID: {$user->ID}<br />Status: " . $status . "<br />\r\r\n\t\t\t\t\t\t\tAction: Purchase post:" . $subject . "<br /><br />" . $message;
         } else {
             $subject = "[" . $blogname . "] Admin Notification: Guest[IP: " . mgm_get_client_ip_address() . "] purchased post " . $post_id;
             $message = "Guest Purchase";
         }
         // mail
         mgm_mail($system_obj->setting['admin_email'], $subject, $message);
     }
 }
/**
 * get post purchase buttons
 * final step for post purchase
 *
 * @param void
 * @return $html
 */
function mgm_get_post_purchase_buttons()
{
    // get current user data - issue #1421
    $user = wp_get_current_user();
    // pack
    $pack = NULL;
    // addon options
    if ($addon_option_ids = mgm_post_var('addon_options')) {
        $addon_options = mgm_get_addon_options_only($addon_option_ids);
        // mgm_pr($addon_options);
    }
    // post purchase
    if (isset($_POST['post_id'])) {
        //issue #1250
        if (isset($_POST['mgm_postpurchase_field']['coupon']) && !empty($_POST['mgm_postpurchase_field']['coupon'])) {
            //issue #1250 - Coupon validation
            if (!empty($_POST['form_action'])) {
                // check if its a valid coupon
                if (!($coupon = mgm_get_coupon_data($_POST['mgm_postpurchase_field']['coupon']))) {
                    //redirect back to the form
                    $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'error_field_value' => $_POST['mgm_postpurchase_field']['coupon']);
                    $redirect = add_query_arg($q_arg, $_POST['form_action']);
                    mgm_redirect($redirect);
                    exit;
                }
            }
        }
        // post id
        $post_id = $_POST['post_id'];
        // gete mgm data
        $post_obj = mgm_get_post($post_id);
        $cost = mgm_convert_to_currency($post_obj->purchase_cost);
        $product = $post_obj->product;
        $allowed_modules = $post_obj->allowed_modules;
        // post data
        $post = get_post($post_id);
        $title = $post->post_title;
        // item name -issue #1380
        $item_name = apply_filters('mgm_post_purchase_itemname', sprintf(__('Purchase Post - %s', 'mgm'), $title));
        // set pack
        $pack = array('duration' => 1, 'item_name' => $item_name, 'buypost' => 1, 'cost' => $cost, 'title' => $title, 'product' => $product, 'post_id' => $post_id, 'allowed_modules' => $allowed_modules);
    } else {
        if (isset($_POST['postpack_id'])) {
            // post pack purchase
            //issue #1250
            if (isset($_POST['mgm_postpurchase_field']['coupon']) && !empty($_POST['mgm_postpurchase_field']['coupon'])) {
                //issue #1250 - Coupon validation
                if (!empty($_POST['form_action'])) {
                    // check if its a valid coupon
                    if (!($coupon = mgm_get_coupon_data($_POST['mgm_postpurchase_field']['coupon']))) {
                        //redirect back to the form
                        $q_arg = array('error_field' => 'Coupon', 'error_type' => 'invalid', 'error_field_value' => $_POST['mgm_postpurchase_field']['coupon']);
                        $redirect = add_query_arg($q_arg, $_POST['form_action']);
                        mgm_redirect($redirect);
                        exit;
                    }
                }
            }
            // post pack purchase
            $postpack_id = $_POST['postpack_id'];
            // pcak id
            $postpack_post_id = $_POST['postpack_post_id'];
            // post id where pack is listed, redirect here
            // get pack
            $postpack = mgm_get_postpack($postpack_id);
            $cost = mgm_convert_to_currency($postpack->cost);
            $product = json_decode($postpack->product, true);
            $modules = json_decode($postpack->modules, true);
            //mgm_pr($postpack);
            // item name -issue #1380
            $item_name = apply_filters('mgm_postpack_purchase_itemname', sprintf(__('Purchase Post Pack - %s', 'mgm'), $postpack->name));
            // post id
            $post_id = mgm_get_postpack_posts_csv($postpack_id);
            // set pack
            $pack = array('duration' => 1, 'item_name' => $item_name, 'buypost' => 1, 'cost' => $cost, 'title' => $postpack->name, 'product' => $product, 'post_id' => $post_id, 'postpack_id' => $postpack_id, 'postpack_post_id' => $postpack_post_id, 'allowed_modules' => $modules);
        }
    }
    // check
    if (!$pack) {
        return __('Error in Payment! No data available ');
        exit;
    }
    // guest token	-issue #1421
    if (isset($_POST['guest_purchase']) && $_POST['guest_purchase'] == TRUE && $user->ID <= 0) {
        $pack['guest_token'] = sanitize_title_for_query(mgm_create_token());
    }
    // addon options
    if (isset($addon_options) && !empty($addon_options)) {
        $pack['addon_options'] = $addon_options;
    }
    // get coupon
    $post_purchase_coupon = mgm_save_partial_fields(array('on_postpurchase' => true), 'mgm_postpurchase_field', $pack['cost'], false, 'postpurchase');
    // alter
    mgm_get_post_purchase_coupon_pack($post_purchase_coupon, $pack);
    // Eg: $_POST['mgm_payment_gateways'] = mgm_paypal
    $cf_payment_gateways = isset($_POST['mgm_payment_gateways']) && !empty($_POST['mgm_payment_gateways']) ? $_POST['mgm_payment_gateways'] : null;
    // bypass step2 if payment gateway is submitted: issue #: 469
    if (!is_null($cf_payment_gateways)) {
        // get pack
        // mgm_get_upgrade_coupon_pack($member, $selected_pack);
        // cost
        if ((double) $pack['cost'] > 0) {
            //get an object of the payment gateway:
            $mod_obj = mgm_get_module($cf_payment_gateways, 'payment');
            // tran options
            $tran_options = array('user_id' => $user->ID);
            // is register & purchase
            if (isset($_POST['post_id'])) {
                $tran_options['post_id'] = (int) $_POST['post_id'];
            }
            // postpack id
            if (isset($_POST['postpack_id'])) {
                $tran_options['postpack_id'] = (int) $_POST['postpack_id'];
            }
            // is register & purchase postpack
            if (isset($_POST['postpack_post_id']) && isset($_POST['postpack_id'])) {
                $tran_options['postpack_post_id'] = (int) $_POST['postpack_post_id'];
                $tran_options['postpack_id'] = (int) $_POST['postpack_id'];
            }
            // create transaction
            $tran_id = mgm_add_transaction($pack, $tran_options);
            // bypass directly to process return if manual payment:
            if ($cf_payment_gateways == 'mgm_manualpay') {
                // set
                $_POST['custom'] = $tran_id;
                // direct call to module return function:
                $mod_obj->process_return();
                // exit
                exit;
            }
            // encode id:
            $tran_id = mgm_encode_id($tran_id);
            $redirect = $mod_obj->_get_endpoint('html_redirect', true);
            $redirect = add_query_arg(array('tran_id' => $tran_id), $redirect);
            // redirect
            mgm_redirect($redirect);
            // this goes to subscribe, mgm_functions.php/mgm_get_subscription_buttons
            // exit
            exit;
        }
    }
    // get payment modules
    $a_payment_modules = mgm_get_class('system')->get_active_modules('payment');
    // init
    $payment_modules = array();
    // when active
    if ($a_payment_modules) {
        // loop
        foreach ($a_payment_modules as $payment_module) {
            // not trial
            if (in_array($payment_module, array('mgm_free', 'mgm_trial'))) {
                continue;
            }
            // store
            $payment_modules[] = $payment_module;
        }
    }
    // init
    $button = '';
    // transaction
    $tran_id = NULL;
    $button_printed = 0;
    // loop modules
    foreach ($payment_modules as $module) {
        // object
        $mod_obj = mgm_get_module($module, 'payment');
        // check buypost support
        if (in_array('buypost', $mod_obj->supported_buttons)) {
            // create transaction
            if (!$tran_id) {
                $tran_id = mgm_add_transaction($pack);
            }
            // button code
            if (isset($pack['allowed_modules'])) {
                // Issue #1562: If no payment module is selected, display all supported modules
                if (!empty($pack['allowed_modules']) && FALSE === in_array($module, $pack['allowed_modules'])) {
                    continue;
                }
            }
            $button_code = $mod_obj->get_button_buypost(array('pack' => $pack, 'tran_id' => $tran_id), true);
            $button_printed++;
            // get button
            $button .= "<div class='mgm_custom_filed_table'>" . $button_code . "</div>";
        }
    }
    // none active
    if ($button_printed == 0) {
        $button .= sprintf('<p class="mgm-no-module"> %s </p>', __('No Payment module active for this Content Purchase.', 'mgm'));
    }
    // if Cost is zero, then process using free module.: issue#: 883
    if ($tran_id && $pack['cost'] == 0 && in_array('mgm_free', $a_payment_modules) && mgm_get_module('mgm_free')->is_enabled()) {
        // module
        $module = 'mgm_free';
        // payments url
        $payments_url = mgm_get_custom_url('transactions');
        // query_args
        $query_args = array('method' => 'payment_return', 'module' => $module, 'custom' => $tran_id);
        // redirector
        if (isset($_REQUEST['redirector'])) {
            // set
            $query_args['redirector'] = $_REQUEST['redirector'];
        }
        // redirect to module to mark the payment as complete
        $redirect = add_query_arg($query_args, $payments_url);
        // redirect
        mgm_redirect($redirect);
    }
    // html
    $return = '<div class="post_purchase_select_gateway">' . __('Please Select a Payment Gateway.', 'mgm') . '</div>' . $button;
    // return
    return $return;
}
function mgm_get_posts_for_level($membership_type = '', $show_all = true)
{
    global $wpdb, $post;
    if (!empty($membership_type)) {
        if (!is_array($membership_type)) {
            $membership_type = array(0 => $membership_type);
        }
        // get post types
        $post_types_in = mgm_get_post_types(true);
        // id
        $post_id_notin = is_numeric($post->ID) ? $post->ID : 0;
        // sql
        $limit = 50;
        $per_page = 10;
        $sql = "SELECT DISTINCT(ID), post_title, post_date, post_content\r\r\n\t\t\t\tFROM " . $wpdb->posts . " A JOIN " . $wpdb->postmeta . " B ON (A.ID = B.post_id ) \r\r\n\t\t\t\tWHERE post_status = 'publish' AND B.meta_key LIKE '_mgm_post%' \r\r\n\t\t\t\tAND post_type IN ({$post_types_in}) AND A.id NOT IN({$post_id_notin}) \r\r\n\t\t\t\tORDER BY post_date DESC LIMIT 0," . $limit;
        // get posts
        $results = $wpdb->get_results($sql);
        // chk
        if (count($results) > 0) {
            // set counter
            $total = 0;
            // loop
            foreach ($results as $id => $obj) {
                // post
                $post_obj = mgm_get_post($obj->ID);
                $access_types = $post_obj->get_access_membership_types();
                $found = false;
                if (!empty($access_types)) {
                    foreach ($access_types as $type) {
                        if (in_array($type, $membership_type)) {
                            $membership = mgm_get_class('membership_types');
                            $obj->access_membership_type = $membership->get_type_name($type);
                            $found = true;
                            $total++;
                            break;
                        }
                    }
                    if ($found && (isset($_GET['show']) && $_GET['show'] == 'all' || $show_all || $total <= $per_page)) {
                        $posts[] = $obj;
                    }
                }
                // branch
            }
            return array('posts' => $posts, 'total' => $total);
        }
    }
    return array();
}
 function membership_type_update()
 {
     global $wpdb;
     extract($_POST);
     // init
     $message = $status = '';
     // new type -------------------------------------------------------------------
     if (isset($new_membership_type) && !empty($new_membership_type)) {
         // new type
         $new_membership_type = trim($new_membership_type);
         // allowed only
         if (strtolower($new_membership_type) != 'none') {
             // set
             $membership_types_obj = mgm_get_class('membership_types');
             // set type, check duplicate
             $success = $membership_types_obj->set_membership_type($new_membership_type);
             // update
             if ($success) {
                 // add redirect url
                 $n_login_redirect_url = isset($new_login_redirect_url) ? $new_login_redirect_url : '';
                 $n_logout_redirect_url = isset($new_logout_redirect_url) ? $new_logout_redirect_url : '';
                 $n_type_code = $membership_types_obj->get_type_code($new_membership_type);
                 // set url
                 $membership_types_obj->set_login_redirect($n_type_code, $n_login_redirect_url);
                 $membership_types_obj->set_logout_redirect($n_type_code, $n_logout_redirect_url);
                 // update
                 $membership_types_obj->save();
                 // message
                 $message = sprintf(__('Successfully created new membership type: %s.', 'mgm'), mgm_stripslashes_deep($new_membership_type));
                 $status = 'success';
             } else {
                 $message = sprintf(__('Error while creating new membership type: %s. Duplicate type.', 'mgm'), mgm_stripslashes_deep($new_membership_type));
                 $status = 'error';
             }
         } else {
             $message = sprintf(__('Error while creating new membership type: %s. Not allowed.', 'mgm'), mgm_stripslashes_deep($new_membership_type));
             $status = 'error';
         }
     }
     // delete/move account ------------------------------------------------------------
     if (isset($remove_membership_type) && count($remove_membership_type) > 0) {
         // get object
         $membership_types_obj = mgm_get_class('membership_types');
         // users
         $users = mgm_get_all_userids(array('ID'), 'get_results');
         // how many removed
         $removed = 0;
         // loop
         foreach ($remove_membership_type as $type_code) {
             // unset
             $membership_types_obj->unset_membership_type($type_code);
             // move
             if (isset($move_membership_type_to[$type_code]) && $move_membership_type_to[$type_code] != 'none') {
                 // loop
                 foreach ($users as $user) {
                     // get
                     $member = mgm_get_member($user->ID);
                     // if users with same membershiptype as that of selected
                     if ($member->membership_type == $type_code) {
                         // set
                         $member->membership_type = $move_membership_type_to[$type_code];
                         // save
                         $member->save();
                     } else {
                         // check if any multiple levels exist:
                         if (isset($member->other_membership_types) && is_array($member->other_membership_types) && count($member->other_membership_types) > 0) {
                             // loop
                             foreach ($member->other_membership_types as $key => $memtypes) {
                                 // make sure its an object:
                                 $memtypes = mgm_convert_array_to_memberobj($memtypes, $user->ID);
                                 // verify
                                 if ($memtypes->membership_type == $type_code) {
                                     // set
                                     $memtypes->membership_type = $move_membership_type_to[$type_code];
                                     // save
                                     mgm_save_another_membership_fields($memtypes, $user->ID, $key);
                                     break;
                                 }
                             }
                         }
                     }
                     // unset
                     unset($member);
                 }
             }
             // remove packs
             $subscription_packs = mgm_get_class('subscription_packs');
             // empty
             $packs = array();
             // set
             foreach ($subscription_packs->packs as $i => $pack) {
                 // if membership_type is same as being deleted
                 if ($pack['membership_type'] == $type_code) {
                     continue;
                     // skip
                 }
                 // filtered
                 $packs[] = $pack;
             }
             // set
             $subscription_packs->set_packs($packs);
             // update
             $subscription_packs->save();
             // removed
             $removed++;
         }
         // ends remove pack:
         // save
         $membership_types_obj->save();
         // message
         $message .= (!empty($message) ? '<br>' : '') . sprintf(__('Successfully removed %d membership type(s).', 'mgm'), $removed);
         // set status
         $status = 'success';
     }
     // update name/redirects ------------------------------------------------------------------------
     // get object
     $membership_types_obj = mgm_get_class('membership_types');
     // ge all users
     $users = mgm_get_all_userids(array('ID'), 'get_results');
     // init
     $updated = 0;
     // loop types
     foreach ($membership_types_obj->get_membership_types() as $type_code => $type_name) {
         // skip new type, in edit otherwise overwritten
         if (isset($n_type_code) && !empty($n_type_code) && $n_type_code == $type_code) {
             continue;
         }
         // urls
         $_login_redirect_url = isset($login_redirect_url[$type_code]) ? $login_redirect_url[$type_code] : '';
         $_logout_redirect_url = isset($logout_redirect_url[$type_code]) ? $logout_redirect_url[$type_code] : '';
         // set urls
         $membership_types_obj->set_login_redirect($type_code, $_login_redirect_url);
         $membership_types_obj->set_logout_redirect($type_code, $_logout_redirect_url);
         // set name
         if (isset($membership_type_names[$type_code]) && !empty($membership_type_names[$type_code]) && $membership_type_names[$type_code] != $type_name) {
             //issue #1127
             $new_type_code = $membership_types_obj->get_type_code($membership_type_names[$type_code]);
             //check
             if ($new_type_code != $type_code) {
                 // get object
                 $obj_sp = mgm_get_class('subscription_packs');
                 //update new
                 foreach ($obj_sp->packs as $key => $pack) {
                     if ($obj_sp->packs[$key]['membership_type'] == $type_code) {
                         $obj_sp->packs[$key]['membership_type'] = $new_type_code;
                         $obj_sp->save();
                     }
                 }
                 // loop
                 foreach ($users as $user) {
                     // get
                     $member = mgm_get_member($user->ID);
                     // if users with same membershiptype as that of selected
                     if (isset($member->membership_type) && $member->membership_type == $type_code) {
                         // set
                         $member->membership_type = $new_type_code;
                         // save
                         $member->save();
                     }
                     // check if any multiple levels exist:
                     if (isset($member->other_membership_types) && is_array($member->other_membership_types) && count($member->other_membership_types) > 0) {
                         // loop
                         foreach ($member->other_membership_types as $key => $memtypes) {
                             // make sure its an object:
                             $memtypes = mgm_convert_array_to_memberobj($memtypes, $user->ID);
                             // verify
                             if ($memtypes->membership_type == $type_code) {
                                 // set
                                 $memtypes->membership_type = $new_type_code;
                                 // save
                                 mgm_save_another_membership_fields($memtypes, $user->ID, $key);
                             }
                         }
                     }
                     // unset
                     unset($member);
                 }
                 //issue #1336
                 $membership_posts = mgm_get_posts_for_level($type_code);
                 if (isset($membership_posts['total']) && $membership_posts['total'] > 0) {
                     foreach ($membership_posts['posts'] as $id => $obj) {
                         $post_id = $obj->ID;
                         // get object
                         $post_obj = mgm_get_post($post_id);
                         // if access set
                         if (is_array($post_obj->access_membership_types)) {
                             $access_membership_types = $post_obj->access_membership_types;
                             foreach ($post_obj->access_membership_types as $key => $access_membership_type) {
                                 if ($access_membership_type == $type_code) {
                                     //update rename
                                     $access_membership_types[$key] = $new_type_code;
                                     // set
                                     $post_obj->access_membership_types = $access_membership_types;
                                 }
                             }
                         }
                         // if access delay set
                         if (is_array($post_obj->access_delay)) {
                             $access_delay = $post_obj->access_delay;
                             if (isset($access_delay[$type_code])) {
                                 $access_delay[$new_type_code] = $access_delay[$type_code];
                                 unset($access_delay[$type_code]);
                                 $post_obj->access_delay = $access_delay;
                             }
                         }
                         // apply filter
                         $post_obj = apply_filters('mgm_post_update', $post_obj, $post_id);
                         // save meta
                         $post_obj->save();
                         // unset
                         unset($post_obj);
                     }
                 }
                 //unset
                 $membership_types_obj->unset_membership_type($type_code);
                 // set
                 $membership_types_obj->set_name($membership_type_names[$type_code], $new_type_code);
             } else {
                 // set
                 $membership_types_obj->set_name($membership_type_names[$type_code], $type_code);
             }
         }
         // update
         $updated++;
     }
     // update
     $membership_types_obj->save();
     // notify
     if (empty($message)) {
         // message
         $message = sprintf(__('Successfully updated %d membership type(s).', 'mgm'), $updated);
         // set status
         $status = 'success';
     }
     // return response
     echo json_encode(array('status' => $status, 'message' => $message));
     exit;
 }
 function _buy_post()
 {
     global $wpdb;
     // get system settings
     $system_obj = mgm_get_class('system');
     $dge = bool_from_yn($system_obj->get_setting('disable_gateway_emails'));
     $dpne = bool_from_yn($system_obj->get_setting('disable_payment_notify_emails'));
     // get passthrough, stop further process if fails to parse
     $custom = $this->_get_transaction_passthrough($_POST['M_custom']);
     // local var
     extract($custom);
     // find user
     //issue #504
     if ($user_id) {
         $user = get_userdata($user_id);
     }
     $blogname = get_option('blogname');
     $tran_success = false;
     //getting purchase post title and & price - issue #981
     $post_obj = mgm_get_post($post_id);
     $purchase_cost = mgm_convert_to_currency($post_obj->purchase_cost);
     $post = get_post($post_id);
     $post_title = $post->post_title;
     // errors
     $errors = array();
     // check
     switch ($_POST['transStatus']) {
         case "Y":
             // status success
             $tran_success = true;
             // status str
             $status_str = __('Last payment was successful', 'mgm');
             //issue #504
             if ($user_id) {
                 // subject
                 $subject = $system_obj->get_template('payment_success_email_template_subject', array('blogname' => $blogname), true);
                 // body
                 $message = $system_obj->get_template('payment_success_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'admin_email' => $system_obj->setting['admin_email']), true);
             }
             // transaction id
             $transaction_id = $this->_get_transaction_id('M_custom');
             // hook args
             $args = array('post_id' => $post_id, 'transaction_id' => $transaction_id);
             // user purchase
             if (isset($user_id) && (int) $user_id > 0) {
                 $args['user_id'] = $user_id;
             } else {
                 // guest purchase
                 $args['guest_token'] = $guest_token;
             }
             // after succesful payment hook
             do_action('mgm_buy_post_transaction_success', $args);
             // backward compatibility
             do_action('mgm_post_purchase_payment_success', $args);
             // new organized name
             break;
         case "C":
             // status
             $status_str = __('Last payment was refunded or denied', 'mgm');
             //issue #504
             if ($user_id) {
                 // subject
                 $subject = $system_obj->get_template('payment_failed_email_template_subject', array('blogname' => $blogname), true);
                 // body
                 $message = $system_obj->get_template('payment_failed_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'payment_type' => 'post purchase payment', 'reason' => $status_str, 'admin_email' => $system_obj->setting['admin_email']), true);
             }
             // error
             $errors[] = $status_str;
             break;
         case "F":
             // reason
             $reason = 'Unknown';
             // status
             $status_str = sprintf(__('Last payment is pending. Reason: %s', 'mgm'), $reason);
             //issue #504
             if ($user_id) {
                 // subject
                 $subject = $system_obj->get_template('payment_pending_email_template_subject', array('blogname' => $blogname), true);
                 // body
                 $message = $system_obj->get_template('payment_pending_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'reason' => $status_str, 'admin_email' => $system_obj->setting['admin_email']), true);
             }
             // error
             $errors[] = $status_str;
             break;
         default:
             // status
             $status_str = sprintf(__('Last payment status: %s', 'mgm'), $_POST['rawAuthMessage']);
             //issue #504
             if ($user_id) {
                 // subject
                 $subject = $system_obj->get_template('payment_unknown_email_template_subject', array('blogname' => $blogname), true);
                 // body
                 $message = $system_obj->get_template('payment_unknown_email_template_body', array('blogname' => $blogname, 'name' => $user->display_name, 'post_title' => $post_title, 'purchase_cost' => $purchase_cost, 'email' => $user->user_email, 'reason' => $status_str, 'admin_email' => $system_obj->setting['admin_email']), true);
             }
             // error
             $errors[] = $status_str;
     }
     // do action
     do_action('mgm_return_post_purchase_payment_' . $this->module, array('post_id' => $post_id));
     // new, individual
     do_action('mgm_return_post_purchase_payment', array('post_id' => $post_id));
     // new, global
     // notify user
     if (!$dpne) {
         if ($user_id && $this->send_payment_email($_POST['M_custom'])) {
             //issue #862
             $subject = mgm_replace_email_tags($subject, $user_id);
             $message = mgm_replace_email_tags($message, $user_id);
             mgm_mail($user->user_email, $subject, $message);
             //send an email to the buyer
             //update as email sent
             $this->update_paymentemail_sent($_POST['M_custom']);
         }
     }
     $status = __('Failed join', 'mgm');
     //overridden on a successful payment
     if ($tran_success) {
         //issue #1421
         if ($user_id) {
             do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
         }
         // mark as purchased
         if (isset($guest_token)) {
             // issue #1421
             if (isset($coupon_id) && isset($coupon_code)) {
                 do_action('mgm_update_coupon_usage', array('guest_token' => $guest_token, 'coupon_id' => $coupon_id));
                 $this->_set_purchased(NULL, $post_id, $guest_token, $_POST['M_custom'], $coupon_code);
             } else {
                 $this->_set_purchased(NULL, $post_id, $guest_token, $_POST['M_custom']);
             }
         } else {
             $this->_set_purchased($user_id, $post_id, NULL, $_POST['M_custom']);
         }
         // status
         $status = __('The post was purchased successfully', 'mgm');
     }
     // transaction status
     mgm_update_transaction_status($_POST['M_custom'], $status, $status_str);
     // notify admin, only if gateway emails on
     if (!$dge) {
         // not for guest
         if ($user_id) {
             $subject = "[" . $blogname . "] Admin Notification: " . $user->user_email . " purchased post " . $post_id;
             $message = "User display name: {$user->display_name}<br />User email: {$user->user_email}<br />User ID: {$user->ID}<br />Status: " . $status . "<br />Action: Purchase post:" . $subject . "<br /><br />" . $message . "<br /><br /><pre>" . print_r($_POST, true) . '</pre>';
         } else {
             $subject = "[" . $blogname . "] Admin Notification: Guest[IP: " . mgm_get_client_ip_address() . "] purchased post " . $post_id;
             $message = "Guest Purchase";
         }
         mgm_mail($system_obj->setting['admin_email'], $subject, $message);
     }
     // error condition redirect
     if (count($errors) > 0) {
         $this->_meta_redirect(add_query_arg(array('status' => 'error', 'errors' => implode('|', $errors)), $this->_get_thankyou_url()));
     }
 }
Example #16
0
_e('Action', 'mgm');
?>
</b>
			</div>
		</div>
		<?php 
$date_format = mgm_get_date_format('date_format');
$date_format_time = mgm_get_date_format('date_format_time');
// loop
if ($data['post_purchases']) {
    foreach ($data['post_purchases'] as $purchase) {
        // check is_expiry
        if ($purchase->is_expire == 'N') {
            $expiry = __('Indefinite', 'mgm');
        } else {
            $expiry = mgm_get_post($purchase->post_id)->get_access_duration();
            $expiry = !$expiry ? __('Indefinite', 'mgm') : date('d/m/Y', 86400 * $expiry + strtotime($purchase->purchase_dt)) . " (" . $expiry . __(' D', 'mgm') . ")";
        }
        // member name
        if (preg_match('/^guest-/', $purchase->user_login)) {
            // guest token
            $guest_token = str_replace('guest-', '', $purchase->user_login);
            // member
            $member = __('Guest', 'mgm') . sprintf(' (%s)', $guest_token);
            // post url
            $post_url = add_query_arg(array('guest_token' => $guest_token), get_permalink($purchase->post_id));
        } else {
            // member
            $member = $purchase->user_login;
            // post url
            $post_url = get_permalink($purchase->post_id);
 function _buy_post()
 {
     global $wpdb;
     // system
     $system_obj = mgm_get_class('system');
     $dge = bool_from_yn($system_obj->get_setting('disable_gateway_emails'));
     $dpne = bool_from_yn($system_obj->get_setting('disable_payment_notify_emails'));
     // passthrough
     $alt_tran_id = $this->_get_alternate_transaction_id();
     // get passthrough, stop further process if fails to parse
     $custom = $this->_get_transaction_passthrough($alt_tran_id);
     // local var
     extract($custom);
     // find user
     $user = null;
     // check
     if (isset($user_id) && (int) $user_id > 0) {
         $user = get_userdata($user_id);
     }
     // errors
     $errors = array();
     // purchase status
     $purchase_status = 'Error';
     // response code
     $response_code = $this->_get_response_code($this->response['response_status'], 'status');
     // process on response code
     switch ($response_code) {
         case 'Approved':
             // status
             $status_str = __('Last payment was successful', 'mgm');
             // purchase status
             $purchase_status = 'Success';
             // transaction id
             $transaction_id = $this->_get_transaction_id();
             // hook args
             $args = array('post_id' => $post_id, 'transaction_id' => $transaction_id);
             // user purchase
             if (isset($user_id) && (int) $user_id > 0) {
                 $args['user_id'] = $user_id;
             } else {
                 // guest purchase
                 $args['guest_token'] = $guest_token;
             }
             // after succesful payment hook
             do_action('mgm_buy_post_transaction_success', $args);
             // backward compatibility
             do_action('mgm_post_purchase_payment_success', $args);
             // new organized name
             break;
         case 'Declined':
         case 'Refunded':
         case 'Denied':
             // status
             $status_str = __('Last payment was refunded or denied', 'mgm');
             // purchase status
             $purchase_status = 'Failure';
             // error
             $errors[] = $status_str;
             break;
         case 'Pending':
         case 'Held for Review':
             // status
             $status_str = sprintf(__('Last payment is pending. Reason: %s', 'mgm'), $this->response['message_text']);
             // purchase status
             $purchase_status = 'Pending';
             // error
             $errors[] = $status_str;
             break;
         default:
             // status
             $status_str = sprintf(__('Last payment status: %s', 'mgm'), $response_code);
             // purchase status
             $purchase_status = 'Unknown';
             // error
             $errors[] = $status_str;
             break;
     }
     // do action
     do_action('mgm_return_post_purchase_payment_' . $this->module, array('post_id' => $post_id));
     // new, individual
     do_action('mgm_return_post_purchase_payment', array('post_id' => $post_id));
     // new, global
     // status
     $status = __('Failed join', 'mgm');
     // overridden on a successful payment
     // check status
     if ($purchase_status == 'Success') {
         // mark as purchased
         if (isset($user->ID)) {
             // purchased by user
             // call coupon action
             do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
             // set as purchased
             $this->_set_purchased($user_id, $post_id, NULL, $alt_tran_id);
         } else {
             // purchased by guest
             if (isset($guest_token)) {
                 // issue #1421, used coupon
                 if (isset($coupon_id) && isset($coupon_code)) {
                     // call coupon action
                     do_action('mgm_update_coupon_usage', array('guest_token' => $guest_token, 'coupon_id' => $coupon_id));
                     // set as purchased
                     $this->_set_purchased(NULL, $post_id, $guest_token, $alt_tran_id, $coupon_code);
                 } else {
                     $this->_set_purchased(NULL, $post_id, $guest_token, $alt_tran_id);
                 }
             }
         }
         // status
         $status = __('The post was purchased successfully', 'mgm');
     }
     // transaction status
     mgm_update_transaction_status($alt_tran_id, $status, $status_str);
     // blog
     $blogname = get_option('blogname');
     // post being purchased
     $post = get_post($post_id);
     // notify user and admin, only if gateway emails on
     if (!$dpne) {
         // notify user
         if (isset($user->ID)) {
             // mgm post setup object
             $post_obj = mgm_get_post($post_id);
             // check
             if ($this->send_payment_email($alt_tran_id)) {
                 // check
                 if (mgm_notify_user_post_purchase($blogname, $user, $post, $purchase_status, $system_obj, $post_obj, $status_str)) {
                     // update as email sent
                     $this->update_paymentemail_sent($alt_tran_id);
                 }
             }
         }
     }
     // notify admin, only if gateway emails on
     if (!$dge) {
         // notify admin,
         mgm_notify_admin_post_purchase($blogname, $user, $post, $status);
     }
     // error condition redirect
     if (count($errors) > 0) {
         mgm_redirect(add_query_arg(array('status' => 'error', 'errors' => implode('|', $errors)), $this->_get_thankyou_url()));
     }
 }
 function post_purchase_export()
 {
     global $wpdb;
     // data
     $data = array();
     // filter
     $sql_filter = $data['search_field_name'] = $data['search_field_value'] = '';
     $search_field_name = mgm_post_var('search_field_name');
     // check
     if (!empty($search_field_name)) {
         // post
         $search_field_value = mgm_post_var('search_field_value');
         $search_field_value_two = mgm_post_var('search_field_value_two');
         // view data
         $data['search_field_name'] = $search_field_name;
         // for display
         //issue #1281
         $data['search_field_value'] = htmlentities($search_field_value, ENT_QUOTES, "UTF-8");
         // for display
         $data['search_field_value_two'] = htmlentities($search_field_value_two, ENT_QUOTES, "UTF-8");
         // for display
         //searc value
         $search_field_value = esc_sql($search_field_value);
         // for sql
         // end date value
         $search_field_value_two = esc_sql($search_field_value_two);
         // for sql
         //current date
         $curr_date = mgm_get_current_datetime();
         $current_date = $curr_date['timestamp'];
         // by field
         switch ($search_field_name) {
             case 'user_login':
                 $sql_filter = " AND (`user_login` LIKE '%{$search_field_value}%' OR `guest_token` LIKE '%{$search_field_value}%')";
                 break;
             case 'post_title':
                 $sql_filter = " AND (`post_title` LIKE '%{$search_field_value}%')";
                 break;
             case 'is_gift':
                 $sql_filter = " AND (`is_gift` = '{$search_field_value}')";
                 break;
             case 'purchase_dt':
                 // date start
                 if (empty($search_field_value)) {
                     $search_field_value = date('Y-m-d', $current_date);
                 }
                 // date end
                 if (empty($search_field_value_two)) {
                     $search_field_value_two = date('Y-m-d', $current_date);
                 }
                 // convert
                 $search_field_value = mgm_format_inputdate_to_mysql($search_field_value);
                 $search_field_value_two = mgm_format_inputdate_to_mysql($search_field_value_two);
                 // set
                 $sql_filter = " AND (DATE_FORMAT(`pp`.`purchase_dt`,'%Y-%m-%d') BETWEEN '{$search_field_value}' AND '{$search_field_value_two}')";
                 break;
         }
     }
     // order
     $sql_order = " ORDER BY u.user_login, p.post_title";
     // sql
     $sql = "SELECT SQL_CALC_FOUND_ROWS p.ID AS post_id, p.post_title, pp.purchase_dt, \r\r\n\t\t        IF(user_id IS NULL, CONCAT('guest-', guest_token), u.user_login) AS user_login, pp.id,\r\r\n\t\t        pp.is_gift,pp.is_expire FROM `" . TBL_MGM_POST_PURCHASES . "` pp \r\r\n\t\t\t\tLEFT JOIN " . $wpdb->posts . " p ON (p.id = pp.post_id) \r\r\n\t\t\t\tLEFT JOIN " . $wpdb->users . " u ON (u.ID = pp.user_id) WHERE 1 \r\r\n\t\t\t\t{$sql_filter} {$sql_order} ";
     // log
     // mgm_log($sql);
     // store
     $data['post_purchases'] = $wpdb->get_results($sql);
     // date format
     $date_format = mgm_get_date_format('date_format');
     // init
     $purchases = array();
     // check
     if (count($data['post_purchases']) > 0) {
         //purchases
         foreach ($data['post_purchases'] as $purchase) {
             // int
             $row = new stdClass();
             // type
             $type = $purchase->is_gift == 'Y' ? __('Gift', 'mgm') : __('Purchase', 'mgm');
             // check is_expiry
             if ($purchase->is_expire == 'N') {
                 $expiry = __('Indefinite', 'mgm');
             } else {
                 $expiry = mgm_get_post($purchase->post_id)->get_access_duration();
                 $expiry = !$expiry ? __('Indefinite', 'mgm') : date($date_format, 86400 * $expiry + strtotime($purchase->purchase_dt)) . " (" . $expiry . __(' D', 'mgm') . ")";
             }
             // member name
             if (preg_match('/^guest-/', $purchase->user_login)) {
                 // guest token
                 $guest_token = str_replace('guest-', '', $purchase->user_login);
                 // username
                 $username = __('Guest', 'mgm') . sprintf(' (%s)', $guest_token);
             } else {
                 // username
                 $username = $purchase->user_login;
             }
             //export fields
             $row->username = $username;
             $row->post = $purchase->post_title;
             $row->type = $type;
             $row->expire_date = $expiry;
             $row->purchase_or_gift_date = date($date_format, strtotime($purchase->purchase_dt));
             // cache
             $purchases[] = $row;
             // unset
             unset($row);
         }
     }
     // default response
     $response = array('status' => 'error', 'message' => __('Error while exporting post (purchase/gift)s.', 'mgm'));
     // check
     if (count($purchases) > 0) {
         // success
         $success = count($purchases);
         // create
         if (mgm_post_var('export_format') == 'csv') {
             $filename = mgm_create_csv_file($purchases, 'post_purchases');
         } else {
             $filename = mgm_create_xls_file($purchases, 'post_purchases');
         }
         // src
         $file_src = MGM_FILES_EXPORT_URL . $filename;
         // message
         $message = sprintf(__('Successfully exported %d post %s.', 'mgm'), $success, $success > 1 ? 'purchases' : 'purchase');
         // init
         $response = array('status' => 'success', 'message' => $message, 'src' => $file_src);
     }
     // return response
     echo json_encode($response);
     exit;
 }