Thinkphp微信公眾號支付接口
來源:易賢網(wǎng) 閱讀:1833 次 日期:2016-08-29 14:10:19
溫馨提示:易賢網(wǎng)小編為您整理了“Thinkphp微信公眾號支付接口”,方便廣大網(wǎng)友查閱!

本文實例為大家分享了Thinkphp微信公眾號支付接口,供大家參考,具體內(nèi)容如下

第一步  先把文件夾的那兩個圖片 配置成一樣的路徑 除了域名要改 其他保持一致。

第二步  把 Weixinpay 這個文件夾放在 \ThinkPHP\Library\Vendor  將Weixinpay文件夾放置到這個Vendor文件夾中

第三步  把  WxJsAPIController.class.php 這個php文件  \Home\Controller  這里面

第四步  把 WxJsAPI這個文件夾  放置在 \Home\View  這個里面

第五步 成功調(diào)用微信公眾號支付功能

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">

    //調(diào)用微信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);

            /*這里寫如果支付成功的話執(zhí)行什么操作*/

          }else{

            //返回跳轉(zhuǎn)到訂單詳情頁面

            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:網(wǎng)頁授權(quán)獲取用戶openid============

    //通過code獲得openid

    //

    //

    if (!isset($_GET['code']))

    {

      //觸發(fā)微信返回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, '頁面跳轉(zhuǎn)中...');

      我是這樣穿這個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:使用統(tǒng)一支付接口,獲取prepay_id============

    //使用統(tǒng)一支付接口

    $unifiedOrder = new \UnifiedOrder_pub();

      /*此處做數(shù)據(jù)庫的查詢 這里操作數(shù)據(jù)庫把產(chǎn)品信息顯示出來*/

      /*此處做數(shù)據(jù)庫的查詢 這里操作數(shù)據(jù)庫把產(chǎn)品信息顯示出來*/

    //設(shè)置統(tǒng)一支付接口參數(shù)

    //設(shè)置必填參數(shù)

    //appid已填,商戶無需重復(fù)填寫

    //mch_id已填,商戶無需重復(fù)填寫

    //noncestr已填,商戶無需重復(fù)填寫

    //spbill_create_ip已填,商戶無需重復(fù)填寫

    //sign已填,商戶無需重復(fù)填寫

    //

    $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");//交易類型

    //非必填參數(shù),商戶可根據(jù)實際情況選填

    //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商戶號

    //$unifiedOrder->setParameter("device_info","XXXX");//設(shè)備號

    //$unifiedOrder->setParameter("attach","XXXX");//附加數(shù)據(jù)

    //$unifiedOrder->setParameter("time_start","XXXX");//交易起始時間

    //$unifiedOrder->setParameter("time_expire","XXXX");//交易結(jié)束時間

    //$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調(diào)起支付============

    $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();

    //存儲微信的回調(diào)

    $xml = $GLOBALS['HTTP_RAW_POST_DATA'];   

    $notify->saveData($xml);

    //驗證簽名,并回應(yīng)微信。

    //對后臺通知交互時,如果微信收到商戶的應(yīng)答不是成功或超時,微信認為通知失敗,

    //微信會通過一定的策略(如30分鐘共8次)定期重新發(fā)起通知,

    //盡可能提高通知的成功率,但微信不保證通知最終能成功。

    if($notify->checkSign() == FALSE){

      $notify->setReturnParameter("return_code","FAIL");//返回狀態(tài)碼

      $notify->setReturnParameter("return_msg","簽名失敗了啊");//返回信息

    }else{

      $notify->setReturnParameter("return_code","SUCCESS");//設(shè)置返回碼

    }

    $returnXml = $notify->returnXml();

    echo $returnXml;

    //==商戶根據(jù)實際情況設(shè)置相應(yīng)的處理流程,此處僅作舉例=======

    //以log文件形式記錄回調(diào)信息

     // $log_ = new Log_();

    $log_name= __ROOT__."/Public/notify_url.log";//log文件路徑

    if($notify->checkSign() == TRUE)

    {

      if ($notify->data["return_code"] == "FAIL") {

        //此處應(yīng)該更新一下訂單狀態(tài),商戶自行增刪操作

        log_result($log_name,"【通信出錯】:\n".$xml."\n");

      }

      elseif($notify->data["result_code"] == "FAIL"){

        //此處應(yīng)該更新一下訂單狀態(tài),商戶自行增刪操作

        log_result($log_name,"【業(yè)務(wù)出錯】:\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'];//訂單總金額,單位為分,詳見支付金額

    /*更新訂單狀態(tài)這里寫數(shù)據(jù)庫的操作*/

  /*更新訂單狀態(tài)這里寫數(shù)據(jù)庫的操作*/

        //此處應(yīng)該更新一下訂單狀態(tài),商戶自行增刪操作

        log_result($log_name,"【支付成功】:\n".$xml."\n");

      }

      //商戶自行增加處理流程,

      //例如:更新訂單狀態(tài)

      //例如:數(shù)據(jù)庫操作

      //例如:推送支付完成信息

    }

  }

  // 打印log

  public function log_result($file,$word)

  {

    $fp = fopen($file,"a");

    flock($fp, LOCK_EX) ;

    fwrite($fp,"執(zhí)行日期:".strftime("%Y-%m-%d-%H:%M:%S",time())."\n".$word."\n\n");

    flock($fp, LOCK_UN);

    fclose($fp);

  }

}

?>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:Thinkphp微信公眾號支付接口
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)