<?php
/**
 * ctl_payment
 *
 * @uses pageFactory
 * @package
 * @version $Id: ctl.payment.php 1867 2008-04-23 04:00:24Z flaboy $
 * @copyright 2003-2007 ShopEx
 * @author Likunpeng <leoleegood@zovatech.com>
 * @license Commercial
 */
include_once('objectPage.php');
class ctl_payment extends objectPage {

    var $workground ='setting';
    var $finder_action_tpl = 'payment/finder_action.html';
    var $object = 'trading/paymentcfg';
    var $editMode = true;
    var $allowExport = false;

    var $disableGridEditCols = "id";
    var $disableColumnEditCols = "id";
    var $disableGridShowCols = "id";
    var $filterUnable = true;
    /**
    * main
    *
    * @access public
    * @return void
    */

    function _detail(){
        return array('show_detail'=>array('label'=>__('支付配置'),'tpl'=>'payment/pay_edit.html'));
    }

    function index(){
         $appmgr = &$this->system->loadModel('system/appmgr');
         $cilent = &$this->system->loadModel('service/apiclient');
         $cilent->url = 'http://sds.ecos.shopex.cn/api.php';
         $cilent->key = '371e6dceb2c34cdfb489b8537477ee1c';
         $payment = $cilent->native_svc("payment.get_all_payments");
         if($payment['result'] == 'succ'){
            $paymentData = $payment['result_msg'];
            if(is_array($paymentData)&&isset($paymentData[0]['pay_name'])){
               $allApp = $appmgr->getPaydata($paymentData);
               file_put_contents(HOME_DIR.'/sendtmp/allApp.log',serialize($allApp));
            }
            if(is_array($allApp)&&isset($allApp[0]['pay_name'])){
               $useApp = $appmgr->getUseapp($allApp);
               file_put_contents(HOME_DIR.'/sendtmp/useApp.log',serialize($useApp));
            }
         }else{
            if(file_exists(HOME_DIR.'/sendtmp/useApp.log'))
                $usep = file_get_contents(HOME_DIR.'/sendtmp/useApp.log');
            if(file_exists(HOME_DIR.'/sendtmp/allApp.log')){
                $allp = file_get_contents(HOME_DIR.'/sendtmp/allApp.log');
            }else{
                $allp = file_get_contents(HOME_DIR.'/sendtmp/defaultApp.log');
            }
            if(is_string($usep))
                $useApp = unserialize($usep);
            if(is_string($allp))
                $allApp = unserialize($allp);
         }
         $this->pagedata['allNum'] = count($allApp);
         $this->pagedata['useNum'] = count($useApp);
         $this->pagedata['allPay'] = $allApp;
         $this->pagedata['usePay'] = $useApp;
         if($_POST['china'] == 'true'){
            $this->display('payment/pay_index_china.html');
         }elseif($_POST['other'] == 'true'){
            $this->display('payment/pay_index_other.html');
         }else{
            $this->page('payment/pay_index.html');
         }

    }

    function show_detail($id){
        $oPay = &$this->system->loadModel('trading/payment');
        $aPay = $oPay->getPaymentById($id);
        $this->pagedata['pay'] = $aPay;
        $this->pagedata['pay_info'] = $this->_getPayOpt($aPay['pay_type'], $aPay['custom_name'], $aPay['fee'], $aPay['config'],1);
        $oPlu = $oPay->loadMethod($aPay['pay_type']);
        if($oPlu){
            $this->pagedata['html'] =  $oPlu->infoPad();
        }
        $this->pagedata['pay_id'] = $id;
        $this->pagedata['order'] = $aPay['orderlist'];
        $this->pagedata['old_pay_type'] = $aPay['pay_type'];
        $this->pagedata['pay_des'] = $aPay['des'];
        $this->pagedata['pay_name'] = $aPay['custom_name'];
        $this->pagedata['paylist'] = $oPay->getPluginsArr(true);
    }

    /**
    * main
    *
    * @access public
    * @return void
    */
    function getPayList(){
        $this->path[] = array('text'=>__('支付方式'));
        $oPay = &$this->system->loadModel('trading/payment');
        $this->pagedata['items'] = $oPay->getMethods();
        $this->page('payment/pay_list.html');
    }

    function _getHtmlString($key,&$val,$rs=array(),&$eventScripts){
        unset($selOption);
        unset($tOptions);
        switch($val['type']){
            case 'string':
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'text','name'=>$key,'value'=>$rs[$key]?$rs[$key]:''));
                break;
            case 'select':
                foreach($val['options'] as $k=>$v){
                    $tOptions[$k] = $v;
                    if($rs[$key] == $k){
                        $selOption = $k;
                    }
                }
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'select','name'=>$key,'value'=>$selOption,'options'=>$tOptions));
                break;
            case 'number':
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'text','name'=>$key,'value'=>$rs[$key]?$rs[$key]:''));
                break;
            case 'file':
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'file','name'=>$key,'value'=>$selOption,'options'=>$tOptions));
                break;
            case "radio":
                foreach($val['options'] as $k => $v){
                    $checked="";
                    if ($rs[$key]==$k)
                        $checked="checked";
                    $tOptions[$k]=$v;
                    if ($rs[$key]==$k)
                        $selOption=$k;
                }

                if ($val['extendcontent']){
                    unset($extendContent);
                    foreach($val['extendcontent'] as $ck => $cv){
                        $scripts.="<script>";
                        if (isset($rs[$key]))
                           if ($rs[$key])
                               $scripts.="$('".$cv['property']['extconId']."').show();";
                           else
                               $scripts.="$('".$cv['property']['extconId']."').hide();";
                        else{
                            if ($cv['property']['display'])
                                $scripts.="$('".$cv['property']['extconId']."').show();";
                            else
                                $scripts.="$('".$cv['property']['extconId']."').hide();";
                        }
                        $scripts.="</script>";
                        $i=0;
                        $type=$cv['property']['type'];
                        $name=$cv['property']['name'];
                        $size=$cv['property']['size']?$cv['property']['size']:4;
                        unset($extendContent);
                        foreach($cv['value'] as $csk => $csv){
                            unset($checked);
                            if (!$rs)
                                $checked='checked=true';
                            if (in_array($csv['value'],$rs[$name]))
                                $checked='checked=true';
                            $csv['imgname'] = $csv['imgname']?"<img src=".$this->system->base_url()."plugins/payment/images/".$csv['imgname'].">":$csv['name'];
                            $val['extendcontent'][$ck]['value'][$csk]['imgname']=$csv['imgname'];
                            $val['extendcontent'][$ck]['value'][$csk]['checked']=$checked;
                        }
                    }

                }
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'radio','name'=>$key,'value'=>$selOption,'options'=>$tOptions),'extendContent'=>$val['extendcontent']);
                if ($val['event']){
                    $aTemp['params']['onclick']=$val['event'].'(this);';
                }
                if ($val['eventscripts']){
                    $eventScripts=$val['eventscripts'].$scripts;
                }
                break;
            default:
                $aTemp=array('labelName'=>$val['label'],'params'=>array('type'=>'text','name'=>$key,'value'=>$rs[$key]?$rs[$key]:''));
                break;
        }
        return $aTemp;
    }

    /**
    * savePayment
    *
    * @access public
    * @return void
    */
    function savePayment(){
        $this->begin('index.php?ctl=trading/payment&act=index');
        $oPay = &$this->system->loadModel('trading/payment');
        if ($_POST['paymethod']==1)
            $_POST['fee'] = $_POST['fee'] / 100;
        if ($_FILES){//是否有文件上传
            $file=&$this->system->loadModel("system/sfile");
            foreach($_FILES as $key => $val){
                if (intval($val['size'])>0){
                    $_POST[$key]=$val['name'];
                    switch ($_POST['pay_type']){
                        case "ICBC"://工商银行
                            if ($key=="keyFile"){//商户私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="key"){
                                    trigger_error(__('商户私钥文件格式有误,请上传key格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            elseif ($key == "certFile"||$key =="icbcFile"){//商户公钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="crt"){
                                    if($key=="certFile")
                                        trigger_error(__('商户公钥文件格式有误,请上传crt格式文件'),E_USER_ERROR);
                                    else
                                        trigger_error(__('工行公钥文件格式有误,请上传crt格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        case "HYL"://广东好易联
                            if ($key == "keyFile"){//私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="pem"){
                                    trigger_error(__('私钥文件格式有误,请上传key格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            elseif ($key == "certFile"){//公钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="cer"){
                                    trigger_error(__('公钥文件格式有误,请上传cer格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        default:
                            break;

                    }
                    $file->UploadPaymentFile($val,$_POST['pay_type']);//上传支付相关文件
                }
            }
        }

        $this->end($oPay->updatePay($_POST), __('保存成功！'));
    }

    function disable($id){
        $payment = $this->system->loadModel('trading/payment');
        $this->begin('index.php?ctl=trading/payment&act=index');
        $this->clear_all_cache();
        $this->end($payment->deletePay($id));
    }
    /**
    * addpayment
    *
    * @access public
    * @return void
    */
    function addPayment(){
        $this->begin('index.php?ctl=trading/payment&act=index');
        $oPay = &$this->system->loadModel('trading/payment');
        if ($_POST['paymethod']==1)
            $_POST['fee'] = $_POST['fee'] / 100;
        if ($_FILES){//是否有文件上传
            $file=&$this->system->loadModel("system/sfile");
            foreach($_FILES as $key => $val){
                if (intval($val['size'])>0){
                    $_POST[$key]=$val['name'];
                    switch ($_POST['pay_type']){
                        case "ICBC"://工商银行
                            if ($key=="keyFile"){//商户私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="key"){
                                    trigger_error(__('文件格式有误,请上传key格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            elseif ($key == "certFile"||$key =="icbcFile"){//商户公钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="crt"){
                                    trigger_error(__('文件格式有误,请上传crt格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        case "HYL"://广东好易联
                            if ($key == "keyFile"){//私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="pem"){
                                    trigger_error(__('文件格式有误,请上传pem格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            elseif ($key == "certFile"){//公钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="cer"){
                                    trigger_error(__('文件格式有误,请上传cer格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        case "skypay":
                            if ($key=="keyFile" || $key =="certFile"){//私钥文件
                                if(substr($val['name'],strrpos($val['name'],".")+1,strlen($val['name']))!="key"){
                                    trigger_error(__('文件格式有误,请上传key格式文件'),E_USER_ERROR);
                                    exit;
                                }
                            }
                            break;
                        default:
                            break;

                    }
                    $file->UploadPaymentFile($val,$_POST['pay_type']);//上传支付相关文件
                }
            }
        }
        $this->end($oPay->insertPay($_POST,$msg),$msg);
    }

    /**
    * addpayment
    *
    * @access public
    * @return void
    */
    function delPayment($sId){
        $this->begin('index.php?ctl=trading/payment&act=index');
        $oPay = &$this->system->loadModel('trading/payment');
        $this->end($oPay->deletePay($sId),__('删除成功！'));
    }
    /**
    * editPayment
    *
    * @access public
    * @return void
    */
    function editPayment($payId,$payName,$paytype){
        $payName = urldecode($payName);
        $this->path[] = array('text'=>__('编辑支付方式'));
        $oPay = &$this->system->loadModel('trading/payment');
        if(isset($payId)){
            $aPay = $oPay->getPaymentById($payId);
            $paytype = substr($paytype,4);
             if(!empty($aPay['pay_type']))
                $paytype = $aPay['pay_type'];
            if(isset($aPay)){
                $this->pagedata['pay'] = $aPay;
                    $this->loadJs();
                $this->pagedata['pay_info'] = $this->_getPayOpt($paytype, $aPay['custom_name'], $aPay['fee'], $aPay['config'],1);
                $oPlu = $oPay->loadMethod($paytype);
                if($oPlu){
                    $this->pagedata['html'] =  $oPlu->infoPad();
                }
                $this->pagedata['centerName'] = $payName;
                $this->pagedata['sPayName'] = $aPay['custom_name'];
                $this->pagedata['paytype'] = $paytype;
                $this->pagedata['order'] = $aPay['orderlist'];
                $this->pagedata['old_pay_type'] = $aPay['pay_type'];
                $this->pagedata['pay_des'] = $aPay['des'];
                $this->pagedata['pay_id'] = $aPay['id'];
                $this->pagedata['custom_name'] = $aPay['custom_name'];

                $this->pagedata['paylist'] = $oPay->getPluginsArr(true);
            }else{
                $this->pagedata['centerName'] = $payName;
                $this->pagedata['custom_name'] = $payName;
                $this->pagedata['paytype'] = $paytype;
                $this->pagedata['paylist'] = $oPay->getPluginsArr(true);
                $this->pagedata['pay_info'] = $this->_getPayOpt($paytype, '', '', '',1);
                $oPlu = $oPay->loadMethod($paytype);
                if($oPlu){
                    $this->pagedata['html'] =  $oPlu->infoPad();
                }
            }

        }
        $this->page('payment/pay_new.html');
    }

    /**
    * detailPayment
    *
    * @access public
    * @return void
    */
    function detailPayment($id){
        $this->path[] = array('text'=>__('支付方式配置'));
        $oPay = &$this->system->loadModel('trading/payment');
        //$oPay->getPluginsArr(true);
        $aPay = $oPay->getPaymentById($id);
        $this->pagedata['pay'] = $aPay;
        $this->pagedata['pay_info'] = $this->_getPayOpt($aPay['pay_type'], $aPay['custom_name'], $aPay['fee'], $aPay['config']);
        $this->pagedata['pay_id'] = $id;
        $this->pagedata['order'] = $aPay['orderlist'];
        $this->pagedata['old_pay_type'] = $aPay['pay_type'];
        $this->pagedata['pay_des'] = $aPay['des'];
        $this->pagedata['pay_name'] = $aPay['custom_name'];
        $this->pagedata['paylist'] = $oPay->getPluginsArr(true);
        $this->page('payment/pay_edit.html');
    }

    /**
    * getPayOpt
    *
    * @access public
    * @return void
    */
    function getPayOpt($sType, $sPayName=''){
        header('Content-Type: text/html;charset=utf-8');
        if(!$sType){
            echo ' ';
        }else{
            echo $this->_getPayOpt($sType, $sPayName);
            $this->loadJs();
        }
    }

    function _getPayOpt($sType, $sPayName='', $nFee='', $config='',$fetch=0){
        $oPay = &$this->system->loadModel('trading/payment');
        $oPlu = $oPay->loadMethod($sType);
        if($aThisPayCur = $oPay->getSupportCur($oPlu)){
            if($aThisPayCur['DEFAULT']){

                $curName = __('商店默认货币');
            }else{
                $oCur = &$this->system->loadModel('system/cur');
                $aCurLang = $oCur->getSysCur();
                if($aThisPayCur['ALL']){
                    $aThisPayCur = $aCurLang;
                }
                foreach($aThisPayCur as $k=>$v){
                    $curName .= $aCurLang[$k].",&nbsp;";
                    $curName=$curName?rtrim($curName,',&nbsp;'):'';
                }
            }
        }
        if($oPlu){
            $aTemp = unserialize($config);
            if($aTemp){
                foreach($aTemp as $key=>$val){
                    if ($key<>'method'&&$key<>'fee')
                       $aPay[$key]=$val;
                }
            }
            $aField = $oPlu->getfields();
            foreach($aField as $key=>$val){
                $PayPlugItem[] = $this->_getHtmlString($key,$val,$aPay,$eventScripts);
            }
        }
        if ($aTemp['method']==1||!isset($aTemp['method']))
            $check1='checked';
        elseif ($aTemp['method']==2)
            $check2='checked';
        $this->pagedata['sPayName'] = $sPayName;
        $this->pagedata['curName']  = $curName;
        $this->pagedata['PayPlugItem'] = $PayPlugItem;
        $this->pagedata['fee'] = array($nFee,$aTemp['fee']);
        $this->pagedata['checked']=array($check1,$check2);
        $this->pagedata['eventScripts'] = $eventScripts;
        $this->pagedata['hiddenmethod'] = $aTemp['method'];
    }

/*    function infoPad($pid){
        header('Content-Type: text/html;charset=utf-8');
        if(!$pid){
            echo ' ';
        }else{
            $oPay = &$this->system->loadModel('trading/payment');
            $oPlu = $oPay->loadMethod($pid);
            $infoPad = '';
            if($oPlu){
                $infoPad = $oPlu->infoPad();
            }
            echo $infoPad;
        }
    }*/
    function loadJs(){
        echo "<script>";
        echo "var ipt=$('setfee').getElements('input[type=radio]').addEvent('click',function(){";
        echo "setDisable(this.value);";
        echo "});";
        echo "var setDisable=function(v){";
        echo "if (v==2){";
        echo "      $('fix').show().getElements('input[type=text]').set('disabled',false);";
        echo "      $('rate').hide().getElements('input[type=text]').set('disabled',true);";
        echo "}else{";
        echo "      $('rate').show().getElements('input[type=text]').set('disabled',false);";
        echo "      $('fix').hide().getElements('input[type=text]').set('disabled',true);";
        echo "}";
        echo "};";
        echo "var mtd=$('hiddenmethod').get('text');";
        echo "setDisable(mtd);";
        echo "</script>";
    }

    function do_install($ident,$type='offline',$is_update=false){
          $app_path = PLUGIN_DIR.'/app/';
          if(is_dir($app_path.$ident))
              $this->install_app($ident);
          else
              $this->install_online($ident);
    }

    function install_online($ident,$url,$is_update=false){
       if(!$url)
           $url = 'http://sds.ecos.shopex.cn/payments/apps/'.$ident.'.tar';
       include(CORE_DIR.'/admin/controller/service/ctl.download.php');
       $download = new ctl_download();
       $_POST = array(
               'download_list'=>array($url),
           'succ_url'=>'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])
            .'/index.php?ctl=trading/payment&act=do_install_online'
           );

       $download->set = 'true';
       $download->start();
       if($is_update){
            $ident = date("Ymd").substr(md5(time().rand(0,9999)),0,5);
            $download->run($download->ident,0);
            $_GET['download'] =$download->ident;
            $this->do_install_online();
       }
    }

    function install_app($ident){
        $appmgr = $this->system->loadModel('system/appmgr');
        $refesh = &$this->system->loadModel('system/addons');
        $payment = $this->system->loadModel('trading/payment');
        if($appmgr->install($ident,'1')){
            $plugin_name = $appmgr->getAppName($ident);
            $data['custom_name'] = $plugin_name['plugin_name'];
            $data['pay_type'] = substr($ident,4);
            $data['disabled'] = 'true';
            $payment->insertPaymentApp($data);
            if(file_exists(HOME_DIR.'/sendtmp/allApp.log')){
                $allApp = file_get_contents(HOME_DIR.'/sendtmp/allApp.log');
            }else{
                $allApp = file_get_contents(HOME_DIR.'/sendtmp/defaultApp.log');
            }
            if(!file_exists(HOME_DIR.'/sendtmp/useApp.log')){
                fwrite(HOME_DIR.'/sendtmp/useApp.log','');
            }
            $useApp = file_get_contents(HOME_DIR.'/sendtmp/useApp.log');
            if(is_string($allApp))
                $allApp = unserialize($allApp);
            if(is_string($useApp))
                $useApp = unserialize($useApp);
            foreach($allApp as $key=>$val){
               if($val['pay_ident'] == $ident){
                   $allApp[$key]['disable'] = 'false';
                   $useApp[] = $val;
               }
            }
            file_put_contents(HOME_DIR.'/sendtmp/useApp.log',serialize($useApp));
            file_put_contents(HOME_DIR.'/sendtmp/allApp.log',serialize($allApp));

            $this->clear_all_cache();
            echo'<script>W.page(\'index.php?ctl=trading/payment&act=index\',{onComplete:function(){$(\'main\').setStyle(\'width\',window.mainwidth);}})</script>';
        }else{
            $this->end(false,'安装失败');
        }
    }

    function do_install_online(){
        $task = HOME_DIR.'/tmp/'.$_GET['download'];
        $temp_mess = file_get_contents($task.'/task.php');
        $down_data = unserialize($temp_mess);
        if($url = $down_data['download_list'][0]){
            $filename = substr($url,strrpos($url,"/")+1);
            $file_path = $task.'/'.$filename;
            $dir_name = substr($filename,0,strrpos($filename,"."));
            if(file_exists($file_path)){
                $appmgr = $this->system->loadModel("system/appmgr");
                $appmgr->instal_ol_app($file_path,$dir_name,$msg,true);
                $this->install_app($dir_name);

            }
        }
    }

    function updateNewPayment($ident){
        if(isset($ident)) $this->install_online($ident);
    }

    function disApp($id){
        $this->begin('index.php?ctl=trading/payment&act=index');
        $paymentObj = &$this->system->loadModel('trading/payment');
        $this->end($paymentObj->disApp($id), __('修改成功！'));
    }

    function startApp($id){
        $this->begin('index.php?ctl=trading/payment&act=index');
        $paymentObj = &$this->system->loadModel('trading/payment');
        $this->end($paymentObj->startApp($id), __('修改成功！'));
    }



}

?>
