denverkurt

class AP_Elcard

Feb 4th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.11 KB | None | 0 0
  1. <?php
  2. if(!class_exists('AP_Elcard')){
  3. class AP_Elcard {
  4.    
  5.     private $api_url = '';
  6.     private $token = '';
  7.     private $login = '';
  8.     private $password = '';
  9.     private $terminal = '';
  10.     public $balance = '';
  11.     private $test = 0;
  12.    
  13.     function __construct($login, $password, $terminal, $test_mode = 0)
  14.     {
  15.         //echo "AP_Elcard start<br />\n";
  16.         $this->login = trim($login);
  17.         $this->password = trim($password);
  18.         $this->terminal = trim($terminal);
  19.         $this->test = intval($test_mode);
  20.        
  21.         $this->replace_token();
  22.     }
  23.    
  24.     function replace_token(){
  25.        
  26.         $post = array(
  27.             'login'=> $this->login,
  28.             'password'=> $this->password,
  29.             'terminal'=> $this->terminal
  30.         );
  31.  
  32.  
  33.         $headers = array(
  34.             'Accept: application/json',
  35.             'Content-Type: application/json',
  36.             'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
  37.         );
  38.        
  39.         $result = $this->request('user/login', $headers, $post);
  40.         if (array($result) AND $result['code'] == 200 AND $output = json_decode($result['output'], true) AND json_last_error() === JSON_ERROR_NONE AND isset($output['keyToken'])){
  41.             $this->token = $output['keyToken'];
  42.         }
  43.         //$this->balance = $this->get_balans()['balance'];
  44.     }
  45.    
  46.     public function get_history($where=0, $account) {
  47.                          
  48.         $where = intval($where); //0-входящие, 1-отправленные
  49.                          
  50.         $data = array();
  51.         $data['error'] = 1;
  52.         $data['trans'] = array();
  53.        
  54.         $headers = array(
  55.             'Accept: application/json',
  56.             'Authorization: '. $this->token,
  57.             'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
  58.         );
  59.        
  60.         $result = $this->request('operation/history', $headers);
  61.         if (array($result) AND $result['code'] == 200 AND $output = json_decode($result['output'], true) AND json_last_error() === JSON_ERROR_NONE AND is_array($output)){
  62.             $data['error'] = 0;
  63.             foreach($output as $res){
  64.                 if(isset($res['comment'])){
  65.                     if($res['fields']['value'] == $account and $where == 0 or $res['fields']['value'] == $account and $where == 1){
  66.                        
  67.                         $comment = $res['comment'];
  68.                        
  69.                         $trans_id = 0;
  70.                         if(preg_match('/gm\((.*?)\)/is', $comment, $item)){
  71.                             if(isset($item[1])){
  72.                                 $trans_id = trim($item[1]);
  73.                             }
  74.                             $trans_id = intval($trans_id);
  75.                         }
  76.                        
  77.                         if($trans_id){
  78.                             $data['trans'][$trans_id] = array(
  79.                                 'date' => $res['timestamp'],
  80.                                 'amount' => $res['amount'] / 100,
  81.                                 'comment' => $res['comment'],
  82.                                 'article' => $res['article'],
  83.                                 'receiver' => $res['fields']['value'],
  84.                                 'trans_id' => $res['id'],
  85.                             );
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.        
  92.         return $data;
  93.     }
  94.    
  95.     public function send_money($destination, $amount, $comment='', $article){
  96.        
  97.         $comment = trim($comment);
  98.        
  99.         $data = array();
  100.         $data['error'] = 1;
  101.         $data['trans_id'] = 0;
  102.        
  103.         if (!$trans_id = $this->get_trans_id()) {// генерация номера транзакции
  104.             return $data;
  105.         }
  106.        
  107.         $post = array(
  108.             'article' => (int)$article,
  109.             'amount' => (int)($amount*100),
  110.             "commission" => 0,
  111.             'fields' => array(
  112.                 (object)array(
  113.                     'id' => 1,
  114.                     'value' => $destination
  115.                 )
  116.             ),
  117.             "extTerminal" => "1234",
  118.             "extTerminalIP" => "",
  119.             'comment' => $comment
  120.         );
  121.    
  122.         $headers = array(
  123.             'Accept: application/json',
  124.             'Content-Type: application/json',
  125.             'Authorization: ' . $this->token,
  126.             'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
  127.         );
  128.        
  129.         $result = $this->request('operation/replenishment/'.$trans_id, $headers, $post);// создание транзакции
  130.        
  131.         if (is_array($result) AND $result['code'] == 204){
  132.        
  133.             $post=array(
  134.                 'operationDate'=> date("c")//'2000-01-23T04:56:07.000+00:00'
  135.             );
  136.            
  137.             $output_code = 0;
  138.            
  139.             do {// старт цикла проверки статуса
  140.                 if ($output_code == 923) {
  141.                     sleep(15);
  142.                 }
  143.                 $result = $this->request_put('operation/replenishment/'.$trans_id, $headers, $post);// потверждение транзакции
  144.  
  145.                 if(is_array($result) AND $result['code'] == 200 AND $output = json_decode($result['output'], true) AND json_last_error() === JSON_ERROR_NONE AND is_array($output)){
  146.                     if ($output['code'] === 0) {
  147.                         $data['error'] = 0;
  148.                         $data['trans_id'] = $trans_id;
  149.                     } else {
  150.                         $this->last_error = $output['description'][0]['value'];
  151.                     }
  152.                     $output_code = $output['code'];
  153.                 }
  154.             } while($output_code == 923);// конец цикла проверки статуса
  155.            
  156.         } else {// если не 204
  157.             if ($output = json_decode($result['output'], true) AND json_last_error() === JSON_ERROR_NONE AND is_array($output)) {
  158.                 if ($output['code'] !== 0 && isset($output['description'])) {
  159.                     $this->last_error = $output['description'][1]['value'];
  160.                 }
  161.             }
  162.         }
  163.  
  164.         return $data;
  165.     }
  166.    
  167.     function get_balans(){
  168.         $data = array();
  169.        
  170.         $headers = array(
  171.             'Accept: application/json',
  172.             'Authorization: '. $this->token,
  173.             'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
  174.         );
  175.        
  176.         $result = $this->request('user/balance', $headers);
  177.  
  178.         if(is_array($result) AND $result['code'] == 200 AND $output = json_decode($result['output'], true) AND json_last_error() === JSON_ERROR_NONE AND isset($output['balance'])){
  179.             $balance = $output['balance'];
  180.             if($balance > 0){
  181.                 $data['balance'] = $balance / 100;
  182.             } else {
  183.                 $data['balance'] = 0;
  184.             }
  185.             return $data;
  186.         }
  187.        
  188.         return false;
  189.     }
  190.  
  191.     function get_trans_id(){
  192.  
  193.         $headers = array(
  194.             'Accept: application/json',
  195.             'Authorization: '. $this->token,
  196.             'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
  197.         );
  198.  
  199.         $result = $this->request('operation/transId', $headers);
  200.  
  201.         if(is_array($result) AND $result['code'] == 200 AND $output = json_decode($result['output'], true) AND json_last_error() === JSON_ERROR_NONE AND isset($output['transId'])){
  202.             return $output['transId'];
  203.         }
  204.         return false;
  205.     }
  206.  
  207.     function request($method, $headers='', $post=''){
  208.        
  209.         $server_url = rtrim($this->api_url, '/').'/' . $method;
  210.        
  211.         $c_options = array(
  212.             CURLOPT_FOLLOWLOCATION => 0,
  213.             CURLOPT_HEADER => 0,
  214.             CURLOPT_HTTP200ALIASES => array(200),
  215.             CURLOPT_HTTPHEADER => $headers,
  216.             CURLOPT_TIMEOUT => 10
  217.         );
  218.        
  219.         if(is_array($post)){
  220.             $c_options[CURLOPT_POST] = 1;
  221.             $c_options[CURLOPT_POSTFIELDS] = json_encode($post);
  222.             $c_options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36';
  223.         } else {
  224.             $c_options[CURLOPT_HTTPGET] = 1;
  225.         }
  226.        
  227.         $c_result = get_curl_parser($server_url, $c_options, 'autopay', 'elpay');
  228.         $this->last_result = $c_result;
  229.        
  230.         if($this->test == 1) {
  231.             echo "request <br />\n";
  232.             print_r($server_url." <br />\n");
  233.             print_r($c_options);
  234.             print_r($c_result);
  235.             echo "<hr />\n";
  236.         }
  237.        
  238.         return $c_result;
  239.     }
  240.     function request_put($method, $headers='', $post=''){
  241.  
  242.         $server_url = rtrim($this->api_url, '/').'/' . $method;
  243.  
  244.         $c_options = array(
  245.             CURLOPT_FOLLOWLOCATION => 0,
  246.             CURLOPT_HEADER => 0,
  247.             CURLOPT_HTTP200ALIASES => array(200),
  248.             CURLOPT_HTTPHEADER => $headers,
  249.             CURLOPT_TIMEOUT => 30
  250.         );
  251.  
  252.         if(is_array($post)){
  253.             $c_options[CURLOPT_POST] = 1;
  254.             $c_options[CURLOPT_CUSTOMREQUEST] = 'PUT';
  255.             $c_options[CURLOPT_POSTFIELDS] = json_encode($post);
  256.             $c_options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36';
  257.         } else {
  258.             $c_options[CURLOPT_HTTPGET] = 1;
  259.         }
  260.  
  261.         $c_result = get_curl_parser($server_url, $c_options, 'autopay', 'elpay');
  262.         $this->last_result = $c_result;
  263.  
  264.         if($this->test == 1) {
  265.             echo "request_put <br />\n";
  266.             print_r($server_url." <br />\n");
  267.             print_r($c_options);
  268.             print_r($c_result);
  269.             echo "<hr />\n";
  270.         }
  271.        
  272.         return $c_result;
  273.     }
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment