在網上、社群裡搜了一下好像沒找到什麼文章詳細分享這種方式的,這些天我花了些時間研究整理了一下,發現這種方式能實時獲取到支付寶裡的二維碼收款記錄,從而很好地實現個人支付寶免簽約收款實時回撥,於是在這裡分享一下。
流程很簡單,主要就是3步:
1.登入網頁支付寶,獲取cookie
2.抓包分析支付寶的交易訂單介面
3.模擬測試訂單介面請求,投入使用
1.先去支付寶官網登入,掃碼登入或賬密登入都闊以。
登入成功後,直接F12(開啟開發者工具),在console中輸入document.cookie並回車,即可獲取到你登入後的cookie。
2.找一找支付寶二維碼收款的交易訂單記錄在哪,簡單瀏覽後馬上發現是在“對賬中心”-“業務查詢”-“交易訂單”選單下。
再次F12開啟開發者工具,進入network,再次點選訂單頁面的“查詢”,抓取請求的介面。
簡單分析一下介面,發現需要用到的必須用到的ctoken(拼接在url後面),billUserId(傳參),這倆都能從cookie中提取到,所以木有問題~而其他的一些介面引數,就照著他的方式傳即可。
3.然後可以直接到Postman等工具裡模擬請求測試一下,除了上面提到那些引數,發現header頭除了cookie以外,referer這個值也是必須的。再回到瀏覽器把這個值複製過來再次測試請求,成功!
最後就直接編寫一些簡單的程式碼方法,在用到的時候去呼叫請求介面就好了,PHP參考程式碼如下:
<?php
/**
* User: gump994
* Date: 2021-08-08
* Time: 18:08
* Description: 利用網頁版支付寶Cookie監聽交易訂單資料,實現個人支付寶收款試試回撥
*
* 【GOGO支付】已經完整實現該模式雲端監聽收款,很穩定,效率很高,歡迎測試體驗~
* 官網地址: https://www.gogozhifu.com
*
* 微信:gump994 郵箱:[email protected]
*
*/ goZfb('your-cookie', 'your-token', 'your-userId'); //呼叫支付寶交易訂單列表介面
function goZfb($cookie, $token, $userId)
{
$url = "https://mbillexprod.alipay.com/enterprise/tradeListQuery.json?ctoken=" . $token;
$header = [
'referer: https://mbillexprod.alipay.com/enterprise/bizTradeOrder.htm',
'origin: https://mbillexprod.alipay.com',
'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
];
$data = [
'billUserId' => $userId,
'pageNum' => 1,
'pageSize' => 20,
'startTime' => date('Y-m-d') . ' 00:00:00',
'endTime' => date("Y-m-d", strtotime("+1 day")) . ' 00:00:00',
'status' => 'ALL',
'queryEntrance' => 1,
'entityFilterType' => 1,
'sortTarget' => 'gmtCreate',
'activeTargetSearchItem' => 'tradeNo',
'tradeFrom' => 'ALL',
'sortType' => 0,
'_input_charset' => 'gbk'
];
$res = go_curl($url, $data, $header, $cookie); print_r($res);
} //傳送Http請求
function go_curl($url, $post = 0, $header = 0, $cookie = 0, $nobaody = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($nobaody) {
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
} ?>
相應程式碼gitee地址https://gitee.com/niaogang/gogozhifu-web-zfb
最後還要補充說明一下,如果用於實現個人收款實時回撥,那麼要考慮一下介面重複請求的頻率、交易金額匹配、保持cookie有效等問題,這些問題解決方法也很多,多思考都是可以解決的。
有任何問題或想法歡迎交流討論~