结账时使用自定义付款方式清除会话

时间:2017-09-01 12:30:13

标签: php opencart session-cookies opencart2.x

我在OpenCart 2.1.0.1中使用自定义付款方式进行信用卡和借记卡付款,但我遇到了问题。当我成功购买并重定向到succsess.tpl页面之后,当我继续购物(在同一浏览器会话中)并再次成功购买时 - 它不会记录为具有新订单ID的新订单!我会附上我的控制器,它可以作为清算会话的代码吗?

class ControllerPaymentFibank extends Controller {
public function index() {
    $this->load->model('checkout/order');

    $this->language->load('payment/fibank');

    $data['button_confirm'] = $this->language->get('button_confirm');

    if (!$this->config->get('fibank_test')) {
        $communication_url  = "https://mdpay.fibank.bg:9443/ecomm/MerchantHandler";
        $payment_url        = "https://mdpay.fibank.bg/ecomm/ClientHandler";
    } else {
        $communication_url  = "https://mdpay-test.fibank.bg:9443/ecomm/MerchantHandler";
        $payment_url        = "https://mdpay-test.fibank.bg/ecomm/ClientHandler";
    }

    $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);

    if ($order_info) {



        $data['action'] = $this->session->data['order_id'];

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/fibank.tpl')) {
            return $this->load->view($this->config->get('config_template') . '/template/payment/fibank.tpl', $data);
        } else {
            return $this->load->view('default/template/payment/fibank.tpl', $data);
        }

    }
}

public function make_payment() {
    $this->load->model('checkout/order');

    $this->language->load('payment/fibank');

    $data['button_confirm'] = $this->language->get('button_confirm');

    if (!$this->config->get('fibank_test')) {
        $communication_url  = "https://mdpay.fibank.bg:9443/ecomm/MerchantHandler";
        $payment_url        = "https://mdpay.fibank.bg/ecomm/ClientHandler";
    } else {
        $communication_url  = "https://mdpay-test.fibank.bg:9443/ecomm/MerchantHandler";
        $payment_url        = "https://mdpay-test.fibank.bg/ecomm/ClientHandler";
    }

    $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);

    if ($order_info) {

        $ip = $this->get_client_ip();

        $amount = $this->currency->format($order_info['total'], 'BGN', '', false);
        $amount *= 100;

        $post_request="command=v&amount=".$amount."&currency=975&client_ip_addr=".$ip."&description=".$this->session->data['order_id']."&msg_type=SMS"; 

        $res = $this->execute_fibank_query($communication_url, $post_request);   

        $result = str_replace("TRANSACTION_ID: ", "", $res);

        $this->model_checkout_order->addOrderHistory($this->session->data['order_id'], '1', $result);   

        $_SESSION['order_id']=$this->session->data['order_id'];

        header("location:".$payment_url."?trans_id=".rawurlencode($result));

    }
    exit(); 
}
public function callback() {
    if (isset($this->request->post['trans_id'])) {
        $tranz_id  = $this->request->post['trans_id'];
    } else {
        $tranz_id  = '';
    }

    if (!$this->config->get('fibank_test')) {
        $communication_url  = "https://mdpay.fibank.bg:9443/ecomm/MerchantHandler";
        $payment_url        = "https://mdpay.fibank.bg/ecomm/ClientHandler";
    } else {
        $communication_url  = "https://mdpay-test.fibank.bg:9443/ecomm/MerchantHandler";
        $payment_url        = "https://mdpay-test.fibank.bg/ecomm/ClientHandler";
    }


    if ($tranz_id!="" && $_SESSION['order_id']>0) {
        $ip = $this->get_client_ip();

        $request="command=c&trans_id=".rawurlencode($tranz_id)."&client_ip_addr=".$ip;

        $result = $this->execute_fibank_query($communication_url, $request);

        $res = explode("\n", $result);

        $fibank_info = str_replace("RESULT: ", "", $res[0]);
        $this->load->model('checkout/order');

        $order_info = $this->model_checkout_order->getOrder($_SESSION['order_id']);

        if ($fibank_info=="OK") {

            $order_status = $this->config->get('fibank_order_status_id');
            $this->model_checkout_order->addOrderHistory($_SESSION['order_id'], $order_status, $result);

            $this->cart->clear();

            header("location:/successfull-transaction");

        } 
        else{
            $order_status = $this->config->get('fibank_order_status_denied_id');
            $this->model_checkout_order->addOrderHistory($_SESSION['order_id'], $order_status, $result);

            $this->cart->clear();

            header("location:/transaction-error");
        }


    }   
}

function execute_fibank_query($url, $request){

    // debug
    $fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    // debug
    curl_setopt($ch, CURLOPT_STDERR, $fp);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
    curl_setopt($ch, CURLOPT_SSLCERT, $this->config->get('fibank_certificate_path')); 
    curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); 
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $this->config->get('fibank_certificate_pass'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    $result = curl_exec($ch);

    return $result;

}


function get_client_ip(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } 
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } 
    else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    return $ip;
}

1 个答案:

答案 0 :(得分:1)

如果查看文件catalog/controller/checkout/success.php,您将看到订单成功后的标准流程。如果您对此进行了修改,则OpenCart可能无法取消会话数据。在该文件中,控制器检查会话中是否有数据集:

if (isset($this->session->data['order_id'])) {

如果确实如此,将使用以下行清除购物车:

$this->cart->clear();

并且,在UserActivity日志记录之后,会话数据为unset,其代码为:

unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
unset($this->session->data['guest']);
unset($this->session->data['comment']);
unset($this->session->data['order_id']);
unset($this->session->data['coupon']);
unset($this->session->data['reward']);
unset($this->session->data['voucher']);
unset($this->session->data['vouchers']);
unset($this->session->data['totals']);