Thinkphp5內核整合微信掃碼支付實例完整源碼,ThinkPHP框架集成微信掃碼支付,壓縮包內置安裝說明和訂單表php
打開首頁生成訂單,並顯示支付二維碼html
public function index() { $wechat = new Wechat(); $data['order'] = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); //訂單號 $data['set'] = "測試"; $data['money'] = 0.01; $data['userid'] = 1; if (!Db::execute('INSERT INTO order_sucaihuo(uid,order_no,order_money,addtime) VALUES(?,?,?,?)', [$data['userid'], $data['order'], $data['money'], time()])) { return '失敗,請重試!'; } $url = $wechat->send($data); $data['url'] = 'http://paysdk.weixin.qq.com/example/qrcode.php?data=' . $url; // return '<img alt="模式二掃碼支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data='.$url.'" style="width:150px;height:150px;"/>'; return view('index', $data); }
回調驗證並更改訂單狀態json
if ($WeChatNotify->notify($xml) == true) { file_put_contents('./time.txt', date("Y-m-d H:i:s")); //$WeChatNotify->getValues() 獲取到xml轉換爲數組的鍵值對 //out_trade_no對應的商戶訂單號 //total_fee爲訂單金額的一百的倍數 也就是total_fee/100爲支付的金額 //還有幾個鍵值對須要用的話能夠打印出來看 都是微信官方定義的 $data = $WeChatNotify->getValues(); file_put_contents('./data.txt', json_encode($data)); if (empty($data) || empty($data['out_trade_no']) || empty($data['total_fee'])) { return; } $orderData = Db::query("SELECT * FROM order_sucaihuo WHERE order_no='" . $data['out_trade_no'] . "' AND state=0"); if (empty($orderData)) { return; } $orderData = $orderData[0]; if ($orderData['order_money'] != $data['total_fee'] / 100) { return; } $orderResult = Db::execute("UPDATE order_sucaihuo SET state=1,update_time=" . time() . ""); if (!$orderResult) { return; } return "SUCCESS"; }