设置Tinkoff Bank API。 你的直觉如何……? 或关于Oauth 2.0的歌曲

几周前,在一个项目中,出现了将CRM与Tinkoff Bank API集成的问题。 特别是有关获取银行帐户对帐单的信息。

该资产具有:

  1. openapi.tinkoff.ru
  2. 电话支持服务(由于技术支持部门很忙-挽救溺水者的工作,溺水者的手工)
  3. Google文档: 24386_policy.pdf (里面带有俄语字母,带有令人愉悦的表情,巫术转向,虽然不合适,但仍然是一件事情……)

在google框的过程中,还发现有评论认为设置Tinkoff Bank API非常有趣且不容易(请参阅banki.ru上的文章“ Tinkoff API-我们对此太愚蠢” )。

是的,我不得不稍作修改,因此,为节省研讨会中其他同志的时间,本文是写的。

我注意到,Tinkoff Bank API使用Oauth 2.0进行授权。

为什么我们需要openapi.tinkoff.ru?

  1. 测试(见下文);
  2. 为了猜测什么是什么以及如何 没有直接的夹板; 我们以直觉的水平工作!...

让我们开始吧。 在“ SSO授权”部分中,单击“如何/隐藏”,然后单击/安全/令牌#refresh-token(“通过刷新令牌发行令牌”),选择Grant_type作为参数,然后在refresh_token字段中(可以在用户帐户)。 点击按钮“尝试一下!” 这些操作的结果是获得了诸如access_token这样的重要信息 (即openapi.tinkoff.ru 演示了接收它可能性)。

接下来,查看“帐户和付款”部分,单击/合伙人/公司/ {INN} /摘录(“声明收据”)。 我们研究了获取参数所需的参数:授权,INN,accountNumber,从头到尾。

授权-我们猜想授权只不过是我们在“ SSO授权”部分中收到的access_token;
INN-我们为其配置API的组织的INN;
从-哪一天开始(放电时间);
直到-哪一天(放电时间)。

因此(我们看一下Oauth 2.0资料), 获取语​​句数据的过程分为两个阶段-首先获得access_token,然后手边具有access_token,我们获得该语句的数据 。 太好了 算法很清楚,我们编写了代码(出于明显的原因,下面的代码中的$ user,$ pass,$ refresh_token,$ inn,$ accountNumber的代码值中的访问参数已更改)。

创建以下文件:

  1. 第一个设置文件是StartSettings.php
  2. 第二个启动文件是Start.php
  3. 用于向API发布数据或从API解析数据的第三个文件是TinkoffInsertData.php ; 我们使用CURL(php)。
  4. 空的数据库转储,可以在其中上载语句的数据: bank.sql ; MySQL数据库(我们通过PDO将数据发送到数据库)。

因此,请看一下代码并对其进行注释!

设置文件-StartSettings.php:

$host = '127.0.0.1'; $db = 'bank'; $user = 'root'; $pass = ''; $charset = 'utf8'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $pdo = new PDO($dsn, $user, $pass, $opt); $user="IKu0jn98kllkI90kklii"; //20  $pass="ds4234SDFsdfsdijoijslkkdjfoIOi"; //30  $refresh_token='dsfh345kljlkjsdf098sdfkljklj098sdfkklKKLjhjihiKL90909llkrre5345dfFDDFretertERTERETfdgd==';// 88  $inn = '750151513135'; $accountNumber = '40802810300000121212';//20  $from_year = '1980'; $from_month = '01'; $from_day = '01'; $till_year = date('Y'); $till_month = date('m'); $till_day = date('d'); 

起始文件-Start.php:

 session_start(); error_reporting(E_ALL); include 'StartSettings.php'; include 'TinkoffInsertData.php'; TinkoffInsertData($user,$pass,$refresh_token, $inn, $accountNumber, $from_year, $from_month, $from_day, $till_year, $till_month, $till_day, $pdo); $stmt = $pdo->prepare("INSERT INTO `bank`.`dateofwork` (dateofwork) VALUES (NOW())"); $stmt->execute(); 

用于向API发布数据或从API解析数据的文件-TinkoffInsertData.php:

 function TinkoffInsertData($user,$pass,$refresh_token, $inn, $accountNumber, $from_year, $from_month, $from_day, $till_year, $till_month, $till_day, $pdo){ //  -    access_token $from_date = $from_year."-".$from_month."-".$from_day.'%2B03%3A00%3A00'; $till_date = $till_year."-".$till_month."-".$till_day.'%2B03%3A00%3A00'; $params=['grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token ]; $headers = [ 'POST /secure/token HTTP/1.1', 'Content-Type: application/x-www-form-urlencoded' ]; $curlURL='https://sso.tinkoff.ru/secure/token'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$curlURL); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_VERBOSE, true); $curl_res = curl_exec($ch); if($curl_res) { $server_output = json_decode($curl_res); } // access_token -     2  $access_token_pos_start = strpos ($curl_res, 'access_token', 0); $access_token_pos_start = $access_token_pos_start + 15; $token_type_pos_start = strpos ($curl_res, "token_type", 0); $access_token = mb_substr($curl_res, $access_token_pos_start, ($token_type_pos_start-$access_token_pos_start-3)); //!....   ..... // ,    sleep,        //sleep(1); //  -     $params=[ 'Authorization'=>$access_token, 'INN'=>$inn, 'accountNumber'=>$accountNumber ]; $headers = [ 'Authorization: Bearer '.$access_token ]; $curlURL='https://sme-partner.tinkoff.ru/api/v1/partner/company/'.$inn.'/excerpt?accountNumber='.$accountNumber.'&from='.$from_date.'&till='.$till_date; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$curlURL); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_VERBOSE, true); $curl_res = curl_exec($ch); if($curl_res) { $server_output = json_decode($curl_res); } $IE_Edge_pos_start = strpos ($curl_res, 'IE=Edge', 0); $IE_Edge_pos_start = $IE_Edge_pos_start + 7; $tinkoff_json = mb_substr($curl_res, $IE_Edge_pos_start); $tinkoff_json = trim($tinkoff_json); $tinkoff_json = json_decode($tinkoff_json); //     ,  ;) foreach ($tinkoff_json as $k=>$v){ if($k=='accountNumber'){ if(!($v==$accountNumber)) die('not that accountNumber'); } } //$tinkoff_array -    json   foreach ($tinkoff_json as $k=>$v){ if($k=='operation'){ $i=0; foreach ($v as $t=>$s){ foreach ($s as $e=>$f){ $tinkoff_array[$i][$e]=$f; } $i++; } } } //   $tinkoff_array    for ($i=0;$i<count($tinkoff_array);$i++){ $temp_id = $pdo->query("SELECT count(*) FROM `justtin`.`tinkoff` WHERE id=".$tinkoff_array[$i]['id'].";")->fetchColumn(); if ($temp_id==0){ if (Get_highly_likely_is_number_bill($tinkoff_array[$i]['paymentPurpose'])!=""){ $stmt = $pdo->prepare("INSERT INTO `justtin`.`tinkoff` (id, date, amount, drawDate, payerName, payerInn, payerAccount, payerCorrAccount, payerBic, payerBank, chargeDate, recipient, recipientInn, recipientAccount, recipientCorrAccount, recipientBic, recipientBank, operationType, uin, paymentPurpose, creatorStatus, payerKpp, executionOrder, date_of_save) VALUES (:id, :date, :amount, :drawDate, :payerName, :payerInn, :payerAccount, :payerCorrAccount, :payerBic, :payerBank, :chargeDate, :recipient, :recipientInn, :recipientAccount, :recipientCorrAccount, :recipientBic, :recipientBank, :operationType, :uin, :paymentPurpose, :creatorStatus, :payerKpp, :executionOrder, NOW())"); $stmt->bindParam(':id', $tinkoff_array[$i]['id']); $stmt->bindParam(':date', $tinkoff_array[$i]['date']); $stmt->bindParam(':amount', $tinkoff_array[$i]['amount']); $stmt->bindParam(':drawDate', $tinkoff_array[$i]['drawDate']); $stmt->bindParam(':payerName', $tinkoff_array[$i]['payerName']); $stmt->bindParam(':payerInn', $tinkoff_array[$i]['payerInn']); $stmt->bindParam(':payerAccount', $tinkoff_array[$i]['payerAccount']); $stmt->bindParam(':payerCorrAccount', $tinkoff_array[$i]['payerCorrAccount']); $stmt->bindParam(':payerBic', $tinkoff_array[$i]['payerBic']); $stmt->bindParam(':payerBank', $tinkoff_array[$i]['payerBank']); $stmt->bindParam(':chargeDate', $tinkoff_array[$i]['chargeDate']); $stmt->bindParam(':recipient', $tinkoff_array[$i]['recipient']); $stmt->bindParam(':recipientInn', $tinkoff_array[$i]['recipientInn']); $stmt->bindParam(':recipientAccount', $tinkoff_array[$i]['recipientAccount']); $stmt->bindParam(':recipientCorrAccount', $tinkoff_array[$i]['recipientCorrAccount']); $stmt->bindParam(':recipientBic', $tinkoff_array[$i]['recipientBic']); $stmt->bindParam(':recipientBank', $tinkoff_array[$i]['recipientBank']); $stmt->bindParam(':operationType', $tinkoff_array[$i]['operationType']); $stmt->bindParam(':uin', $tinkoff_array[$i]['uin']); $stmt->bindParam(':paymentPurpose', $tinkoff_array[$i]['paymentPurpose']); $stmt->bindParam(':creatorStatus', $tinkoff_array[$i]['creatorStatus']); $stmt->bindParam(':payerKpp', $tinkoff_array[$i]['payerKpp']); $stmt->bindParam(':executionOrder', $tinkoff_array[$i]['executionOrder']); $stmt->execute(); } } } } 

读者:我希望本文能帮助您通过网络服务和客户服务获利。 愿力量与您同在!

Tinkoff Bank技术支持人员:希望本文能减轻您的负担! 祝你好运

Source: https://habr.com/ru/post/zh-CN431908/


All Articles