当前位置: 移动技术网 > IT编程>开发语言>PHP > thinkphp整合微信支付代码分享

thinkphp整合微信支付代码分享

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

本文实例为大家分享了thinkphp整合微信支付代码,供大家参考,具体内容如下

下载:  

将微信支付sdk放在第三方类库vendor下面,请切记把wxpay.config.php里面的商户信息修改为您的公众号信息,以避免造成资金的流失。

php端代码

public function pay(){ 
  //商户基本信息,可以写死在wxpay.config.php里面,其他详细参考wxpayconfig.php     
  vendor('pay.jsapi'); 
  $tools = new \jsapipay(); 
  $openid = $tools->getopenid(); 
  $out_trade_no=date('yhis').rand(100,1000); 
  $total_fee='测试'; 
  $body='啥也不说'; 
  $total_fee=1; 
  $input = new \wxpayunifiedorder(); 
  $input->setbody($body); 
  $input->setout_trade_no($out_trade_no); 
  $input->settotal_fee($total_fee); 
  $input->setnotify_url("http://xx.xxx.com/pay/notify.php"); 
  $input->settrade_type("jsapi"); 
  $input->setopenid($openid); 
  $order = \wxpayapi::unifiedorder($input); 
  $this->jsapiparameters = $tools->getjsapiparameters($order); 
  $this->display(); 
} 

 html端代码

<!doctype html> 
<head> 
  <meta charset="utf-8"> 
  <title>微信安全支付</title> 
  <meta name="keywords" content="" /> 
  <meta name="handheldfriendly" content="true"> 
  <meta name="mobileoptimized" content="320"> 
  <meta name="format-detection" content="telephone=no"> 
  <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate" /> 
  <meta http-equiv="pragma" content="no-cache" /> 
  <meta http-equiv="expires" content="0" /> 
  <meta http-equiv="cleartype" content="on"> 
  <meta id="viewport" name="viewport" content="width=320, initial-scale=1.0,maximum-scale=1.0, user-scalable=0," /> 
  <script type="text/javascript"> 
    //调用微信js api 支付 
    function jsapicall() 
    { 
      weixinjsbridge.invoke( 
        'getbrandwcpayrequest', 
        {$jsapiparameters}, 
        function(res){ 
          weixinjsbridge.log(res.err_msg); 
          if(res.err_msg == 'get_brand_wcpay_request:cancel') { 
            alert("您已取消了此次支付"); 
            return; 
          } else if(res.err_msg == 'get_brand_wcpay_request:fail') { 
            alert("支付失败"); 
            return; 
          } else if(res.err_msg == 'get_brand_wcpay_request:ok') { 
          alert("支付成功!");//跳转到订单页面 
           
        } else { 
          alert("未知错误"+res.error_msg); 
          return; 
        } 
      } 
      ); 
    } 
    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 type="button" class="btn btn-danger btn-lg btn-block" onclick="callpay()"> 
    确认支付 
  </button> 
</body> 
</html> 

就这么简单。

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

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

相关文章:

验证码:
移动技术网