<?php
namespace App\Controller;
class PayController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
    public function go_buy(){
        $return=array('status'=>1,'msg'=>'');
        $id=I('id');
        if(empty($id)){
            $return['msg']='没有此订单信息';
            $return['status']=3;
            $this->ajaxReturn($return);
        }
        $where=array();
        $where['order_id']=$id;
        $where['is_paid']=array('neq',1);
        $pay_info = M('PayLog')->where($where)->field(true)->find();
        $title_name=I('title');if(empty($title_name))$title_name="微信/支付宝支付";
        $pay_code = 'Mmscan';
        $p_map['status']=1;
        $p_map['code']=$pay_code;
        $payconfig=M('Payment')->where($p_map)->field(true)->find();
        $tran_code='SMZF010';
        $pay_type_code=strtoupper($pay_info['pay_code']);
        if($pay_type_code=='HANCHAO_SALIPAY'){
            $pay_type='ZFBZF';
        }
        if($pay_type_code=='HANCHAO_SWEIXIN'){
            $pay_type='WXZF';
        }
        $user_id=get_openid();
        $pay = new \Common\Vendor\Pay($pay_code,$payconfig);
        $vo = new \Common\Vendor\Pay\PayVo();
        $vo->setBody($title_name)
            ->setFee($pay_info['need_pay'])
            ->setTitle($title_name)
            ->setOrderNo($pay_info['order_sn'])
            ->setTranCode($tran_code)
            ->setPayType($pay_type)
            ->setParam($pay_info)
            ->setUserId($user_id);
        $RequestForm=$pay->buildRequestForm($vo);
        echo $RequestForm;
    }
    /*支付结果返回*/
    public function notify() {
        $apitype = I('get.apitype');
        $config=idtoname($apitype,'Payment','*','code');
        $pay = new \Common\Vendor\Pay($apitype,$config);
        if(in_array(strtolower($apitype),array('chinaums','mmscan','starpos'))){
            $notify = $_POST;
        }elseif (IS_POST && !empty($_POST)) {
            $notify = I('post.');
        } elseif (IS_GET && !empty($_GET)) {
            $notify = I('get.');
            unset($notify['method']);
            unset($notify['apitype']);
        } else {
            $return['status']=3;
            $return['msg']='支付失败！(参数错误)';
        }
        if ($pay->verifyNotify($notify)) {
            //获取订单信息
            $info = $pay->getInfo();
            if ($info['status']) {
                if(!empty($info['shop_id'])){
                    $map=array();
                    $map['distribute']=$info['notifyId'];
                    $count=M('OrderInfo')->where($map)->count();
                    if($count==0) {
                        $order = array();
                        $order['order_sn'] = createOrderNo();//订单编号
                        $order['type'] = 13;//订单类型
                        $order['shop_id'] = $info['shop_id'];//商家id
                        $order['sa_id'] = 0;
                        $order['sign_building'] = '';//借用字段记录当前收银员用户名
                        $order['shipping_status'] = 4;//配送状态
                        $order['pay_code'] = 'CHINAUMS';//支付方式id
                        $order['assist_code'] = $info['pay_code'];//支付方式辅助code
                        $order['payment_id'] = 0;//支付方式id
                        $order['pay_fee'] = 0;//支付手续费
                        $order['pay_status'] = 1;//支付状态
                        $order['goods_amount'] = $info['money'];//订单实际金额(减去折扣的) 没减红包
                        $order['goods_allmoney'] = $info['money'];//商品总价
                        $order['pay_amount'] = $info['money'];//在线支付的金额
                        $order['order_amount'] = $info['money'];//应付款金额减去折扣和红包 订单总金额
                        $order['order_money'] = $info['money'];//应付款金额减去折扣和红包 订单总金额
                        $order['status'] = 1;//订单状态
                        $order['addtime'] = NOW_TIME;
                        $order['source'] = 2;//来源0电脑，1手机版，2app
                        $order['distribute'] = $info['notifyId'];
                        $order_id = M('OrderInfo')->add($order);
                        if (empty($order_id)) {
                            $return['msg'] = '添加订单失败';
                            return $return;
                        }
                        $map = array();
                        $map['id'] = $order_id;
                        $map['pay_status'] = 1;
                        $order_info = M('OrderInfo')->where($map)->field(true)->find();
                        if (empty($order_info)) {
                            $return['msg'] = '订单信息错误!';
                            return $return;
                        }
                        $pay_code = strtoupper($order_info['pay_code']);
                        $payment_info = array();
                        $payment_info['order_id'] = $order_id;
                        $payment_info['user_id'] = $order_info['uid'];
                        $payment_info['type'] = $order_info['type'];
                        $payment_info['order_sn'] = $info['out_trade_no'];
                        $payment_info['use_money'] = $order_info['used_money'];//使用现金或收款码支付的钱
                        $payment_info['need_pay'] = $order_info['pay_amount'];//还需支付的金额
                        $payment_info['use_score'] = $order_info['used_score'];//积分
                        $payment_info['use_score_money'] = $order_info['used_score_money'];//积分所抵的钱
                        $payment_info['order_total_price'] = $order_info['order_money'];//订单实际支付的金额=商品总价+总运费-活动折扣-红包折扣
                        $payment_info['payment_id'] = $order_info['payment_id'];//支付方式
                        $payment_info['payment_fee'] = $order_info['pay_fee'];//支付手续费
                        $payment_info['pay_amount'] = $order_info['pay_amount'];//
                        $payment_info['pay_code'] = $pay_code;//支付code
                        $payment_info['is_paid'] = 0;
                        $payment_info['create_time'] = NOW_TIME;
                        //生成支付单
                        $pay_log_id = M('PayLog')->add($payment_info);
                        if (empty($pay_log_id)) {
                            $return['msg'] = '支付单信息错误!';
                            return $return;
                        }
                    }
                }
                D('App/OrderInfo')->endorder($info);
                if (I('get.method') == "return") {
                    $return['status']=1;
                    $return['msg']='支付成功';
                }else{
                    $pay->notifySuccess();
                }
            } else {
                $return['status']=3;
                $return['msg']='支付失败';
            }
        } else {
            $return['status']=3;
            $return['msg']='支付失败！(验签错误)';
        }
        $this->ajaxReturn($return);
    }
}
?>