實(shí)現(xiàn)思路
- 生成一個(gè)二維碼,加入要處理的url連接
- 在用戶掃完碼后,在對(duì)應(yīng)的腳本中,判斷掃碼終端,調(diào)用相應(yīng)的支付
- 若能夠掃碼之后能喚起相應(yīng)app,支付寶要用手機(jī)網(wǎng)站支付方式,微信要使用jsapi支付方式
效果展示
提示: 因?yàn)轫?xiàng)目即將上線,所以上面的支付二維碼連接被我替換了(注意在生成二維碼時(shí)加入的連接,要帶上http協(xié)議)
實(shí)現(xiàn)
步驟生成二維碼
//我的url指向了checkTerrace方法
$url = self::ADMIN_URL . 'params=' . $params;
//ADMIN_URL是生成二維碼的url,請(qǐng)?zhí)鎿Q成自己
處理用戶掃碼操作(checkTerrace方法)
public function checkTerrace()
{
$pay_type = $this->getPayType(); //該方法使用來(lái)判斷用戶掃碼終端的
$params = $this->request->get('params'); //生成二維碼url帶的參數(shù)(看個(gè)人需求,我的項(xiàng)目需要額外參數(shù))
$params = $this->desDecode($params); //這里是因?yàn)槲覍?duì)參數(shù)進(jìn)行了desc加密,看個(gè)人需求
if ($pay_type === 'alipay') { //如果用戶是通過(guò)支付寶掃碼,進(jìn)行支付寶相關(guān)操作
if ($params === false) {
echo "系統(tǒng)錯(cuò)誤!,請(qǐng)稍后重試";
exit;
}
$res = $this->createOrder($pay_type, $params);
if (!$res) {
echo "系統(tǒng)錯(cuò)誤,請(qǐng)稍后重試";
exit;
}
$this->aliPay($res);
} elseif ($pay_type === 'wechat') { //如果用戶是通過(guò)微信掃碼,進(jìn)行微信相關(guān)操作
if ($params === false) {
echo "系統(tǒng)錯(cuò)誤,請(qǐng)稍后重試";
exit;
}
$prepare = $this->wechat($pay_type, $params);
$this->assign('json', $prepare);
return $this->display('wpay.html');
} elseif ($pay_type === false) {
echo "請(qǐng)使用支付寶或微信進(jìn)行掃碼";
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
*/
//這個(gè)邏輯就不貼代碼了
private function createOrder($pay_type, $params)
{
/*生成訂單相關(guān)邏輯代碼*/
}
支付寶支付
/**
* 喚起支付寶app
*
* @param array $api_params
* @return void
* @date 2021-02-04
*/
private function aliPay($api_params)
{
$config = [
'notify_url' => '異步回調(diào)地址',
'is_open_certificate' => true
];
$domain = urlencode($api_params['domain']);
$api = [
'out_trade_no' => $api_params['trade_no'],
'total_amount' => '0.01',
'subject' => '商品標(biāo)題',
'passback_params' => $domain
];
$pay = new Pay($config);
$res = $pay->driver('alipay')->gateway('wap')->pay($api); //調(diào)用支付寶手機(jī)網(wǎng)站支付
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 "微信授權(quán)失敗...";
exit;
}
$api_params = $this->createOrder($pay_type, $params); //用戶openID獲取成功后才進(jìn)行訂單生產(chǎn)操作
if (!$api_params) {
echo "系統(tǒng)錯(cuò)誤,請(qǐng)稍后重試";
exit;
}
$config = ['notify_url' => '微信異步回調(diào)地址'];
$api = [
'body' => '我是標(biāo)題',
'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); //調(diào)用微信jsapi支付
return $res;
}
靜默獲取openID
/**
* 獲取用戶的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']; //這里授權(quán)后微信跳轉(zhuǎn)的地址,要寫在訂單處理處,否則會(huì)造成因?yàn)槌绦蛱D(zhuǎn)到微信授權(quán)頁(yè)面,導(dǎo)致腳本邏輯終止
$redirect_uri = urlencode($redirect_uri);
$url = $this->codeUrl . 'redirect_uri=' . $redirect_uri . 'appid=' . $this->appId . 'scope=snsapi_baseresponse_type=codestate=STATE#wechat_redirect'; //使用用戶靜默授權(quán)模式(因?yàn)槲也恍枰@取用戶信息所有就沒(méi)采用用戶手段授權(quán)模式)
header("location:{$url}"); //跳轉(zhuǎn)到微信授權(quán)頁(yè)面
} 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存儲(chǔ)到session中
} else {
$_SESSION['open_id'] = false;
}
return $_SESSION['open_id'];
}
}
前端輪詢判斷監(jiān)聽(tīng)訂單支付狀態(tài)
$(function() {
$("#code").qrcode({
//jQuery生成二維碼
width: 165, //寬度
height: 167, //高度
text: $('input[name="url"]').val()
});
var startTime = Date.parse(new Date())/1000;
//設(shè)置定時(shí)器
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">點(diǎn)擊重新獲取/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>請(qǐng)?jiān)谑謾C(jī)端操作/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">點(diǎn)擊重新獲取/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>請(qǐng)?jiān)谑謾C(jī)端操作/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);
})
前端效果:
用戶進(jìn)入支付頁(yè)面但是一直為掃碼,超過(guò)一定時(shí)間
用戶掃碼后一直未進(jìn)行支付,超過(guò)一定時(shí)間
到此這篇關(guān)于PHP實(shí)現(xiàn)一個(gè)二維碼同時(shí)支持支付寶和微信支付的示例的文章就介紹到這了,更多相關(guān)PHP 支付寶和微信支付內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- php+laravel 掃碼二維碼簽到功能
- Thinkphp使用Zxing擴(kuò)展庫(kù)解析二維碼內(nèi)容圖文講解
- PHP基于phpqrcode類生成二維碼的方法示例詳解
- PHP基于phpqrcode類庫(kù)生成二維碼過(guò)程解析
- thinkphp3.2框架集成QRcode生成二維碼的方法分析
- PHP生成二維碼與識(shí)別二維碼的方法詳解【附源碼下載】
- php生成二維碼不保存服務(wù)器還有下載功能的實(shí)現(xiàn)代碼
- PHP二維碼的生成與識(shí)別案例