Tinkoff Bank APIのセットアップ。 あなたの直観はどうですか....? またはOauth 2.0に関する歌

数週間前、プロジェクトの1つで、CRMをTinkoff Bank APIと統合するという疑問が生じました。 特に、銀行口座の明細書を取得することについてでした。



資産には次のものがありました。



  1. openapi.tinkoff.ru
  2. 電話サポートサービス(技術サポート部門の忙しさから、dr死者を救う仕事、dr死者自身の手仕事)。
  3. Googled文書: 24386_policy.pdf (ロシア語の文字、面白い表現、魔術の回転、不適切、しかしそれにもかかわらず、...)


Googleアカウントの過程で、Tinkoff Bank APIのセットアップは非常に楽しく、簡単ではないというフィードバックも見つかりました(banki.ru 「Tinkoff API- これにはバカすぎます」に関する記事を参照)。



はい、私はワークショップの他の仲間の時間を節約するために、少しいじくり回さなければなりませんでした、この記事は書かれました。



Tinkoff Bank APIは認証にOauth 2.0を使用していることに注意してください。



なぜopenapi.tinkoff.ruが必要なのですか?



  1. テスト用(以下を参照)。
  2. 何が何で、どのように推測するため。 直接の添え木はありません。 私たちは直感のレベルで働いています!...


始めましょう。 「SSO承認」セクションで、「方法/非表示」をクリックし、次に/ secure / token#refresh-token(「更新トークンによるトークンの発行」)をクリックし、パラメータとしてgrant_typeを選択し、refresh_tokenフィールドで(取得できます)ユーザーアカウント)。 「Try it out!」ボタンをクリックしてください。これらのアクションの結果は、access_tokenなどの重要なものを取得することです (つまり、openapi.tinkoff.ru それを受け取る可能性を示します)。



次に、「アカウントと支払い」セクションを見て、/パートナー/会社/ {INN} /抜粋(「明細書の受領」)をクリックします。 取得に必要なパラメーターは、Authorization、INN、accountNumber、from、tillです。



認可-認可はaccess_tokenに過ぎないと推測します。これは、「SSO認可」セクションで受け取ったものです。

INN-APIを構成する組織のINN。

from-開始日(放電期間);

まで-何日(放電期間)。



したがって(Oauth 2.0マテリアルを見る)、 ステートメントデータの受信は2段階で行われます。最初に access_token を取得し、次に access_token を取得して、このステートメントのデータを取得します 。 素晴らしい。 アルゴリズムは明確で、コードを記述します($ user、$ pass、$ refresh_token、$ inn、$ accountNumberのコード値のアクセスパラメータ-明らかな理由により、以下のコードで変更されています)。



次のファイルを作成します。



  1. 最初の設定ファイルはStartSettings.phpです
  2. 2番目の開始ファイルはStart.phpです
  3. APIへ/からデータを投稿/解析するための3番目のファイルは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へ/からデータを投稿/解析するためのファイル-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(); } } } }
      
      





読者:この資料が、Webサービスと顧客のサービスの収益化に役立つことを願っています。 力があなたと共に来るように!



Tinkoff Bankの技術サポート担当者:この記事があなたの負担を減らすことを願っています! 頑張って



All Articles