public function passback()
 {
     $params = array();
     foreach ($_REQUEST as $k => $v) {
         $params[$k] = $v;
     }
     $passback = Twocheckout_Return::check($params, "tango", 'array');
     if ($passback['code'] == 'Success') {
         $id = $params['merchant_order_id'];
         $order_number = $params['order_number'];
         $invoice_id = $params['invoice_id'];
         $data = array('active' => 1, 'order_number' => $order_number, 'last_invoice' => $invoice_id);
         $this->ion_auth->update($id, $data);
         $this->load->view('/include/header');
         $this->load->view('/include/navblank');
         $this->load->view('/order/return_success');
         $this->load->view('/include/footer');
     } else {
         $this->load->view('/include/header');
         $this->load->view('/include/navblank');
         $this->load->view('/order/return_failed');
         $this->load->view('/include/footer');
     }
 }
 /**
  * @param $blog_id
  *   This function will handler
  *   Checkout process
  *   -With Coupon
  *   -Without Coupon
  *   -Recurring
  *   -One time
  *   -DownGrade level
  *   -UpGrade level
  *   -Manual extends subscription time
  */
 public function process_checkout($blog_id, $domain = false)
 {
     global $current_site, $current_user, $psts, $wpdb;
     $site_name = $current_site->site_name;
     if (!empty($domain)) {
         //Get blog name from signup as per WP Signup or BP Signup
         $site_name = $domain;
     }
     //Processing User submitted form
     if (isset($_POST['2co_checkout_button'])) {
         //validate
         if (!$this->check_nonce()) {
             $psts->errors->add('general', __('Whoops, looks like you may have tried to submit your payment twice so we prevented it. Check your subscription info below to see if it was created. If not, please try again.', 'psts'));
         }
         if (!isset($_POST['period']) || !isset($_POST['level'])) {
             $psts->errors->add('general', __('Please choose your desired level and payment plan.', 'psts'));
             return;
         }
         //If free level is selected, activate a trial
         if (!empty($domain) && !$psts->prevent_dismiss() && '0' === $_POST['level'] && '0' === $_POST['period']) {
             $psts->activate_user_blog($domain, true, $_POST['level'], $_POST['period']);
             $esc_domain = esc_url($domain);
             //Set complete message
             $this->complete_message = __('Your trial blog has been setup at <a href="' . $esc_domain . '">' . $esc_domain . '</a>', 'psts');
             return;
         }
         add_action('wp_head', array(&$this, 'checkout_js'));
         wp_enqueue_script(array('jquery'));
         //prepare vars
         $amount_off = false;
         $payment_amount = $init_amount = $psts->get_level_setting($_POST['level'], 'price_' . $_POST['period']);
         $trial_days = $psts->get_setting('trial_days', 0);
         $cp_code = false;
         $is_trial = $psts->is_trial_allowed($blog_id);
         $setup_fee = (double) $psts->get_setting('setup_fee', 0);
         $has_coupon = isset($_SESSION['COUPON_CODE']) && $psts->check_coupon($_SESSION['COUPON_CODE'], $blog_id, $_POST['level']) ? true : false;
         $has_setup_fee = $psts->has_setup_fee($blog_id, $_POST['level']);
         $recurring = $psts->get_setting('recurring_subscriptions', 1);
         $params = array('sid' => $psts->get_setting('2co_acc_number'), 'currency' => $psts->get_setting('2co_currency', 'USD'), 'x_receipt_link_url' => $psts->checkout_url($blog_id, $domain), 'mode' => '2CO', 'merchant_order_id' => $blog_id, 'period' => esc_attr($_POST['period']), 'level' => esc_attr($_POST['level']), '2co_cart_type' => 'ProSites', 'demo' => $psts->get_setting('2co_checkout_mode'));
         //build products params
         $addition_params = array('li_0_type' => 'product', 'li_0_name' => $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name'), 'li_0_price' => $init_amount);
         //if have setup fee
         if ($has_setup_fee) {
             $addition_params['li_0_startup_fee'] = $setup_fee;
         }
         //if have trial time
         if ($is_trial) {
             $init_amount = $init_amount - $payment_amount;
         }
         //case have coupon
         if ($has_coupon) {
             $coupon_value = $psts->coupon_value($_SESSION['COUPON_CODE'], $payment_amount);
             $amount_off = $payment_amount - $coupon_value['new_total'];
             $init_amount -= $amount_off;
             $addition_params = array_merge($addition_params, array('li_1_type' => 'coupon', 'li_1_name' => $_SESSION['COUPON_CODE'], 'li_1_price' => $amount_off));
         }
         if ($recurring) {
             $addition_params = array_merge($addition_params, array('li_0_recurrence' => esc_attr($_POST['period']) . ' Month', 'li_0_duration' => 'Forever'));
         }
         //check if this is downgrade,require no money
         if (!empty($blog_id)) {
             $cur_level = $psts->get_level($blog_id);
             //To Do: Update downgrade logic, to avoid free subscription for next period if downgraded at the end of subscription
             if ($cur_level > 0) {
                 if ($cur_level > $_POST['level']) {
                     /**
                      * Case downgrade
                      * If period is same,so it is simple.When the current level expire,we will downgrade the leve.
                      * For cost for first period of new level will be nearly free.
                      */
                     $old = $wpdb->get_row($wpdb->prepare("SELECT expire, level, term, amount FROM {$wpdb->base_prefix}pro_sites WHERE blog_ID = %d", $blog_id));
                     if ($old->term == $_POST['period']) {
                         $addition_params = array_merge($addition_params, array_merge(array('li_2_type' => 'coupon', 'li_2_name' => __('First month is free due to new level apply to next month', 'ptst'), 'li_2_price' => $init_amount - 0.01)));
                     } elseif ($old->term < $_POST['period'] || $old->term > $_POST['period']) {
                         /**
                          * This case is when the new period smaller than current or larger
                          * 2checkout not support for update customer infomation,
                          * and the only way is using the checkout.Some issue will happend
                          * Example current is 3 months,but user want to downgrade to 1 month.The point is if we subscription for client now,it will
                          * make client need to pay for 3 months before the old expire end. So for this case,we only cancel the subscription,
                          * and send the checkout url when this subscrition expire via email.
                          */
                         update_option('psts_2co_recuring_next_plan', array('action' => 'downgrade', 'level' => $_POST['level'], 'type' => 'email'));
                         $this->complete_message = __('Your 2Checkout subscription modification was not done automate! You will recive an email about the new upgrade when current subsciprion expire.', 'psts');
                     }
                 } elseif ($cur_level < $_POST['level']) {
                     /**
                      * Case upgrade
                      */
                     //get the unuse balance
                     $balance_left = $this->cal_unused_balance($blog_id);
                     $addition_params = array_merge($addition_params, array_merge(array('li_2_type' => 'coupon', 'li_2_name' => __('Balance left of last subscription', 'ptst'), 'li_2_price' => $balance_left)));
                 }
             }
         }
         //create form
         $params = array_merge($params, $addition_params);
         $this->set_gateway_param();
         //all set,now generate the form and submit
         Twocheckout_Charge::redirect($params, 'checkout');
         exit;
     } elseif (isset($_REQUEST['credit_card_processed']) && strtolower($_REQUEST['credit_card_processed']) == 'y') {
         //Processing 2checkout response after user returns from 2checkout site
         $check = Twocheckout_Return::check($_REQUEST, $psts->get_setting('2co_secret_word'), 'array');
         if ($check['response_code'] == 'Success') {
             //Activate the blog
             $blog_id = $psts->activate_user_blog($domain);
             if (!$this->check_profile_id_exist($blog_id, $_REQUEST['order_number'])) {
                 //profile not exist
                 //do the check
                 //get current level
                 $cur_level = $psts->get_level($blog_id);
                 $modify = false;
                 if (is_pro_site($blog_id) && !is_pro_trial($blog_id)) {
                     $modify = true;
                     if ($cur_level != 0 && $cur_level == $_REQUEST['level']) {
                         $modify = false;
                     }
                 }
                 //now go
                 if ($modify) {
                     //this case user is modify the subscription,we will need to check upgrade or downgrade,and refund the diff
                     $scenario = '';
                     if ($cur_level < $_REQUEST['level']) {
                         $scenario = 'upgrade';
                     } elseif ($cur_level > $_REQUEST['level']) {
                         $scenario = 'downgrade';
                     }
                     $this->tcheckout_modify_subscription($blog_id, $scenario);
                 } elseif ($modify == false && (is_pro_site($blog_id) && !is_pro_trial($blog_id))) {
                     //site is in subscription,but user extend to longer
                     //$this->tcheckout_modify_subscription( $blog_id, 'extend' );
                 } else {
                     $this->tcheckout_hander_new_subscription($blog_id);
                 }
             } else {
                 $psts->errors->add('general', __('Your transaction has already settled!', 'psts'));
             }
         } else {
             $psts->errors->add('general', __('There was a problem validating the 2Checkout payment:<br /><strong>MD5 Hash did not match!</strong><br />Please contact the seller directly for assistance.', 'psts'));
         }
     }
 }
function pmpro_twocheckoutValidate()
{
    $params = array();
    foreach ($_REQUEST as $k => $v) {
        $params[$k] = $v;
    }
    //2Checkout uses an order number of 1 in the hash for demo orders for some reason
    if (!empty($params['demo']) && $params['demo'] == 'Y') {
        $params['order_number'] = 1;
    }
    //is this a return call or notification
    if (empty($params['message_type'])) {
        $check = Twocheckout_Return::check($params, pmpro_getOption('twocheckout_secretword'), 'array');
    } else {
        $check = Twocheckout_Notification::check($params, pmpro_getOption('twocheckout_secretword'), 'array');
    }
    if (empty($check)) {
        $r = false;
    } else {
        if (empty($check['response_code'])) {
            $r = false;
        } else {
            $r = $check['response_code'];
        }
    }
    /**
     * Filter if an twocheckout request is valid or not.
     *
     * @since 1.8.6.3
     *
     * @param bool $r true or false if the request is valid
     * @param mixed $check remote post object from request to Twocheckout
     */
    $r = apply_filters('pmpro_twocheckout_validate', $r, $check);
    return $check['response_code'] === 'Success';
}
function pmpro_twocheckoutValidate()
{
    $params = array();
    foreach ($_REQUEST as $k => $v) {
        $params[$k] = $v;
    }
    //2Checkout uses an order number of 1 in the hash for demo orders for some reason
    if ($params['demo'] == 'Y') {
        $params['order_number'] = 1;
    }
    //is this a return call or notification
    if (empty($params['message_type'])) {
        $check = Twocheckout_Return::check($params, pmpro_getOption('twocheckout_secretword'), 'array');
    } else {
        $check = Twocheckout_Notification::check($params, pmpro_getOption('twocheckout_secretword'), 'array');
    }
    return $check['response_code'] === 'Success';
}
 public function testReturnCheck()
 {
     $params = array('sid' => '1817037', 'key' => '7AB926D469648F3305AE361D5BD2C3CB', 'total' => '0.01', 'order_number' => '4774380224');
     $result = Twocheckout_Return::check($params, 'tango');
     $this->assertEquals("Success", $result['response_code']);
 }