|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Payneteasy |
| 4 | + * @copyright 2007-2026 Payneteasy |
| 5 | + * @license Property of Payneteasy |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Payneteasy\lib; |
| 9 | + |
| 10 | +defined('PAYNETEASY_LIB') or die('Restricted access'); |
| 11 | + |
| 12 | +include_once('ApiException.php'); |
| 13 | + |
| 14 | +class Api { |
| 15 | + private const URL = 'paynet/api/v2/'; |
| 16 | + private const USERAGENT = 'Payneteasy-Client/1.0'; |
| 17 | + |
| 18 | + private string $login; |
| 19 | + private string $gate; |
| 20 | + private string $control_key; |
| 21 | + private string $endpoint; |
| 22 | + private bool $is_direct; |
| 23 | + |
| 24 | + public function __construct(string $gate, string $login, string $control_key, string $endpoint, bool $is_direct) { |
| 25 | + $this->gate = $gate; |
| 26 | + $this->login = $login; |
| 27 | + $this->control_key = $control_key; |
| 28 | + $this->endpoint = $endpoint; |
| 29 | + $this->is_direct = $is_direct; |
| 30 | + } |
| 31 | + |
| 32 | + public static function trace(array $a1, array $a2=null): void { |
| 33 | + $prefix = isset($a2) ? ' -> ' : ' <- '; |
| 34 | + |
| 35 | + ksort($a1); |
| 36 | + foreach ($a1 as $key => $value) |
| 37 | + error_log("$prefix'$key' => '$value'"); |
| 38 | + |
| 39 | + if (isset($a2)) { |
| 40 | + error_log(''); |
| 41 | + |
| 42 | + ksort($a2); |
| 43 | + foreach ($a2 as $key => $value) |
| 44 | + error_log(" <- '$key' => '$value'"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static function fetch_github_version(string $repo=''): string { |
| 49 | + $Curl = curl_init($url = sprintf('https://api.github.com/repos/%s/releases/latest', $repo)); |
| 50 | + |
| 51 | + curl_setopt_array($Curl, [ CURLOPT_USERAGENT => self::USERAGENT, CURLOPT_RETURNTRANSFER => 1 ]); |
| 52 | + |
| 53 | + $response = curl_exec($Curl); |
| 54 | + |
| 55 | + if ($err = curl_errno($Curl)) |
| 56 | + $errmsg = "Github request error, CURL errno: $err"; |
| 57 | + elseif (($err = curl_getinfo($Curl, CURLINFO_HTTP_CODE)) != 200) |
| 58 | + $errmsg = "Github request error, HTTP code: '{$err}'"; |
| 59 | + |
| 60 | + if (!empty($errmsg)) |
| 61 | + throw new ApiException($errmsg); |
| 62 | + elseif (empty($response)) |
| 63 | + throw new ApiException('Github version response is empty'); |
| 64 | + |
| 65 | + curl_close($Curl); |
| 66 | + |
| 67 | + if (!preg_match('/(?:\d+\.)+\d+$/', ($tag = array_reverse(preg_split('/ +/', (json_decode($response, true)['name'])))[0]), $match)) |
| 68 | + throw new ApiException("Github version tag is malformed: '$tag'"); |
| 69 | + |
| 70 | + return $match[0]; |
| 71 | + } |
| 72 | + |
| 73 | + public function check_config_input(string $gate, string $sandbox, string $login, string $control_key, string $endpoint): array { |
| 74 | + return array_reduce([ |
| 75 | + [ 'Gateway URL', $gate, '|^https?://(?:\\w+(?:-\\w+)*\\.)+(?:\\w+(?:-\\w+)*)/|' ], |
| 76 | + [ 'Sandbox URL', $sandbox, '|^https?://(?:\\w+(?:-\\w+)*\\.)+\\w+/|' ], |
| 77 | + [ 'Login', $login, '/^[a-z][\\w-]*\\w$/i' ], |
| 78 | + [ 'Control key', $control_key, '/^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/i' ], |
| 79 | + [ 'End point Id', $endpoint, '/^\d+$/' ]], |
| 80 | + function($iter, $entry){ if (!preg_match($entry[2], $entry[1])) $iter[] = "{$entry[0]} is invalid"; return $iter; }, |
| 81 | + []); |
| 82 | + } |
| 83 | + |
| 84 | + public function is_configured(): bool |
| 85 | + { return $this->login && $this->control_key && $this->endpoint; } |
| 86 | + |
| 87 | + public function is_direct(): bool |
| 88 | + { return $this->is_direct; } |
| 89 | + |
| 90 | + public function sale(array $data): array |
| 91 | + { return $this->execute($this->is_direct ? 'sale' : 'sale-form', $this->signed($data)); } |
| 92 | + |
| 93 | + public function return(array $data): array |
| 94 | + { return $this->execute('return', $this->signed($data, null, true)); } |
| 95 | + |
| 96 | + public function status(array $data): array |
| 97 | + { return $this->execute('status', $this->signed($data, $this->login.$data['client_orderid'].$data['orderid'].$this->control_key)); } |
| 98 | + |
| 99 | + private function signed(array $data, string $str=null, bool $add_login=false): array { |
| 100 | + if (isset($str) || $add_login) |
| 101 | + $data['login'] = $this->login; |
| 102 | + |
| 103 | + $data['control'] = sha1($str ?? $this->endpoint.$data['client_orderid'].($data['amount'] * 100).$data['email'].$this->control_key); |
| 104 | + return $data; |
| 105 | + } |
| 106 | + |
| 107 | + private function execute(string $action, array $data): array { |
| 108 | + $Curl = curl_init($this->gate . self::URL . "$action/{$this->endpoint}"); |
| 109 | + |
| 110 | + curl_setopt_array($Curl, [ |
| 111 | + CURLOPT_HEADER => 0, |
| 112 | + CURLOPT_USERAGENT => self::USERAGENT, |
| 113 | + CURLOPT_SSL_VERIFYHOST => 0, |
| 114 | + CURLOPT_SSL_VERIFYPEER => 0, |
| 115 | + CURLOPT_POST => 1, |
| 116 | + CURLOPT_RETURNTRANSFER => 1, |
| 117 | + CURLOPT_POSTFIELDS => http_build_query($data) ]); |
| 118 | + |
| 119 | + $response = curl_exec($Curl); |
| 120 | + |
| 121 | + if ($err = curl_errno($Curl)) |
| 122 | + $errmsg = "Card processing error, CURL errno: '$err'"; |
| 123 | + elseif (($err = curl_getinfo($Curl, CURLINFO_HTTP_CODE)) != 200) |
| 124 | + $errmsg = "Card processing error, HTTP code: '$err'"; |
| 125 | + |
| 126 | + curl_close($Curl); |
| 127 | + |
| 128 | + if (!empty($errmsg)) |
| 129 | + throw new ApiException($errmsg); |
| 130 | + elseif (empty($response)) |
| 131 | + throw new ApiException('Card processing response is empty'); |
| 132 | + |
| 133 | + parse_str($response, $result); |
| 134 | + array_walk($result, fn(&$v) => $v = rtrim($v)); |
| 135 | + |
| 136 | + if ($result['type'] == 'validation-error') |
| 137 | + throw new ApiException("Card processing returned error: '{$result['error-message']}'", [ $data, $result ]); |
| 138 | + |
| 139 | + return $result; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +?> |
0 commit comments