|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sipay; |
| 4 | + |
| 5 | +class Ecommerce |
| 6 | +{ |
| 7 | + protected $logger; |
| 8 | + protected $key; |
| 9 | + protected $secret; |
| 10 | + protected $resource; |
| 11 | + protected $environment; |
| 12 | + protected $version; |
| 13 | + protected $mode; |
| 14 | + protected $timeout; |
| 15 | + |
| 16 | + public function __construct($config_path) |
| 17 | + { |
| 18 | + if (gettype($config_path) != "string") { |
| 19 | + throw new \Exception('$config_path must be a string.'); |
| 20 | + } |
| 21 | + |
| 22 | + $config = parse_ini_file($config_path, true); |
| 23 | + |
| 24 | + $this->logger = new Logger($config['logger']); |
| 25 | + |
| 26 | + $cred = $config['credentials']; |
| 27 | + $this->setKey(isset($cred['key']) ? $cred['key'] : ''); |
| 28 | + $this->setSecret(isset($cred['secret']) ? $cred['secret'] : ''); |
| 29 | + $this->setResource(isset($cred['resource']) ? $cred['resource'] : ''); |
| 30 | + |
| 31 | + $api = $config['api']; |
| 32 | + $this->setEnvironment(isset($api['environment']) ? $api['environment'] : ''); |
| 33 | + $this->setVersion(isset($api['version']) ? $api['version'] : ''); |
| 34 | + $this->setMode(isset($api['mode']) ? $api['mode'] : ''); |
| 35 | + |
| 36 | + $connection = $config['connection']; |
| 37 | + $this->setTimeout(isset($connection['timeout']) ? $connection['timeout'] : '30'); |
| 38 | + } |
| 39 | + |
| 40 | + public function getKey() |
| 41 | + { |
| 42 | + return $this->key; |
| 43 | + } |
| 44 | + |
| 45 | + public function setKey($key) |
| 46 | + { |
| 47 | + if (gettype($key) != "string") { |
| 48 | + throw new \Exception('$key must be a string.'); |
| 49 | + } |
| 50 | + if(!preg_match("/^[\w-]{6,32}$/", $key)) { |
| 51 | + throw new \Exception('$key don\'t match with pattern.'); |
| 52 | + } |
| 53 | + $this->key = $key; |
| 54 | + } |
| 55 | + |
| 56 | + public function getSecret() |
| 57 | + { |
| 58 | + return $this->secret; |
| 59 | + } |
| 60 | + |
| 61 | + public function setSecret($secret) |
| 62 | + { |
| 63 | + if (gettype($secret) != "string") { |
| 64 | + throw new \Exception('$secret must be a string.'); |
| 65 | + } |
| 66 | + if(!preg_match("/^[\w-]{6,32}$/", $secret)) { |
| 67 | + throw new \Exception('$secret don\'t match with pattern.'); |
| 68 | + } |
| 69 | + $this->secret = $secret; |
| 70 | + } |
| 71 | + |
| 72 | + public function getResource() |
| 73 | + { |
| 74 | + return $this->resource; |
| 75 | + } |
| 76 | + |
| 77 | + public function setResource($resource) |
| 78 | + { |
| 79 | + if (gettype($resource) != "string") { |
| 80 | + throw new \Exception('$resource must be a string.'); |
| 81 | + } |
| 82 | + if(!preg_match("/^[\w-]{6,32}$/", $resource)) { |
| 83 | + throw new \Exception('$resource don\'t match with pattern.'); |
| 84 | + } |
| 85 | + $this->resource = $resource; |
| 86 | + } |
| 87 | + |
| 88 | + public function getEnvironment() |
| 89 | + { |
| 90 | + return $this->environment; |
| 91 | + } |
| 92 | + |
| 93 | + public function setEnvironment($environment) |
| 94 | + { |
| 95 | + if (gettype($environment) != "string") { |
| 96 | + throw new \Exception('$environment must be a string.'); |
| 97 | + } |
| 98 | + if(!in_array($environment, array('develop','sandbox', 'staging', 'live'))) { |
| 99 | + throw new \Exception('$environment must be sandbox, staging or live.'); |
| 100 | + } |
| 101 | + $this->environment = $environment; |
| 102 | + } |
| 103 | + |
| 104 | + public function getVersion() |
| 105 | + { |
| 106 | + return $this->version; |
| 107 | + } |
| 108 | + |
| 109 | + public function setVersion($version) |
| 110 | + { |
| 111 | + if (gettype($version) != "string") { |
| 112 | + throw new \Exception('$version must be a string.'); |
| 113 | + } |
| 114 | + if($version != "v1") { |
| 115 | + throw new \Exception('$version must be v1.'); |
| 116 | + } |
| 117 | + $this->version = $version; |
| 118 | + } |
| 119 | + |
| 120 | + public function getMode() |
| 121 | + { |
| 122 | + return $this->mode; |
| 123 | + } |
| 124 | + |
| 125 | + public function setMode($mode) |
| 126 | + { |
| 127 | + if (gettype($mode) != "string") { |
| 128 | + throw new \Exception('$mode must be a string.'); |
| 129 | + } |
| 130 | + if(!in_array($mode, array('sha256','sha512'))) { |
| 131 | + throw new \Exception('$mode must be sha256 or sha512.'); |
| 132 | + } |
| 133 | + $this->mode = $mode; |
| 134 | + } |
| 135 | + |
| 136 | + public function getTimeout() |
| 137 | + { |
| 138 | + return $this->timeout; |
| 139 | + } |
| 140 | + |
| 141 | + public function setTimeout($timeout) |
| 142 | + { |
| 143 | + if (!is_numeric($timeout)) { |
| 144 | + throw new \Exception('$timeout must be a integer.'); |
| 145 | + } |
| 146 | + $timeout = intval($timeout); |
| 147 | + if($timeout <= 0) { |
| 148 | + throw new \Exception('$timeout must be geater than 0.'); |
| 149 | + } |
| 150 | + $this->timeout = $timeout; |
| 151 | + } |
| 152 | + |
| 153 | + private function send($payload, $endpoint) |
| 154 | + { |
| 155 | + $url = 'https://'.$this->environment.'.sipay.es/mdwr/'.$this->version.'/'.$endpoint; |
| 156 | + $data = array( |
| 157 | + 'key' => $this->key, |
| 158 | + 'nonce' => "".time(), |
| 159 | + 'mode' => $this->mode, |
| 160 | + 'resource' => $this->resource, |
| 161 | + 'payload' => $payload |
| 162 | + ); |
| 163 | + |
| 164 | + $body = json_encode($data); |
| 165 | + $signature = hash_hmac($this->mode, $body, $this->secret); |
| 166 | + |
| 167 | + $options = array( |
| 168 | + 'http' => array( |
| 169 | + 'header' => "Content-type: application/json\r\nContent-Signature: ".$signature."\r\n", |
| 170 | + 'method' => 'POST', |
| 171 | + 'content' => $body, |
| 172 | + 'timeout' => $this->timeout, |
| 173 | + 'ignore_errors' => true |
| 174 | + ), |
| 175 | + ); |
| 176 | + $context = stream_context_create($options); |
| 177 | + try { |
| 178 | + $response_body = file_get_contents($url, false, $context); |
| 179 | + } catch (Exception $e) { |
| 180 | + $this->logger->error( |
| 181 | + 'sipay.request', 'request.response', 'E-0004', 'Request Error', |
| 182 | + array('error_msg' => $e->getMessage()) |
| 183 | + ); |
| 184 | + $response = null; |
| 185 | + $response_body = null; |
| 186 | + } |
| 187 | + |
| 188 | + if(!is_null($response_body)) { |
| 189 | + if(isset($http_response_header)) { |
| 190 | + if($response_body == "") { |
| 191 | + $this->logger->error('sipay.request', 'request.response', 'E-0001', 'Request Error', array()); |
| 192 | + $response = null; |
| 193 | + |
| 194 | + }else{ |
| 195 | + $is_json = false; |
| 196 | + foreach($http_response_header as $header){ |
| 197 | + if(substr($header, 0, 30) === "Content-Type: application/json") { |
| 198 | + $is_json = true; |
| 199 | + break; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + if($is_json) { |
| 204 | + $response = json_decode($response_body, true); |
| 205 | + |
| 206 | + }else{ |
| 207 | + $this->logger->error('sipay.request', 'request.response', 'E-0003', 'Response no json', array()); |
| 208 | + $response = null; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + }else{ |
| 213 | + $this->logger->error('sipay.request', 'request.response', 'E-0002', 'Request Error', array()); |
| 214 | + $response = null; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + return array($body, $response); |
| 219 | + } |
| 220 | +} |
0 commit comments