您好,欢迎访问一九零五行业门户网

2016封装的微信支付,太好用了

一直在做微信,遇到过很多坑,今天写出来,让大家少走点弯路,第一次写文字,有点土,如果有不懂的童鞋,可以联系我qq1034100429,或者发私信。
getaccesstoken 获取access_token
//发起支付
public function sendpay($openid, $title, $out_trade_no, $total_fee, $notify_url)
sendtpl 发送模板消息appid = c('weixin_appid');
        $this->appsecret = c(weixin_app_secret);
        $this->mchid = c(weixin_mchid);
        $this->wechatkey = c(weixin_key);
        $this->token = c('weixin_app_token');
}
    public function chushi()
    {
        echo  $this->getaccesstoken();
    }
    public function checksignature()
    {
        $signature = $_get['signature'];
        $timestamp = $_get['timestamp'];
        $nonce = $_get['nonce'];
        $token = $this->token;
        $arrtemp = array($token, $timestamp, $nonce);
        sort($arrtemp, sort_string);
        $arrtemp = implode($arrtemp);
        $arrtemp = sha1($arrtemp);
        if ($arrtemp == $signature) {
            return true;
        } else {
            return false;
        }
    }
    public function valid()
    {
        $echostr = $_get['echostr'];
        if ($this->checksignature()) {
            echo $echostr;
            exit;
        }
    }
////根据返回的信息,做出相应的处理
//    public function responsemsg()
//    {
//        $poststr = file_get_contents('php://input');
//        if (!empty($poststr)){
//            /* libxml_disable_entity_loader is to prevent xml external entity injection,
//               the best way is to check the validity of xml by yourself */
//            libxml_disable_entity_loader(true);
//            $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
//            $fromusername = $postobj->fromusername;
//            $tousername = $postobj->tousername;
//            $keyword = trim($postobj->content);
//            $msgtype = $postobj->msgtype;
//            $event=$postobj->event;
//            $time = time();
//            $texttpl = 
//                            
//                            
//                            %s
//                            
//                            
//                            0
//                            ;
//            if(!empty( $keyword ))
//            {
//                $msgtype = text;
//                $contentstr = welcome to wechat world!;
//                $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
//                echo $resultstr;
//            }else{
//                echo input something...;
//            }
//
//        }else {
//            echo ;
//            exit;
//        }
//    }
    public function getaccesstoken()
    {
        $access_token = s('access_token');
        if(!empty($access_token)){
return $access_token;
        }else{
            $url = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= . $this->appid . &secret= . $this->appsecret;;
            $ret = $this->curlget($url);
            $ret = json_decode($ret,true);
            s('access_token',$ret['access_token'],7100);
return $ret['access_token'];
        }
    }
    //根据openid拉取用户信息
    public  function userinfo($openid){
        $access_token = $this->getaccesstoken();
        $url = https://api.weixin.qq.com/cgi-bin/user/info?access_token=.$access_token.&openid=.$openid.&lang=zh_cn;
        $userinfo = json_decode($this->curlget($url),true);
        return $userinfo;
    }
    function p($arr){
        echo ;
        print_r($arr);
    }
    function curlget($url)
    {
        $ch = curl_init();
        curl_setopt($ch, curlopt_url, $url);
        curl_setopt($ch, curlopt_ssl_verifypeer, false);
        curl_setopt($ch, curlopt_ssl_verifyhost, false);
        curl_setopt($ch, curlopt_returntransfer, 1);
        curl_setopt($ch, curlopt_header, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    public function jingmo($url,$code,$state){
        if($code){
            $url = https://api.weixin.qq.com/sns/oauth2/access_token?appid=.$this->appid.&secret=.$this->appsecret.&code=.$code.&grant_type=authorization_code;
            $arrtemp = json_decode($this->curlget($url),true);
            $ret = $this->getuserinfo($arrtemp['openid'],$arrtemp['access_token']);
            return  $ret;
        }else{
            $url = https://open.weixin.qq.com/connect/oauth2/authorize?appid=.$this->appid.&redirect_uri=.$url.&response_type=code&scope=snsapi_userinfo&state=.$state.#wechat_redirect;
            header(location: $url);
        }
    }
    public function getuserinfo($openid,$user_access_token){
        $userurl = https://api.weixin.qq.com/sns/userinfo?access_token=.$user_access_token.&openid=.$openid.&lang=zh_cn;
        $userinfo = json_decode($this->curlget($userurl),true);
        return $userinfo;
    }
    public function getjsapi_ticket(){
        $js_ticket = s(js_ticket);
        if(!empty($js_ticket)){
            return $js_ticket;
        }else{
            $access_token = $this->getaccesstoken();
            $url = https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=.$access_token.&type=jsapi;
            $res = json_decode($this->curlget($url),true);
            s(js_ticket,$res['ticket'],7100);
            return $res['ticket'];
        }
    }
    public function getjssign(){
        echo ;
        $js_ticket = $this->getjsapi_ticket();
        $time = time();
        $arr = array(
            'noncestr'=>md5($time),
            'jsapi_ticket'=>$js_ticket,
            'timestamp'=>$time,
            'url'=>'http://'.$_server['http_host'].$_server['request_uri'],
);
        $this->p($arr);
        ksort($arr);
        $string=;
        $i=1;
        foreach($arr as $key=>$val){
            if($i==1){
                $string.=$key.=.$val;
            }else{
                $string.=&.$key.=.$val;
            }
            $i++;
            echo $i;
        }
        // echo $string;
        $signature =strtolower($string);
        $signature =  sha1($string);
        $jsconfig = array(
            'debug'=>true,
            'appid'=>$this->appid,
            'timestamp'=>$time,
            'noncestr'=>md5($time),
            'signature'=>$signature,
            ' jsapilist'=>['onmenusharetimeline','onmenushareappmessage','onmenushareqq'],
        );
        $json = json_encode($jsconfig);
        return $json;
    }
    //生成大写签名
    function createsign($arr,$biaozhi){
        ksort($arr);
        $string=;
        $i=1;
        foreach($arr as $key=>$val){
            if($i==1){
                $string.=$key.=.$val;
            }else{
                $string.=&.$key.=.$val;
            }
            $i++;
        }
        $signtemp = $string&key= . $this->wechatkey;
        $sign = strtoupper(md5($signtemp));
        $arr[$biaozhi] = $sign;
        //$json = json_encode($arr);
        return $arr;
    }
    //支付
//发起支付
    public function sendpay($openid, $title, $out_trade_no, $total_fee, $notify_url)
    {
        $time = time();
        $arr = array(
            'appid' => $this->appid,
            'mch_id' => $this->mchid,
            'nonce_str' => md5($time),
            'body' => kjhk,
            'out_trade_no' => $out_trade_no,
            'total_fee' => $total_fee,
            'spbill_create_ip' => 127.0.0.1,
            'notify_url' => $notify_url,
            'trade_type' => jsapi,
            'openid' => $openid,
        );
        $biaozhi ='sign';
        $arr = $this->createsign($arr,$biaozhi);
        $xml = ;
        foreach ($arr as $key=>$val) {
            $xml.=.$val..$key.>;
        }
        $xml.=;
        $url = https://api.mch.weixin.qq.com/pay/unifiedorder;
        $resxml =  $this->postcurltransfer($url,$xml);
        $res = simplexml_load_string($resxml);
        $cutime = time();
        $jsapiarr=array(
            'appid'=>$this->appid,
            'timestamp'=>$cutime,
            'noncestr'=>md5($cutime),
            'package'=>prepay_id=.$res->prepay_id,
            'signtype'=>md5,
        );
        $biaozhi = 'paysign';
        $jsapi = $this->createsign($jsapiarr,$biaozhi);
       // $jsapijson = json_encode($jsapi);
        return $jsapi;
    }
    public function getpaysign(){
        $time = time();
        $arr = array(
            'appid'=>$this->appid,
            'mch_id'=>$this->mchid,
            'nonce_str'=>md5($time),
            'body'=>sdf,
            'out_trade_no'=>2016.$time,
            'total_fee'=>1,
            'spbill_create_ip'=>127.0.0.1,
            'notify_url'=>http://www.baidu.com,
            'trade_type'=>jsapi,
        );
        ksort($arr);
        $string=;
        $i=1;
        foreach($arr as $key=>$val){
            if($i==1){
                $string.=$key.=.$val;
            }else{
                $string.=&.$key.=.$val;
            }
            $i++;
        }
        $string.=&key=.$this->wechatkey;
        $sign = strtoupper(md5($string));
        $arr['sign'] = $sign;
        return $arr;
    }
    function post($curlpost,$url){
        $curl = curl_init();
        curl_setopt($curl, curlopt_url, $url);
        curl_setopt($curl, curlopt_header, false);
        curl_setopt($curl, curlopt_returntransfer, true);
        curl_setopt($curl, curlopt_nobody, true);
        curl_setopt($curl, curlopt_post, true);
        curl_setopt($curl, curlopt_postfields, $curlpost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }
    private function postcurltransfer($url, $data)
    {
        $curl = curl_init();
        curl_setopt($curl, curlopt_url, $url);
        curl_setopt($curl, curlopt_returntransfer, 1);
        curl_setopt($curl, curlopt_post, 1);
        curl_setopt($curl, curlopt_postfields, $data);
        $str = curl_exec($curl);
        curl_close($curl);
return $str;
    }
    //发送模板消息
    public function sendtpl($openid, $url, $template_id, $content, $topcolor=#ff0000)
    {
        $arr = array(
            'touser' => $openid,
            'template_id' => $template_id,
            'url' => $url,
            'topcolor' => $topcolor,
            'data' => $content,
        );
        $arrjson = json_encode($arr);
        $accesstoken = $this->getaccesstoken();
        $sendurl = https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=.$accesstoken;
        return $this->postcurltransfer($sendurl, $arrjson);
    }
}
上面的是微信支付简单封装
如果发起支付,请先对应好微信公众号的资料。
包括微信支付目录
我这里是
http://域名//index.php/home/index/
好像是这个
获取access_token的过程jingmo($url, $code, $state);
        session('userinfo',$userinfo);
        if($code) {
            header(location: $state);
        }else{
        }
    }
}公共函数
function.phpfunction getuserinfo(){   //获取用户信息
    import(weixin);
    $weixin = new \org\weixin;
    $info = session('userinfo');
    if($info){
        return $info;
        die;
    }else {
        $state = 'http://' . $_server['http_host'] . $_server['request_uri'];
        $code = '';
        $url = 'http://' . $_server['http_host'] . u('home/weixin/zhanshi');
        $url = urlencode($url);
        $weixin->jingmo($url, $code, $state);
    }
}测试支付sendpay($openid, $title, $out_trade_no, $total_fee, $notify_url);
        $this->assign('result',$result);
        $this->display();
    }
//这里是支付结果通知
    public function paynotify(){
        $weixin = new \org\weixin;
        $xml = file_get_contents(php://input);
        $p = simplexml_load_string($xml, 'simplexmlelement', libxml_nocdata);
        $arr = $this->object_array($p);
        $out_trade_no = $arr['out_trade_no'];
        $transaction_id= $arr['transaction_id'];
        $openid = $arr['openid'];
        $sign = $arr['sign'];
        //处理支付结果
        unset($arr['sign']);
        $biaozhi = sign;
        $ret = $weixin->createsign($arr, $biaozhi);
        if($ret['sign'] === $sign) {
            $aa = 成功;
        } else {
            $aa = 失败;
        }
        $file  = './logweixin.txt';//要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个
        $content = $out_trade_no.***.$transaction_id.第一次写入的内容\n;
        if($f  = file_put_contents($file, $content,file_append)){// 这个函数支持版本(php 5)
                      echo 写入成功。
;
          }
    //这里处理支付结果的过程
        echo success;
    }
public function getaccesstoken(){
        $weixin = new \org\weixin;
        echo $weixin->getaccesstoken();
}
}
其它类似信息

推荐信息