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()));
         }
     }
 }
 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()));
     }
 }
 /**
  * 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()));
     }
 }