<?php
/**
 * User: 呆呆
 * Agreement: 禁止使用本软件（系统）用于任何违法违规业务或项目,造成的任何法律后果允由使用者（或运营者）承担
 * Date: 2021/3/3
 * Time: 14:34
 */
namespace app\common\mq_gyzf\controller;//跟目录路径一样
use think\Db;
use think\Controller;

class Index extends Controller
{
    public function getReturn($code = 1, $data = "成功", $json = null)//系统需，要不要动
    {
        return array("code" => $code, "data" => $data, "json" => $json);
    }

    public function Cookie($atad)//监控心跳
    {
        $code = 1;
        $data = '该通道不支持监控心跳';
        return $this->getReturn($code,$data,array("time"=>time()));
    }
    
    public function cehsald($atad,$api='')//监控回调
    {
        if(!$api){
            return $this->getReturn(-1, '交易失败', $atad); 
        }
        $row = Db::name("pay_gfg")->where("pid", $atad['pid'])->where("mid",$atad['mid'])->find();
        $data = Db::name("pay_user")->where("id",$atad['pid'])->find();
        $sign = sign($api,$row['ck'],$data,'sign');
        if($sign==$api['sign']){
           return $this->getReturn(1, '交易成功', $atad); 
        }else{
           return $this->getReturn(-1, '交易失败', $atad); 
        }
    }
    
    public function fukyem($data)//支付页面
    {
        return $this->redirect($data['mid_url']);
    }
    
    public function exec($api,$atad,$money,$type)//拉起支付
    {
        $orderId = Db::table('pay_order')->where("pid",$atad["pid"])->where("state",1)->count();
        $orderId = 'YZF'.date("YmdHms").$orderId.rand(10, 99).rand(11, 89).rand(12, 79).rand(13, 69);
        if($type =='alipay'){
            $type = 'alipay';
        }else if($type =='weixin'){
            $type = 'wxpay';
        }else if($type =='qq'){
            $type = 'qqpay';
        }
        if(!config("ms")){//回调通知地址
            $notify_url = url('/pay/index/api_notify','','html',true);
            $return_url = url('/pay/index/api_refer','','html',true);
        }else{
            $notify_url = url('api_notify','','html',true);
            $return_url = url('api_refer','','html',true);
        }
        $data = array(
		    "pid" => $atad['zhanghao'],//商户ID
		    "out_trade_no" => $orderId,//商户订单号
		    "money"	=> $money,//付款金额
		    "type" => $type,//支付方式
		    "name"	=> $api['name'],//商品名称
		    "sitename"	=> $api['sitename'],//网站名称
		    "notify_url" => $notify_url,//异步通知地址
		    "return_url" => $return_url,//同步跳转地址
        );
        //建立请求
        if(empty($api['name']))$data['name']=$api['record'];//商品名称
        if(!empty($api['param']))$data['param']=$api['param'];//业务扩展叁数
        $cls = pay('yzf');//易支付接口
        $data['sign'] = $cls->sign($data,$atad['ck']);//数据签名
        $url = $atad['bf'].'/submit.php?'.http_build_query($data);//拉起支付
        $atad['data'] = $atad['zhanghao'].'|'.$atad['ck'].'|'.$atad['bf'];//通道监控数据
        $atad['pay_id']  = $orderId;//通道订单号
        $atad['mid_url'] = $url;//通道二维码
        return $this->getReturn(1, $atad, $atad['data']);
    }
}