Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(!class_exists('AP_Elcard')){
- class AP_Elcard {
- private $api_url = '';
- private $token = '';
- private $login = '';
- private $password = '';
- private $terminal = '';
- public $balance = '';
- private $test = 0;
- function __construct($login, $password, $terminal, $test_mode = 0)
- {
- //echo "AP_Elcard start<br />\n";
- $this->login = trim($login);
- $this->password = trim($password);
- $this->terminal = trim($terminal);
- $this->test = intval($test_mode);
- $this->replace_token();
- }
- function replace_token(){
- $post = array(
- 'login'=> $this->login,
- 'password'=> $this->password,
- 'terminal'=> $this->terminal
- );
- $headers = array(
- 'Accept: application/json',
- 'Content-Type: application/json',
- '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',
- );
- $result = $this->request('user/login', $headers, $post);
- 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'])){
- $this->token = $output['keyToken'];
- }
- //$this->balance = $this->get_balans()['balance'];
- }
- public function get_history($where=0, $account) {
- $where = intval($where); //0-входящие, 1-отправленные
- $data = array();
- $data['error'] = 1;
- $data['trans'] = array();
- $headers = array(
- 'Accept: application/json',
- 'Authorization: '. $this->token,
- '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',
- );
- $result = $this->request('operation/history', $headers);
- 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)){
- $data['error'] = 0;
- foreach($output as $res){
- if(isset($res['comment'])){
- if($res['fields']['value'] == $account and $where == 0 or $res['fields']['value'] == $account and $where == 1){
- $comment = $res['comment'];
- $trans_id = 0;
- if(preg_match('/gm\((.*?)\)/is', $comment, $item)){
- if(isset($item[1])){
- $trans_id = trim($item[1]);
- }
- $trans_id = intval($trans_id);
- }
- if($trans_id){
- $data['trans'][$trans_id] = array(
- 'date' => $res['timestamp'],
- 'amount' => $res['amount'] / 100,
- 'comment' => $res['comment'],
- 'article' => $res['article'],
- 'receiver' => $res['fields']['value'],
- 'trans_id' => $res['id'],
- );
- }
- }
- }
- }
- }
- return $data;
- }
- public function send_money($destination, $amount, $comment='', $article){
- $comment = trim($comment);
- $data = array();
- $data['error'] = 1;
- $data['trans_id'] = 0;
- if (!$trans_id = $this->get_trans_id()) {// генерация номера транзакции
- return $data;
- }
- $post = array(
- 'article' => (int)$article,
- 'amount' => (int)($amount*100),
- "commission" => 0,
- 'fields' => array(
- (object)array(
- 'id' => 1,
- 'value' => $destination
- )
- ),
- "extTerminal" => "1234",
- "extTerminalIP" => "",
- 'comment' => $comment
- );
- $headers = array(
- 'Accept: application/json',
- 'Content-Type: application/json',
- 'Authorization: ' . $this->token,
- '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'
- );
- $result = $this->request('operation/replenishment/'.$trans_id, $headers, $post);// создание транзакции
- if (is_array($result) AND $result['code'] == 204){
- $post=array(
- 'operationDate'=> date("c")//'2000-01-23T04:56:07.000+00:00'
- );
- $output_code = 0;
- do {// старт цикла проверки статуса
- if ($output_code == 923) {
- sleep(15);
- }
- $result = $this->request_put('operation/replenishment/'.$trans_id, $headers, $post);// потверждение транзакции
- 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)){
- if ($output['code'] === 0) {
- $data['error'] = 0;
- $data['trans_id'] = $trans_id;
- } else {
- $this->last_error = $output['description'][0]['value'];
- }
- $output_code = $output['code'];
- }
- } while($output_code == 923);// конец цикла проверки статуса
- } else {// если не 204
- if ($output = json_decode($result['output'], true) AND json_last_error() === JSON_ERROR_NONE AND is_array($output)) {
- if ($output['code'] !== 0 && isset($output['description'])) {
- $this->last_error = $output['description'][1]['value'];
- }
- }
- }
- return $data;
- }
- function get_balans(){
- $data = array();
- $headers = array(
- 'Accept: application/json',
- 'Authorization: '. $this->token,
- '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',
- );
- $result = $this->request('user/balance', $headers);
- 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'])){
- $balance = $output['balance'];
- if($balance > 0){
- $data['balance'] = $balance / 100;
- } else {
- $data['balance'] = 0;
- }
- return $data;
- }
- return false;
- }
- function get_trans_id(){
- $headers = array(
- 'Accept: application/json',
- 'Authorization: '. $this->token,
- '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',
- );
- $result = $this->request('operation/transId', $headers);
- 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'])){
- return $output['transId'];
- }
- return false;
- }
- function request($method, $headers='', $post=''){
- $server_url = rtrim($this->api_url, '/').'/' . $method;
- $c_options = array(
- CURLOPT_FOLLOWLOCATION => 0,
- CURLOPT_HEADER => 0,
- CURLOPT_HTTP200ALIASES => array(200),
- CURLOPT_HTTPHEADER => $headers,
- CURLOPT_TIMEOUT => 10
- );
- if(is_array($post)){
- $c_options[CURLOPT_POST] = 1;
- $c_options[CURLOPT_POSTFIELDS] = json_encode($post);
- $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';
- } else {
- $c_options[CURLOPT_HTTPGET] = 1;
- }
- $c_result = get_curl_parser($server_url, $c_options, 'autopay', 'elpay');
- $this->last_result = $c_result;
- if($this->test == 1) {
- echo "request <br />\n";
- print_r($server_url." <br />\n");
- print_r($c_options);
- print_r($c_result);
- echo "<hr />\n";
- }
- return $c_result;
- }
- function request_put($method, $headers='', $post=''){
- $server_url = rtrim($this->api_url, '/').'/' . $method;
- $c_options = array(
- CURLOPT_FOLLOWLOCATION => 0,
- CURLOPT_HEADER => 0,
- CURLOPT_HTTP200ALIASES => array(200),
- CURLOPT_HTTPHEADER => $headers,
- CURLOPT_TIMEOUT => 30
- );
- if(is_array($post)){
- $c_options[CURLOPT_POST] = 1;
- $c_options[CURLOPT_CUSTOMREQUEST] = 'PUT';
- $c_options[CURLOPT_POSTFIELDS] = json_encode($post);
- $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';
- } else {
- $c_options[CURLOPT_HTTPGET] = 1;
- }
- $c_result = get_curl_parser($server_url, $c_options, 'autopay', 'elpay');
- $this->last_result = $c_result;
- if($this->test == 1) {
- echo "request_put <br />\n";
- print_r($server_url." <br />\n");
- print_r($c_options);
- print_r($c_result);
- echo "<hr />\n";
- }
- return $c_result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment