<?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']);
        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);
    }
}

