<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 微信支付
 */
class WeixinpayController extends Controller{

    /**
     * notify_url接收页面
     */
    public function notify(){
        // 导入微信支付sdk
        Vendor('Weixinpay.Weixinpay');
        $wxpay=new \Weixinpay();
        $result=$wxpay->notify();
        if ($result) {
            // 验证成功 修改数据库的订单状态等 $result['out_trade_no']为订单号
            $out_trade_no=$result['out_trade_no'];
            $where['id']=$result['out_trade_no'];
            $chkorder = M('dingdan')->where($where)->find();
            if($chkorder['status'] == 0){
                $data['status']=1;
                M('dingdan')->where($where)->save($data);
                $dlist  = M('dingdan')->find($out_trade_no);
                $name = $dlist['goodname'];
                $shuliang = $dlist['shuliang'];
                $jiage = $dlist['zongjia'];
                $dingdanhao = $dlist['dingdanhao'];

                $gid = $dlist['gid'];
                $glist = M('good')->find($gid);
                if($glist['fakaleixin'] == '卡密发卡'){
                    $newkucun = $glist['kucun']-$shuliang;
                    M('good')->where("id=$gid")->setField('kucun',$newkucun);
                }

                $klist =  M('kami')->where("status=0 and g_id=$gid")->limit($shuliang)->order('rand()')->select();

                foreach ($klist as $key=>$v)
                {
                    $arr[] = $v['id'];
                    $cdk[] = $v['cdk'];
                }
                $where['id'] =  array('in',$arr);
                M('kami')->where($where)->setField("status",1);
                M('kami')->where($where)->setField("d_id",$out_trade_no);
                session('cdk',$cdk);
                session('name',$name);
                session('shuliang',$shuliang);
                session('zongjia',$jiage);
                session('gid',$gid);
                session('dingdanhao',$dingdanhao);
            }else{
                $this->redirect('Index/buyend');
                //$this->error('该订单已支付！','Home/Index/buyend');
            }
//            $this->redirect(U('Home/Index/buyend',array('name'=>$name,'shuliang'=>$shuliang,'zongjia'=>$jiage)));

        }
        else
        {
            $this->success('支付失败',U('Home/Buy/enddie'));
        }
    }

    /**
     * 公众号支付 必须以get形式传递 out_trade_no 参数
     * 示例请看 /Application/Home/Controller/IndexController.class.php
     * 中的weixinpay_js方法
     */
    public function pay(){
        // 导入微信支付sdk
        Vendor('Weixinpay.Weixinpay');
        $wxpay=new \Weixinpay();
        // 获取jssdk需要用到的数据
        $data=$wxpay->getParameters();
        // 将数据分配到前台页面
        $assign=array(
            'data'=>json_encode($data)
            );
        $this->assign($assign);
       
       $id  =  $_GET['state'];
   
       $list  = M('tiyandindan')->find($id);
       $fl_id = $list['fl_id'];
       $shu =  $list['shuliang'];
       $this->assign('fl_id',$fl_id);
       $this->assign('shu',$shu);
       $this->display();
    }

    /**
     * 微信订单
     * 2016.11.07
     * wangyang
     */
    public function doweixin(){
        // 虚拟的订单 请根据实际业务更改
        $time=time();
        $where['id']=$_GET['order'];
        $orderinfo=M('dingdan')->where($where)->find();
  
        $order=array(
            'body'=>$orderinfo['goodname'],
            'total_fee'=>$orderinfo['zongjia']*100,//乘以100
            'out_trade_no'=>$orderinfo['id'],
            'product_id'=>'1'
            );
        weixinpay($order);
    }

    /**
     * 微信 公众号jssdk支付
     */
public function wexinpay_js(){
    // 此处根据实际业务情况生成订单 然后拿着订单去支付
    
    // 用时间戳虚拟一个订单号  （请根据实际业务更改）
    //$out_trade_no=time();
    $out_trade_no=$_GET['order'];

    // 组合url
    $url=U('Home/Weixinpay/pay',array('out_trade_no'=>$out_trade_no));

    redirect($url);
}

}