当前位置: 移动技术网 > IT编程>开发语言>PHP > Thinkphp微信公众号支付接口

Thinkphp微信公众号支付接口

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了thinkphp微信公众号支付接口,供大家参考,具体内容如下

第一步  先把文件夹的那两个图片 配置成一样的路径 除了域名要改 其他保持一致。

第二步  把 weixinpay 这个文件夹放在 \thinkphp\library\vendor  将weixinpay文件夹放置到这个vendor文件夹中

第三步  把  wxjsapicontroller.class.php 这个php文件  \home\controller  这里面

第四步  把 wxjsapi这个文件夹  放置在 \home\view  这个里面

第五步 成功调用微信公众号支付功能

jsapicall.html

<!doctype html>
 <html>
 <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'/>
  <title>微信安全支付</title>



  <script type="text/javascript">
    //调用微信js api 支付
    function jsapicall()
    {
      weixinjsbridge.invoke(
        'getbrandwcpayrequest',
        <?php echo $jsapiparameters; ?>,
        function(res){
          weixinjsbridge.log(res.err_msg);
          if(res.err_msg == "get_brand_wcpay_request:ok"){
          //alert(res.err_code+res.err_desc+res.err_msg);
            /*这里写如果支付成功的话执行什么操作*/
          }else{
            //返回跳转到订单详情页面
            alert(支付失败);
              
          }
        }
      );
    }
    function callpay()
    {
      if (typeof weixinjsbridge == "undefined"){
        if( document.addeventlistener ){
          document.addeventlistener('weixinjsbridgeready', jsapicall, false);
        }else if (document.attachevent){
          document.attachevent('weixinjsbridgeready', jsapicall); 
          document.attachevent('onweixinjsbridgeready', jsapicall);
        }
      }else{
        jsapicall();
      }
  
  
    }
 



 
  </script>
 </head>
 <body>
 <button style="width:210px; height:30px; background-color:#fe6714; border:0px #fe6714 solid; cursor: pointer; color:white; font-size:16px;" type="button" onclick="jsapicall()" id="asd" >购买</button>

  </div>
 </body>
 </html>

wxjsapicontroller.class.php

<?php

namespace home\controller;
use think\controller;

class wxjsapicontroller extends controller{
  public function _initialize()
  {
    //引入wxpaypubhelper
    vendor('weixinpay.wxpaypubhelper');
  }  

  public function jsapicall()
  {  

    //使用jsapi接口
    $jsapi = new \jsapi_pub();
    
    //=========步骤1:网页授权获取用户openid============
    //通过code获得openid
    //
    //

    if (!isset($_get['code']))
    {
      //触发微信返回code码
      $rurl=urlencode(c('wxconfig.js_api_call_url').'/id/'.$_get[id]);  
      /*注意一下这个 id 是为了可以成功的传个 id值才这样写 如果你不需要的话也可以直接写成
  $rurl=urlencode(c('wxconfig.js_api_call_url'));

      $this->redirect('wxjsapi/jsapicall',array('id' =>55),0, '页面跳转中...');
      我是这样穿这个id过来的值得你们也可以自己改成其他的来传这个id 或是删除他不要
      */
       

      $url = $jsapi->createoauthurlforcode($rurl);
      // echo $url;exit();
      header("location: $url");
    }else
    {

      //获取code码,以获取openid
      $code = $_get['code'];
      $jsapi->setcode($code);
      $openid = $jsapi->getopenid();//openid 这里是为了获取用户当前的openid 如果你有做微信登陆的话就可以无视他。
    }



    //=========步骤2:使用统一支付接口,获取prepay_id============
    //使用统一支付接口
    $unifiedorder = new \unifiedorder_pub();

      /*此处做数据库的查询 这里操作数据库把产品信息显示出来*/

      /*此处做数据库的查询 这里操作数据库把产品信息显示出来*/


    //设置统一支付接口参数
    //设置必填参数
    //appid已填,商户无需重复填写
    //mch_id已填,商户无需重复填写
    //noncestr已填,商户无需重复填写
    //spbill_create_ip已填,商户无需重复填写
    //sign已填,商户无需重复填写
    //

    $notify_url="你的域名/index.php/home/wxjsapi/notify";

    $unifiedorder->setparameter("openid",$openid);//openid
    $unifiedorder->setparameter("body",'商品的名字');//商品描述
    $unifiedorder->setparameter("out_trade_no",'123456789');//商户订单号
    $unifiedorder->setparameter("total_fee",1*100);//总金额 微信的钱1*100等于1
    $unifiedorder->setparameter("notify_url",$notify_url);//通知地址
  

    $unifiedorder->setparameter("trade_type","jsapi");//交易类型
    //非必填参数,商户可根据实际情况选填
    //$unifiedorder->setparameter("sub_mch_id","xxxx");//子商户号
    //$unifiedorder->setparameter("device_info","xxxx");//设备号
    //$unifiedorder->setparameter("attach","xxxx");//附加数据
    //$unifiedorder->setparameter("time_start","xxxx");//交易起始时间
    //$unifiedorder->setparameter("time_expire","xxxx");//交易结束时间
    //$unifiedorder->setparameter("goods_tag","xxxx");//商品标记
    //$unifiedorder->setparameter("openid","xxxx");//用户标识
    //$unifiedorder->setparameter("product_id","xxxx");//商品id
  

    $prepay_id = $unifiedorder->getprepayid();

    // echo $prepay_id;exit();
    //=========步骤3:使用jsapi调起支付============
    $jsapi->setprepayid($prepay_id);
    
    $jsapiparameters = $jsapi->getparameters();

    $web_host='你的域名';//填写的话 如 http://nicaicai.imwork.net 最后面不用加 /
    $this->assign('hosts',$web_host);
    $this->assign('jsapiparameters',$jsapiparameters);
    $this->display();
    //echo $jsapiparameters;

  }


  public function notify()
  {
    //使用通用通知接口
    $notify = new \notify_pub();

    //存储微信的回调
    $xml = $globals['http_raw_post_data'];   

    $notify->savedata($xml);
    

    //验证签名,并回应微信。
    //对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,
    //微信会通过一定的策略(如30分钟共8次)定期重新发起通知,
    //尽可能提高通知的成功率,但微信不保证通知最终能成功。
    if($notify->checksign() == false){

      $notify->setreturnparameter("return_code","fail");//返回状态码
      $notify->setreturnparameter("return_msg","签名失败了啊");//返回信息
    }else{
      $notify->setreturnparameter("return_code","success");//设置返回码
    }
    $returnxml = $notify->returnxml();
    echo $returnxml;
    
    //==商户根据实际情况设置相应的处理流程,此处仅作举例=======
    
    //以log文件形式记录回调信息
     // $log_ = new log_();
    $log_name= __root__."/public/notify_url.log";//log文件路径
    
    
    if($notify->checksign() == true)
    {
      if ($notify->data["return_code"] == "fail") {


        //此处应该更新一下订单状态,商户自行增删操作
        log_result($log_name,"【通信出错】:\n".$xml."\n");
      }
      elseif($notify->data["result_code"] == "fail"){
    

        //此处应该更新一下订单状态,商户自行增删操作
        log_result($log_name,"【业务出错】:\n".$xml."\n");
      }
      else{ 

  /*查看支付成功的返回值请去 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 */

  $xmlss=$notify->data["out_trade_no"];//订单号
  $total_fee=$notify->data['total_fee'];//订单总金额,单位为分,详见支付金额
    

    /*更新订单状态这里写数据库的操作*/
   
  /*更新订单状态这里写数据库的操作*/


        //此处应该更新一下订单状态,商户自行增删操作
        log_result($log_name,"【支付成功】:\n".$xml."\n");
      }
    
      //商户自行增加处理流程,
      //例如:更新订单状态
      //例如:数据库操作
      //例如:推送支付完成信息
    }
  }

  // 打印log
  public function log_result($file,$word)
  {
    $fp = fopen($file,"a");
    flock($fp, lock_ex) ;
    fwrite($fp,"执行日期:".strftime("%y-%m-%d-%h:%m:%s",time())."\n".$word."\n\n");
    flock($fp, lock_un);
    fclose($fp);
  }

}


?>

源码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网