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


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. 支付:大批量用户同时付款处理比较复杂,价格会有略微差异。
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值