<?php
class shop_etao extends shopPage {
    public $noCache = true;

    public function verify() {
        unset($_GET);
        if($_SERVER['QUERY_STRING']){
            parse_str($_SERVER['QUERY_STRING'],$_GET_);
                                
        }
        unset($_GET_['action_etao-verify_html']);
        /*$_GET_ = array (
            'is_success' => 'T',
            'notify_id' => 'RqPnCoPT3K9%2Fvwbh3I72Lfcoks4wnhDwizOkroj3e2%2BuHOdKgeySS%2FFBtnYahnkRyvMT',
            'real_name' => 'dongdong101822',
            'target_url' => 'http://smb.vip.ishopex.cn/485',
            'token' => '20130716dbfa54bad5ac4104a6e98f66a4a089a3',
            'user_id' => '2088002727886765',
            'sign' => 'bf5695bad66cfdf0975d02deb66b801f',
            'sign_type' => 'MD5',
        );*/
        //$_GET_['notify_id']= str_replace("+"," ",$_GET_['notify_id']);
        
        if ( !$_GET_['sign'] || 'T' !== $_GET_['is_success'] || !$this->notify_verify($_GET_) ) {

            if($_GET['target_url']){
                header('Location:'.$_GET['target_url']); exit;
            }else{
                header('Location:'.$this->system->base_url());exit;
            }      
            
        }      
       
        //此处可将token 保存session操作
        
        $data = array(
            'open_type'=>'alipay',
            'open_id'=>$_GET['user_id'],
            'realname'=>$_GET['real_name'],
            'province'=>'--',
            'city'=>'--',
        );

        $accountMdl = $this->system->loadModel('member/account');
        $mem = $accountMdl->createotherlogin($data);

        // 在本系统登陆
        $this->system->setCookie('MEMBER',$mem['secstr'],null);
        $this->system->setCookie('UNAME',$mem['uname'],null);
        $this->system->setCookie('NAME',$mem['name'],null);
        $this->system->setCookie('MLV',$mem['member_lv_id'],null);
        $this->system->setCookie('CUR',$mem['cur'],null);
        $this->system->setCookie('LANG',$mem['lang'],null);
        $this->system->setCookie('LOGIN_TYPE',$mem['open_type'],null);

        header('Location:'.$_GET['target_url']);exit;
    }

    public function notify_verify($str){
    
        $transport = 'http';   // https需服务器支持
        if($transport == 'https'){
            $alipay_gateway_new = 'https://mapi.alipay.com/gateway.do?service=notify_verify&';
        }else{
            $alipay_gateway_new = 'http://notify.alipay.com/trade/notify_query.do?';
        }
       
        $params = array(
            "partner" => $this->system->getConf('app.etao.alipay.pid'),
            "notify_id"    => $str['notify_id'],                     
        ); 
        $url = $alipay_gateway_new.'partner='.$params['partner'].'&notify_id='.$params['notify_id'];  
        
        $resp = $this->system->loadModel('utility/http_client')->get($url);        
        $resp = json_decode($resp, true);

        if($resp){
            return true;
        }else{
            return false;
        }
    }

    private function sign($data){
        ksort($data);
        $str = '';
        foreach ($data as $k => $v) {
            if ( 'sign' == $k || 'sign_type' == $k || '' === $v || NULL===$v ) continue;
            $str .= "{$k}={$v}&";
        }
        $str = trim($str,'&').$this->system->getConf('app.etao.alipay.key');
        return strtoupper(md5(trim($str)));
    }

    public function goods() {
        $this->contentType = 'text/xml;charset=UTF-8';
        $goods_id = (int)trim($_GET['feed'],'/');        
        include dirname(__FILE__).'/etao_mdl_feed.php';
        $etaoMdl = new etao_mdl_feed();
        echo $etaoMdl->feedgoods($goods_id);
    }
}

