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)到訂單詳情頁(yè)面
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" >購(gòu)買</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)頁(yè)授權(quán)獲取用戶openid============
//通過code獲得openid
//
//
if (!isset($_GET['code']))
{
//觸發(fā)微信返回code碼
$rUrl=urlencode(C('WxConfig.JS_API_CALL_URL').'/id/'.$_GET[id]);
/*注意一下這個(gè) id 是為了可以成功的傳個(gè) id值才這樣寫 如果你不需要的話也可以直接寫成
$rUrl=urlencode(C('WxConfig.JS_API_CALL_URL'));
$this->redirect('WxJsAPI/jsApiCall',array('id' =>55),0, '頁(yè)面跳轉(zhuǎn)中...');
我是這樣穿這個(gè)id過來的值得你們也可以自己改成其他的來傳這個(gè)id 或是刪除他不要
*/
$url = $jsApi->createOauthUrlForCode($rUrl);
// echo $url;exit();
Header("Location: $url");
}else
{
//獲取code碼,以獲取openid
$code = $_GET['code'];
$jsApi->setCode($code);
$openid = $jsApi->getOpenId();//openid 這里是為了獲取用戶當(dāng)前的openid 如果你有做微信登陸的話就可以無視他。
}
//=========步驟2:使用統(tǒng)一支付接口,獲取prepay_id============
//使用統(tǒng)一支付接口
$unifiedOrder = new \UnifiedOrder_pub();
/*此處做數(shù)據(jù)庫(kù)的查詢 這里操作數(shù)據(jù)庫(kù)把產(chǎn)品信息顯示出來*/
/*此處做數(shù)據(jù)庫(kù)的查詢 這里操作數(shù)據(jù)庫(kù)把產(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');//商戶訂單號(hào)
$unifiedOrder->setParameter("total_fee",1*100);//總金額 微信的錢1*100等于1
$unifiedOrder->setParameter("notify_url",$NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交易類型
//非必填參數(shù),商戶可根據(jù)實(shí)際情況選填
//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商戶號(hào)
//$unifiedOrder->setParameter("device_info","XXXX");//設(shè)備號(hào)
//$unifiedOrder->setParameter("attach","XXXX");//附加數(shù)據(jù)
//$unifiedOrder->setParameter("time_start","XXXX");//交易起始時(shí)間
//$unifiedOrder->setParameter("time_expire","XXXX");//交易結(jié)束時(shí)間
//$unifiedOrder->setParameter("goods_tag","XXXX");//商品標(biāo)記
//$unifiedOrder->setParameter("openid","XXXX");//用戶標(biāo)識(shí)
//$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();
//存儲(chǔ)微信的回調(diào)
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
$notify->saveData($xml);
//驗(yàn)證簽名,并回應(yīng)微信。
//對(duì)后臺(tái)通知交互時(shí),如果微信收到商戶的應(yīng)答不是成功或超時(shí),微信認(rèn)為通知失敗,
//微信會(huì)通過一定的策略(如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í)際情況設(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,"【通信出錯(cuò)】:\n".$xml."\n");
}
elseif($notify->data["result_code"] == "FAIL"){
//此處應(yīng)該更新一下訂單狀態(tài),商戶自行增刪操作
log_result($log_name,"【業(yè)務(wù)出錯(cuò)】:\n".$xml."\n");
}
else{
/*查看支付成功的返回值請(qǐng)去 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 */
$xmlss=$notify->data["out_trade_no"];//訂單號(hào)
$total_fee=$notify->data['total_fee'];//訂單總金額,單位為分,詳見支付金額
/*更新訂單狀態(tài)這里寫數(shù)據(jù)庫(kù)的操作*/
/*更新訂單狀態(tài)這里寫數(shù)據(jù)庫(kù)的操作*/
//此處應(yīng)該更新一下訂單狀態(tài),商戶自行增刪操作
log_result($log_name,"【支付成功】:\n".$xml."\n");
}
//商戶自行增加處理流程,
//例如:更新訂單狀態(tài)
//例如:數(shù)據(jù)庫(kù)操作
//例如:推送支付完成信息
}
}
// 打印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!