<?php
/**
 * User: 呆呆
 * Agreement: 禁止使用本软件（系统）用于任何违法违规业务或项目,造成的任何法律后果允由使用者（或运营者）承担
 * Date: 2024/11/24
 * Time: 18:58
 */
namespace app\common\Pay_Pro\controller;//跟目录路径一样

class Index
{
    /**************************核心控制器（不可修改）**************************/
    
    static public $config = [
		'name'        => 'Pay_Pro', //插件标识【需和目录名称一致，不能有重复】
		'showname'    => '收款快报', //插件名称
		'author'      => '呆呆', //插件作者
		'types'       => ['alipay','qqpay','wxpay'], //支付方式，可选的有alipay,qqpay,wxpay,bank
	];

	/**
	 * 插件扩展[更多功能]菜单列表
	 */
    public function Lists($Config,$Api,$Login)
    {
		$data = [
			[
				"function"  => "url",//公开函数
				"name" => "PC监控端",//函数名称
				"data"  => [],//请求叁数
			],
		];
		return $data;
    }
    
    /**
	 * 检测心跳
	 */
    public function Cookie($Pay,$Json)
    {
        return Cookie_json($Pay,-1,'不支持检测心跳');
    }
    
    /**
	 * 检测回调
	 */
    public function Status($Order,$Type,$Pay,$Json)
    {
        return Status_json($Order,false,'不支持检测回调');
    }
    
    /**
	 * 异步回调
	 * 正确语法：processNotify($Order,$api_trade_no = '')
	 */
    public function Notify_url($Order,$Api,$Type,$Pay,$Json)
    {
        if(false == true){
            processNotify($Order,$api_trade_no = '');
            return 'success';
        }else{
            return 'fail';
        }
    }
    
    /**
	 * 同步回调
	 * 正确语法：success
	 */
    public function Return_url($Order,$Api,$Type,$Pay,$Json)
    {
        if(false == true){
            return 'success';
        }else{
            return 'fail';
        }
    }
    
    /**
	 * 拉起支付
	 */
	public function Exec($Order,$Api,$Type,$Pay,$Json)
    {
        if($Order['type'] == 'alipay'){
            $Order['price'] = pay_price($Order,$Json['tdid']);
            $url = $Json['file'];
            $h5 = 'alipays://platformapi/startapp?appId=2021003124679502&closeAllActivityAnimation=true&startMultApp=YES&transAnimate=true&url='.urldecode($Json['file']);
            return Pay_success($Order,['type'=>'page','page'=>'Pay_Pro','data'=>['url'=>$url,'h5'=>$h5,'Json'=>$Json]]);
        }else if($Order['type'] == 'qqpay'){
            $Order['price'] = pay_price($Order,$Json['tdid']);
            $url = $Json['file'];
            $h5 = 'mqqapi://forward/url?version=1&amp;src_type=web&amp;url_prefix='.base64_encode('https://qun.qq.com/qrcode/index?data='.urlencode($Json['file']));
            return Pay_success($Order,['type'=>'page','page'=>'Pay_Pro','data'=>['url'=>$url,'h5'=>$h5,'Json'=>$Json]]);
        }else if($Order['type'] == 'wxpay'){
            $Order['price'] = pay_price($Order,$Json['tdid']);
            $url = $Json['file'];
            $h5 = 'weixin://';
            return Pay_success($Order,['type'=>'page','page'=>'Pay_Pro','data'=>['url'=>$url,'h5'=>$h5,'Json'=>$Json]]);
        }else{
            return Pay_error('支付插件内部错误！');
        }
    }
    
    /**************************核心控制器（不可修改）**************************/
    
    /**
	 * 外接监控端心跳
	 */
    public function Heart($Json,$Api)
    {
        error_reporting(0);
        $key = $Api['key'];
        if(empty($key)){
            return json()->code(404);
        }
        if(processHeart($Json,1)){
            $txt = '心跳正常';
        }else{
            $txt = '心跳异常';
        }
        return '账号['.$Json['common_mid'].']'.$txt;
    }
    
    /**
	 * 外接监控端回调
	 */
    public function Order($Json,$Api)
    {
        error_reporting(0);
        $key = $Api['key'];
        if(empty($key)){
            return json()->code(404);
        }
        $price = $Api['money'];
        if(empty($price)){
            return json()->code(404);
        }
        $time = $Api['time'];
        if(empty($time)){
            return json()->code(404);
        }
        $api_trade_no = $Api['api_trade_no'];
        $json = $Json['common_json'];
        if($key !== $json['key']){
            return '软件通信密钥错误或者不对';
        }
        $txt = '检测无订单记录...';
        $list = \think\facade\Cache::store('redis_Order')->getTagItems('Order_'.md5(env('token')));
        $data = [];
        foreach($list as $value){
            $Data = \think\facade\Cache::store('redis_Order')->get($value);
            if(empty($Data)){
                continue;
            }
            if(!is_array($Data)){
                continue;
            }
            $Order = $Data[0];
            if($Order['mid'] !== $Json['common_mid']){
                continue;
            }
            $json = $Order['pay_json'];
            if(!is_array($json)){
                continue;
            }
            $tdid = isset($json['data']['Json']['tdid']) ? intval($json['data']['Json']['tdid']) : 5;
            $pay_time = date('Y-m-d H:i:s', $time);
            // 检查交易时间是否在订单支付时间之前
            if(strtotime($Order['pay_time']) >= strtotime($pay_time)){
                continue;
            }
			// 判断交易时间是否在付款时间内
            if (abs(strtotime($Order['pay_time']) - strtotime($pay_time)) <= (60*$tdid))  {
                $data[] = $Order;
            }
        }
        foreach($data as $order){
            if($price == $order['price']){
                if(processNotify($order,$api_trade_no)){
                    $txt = '回调成功'.'['.$order['trade_no'].']';
                }else{
                    $txt = '回调失败';
                }
            }
        }
        return '账号['.$Json['common_mid'].']'.$txt;
    }

    public function url($Json,$Api)
    {
		return redirect('https://wwzq.lanzoum.com/b01olbhmh');
    }
    
}