zoukankan      html  css  js  c++  java
  • 微信小程序支付

    public function http_get($url){
        $ch = curl_init($url);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch,CURLOPT_TIMEOUT,60);
        $data = curl_exec($ch);
        curl_close($ch);
        return json_decode($data,true);
    }
    
    //curl的post请求
    public function postCurl($url,$data,$type){
        if($type == 'json'){
            $data = json_encode($data);//对数组进行json编码
            $header= array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
        }
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
        if(!empty($data)){
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
        $res = curl_exec($curl);
        if(curl_errno($curl)){
            echo 'Error+'.curl_error($curl);
        }
        curl_close($curl);
        return $res;
    }
    
    //POST方式调用微信小程序 支付/提现 接口
    public function curl_post_ssl($url, $xmldata, $second=30,$aHeader=array()){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        //这里设置代理,如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xmldata);
        $data = curl_exec($ch);
        if(curl_errno($ch)){
            echo 'Error+'.curl_error($ch);
        }
        curl_close($ch);
        return $data;
    }
    
    public function curl_post_ssl2($url, $xmldata, $second=30,$aHeader=array()){
        $isdir = str_replace('Api.php','',__FILE__) . 'cert/';
    
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        //这里设置代理,如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
    
        curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
        curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//证书位置
        curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
        curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//证书位置
        curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
        //curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');这个是可选的
    
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xmldata);
        $data = curl_exec($ch);
        if(curl_errno($ch)){
            echo 'Error+'.curl_error($ch);
        }
        curl_close($ch);
        return $data;
    }
    
    //商户系统内部订单号,要求32个字符内
    public function getNonceStr($length = 32) {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ( $i = 0; $i < $length; $i++ ) {
            $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return $str;
    }
    
    //获取IP地址
    public function getip() {
        static $ip = '';
        $ip = $_SERVER['REMOTE_ADDR'];
        if(isset($_SERVER['HTTP_CDN_SRC_IP'])) {
            $ip = $_SERVER['HTTP_CDN_SRC_IP'];
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#d{1,3}.d{1,3}.d{1,3}.d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] AS $xip) {
                if (!preg_match('#^(10|172.16|192.168).#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        }
        return $ip;
    }
    
    //生成签名
    public function makeSign($data){
        //获取微信支付秘钥
        $key = "";
        // 去空
        $data=array_filter($data);
        //签名步骤一:按字典序排序参数
        ksort($data);
        $string_a=http_build_query($data);
        $string_a=urldecode($string_a);
        //签名步骤二:在string后加入KEY
        $string_sign_temp=$string_a."&key=".$key;
        //签名步骤三:MD5加密
        $sign = md5($string_sign_temp);
        // 签名步骤四:所有字符转为大写
        $result=strtoupper($sign);
        return $result;
    }
    
    /**
     * 将一个数组转换为 XML 结构的字符串
     * @param array $arr 要转换的数组
     * @param int $level 节点层级, 1 为 Root.
     * @return string XML 结构的字符串
     */
    public function array2xml($arr, $level = 1) {
        $s = $level == 1 ? "<xml>" : '';
        foreach($arr as $tagname => $value) {
            if (is_numeric($tagname)) {
                $tagname = $value['TagName'];
                unset($value['TagName']);
            }
            if(!is_array($value)) {
                $s .= "<{$tagname}>".(!is_numeric($value) ? '<![CDATA[' : '').$value.(!is_numeric($value) ? ']]>' : '')."</{$tagname}>";
            } else {
                $s .= "<{$tagname}>" . $this->array2xml($value, $level + 1)."</{$tagname}>";
            }
        }
        $s = preg_replace("/([x01-x08x0b-x0cx0e-x1f])+/", ' ', $s);
        return $level == 1 ? $s."</xml>" : $s;
    }
    
    //将xml转为array
    public function xml2array($xml){
    //禁止引用外部xml实体
        libxml_disable_entity_loader(false);
        $result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }
    
    public function wxPay(Request $request){
        $uid = $request->param('uid');
        $openid = Db::name('member')->where('id',$uid)->value('wxopenid');
    
        $data = [
            'appid' => '',
            'mch_id' => '',//商户号
            'nonce_str' => self::getNonceStr(),//随机字符串,长度要求在32位以内
            'body' => '互帮互带_福豆充值',//商品描述
            'out_trade_no' => self::create_order_no(),//商户订单号
            'total_fee' => 1,//标价金额,单位分
            'spbill_create_ip' => self::getip(),//终端IP
            'notify_url' => config('website').'/api/v3.api/wxNotify',//通知地址
            'trade_type' => 'JSAPI',//交易类型
            'openid' => $openid
        ];
    
        $data['sign'] = self::makeSign($data);
    
        $xmldata = self::array2xml($data);
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $res = self::curl_post_ssl($url, $xmldata);
    
        if (empty($res)) {
            return json(['status'=>0,'data'=>'','msg'=>'连接失败']);
        }
    
        $content = self::xml2array($res);
    
        $return_code = strval($content['return_code']);
        $result_code = strval($content['result_code']);
    
        if ($return_code == 'FAIL') {
            return json(['status'=>0,'data'=>'','msg'=>$return_code]);
        }
    
        if ($result_code == 'FAIL') {
            return ['status' => 1, 'msg' => strval($content['err_code']), ':' . strval($content['err_code_des'])];
        }
    
        //第二次签名
        $timest = (string)time();
    
        $resdata = [
            'appId' => '',
            'package' => 'prepay_id='.strval($content['prepay_id']),
            'nonceStr' => self::getNonceStr(),
            'timeStamp' => $timest,
            'signType' => 'MD5'
        ];
    
        $resdata['paySign'] = self::makeSign($resdata);
    
        return json(['status'=>1,'data'=>$resdata,'msg'=>'查询支付参数成功']);
    }
    
    //回调
    public function wxNotify()
    {
        //真的成功支付之后,才会触发这个回调方法
        $notify = file_get_contents("php://input");
        $data = self::xml2array($notify);
    
    }
    

      

  • 相关阅读:
    仿QQ消息弹出框
    WPF,Silverlight与XAML读书笔记第十六 资源之二进制资源
    WPF,Silverlight与XAML读书笔记第十七 资源之逻辑资源
    WPF,Silverlight与XAML读书笔记第十 WPF框架与工作方式介绍
    WPF,Silverlight与XAML读书笔记第五 XAML与传统过程式代码的关系
    再谈ASP.NET第七 跨应用、跨服务器的表单验证
    WPF,Silverlight与XAML读书笔记第六 WPF新概念之一逻辑树与可视树
    WPF,Silverlight与XAML读书笔记第八 WPF新概念之三路由事件
    WPF,Silverlight与XAML读书笔记第十四 独立存储&部署
    WPF,Silverlight与XAML读书笔记第七 WPF新概念之二依赖属性
  • 原文地址:https://www.cnblogs.com/qczy/p/10939193.html
Copyright © 2011-2022 走看看