class  Bmpay
{
    private $config = [
        'fxid'=>'',//商户号
        'key'=>'',//秘钥
        'fxpay'=>'',//请求类型   【微信wap:wxwap】【微信扫码:wxsm】【支付宝wap:zfbwap】【支付宝扫码:zfbsm】【银联扫码:ylsm】
    ];
    private $fxgetway = 'http://www.baimipay.com/Pay';//网关
    public function __construct($payInfo=[]){
        if($payInfo)
        {
           foreach ($payInfo as $field => $val) {
                $this->config[$field]=$val['val'];
            } 
        }
    }
    public function index($data)
    {
        // var_dump($data);
        // die;
        $data = array(
            "fxid" => $this->config['fxid'], //商户号
            "fxddh" => $data['order_sn'], //商户订单号
            "fxdesc" => $data['subject'], //商品名
            "fxfee" => $data['total'], //支付金额 单位元
            "fxattch" => $data['attach'], //附加信息
            "fxnotifyurl" => $data['notify_url'], //异步回调 , 支付结果以异步为准
            "fxbackurl" => $data['return_url'], //同步回调 不作为最终支付结果为准,请以异步回调为准
            "fxpay" => $this->config['fxpay'], //支付类型 此处可选项以网站对接文档为准 微信公众号:wxgzh   微信H5网页:wxwap  微信扫码:wxsm   支付宝H5网页:zfbwap  支付宝扫码:zfbsm 等参考API
            "fxip" => getClientIP(0, true), //支付端ip地址
            'fxbankcode'=>'',
            'fxfs'=>'',
        );
        // $fxgetway = "http://" . $_SERVER['HTTP_HOST'] . "/Pay"; //网关
        $data["fxsign"] = md5($data["fxid"] . $data["fxddh"] . $data["fxfee"] . $data["fxnotifyurl"] . $this->config['key']); //加密
        $r = getHttpContent($this->fxgetway, "POST", $data);
        $backr = $r;
        $r = json_decode($r, true); //json转数组

        if(empty($r)){
            FLog('empty $r');
            FLog($backr);
            return ['code'=>0,'msg'=>'支付请求失败'];
            //如果转换错误,原样输出返回
            // var_dump($backr);
            // die;
        }
        // var_dump($r);
        //验证返回信息
        if ($r["status"] == 1) {
            // header('Location:' . $r["payurl"]); //转入支付页面
            // exit();
            return ['code'=>1,'msg'=>'success','data'=>['payurl'=>$r['payurl']]];
        } else {
            // {"errDes":"【平台风控提醒】平台用户进行中订单超过设置阈值,请稍后下单","retCode":"FAIL","retMsg":"调用支付渠道失败"}
            $error=json_decode($r['error']);//输出错误信息
            FLog('$error');
            FLog($error);
            return ['code'=>0,'msg'=>'error'];
        }
    }
}

function getHttpContent($url, $method = 'GET', $postData = array()) {
    $data = '';
    $user_agent = $_SERVER ['HTTP_USER_AGENT'];
    $header = array(
        "User-Agent: $user_agent"
    );
    if (!empty($url)) {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30); //30秒超时
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
            if(strstr($url,'https://')){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            }

            if (strtoupper($method) == 'POST') {
                $curlPost = is_array($postData) ? http_build_query($postData) : $postData;
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
            }
            $data = curl_exec($ch);
            curl_close($ch);
        } catch (Exception $e) {
            $data = '';
        }
    }
    return $data;
}

function getClientIP($type = 0, $adv = false) {
    global $ip;
    $type = $type ? 1 : 0;
    if ($ip !== NULL)
        return $ip[$type];
    if ($adv) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown', $arr);
            if (false !== $pos)
                unset($arr[$pos]);
            $ip = trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u", ip2long($ip));
    $ip = $long ? array(
        $ip,
        $long) : array(
        '0.0.0.0',
        0);
    return $ip[$type];
}

 

posted on 2020-08-27 17:25  思君邪  阅读(418)  评论(0编辑  收藏  举报