<?php
namespace app\sever\controller;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
use \think\Controller;
class Alipay extends Controller
{
    //授权获取PID和KEY
    public function redirect_uri(){
        //由API_PAY网站通知过来
        ini_set('max_execution_time','0');
        $post = $this->request->param();
        
	    $query = Db::name('land')->where('id',$post['id'])->find();
		$json1 = json_decode(base64_decode($post['json']),true);
        $obj['alipay_status']="4";
        $obj['alipay_dmf_pid']=$json1['alipay_dmf_pid'];
        $obj['alipay_dmf_key']=$json1['alipay_dmf_key'];
        
        $json= json_encode($obj);
		$if = Db::name('land')->where('id',$post['id'])->update(array("json"=>$json));
		if($if){
		echo "1";
		} else {
		echo "0";
		}
    
    }
    
    //由API_PAY网站通知过来
    public function notifyurl(){
        
     $num = $this->request->param('out_trade_no');
     $status = $this->request->param('trade_status');

     if($status=='TRADE_SUCCESS'){
         
       $update = Db::name('orders')->where('num',$num)->update(array(
               'pay_time'=>time(),
               'state'=>2,
               'http'=>'还未请求',
            ));	
 
      if ($update){
             $where['num'] = $num;
             $where['state'] = 2;
             $takesdata = Db::name('orders')->where($where)->find();
              //得到提交的信息
             $uid = $takesdata['land_id'];//收款账号id
             //用户id
             $userid = $takesdata['userid'];//用户id
             //金额
             $money = floatval($takesdata['money']);//金额
             //支付类型
             $payc = intval($takesdata['payc']);
             //转账备注
                
            
             $where_1['userid'] = $userid;
             $where_1['num'] = $num;
             $where_1['api_state'] = 1;
             $order = Db::name('orders')->where($where_1)->find();
             $user = Db::name('user')->where('id',$userid)->find();
             
             api_request($order['id'],$user);
             
             exit('success');
             
            }else{
                exit('fall1');
            }
        }else{
            exit('fall2');
        }
     }
}