支付寶、微信免接口支付方案


title: 支付寶、微信免簽約接口支付方案
tags: 支付寶、微信


背景

相信很多開發者或者運營者,在爲網站(博彩、棋牌 )即時到賬充值發愁、還在爲無法申請支付寶即時到賬接口而擔憂。

在這裏我將爲你提供一總思路,爲你解決這一切。實現即充即到,支付寶到賬的同時,相應的金額充值到您網站的相應賬戶上 。

本文參考了github上面ChenPay項目

實現思路

  • 登錄支付寶
  • 獲取支付寶COOKIE
  • 獲取獲取賬單接口
  • 定時監控接口
  • 排隊機制,如果商品 100元 ,可以設置99.99 100.01 100.2 等不同價位判斷不同付款用戶。
  • 根據用戶付款,提供不同的商品給用戶
  • Cookie可能會到期,添加郵件提醒

一、獲取接口

我們打開支付寶網站,登錄上去並且查詢自己的交易記錄,我們可以看到選定時間的交易記錄表單。

看到表單,如果你從事的web開發,這時候你就應該有一點想法了,表單一般都是通過json文件返回的,那麼支付寶這個交易記錄是否也存在這樣一個json.
瀏覽器按F12再刷新一下,可以看到tradeListQuery.json
[外鏈圖片轉存失敗(img-gF5A4Ysy-1565105697750)(https://ws1.sinaimg.cn/large/5fe79106ly1fx1twczbh6j20je0em76s.jpg)]
我們點擊tradeListQuery.json 可以看到 https://mbillexprod.alipay.com/enterprise/tradeListQuery.json

避免頻繁訪問

爲了防止一個接口多次訪問,而被支付寶拒絕,我們再次找一個接口。
[外鏈圖片轉存失敗(img-72jzR1o9-1565105697752)(https://ws1.sinaimg.cn/large/5fe79106ly1fx1yr28y27j21ru154k27.jpg)]

接口地址我直接給你:

https://mbillexprod.alipay.com/enterprise/fundAccountDetail.json

獲取cookie

往下翻我們找到Request Headers ,可以看到Cookie

獲取cookie

public static function getCookieName($name = 'uid', $cookie = false)
   {
       $getCookie = explode($name . '=', $cookie);
       if (count($getCookie) <= 1) throw new PayException('cookie有誤', 445);
       if ($name == 'uid') return explode('"', $getCookie[1])[0];
       else return explode(';', $getCookie[1])[0];
   }

二、模擬登錄

好了接口和cookie有了,那麼該考慮它的參數以及怎麼訪問它了,下面是PHP的演示代碼:

接口一

  public function HtmlOne()
    {
        try {
            return (new \GuzzleHttp\Client())
                ->request('POST', "https://mbillexprod.alipay.com/enterprise/tradeListQuery.json", [
                    'timeout' => 10,
                    'headers' => [
                        'Cookie' => $this->cookie,
                        'Origin' => 'https://mbillexprod.alipay.com',
                        'Accept-Encoding' => 'gzip, deflate, br',
                        'Accept-Language' => 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
                        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
                        'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
                        'Accept' => 'application/json, text/javascript',
                        'Referer' => 'https://mbillexprod.alipay.com/enterprise/tradeListQuery.htm',
                        'X-Requested-With' => 'XMLHttpRequest',
                        'Connection' => 'keep-alive',
                    ],
                    'body' => 'queryEntrance=1&billUserId=' . Cookie::getCookieName('uid', $this->cookie) .
                        '&status=SUCCESS&entityFilterType=0&activeTargetSearchItem=tradeNo&tradeFrom=ALL&startTime=' .
                        date('Y-m-d', strtotime('-1 day')) . '+00%3A00%3A00&endTime=' . date('Y-m-d') .
                        '+23%3A59%3A59&pageSize=20&pageNum=1&total=1&sortTarget=gmtCreate&order=descend&sortType=0&_input_charset=gbk&ctoken=' .
                        Cookie::getCookieName('ctoken', $this->cookie),
                ])
                ->getBody();
        } catch (GuzzleException $e) {
            throw new PayException($e->getMessage(), 500);
        } catch (PayException $e) {
            throw new PayException($e->getMessage(), 445);
        }
    }

接口二

 public function HtmlTwo()
    {
        try {
            return (new \GuzzleHttp\Client())
                ->request('POST', "https://mbillexprod.alipay.com/enterprise/fundAccountDetail.json", [
                    'timeout' => 10,
                    'headers' => [
                        'Cookie' => $this->cookie,
                        'Origin' => 'https://mbillexprod.alipay.com',
                        'Accept-Encoding' => 'gzip, deflate, br',
                        'Accept-Language' => 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
                        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
                        'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
                        'Accept' => 'application/json, text/javascript',
                        'Referer' => 'https://mbillexprod.alipay.com/enterprise/fundAccountDetail.htm',
                        'X-Requested-With' => 'XMLHttpRequest',
                        'Connection' => 'keep-alive',
                    ],
                    'body' => 'queryEntrance=1&billUserId=' . Cookie::getCookieName('uid', $this->cookie) .
                        '&showType=1&type=&precisionQueryKey=tradeNo&' .
                        'startDateInput=' . date('Y-m-d', strtotime('-1 day')) . '+00%3A00%3A00&endDateInput=' . date('Y-m-d') . '+23%3A59%3A59&' .
                        'pageSize=20&pageNum=1&total=1&sortTarget=tradeTime&order=descend&sortType=0&' .
                        '_input_charset=gbk&ctoken=' . Cookie::getCookieName('ctoken', $this->cookie)
                ])
                ->getBody();
        } catch (GuzzleException $e) {
            throw new PayException($e->getMessage(), 500);
        } catch (PayException $e) {
            throw new PayException($e->getMessage(), 445);
        }
    }

簡而言之,就是我們通過爬蟲的來模擬登錄支付寶,並調用上面的json。

三、處理交易數據

好了到了這裏我們就假設已經得到了json的數據,現在我們來處理這些交易數據:
1.用戶通過掃二維碼,向我們支付。
2.系統檢測到json中有一筆新交易。
3.根據用戶支付餘額,系統自動更改數據庫中的用戶的餘額、或者提供商品給用戶。

下面我們來獲取最新訂單號:

public function DataContrast($fee, $time, $Minute = 3)
    {
        // TODO: Implement DataContrast() method.
        if (isset($this->json['result']['detail']) && is_array($this->json['result']['detail']))
            foreach ($this->json['result']['detail'] as $item) {
                if ($this->url && $item['tradeFrom'] == '外部商戶' && $item['direction'] == '賣出' &&
                    strtotime($item['gmtCreate']) > $time - $Minute * 60 && strtotime($item['gmtCreate']) < $time &&
                    $item['totalAmount'] == $fee) {
                    return $item['tradeNo'];
                }
                if (!$this->url && $item['signProduct'] == '轉賬收款碼' && $item['accountType'] == '交易' &&
                    strtotime($item['tradeTime']) > $time - $Minute * 60 && strtotime($item['tradeTime']) < $time &&
                    $item['tradeAmount'] == $fee) {
                    return $item['tradeNo'];
                }
            }
        return false;
    }

總結

支付寶的方案我已經大致的給了岀來,具體則麼操作還是需要自己實現的,微信支付的實現方案基本上雷同。
關於免接口支付的優缺點:

  • 優點:
    1.省錢:支付寶接口每筆流量都需要費用,自己做接口省。
    2.安全可靠:自己實現代碼,不存在後臺,免於其他第三方小支付平臺的掣肘。
    3.無限制:不管你是違規應用如SSC,還是正規應用都可使用這個支付。

  • 缺點:

  1. 費心:cookie會失效,我們需要定期更新cookie.
  2. 支付:大批量用戶同時付款處理比較複雜,價格會有略微差異。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章