<?php
error_reporting(0); 

function fastpay_order($paydata, $type="http"){
if (!is_array($paydata)) {exit("data错误");}
$data = array(
    'appkey'=>FAST_APPKEY,//你的appkey
    'uid'=>'',//你的用户id
    'total_fee'=>'',//你的金额
    'order_no'=>'',//你的订单号
    'pay_title'=>'微信支付',//你的订单号
    'me_param'=>'',//其他参数,可返回回调里面
    'notify_url'=>PAY_NOTIFY_URL,//异步回调地址
    'me_back_url'=>PAY_RETURN_URL,//支付成功后返回
    'me_eshop_openid'=>'',//付款用户openid
    'me_party'=>'',//根据其他支付插件,异步回调返回同样参数,比如填写codepay,码支付,我们异步回调的时候就按码支付的回调参数返回
    'sign'=>''//签名
    );
    $data=array_merge($data, $paydata);
    extract($data);
    if (empty($appkey)) {
        exit("appkey没有填写");
    }
    if (empty($total_fee)) {
        exit("金额不能为空");
    }
    if (empty($uid)) {
        exit("付款用户id不能为空");
    }
    if (empty($order_no)) {
        $data['order_no']=md5(time() . mt_rand(1, 1000000));
    }
    if (!empty($me_back_url)) {
        $data['me_back_url']=urlencode($me_back_url);//
    }
    if (!empty($notify_url)) {
        $data['notify_url']=urlencode($notify_url);//
    }
    try {
        $data['total_fee']=bcadd($total_fee, 0, 2);
    } catch (Exception $e) {
        print $e->getMessage();
        exit("php不支持bcadd");
    }
    if (empty($sign)) {
        $data['sign']=pay_sign($data);
    }
    $url_quer=http_build_query($data);
    $url=($type=='https') ? "/ubopay/nb/fpay/pays.php?{$url_quer}" : "/ubopay/nb/fpay/pay.php?{$url_quer}" ;
    return $url;
}




//获取用户openid
function get_openid($scope="snsapi_base")
{
    if (PAY_OPENID!=1) {
        return '';
    }
    if (stripos(PAY_OPENID, "xxx.cn")!==false   ||  stripos(PAY_OPENID, "xxx.com")!==false) {
        exit("在请Fast_Cofig.php里面开启汇款,并且填写汇款域名");
    }


    if (!empty($_COOKIE['pay_openid'])) {
        return $_COOKIE['pay_openid'];
    }

    if (!isset($_GET['pay_openid'])) {
        //获取openid后返回的页面
        $back_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $back_url=urlencode($back_url);//url编码
        $login_api=API_OPENID."&back_url={$back_url}&scope={$scope}";
        $login_api=API_OPENID."&back_url={$back_url}&scope={$scope}";
        exit("<meta http-equiv='Refresh' content='0;URL={$login_api}'>");
    } else {
        $time_out=time()+3600*3;//一天后过期
        $_COOKIE['pay_openid']=$_GET['pay_openid'];
        setcookie("pay_openid", $_GET['pay_openid'], $time_out, "/");
        return $_GET['pay_openid'];
    }
}




//获取用户信息
function get_openid_info($arr)
{
    if (!is_array($arr)) {
        exit("data错误");
    }
    if (stripos(PAY_OPENID, "xxx.cn")!==false   ||  stripos(PAY_OPENID, "xxx.com")!==false) {
        exit("请联系客服在Fast_Cofig.php里面填写获取用户信息的地址");
    }

    if (!empty($_COOKIE['pay_user_info'])) {
        $res=$_COOKIE['pay_user_info'];
        if (is_array($res)) {
            $res=json_encode($res);
        }
        $res=stripslashes($res);
        $res=str_replace("'", "", $res);
        return $res;
    }
    if (isset($arr['fast_weixin_token'])) {
        $data=array();
        $data['fast_weixin_token']=$arr['fast_weixin_token'];
        $res=get_cur(API_OPENID_INFO, $data, "POST");
        $time_out=time()+3600*3;//一天后过期
        $_COOKIE['pay_user_info']=$res;
        setcookie("pay_user_info", $res, $time_out, "/");
        setcookie("fast_weixin_token", $arr['fast_weixin_token'], $time_out, "/");//以后可以通过这个获取用户信息,可以保存数据库
        $res=stripslashes($res);
        $res=str_replace("'", "", $res);
        return $res;
    }


    if (empty($arr['back_url'])) {
        $arr['back_url']='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    }

    $sign=md5(FAST_APPKEY.SECRET_KEY.$arr['back_url']);
    $options['appkey']=FAST_APPKEY;
    $options['sign']=$sign;
    $options['back_url']=$arr['back_url'];
    $options['scope']="snsapi_userinfo";
    $url=http_build_query($options);
    $login_api=API_OPENID_INFO."&{$url}";
    exit("<meta http-equiv='Refresh' content='0;URL={$login_api}'>");
}







function get_weixin_url($arr)
{
    if (!is_array($arr)) {
        exit("data错误");
    }

    if (empty($arr['title'])) {
        exit("平台名称不能为空");
    }
    if (empty($arr['uid'])) {
        exit("推广id不能为空");
    }
    if (empty($arr['url'])) {
        exit("推广url不能为空");
    }

    $sign=md5(FAST_APPKEY.SECRET_KEY.$arr['url']);
    $options['appkey']=FAST_APPKEY;
    $options['sign']=$sign;
    $options['url']=$arr['url'];
    $options['uid']=$arr['uid'];
    $options['title']=$arr['title'];
    $res=get_cur(API_WEIXIN_URL, $options, "POST");
    return $res;
}



//微信汇款接口
function fast_pay($paydata){
if (!is_array($paydata)) {
exit("data错误");
}

    if (!isset($paydata['sh'])) {
        $paydata['sh']=0;
    }

    if (empty($paydata['appkey'])) {
        $paydata['appkey']=FAST_APPKEY;
    }
    $data=array();
    $data['appkey']=(string)$paydata['appkey'];
    $data['openid']=(string)$paydata['openid'];
    $data['amount']=(string)$paydata['amount'];
    $data['billno']=(string)$paydata['billno'];
    ;
    $data['desc']=(string)$paydata['desc'];
    $data['uid']=$paydata['uid'];
    $data['pay_way']='wepay';
    $data['sh']=$paydata['sh'];
    $str_sign="appkey={$data['appkey']}&openid={$data['openid']}&amount={$data['amount']}&billno={$data['billno']}&secretkey=".SECRET_KEY."&";
    $sign=md5($str_sign);
    $data['sign']=$sign;//支付签名
    $res=get_cur(API_TRANSFERS, $data, "POST");
    return $res;
}





//支付下单计算签名
function pay_sign($paydata){
if (!is_array($paydata)) {
exit("data错误");
}
$str_sign="appkey={$paydata['appkey']}&order_no={$paydata['order_no']}&secretkey=".SECRET_KEY."&total_fee={$paydata['total_fee']}&uid={$paydata['uid']}&";
$sign=md5($str_sign);
return $sign;
}
//异步回调计算签名
function notify_sign($paydata){
if (!is_array($paydata)) {
exit("data错误");
}
$str_sign="appkey={$paydata['appkey']}&order_no={$paydata['order_no']}&secretkey=".SECRET_KEY."&me_pri={$paydata['me_pri']}&uid={$paydata['uid']}&";
$sign=md5($str_sign);
return $sign;
}
//模拟http
function get_cur($url, $data="", $type="GET", $header=""){
$HTTP_REFERER='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_REFERER, $HTTP_REFERER);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$temp = curl_exec($ch);
curl_close($ch);
return $temp;
}
//判断是否是微信端
function is_weixin_pay(){
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
return true;
}
return false;
}
//日志记录
function logger($log_content){
$log_filename = "log.xml";
if (is_array($log_content)) {
$log_content = print_r($log_content, true);
}
file_put_contents($log_filename, date('H:i:s') . " " . $log_content . "\r\n", FILE_APPEND);
}
