- 生成一個二維碼,加入要處理的url鏈接
- 在用戶掃完碼後,在對應的腳本中,判斷掃碼終端,調用相應的支付
- 若可以掃碼以後能喚起相應app,支付寶要用手機網站支付方式,微信要使用jsapi支付方式
提示: 由於項目即將上線,因此上面的支付二維碼鏈接被我替換了(注意在生成二維碼時加入的鏈接,要帶上http協議)javascript
//個人url指向了checkTerrace方法 $url = self::ADMIN_URL . 'params=' . $params; //ADMIN_URL是生成二維碼的url,請替換成本身
public function checkTerrace() { $pay_type = $this->getPayType(); //該方法使用來判斷用戶掃碼終端的 $params = $this->request->get('params'); //生成二維碼url帶的參數(看我的需求,個人項目須要額外參數) $params = $this->desDecode($params); //這裏是由於我對參數進行了desc加密,看我的需求 if ($pay_type === 'alipay') { //若是用戶是經過支付寶掃碼,進行支付寶相關操做 if ($params === false) { echo "系統錯誤!,請稍後重試"; exit; } $res = $this->createOrder($pay_type, $params); if (!$res) { echo "系統錯誤,請稍後重試"; exit; } $this->aliPay($res); } elseif ($pay_type === 'wechat') { //若是用戶是經過微信掃碼,進行微信相關操做 if ($params === false) { echo "系統錯誤,請稍後重試"; exit; } $prepare = $this->wechat($pay_type, $params); $this->assign('json', $prepare); return $this->display('wpay.html'); } elseif ($pay_type === false) { echo "請使用支付寶或微信進行掃碼"; exit; } }
/** * 判斷掃碼終端 * * @return string|boolean * @date 2021-02-04 */ private function getPayType() { if (strstr($_SERVER['HTTP_USER_AGENT'], 'AlipayClient')) { return "alipay"; } elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) { return "wechat"; } else { return false; } }
/** * 生成訂單 * * @param string $pay_type * @param json $params * @return void * @date 2021-02-04 */ //這個邏輯就不貼代碼了 private function createOrder($pay_type, $params) { /*生成訂單相關邏輯代碼*/ }
/** * 喚起支付寶app * * @param array $api_params * @return void * @date 2021-02-04 */ private function aliPay($api_params) { $config = [ 'notify_url' => '異步回調地址', 'is_open_certificate' => true ]; $domain = urlencode($api_params['domain']); $api = [ 'out_trade_no' => $api_params['trade_no'], 'total_amount' => '0.01', 'subject' => '商品標題', 'passback_params' => $domain ]; $pay = new Pay($config); $res = $pay->driver('alipay')->gateway('wap')->pay($api); //調用支付寶手機網站支付 echo $res; }
/** * 喚起微信app * * @return void * @date 2021-02-04 */ public function wechat($pay_type, $params) { $opend_id = $this->getOpenId(); //處理微信jsapi支付以前,要先獲取用戶的openID if (!$opend_id) { echo "微信受權失敗..."; exit; } $api_params = $this->createOrder($pay_type, $params); //用戶openID獲取成功後才進行訂單生產操做 if (!$api_params) { echo "系統錯誤,請稍後重試"; exit; } $config = ['notify_url' => '微信異步回調地址']; $api = [ 'body' => '我是標題', 'out_trade_no' => $api_params['trade_no'], 'total_fee' => 1, 'openid' => $opend_id, 'attach' => $api_params['domain'] ]; $pay = new Pay($config); $res = $pay->driver('wechat')->gateway('mp')->pay($api); //調用微信jsapi支付 return $res; }
/** * 獲取用戶的openid * * @return void * @date 2021-02-04 */ public function getOpenId() { if (isset($_SESSION['open_id']) && $_SESSION['open_id']) { return $_SESSION['open_id']; } if (!$this->request->get('code')) { $redirect_uri = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; //這裏受權後微信跳轉的地址,要寫在訂單處理處,不然會形成由於程序跳轉到微信受權頁面,致使腳本邏輯終止 $redirect_uri = urlencode($redirect_uri); $url = $this->codeUrl . 'redirect_uri=' . $redirect_uri . '&appid=' . $this->appId . '&scope=snsapi_base&response_type=code&state=STATE#wechat_redirect'; //使用用戶靜默受權模式(由於我不須要獲取用戶信息全部就沒采用用戶手段受權模式) header("location:{$url}"); //跳轉到微信受權頁面 } else { $openidurl = $this->openidUrl . 'appid=' . $this->appId . '&secret=' . $this->appSecret . '&code=' . $this->request->get('code') . '&grant_type=authorization_code'; $data = Http::get($openidurl); $data = json_decode($data, true); if ($data['openid']) { $_SESSION['open_id'] = $data['openid']; //獲取到的用戶openID存儲到session中 } else { $_SESSION['open_id'] = false; } return $_SESSION['open_id']; } }
$(function() { $("#code").qrcode({ //jQuery生成二維碼 width: 165, //寬度 height: 167, //高度 text: $('input[name="url"]').val() }); var startTime = Date.parse(new Date())/1000; //設置定時器 var poll_request = setInterval( function() { $.ajax({ url: '/company/StoreSetting/checkStatus', data:{time:startTime}, dataType:'json', type:'get', success:function(res) { if (res.code == 400) { var result = clearTimer(poll_request, startTime); if (result) { var html = `<img src="/Static/images/paybg.png">`+ `<div class="notify" id="notify">`+ `<img src="/Static/images/pay_time_out.png" alt="">`+ `<span class="pay_tip">點擊從新獲取</span>`+ `</div>`; $('.qrcode-img').empty(); $('.qrcode-img').append(html); } } else if(res.code == 500) { var html = `<img src="/Static/images/paybg.png">`+ `<div class="notify">`+ `<img src="/Static/images/pay_error.png" alt="">`+ `<span class="pay_tip">已掃碼<br>請在手機端操做</span>`+ `</div>`; $('.qrcode-img').empty(); $('.qrcode-img').append(html); clearTimer(poll_request, startTime); } else if(res.code == 200) { clearInterval(poll_request) layer.msg("支付成功", {icon:6}, function() { window.location.reload() }) // layer.msg("支付成功", {icon:6}, function() { // }) } } }) }, 2000); }) function clearTimer(index, startTime) { if (((Date.parse(new Date())/1000) - startTime) > 60) { clearInterval(index) return 'reload'; } return false; } //刷新二維碼 $('.qrcode-img').on("click", '#notify', function() { $('.qrcode-img').empty() $("#code").qrcode({ width: 165, //寬度 height: 167, //高度 text: $('input[name="url"]').val() }); var startTime = Date.parse(new Date())/1000; var poll_request = setInterval( function() { $.ajax({ url: '/company/StoreSetting/checkStatus', data:{time:startTime}, dataType:'json', type:'get', success:function(res) { if (res.code == 400) { var result = clearTimer(poll_request, startTime); if (result) { var html = `<img src="/Static/images/paybg.png">`+ `<div class="notify" id="notify">`+ `<img src="/Static/images/pay_time_out.png" alt="">`+ `<span class="pay_tip">點擊從新獲取</span>`+ `</div>`; $('.qrcode-img').empty(); $('.qrcode-img').append(html); } } else if(res.code == 500) { var html = `<img src="/Static/images/paybg.png">`+ `<div class="notify">`+ `<img src="/Static/images/pay_error.png" alt="">`+ `<span class="pay_tip">已掃碼<br>請在手機端操做</span>`+ `</div>`; $('.qrcode-img').empty(); $('.qrcode-img').append(html); clearTimer(poll_request, startTime); } else if(res.code == 200) { clearInterval(poll_request) layer.msg("支付成功", {icon:6}, function() { window.location.reload() }) // layer.msg("支付成功", {icon:6}, function() { // }) } } }) }, 2000); })
前端效果:
用戶進入支付頁面可是一直爲掃碼,超過必定時間
用戶掃碼後一直未進行支付,超過必定時間
php
只是給你們提供了一個思路,但願可以對須要的人有些幫助html