教你Thinkphp5如何實現微信掃碼支付功能,Thinkphp3也差不多,需要的可以自動調整哦。
打開首頁生成訂單,並顯示支付二維碼
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);
}
-
回調驗證並更改訂單狀態
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";
}
本文轉自:http://www.sucaihuo.com/php/3261.html
轉載請註明出處!