The example in this article shares the Thinkphp WeChat public account payment interface for your reference. The specific content is as follows
The first step is to configure the two pictures in the folder to the same path except that the domain name needs to be changed. Others remain the same.
The second step is to put the Weixinpay folder in \ThinkPHP\Library\Vendor and the Weixinpay folder is to be placed in the Vendor folder.
The third step is to put WxJsAPIController.class. php this php file \Home\Controller inside
The fourth step is to place the WxJsAPI folder inside \Home\View
The fifth step is to successfully call the WeChat official account payment function
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)品信息顯示出來*/ //設置統(tǒng)一支付接口參數(shù) //設置必填參數(shù) //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");//交易類型 //非必填參數(shù),商戶可根據(jù)實際情況選填 //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商戶號 //$unifiedOrder->setParameter("device_info","XXXX");//設備號 //$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); //驗證簽名,并回應微信。 //對后臺通知交互時,如果微信收到商戶的應答不是成功或超時,微信認為通知失敗, //微信會通過一定的策略(如30分鐘共8次)定期重新發(fā)起通知, //盡可能提高通知的成功率,但微信不保證通知最終能成功。 if($notify->checkSign() == FALSE){ $notify->setReturnParameter("return_code","FAIL");//返回狀態(tài)碼 $notify->setReturnParameter("return_msg","簽名失敗了啊");//返回信息 }else{ $notify->setReturnParameter("return_code","SUCCESS");//設置返回碼 } $returnXml = $notify->returnXml(); echo $returnXml; //==商戶根據(jù)實際情況設置相應的處理流程,此處僅作舉例======= //以log文件形式記錄回調(diào)信息 // $log_ = new Log_(); $log_name= __ROOT__."/Public/notify_url.log";//log文件路徑 if($notify->checkSign() == TRUE) { if ($notify->data["return_code"] == "FAIL") { //此處應該更新一下訂單狀態(tài),商戶自行增刪操作 log_result($log_name,"【通信出錯】:\n".$xml."\n"); } elseif($notify->data["result_code"] == "FAIL"){ //此處應該更新一下訂單狀態(tài),商戶自行增刪操作 log_result($log_name,"【業(yè)務出錯】:\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ù)庫的操作*/ //此處應該更新一下訂單狀態(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); } } ?>
The above is the entire content of this article. I hope it will be helpful to everyone's learning, and I also hope that everyone will visit PHP Chinese website.
For more articles related to the Thinkphp WeChat public account payment interface, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)