@@ -36,8 +36,8 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | class Api |
| 38 | 38 | { |
| 39 | - protected $keyId; |
|
| 40 | - protected $publicKeyId; |
|
| 39 | + protected $keyId; |
|
| 40 | + protected $publicKeyId; |
|
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * Crear un nuevo objeto de Instapago. |
@@ -48,11 +48,11 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | public function __construct($keyId, $publicKeyId) |
| 50 | 50 | { |
| 51 | - if (empty($keyId) || empty($publicKeyId)) { |
|
| 52 | - throw new Exceptions\InstapagoException('Los parámetros "keyId" y "publicKeyId" son requeridos para procesar la petición.'); |
|
| 53 | - } |
|
| 54 | - $this->publicKeyId = $publicKeyId; |
|
| 55 | - $this->keyId = $keyId; |
|
| 51 | + if (empty($keyId) || empty($publicKeyId)) { |
|
| 52 | + throw new Exceptions\InstapagoException('Los parámetros "keyId" y "publicKeyId" son requeridos para procesar la petición.'); |
|
| 53 | + } |
|
| 54 | + $this->publicKeyId = $publicKeyId; |
|
| 55 | + $this->keyId = $keyId; |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | public function directPayment($fields) |
| 69 | 69 | { |
| 70 | - return $this->payment('2', $fields); |
|
| 70 | + return $this->payment('2', $fields); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | public function reservePayment($fields) |
| 84 | 84 | { |
| 85 | - return $this->payment('1', $fields); |
|
| 85 | + return $this->payment('1', $fields); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -98,26 +98,26 @@ discard block |
||
| 98 | 98 | */ |
| 99 | 99 | private function payment($type, $fields) |
| 100 | 100 | { |
| 101 | - (new Validator())->payment()->validate($fields); |
|
| 101 | + (new Validator())->payment()->validate($fields); |
|
| 102 | 102 | |
| 103 | - $fields = [ |
|
| 104 | - 'KeyID' => $this->keyId, |
|
| 105 | - 'PublicKeyId' => $this->publicKeyId, |
|
| 106 | - 'amount' => $fields['amount'], |
|
| 107 | - 'description' => $fields['description'], |
|
| 108 | - 'cardHolder' => $fields['card_holder'], |
|
| 109 | - 'cardHolderId' => $fields['card_holder_id'], |
|
| 110 | - 'cardNumber' => $fields['card_number'], |
|
| 111 | - 'cvc' => $fields['cvc'], |
|
| 112 | - 'expirationDate' => $fields['expiration'], |
|
| 113 | - 'statusId' => $type, |
|
| 114 | - 'IP' => $fields['ip'], |
|
| 115 | - ]; |
|
| 103 | + $fields = [ |
|
| 104 | + 'KeyID' => $this->keyId, |
|
| 105 | + 'PublicKeyId' => $this->publicKeyId, |
|
| 106 | + 'amount' => $fields['amount'], |
|
| 107 | + 'description' => $fields['description'], |
|
| 108 | + 'cardHolder' => $fields['card_holder'], |
|
| 109 | + 'cardHolderId' => $fields['card_holder_id'], |
|
| 110 | + 'cardNumber' => $fields['card_number'], |
|
| 111 | + 'cvc' => $fields['cvc'], |
|
| 112 | + 'expirationDate' => $fields['expiration'], |
|
| 113 | + 'statusId' => $type, |
|
| 114 | + 'IP' => $fields['ip'], |
|
| 115 | + ]; |
|
| 116 | 116 | |
| 117 | - $obj = $this->curlTransaccion('payment', $fields, 'POST'); |
|
| 118 | - $result = $this->checkResponseCode($obj); |
|
| 117 | + $obj = $this->curlTransaccion('payment', $fields, 'POST'); |
|
| 118 | + $result = $this->checkResponseCode($obj); |
|
| 119 | 119 | |
| 120 | - return $result; |
|
| 120 | + return $result; |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
@@ -134,18 +134,18 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | public function continuePayment($fields) |
| 136 | 136 | { |
| 137 | - (new Validator())->release()->validate($fields); |
|
| 138 | - $fields = [ |
|
| 139 | - 'KeyID' => $this->keyId, //required |
|
| 140 | - 'PublicKeyId' => $this->publicKeyId, //required |
|
| 141 | - 'id' => $fields['id'], //required |
|
| 142 | - 'amount' => $fields['amount'], //required |
|
| 143 | - ]; |
|
| 137 | + (new Validator())->release()->validate($fields); |
|
| 138 | + $fields = [ |
|
| 139 | + 'KeyID' => $this->keyId, //required |
|
| 140 | + 'PublicKeyId' => $this->publicKeyId, //required |
|
| 141 | + 'id' => $fields['id'], //required |
|
| 142 | + 'amount' => $fields['amount'], //required |
|
| 143 | + ]; |
|
| 144 | 144 | |
| 145 | - $obj = $this->curlTransaccion('complete', $fields, 'POST'); |
|
| 146 | - $result = $this->checkResponseCode($obj); |
|
| 145 | + $obj = $this->curlTransaccion('complete', $fields, 'POST'); |
|
| 146 | + $result = $this->checkResponseCode($obj); |
|
| 147 | 147 | |
| 148 | - return $result; |
|
| 148 | + return $result; |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | /** |
@@ -161,20 +161,20 @@ discard block |
||
| 161 | 161 | */ |
| 162 | 162 | public function query($id_pago) |
| 163 | 163 | { |
| 164 | - (new Validator())->query()->validate([ |
|
| 165 | - 'id' => $id_pago, |
|
| 166 | - ]); |
|
| 164 | + (new Validator())->query()->validate([ |
|
| 165 | + 'id' => $id_pago, |
|
| 166 | + ]); |
|
| 167 | 167 | |
| 168 | - $fields = [ |
|
| 169 | - 'KeyID' => $this->keyId, //required |
|
| 170 | - 'PublicKeyId' => $this->publicKeyId, //required |
|
| 171 | - 'id' => $id_pago, //required |
|
| 172 | - ]; |
|
| 168 | + $fields = [ |
|
| 169 | + 'KeyID' => $this->keyId, //required |
|
| 170 | + 'PublicKeyId' => $this->publicKeyId, //required |
|
| 171 | + 'id' => $id_pago, //required |
|
| 172 | + ]; |
|
| 173 | 173 | |
| 174 | - $obj = $this->curlTransaccion('payment', $fields, 'GET'); |
|
| 175 | - $result = $this->checkResponseCode($obj); |
|
| 174 | + $obj = $this->curlTransaccion('payment', $fields, 'GET'); |
|
| 175 | + $result = $this->checkResponseCode($obj); |
|
| 176 | 176 | |
| 177 | - return $result; |
|
| 177 | + return $result; |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
@@ -189,20 +189,20 @@ discard block |
||
| 189 | 189 | */ |
| 190 | 190 | public function cancel($id_pago) |
| 191 | 191 | { |
| 192 | - (new Validator())->query()->validate([ |
|
| 193 | - 'id' => $id_pago, |
|
| 194 | - ]); |
|
| 192 | + (new Validator())->query()->validate([ |
|
| 193 | + 'id' => $id_pago, |
|
| 194 | + ]); |
|
| 195 | 195 | |
| 196 | - $fields = [ |
|
| 197 | - 'KeyID' => $this->keyId, //required |
|
| 198 | - 'PublicKeyId' => $this->publicKeyId, //required |
|
| 199 | - 'id' => $id_pago, //required |
|
| 200 | - ]; |
|
| 196 | + $fields = [ |
|
| 197 | + 'KeyID' => $this->keyId, //required |
|
| 198 | + 'PublicKeyId' => $this->publicKeyId, //required |
|
| 199 | + 'id' => $id_pago, //required |
|
| 200 | + ]; |
|
| 201 | 201 | |
| 202 | - $obj = $this->curlTransaccion('payment', $fields, 'DELETE'); |
|
| 203 | - $result = $this->checkResponseCode($obj); |
|
| 202 | + $obj = $this->curlTransaccion('payment', $fields, 'DELETE'); |
|
| 203 | + $result = $this->checkResponseCode($obj); |
|
| 204 | 204 | |
| 205 | - return $result; |
|
| 205 | + return $result; |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /** |
@@ -217,27 +217,27 @@ discard block |
||
| 217 | 217 | */ |
| 218 | 218 | public function curlTransaccion($url, $fields, $method) |
| 219 | 219 | { |
| 220 | - $client = new Client([ |
|
| 221 | - 'base_uri' => 'https://api.instapago.com/', |
|
| 222 | - ]); |
|
| 220 | + $client = new Client([ |
|
| 221 | + 'base_uri' => 'https://api.instapago.com/', |
|
| 222 | + ]); |
|
| 223 | 223 | |
| 224 | - $args = []; |
|
| 225 | - if (!in_array($method, ['GET', 'POST', 'DELETE'])) { |
|
| 226 | - throw new Exception('Not implemented yet', 1); |
|
| 227 | - } |
|
| 228 | - $key = ($method == 'GET') ? 'query' : 'form_params'; |
|
| 224 | + $args = []; |
|
| 225 | + if (!in_array($method, ['GET', 'POST', 'DELETE'])) { |
|
| 226 | + throw new Exception('Not implemented yet', 1); |
|
| 227 | + } |
|
| 228 | + $key = ($method == 'GET') ? 'query' : 'form_params'; |
|
| 229 | 229 | |
| 230 | - $args[$key] = $fields; |
|
| 230 | + $args[$key] = $fields; |
|
| 231 | 231 | |
| 232 | - try { |
|
| 233 | - $request = $client->request($method, $url, $args); |
|
| 234 | - $body = $request->getBody()->getContents(); |
|
| 235 | - $obj = json_decode($body, true); |
|
| 232 | + try { |
|
| 233 | + $request = $client->request($method, $url, $args); |
|
| 234 | + $body = $request->getBody()->getContents(); |
|
| 235 | + $obj = json_decode($body, true); |
|
| 236 | 236 | |
| 237 | - return $obj; |
|
| 238 | - } catch (\GuzzleHttp\Exception\ConnectException $e) { |
|
| 239 | - throw new Exceptions\TimeoutException('Cannot connect to api.instapago.com'); |
|
| 240 | - } |
|
| 237 | + return $obj; |
|
| 238 | + } catch (\GuzzleHttp\Exception\ConnectException $e) { |
|
| 239 | + throw new Exceptions\TimeoutException('Cannot connect to api.instapago.com'); |
|
| 240 | + } |
|
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | /** |
@@ -249,41 +249,41 @@ discard block |
||
| 249 | 249 | */ |
| 250 | 250 | public function checkResponseCode($obj) |
| 251 | 251 | { |
| 252 | - $code = $obj['code']; |
|
| 252 | + $code = $obj['code']; |
|
| 253 | 253 | |
| 254 | - if ($code == 400) { |
|
| 255 | - throw new Exceptions\InvalidInputException( |
|
| 256 | - 'Error al validar los datos enviados.' |
|
| 257 | - ); |
|
| 258 | - } |
|
| 259 | - if ($code == 401) { |
|
| 260 | - throw new Exceptions\AuthException( |
|
| 261 | - 'Error de autenticación, ha ocurrido un error' |
|
| 262 | - .' con las llaves utilizadas.'); |
|
| 263 | - } elseif ($code == 403) { |
|
| 264 | - throw new Exceptions\BankRejectException( |
|
| 265 | - 'Pago Rechazado por el banco.' |
|
| 266 | - ); |
|
| 267 | - } elseif ($code == 500) { |
|
| 268 | - throw new Exceptions\InstapagoException( |
|
| 269 | - 'Ha Ocurrido un error interno dentro del servidor.' |
|
| 270 | - ); |
|
| 271 | - } elseif ($code == 503) { |
|
| 272 | - throw new Exceptions\InstapagoException( |
|
| 273 | - 'Ha Ocurrido un error al procesar los parámetros de entrada.' |
|
| 274 | - .' Revise los datos enviados y vuelva a intentarlo.' |
|
| 275 | - ); |
|
| 276 | - } elseif ($code == 201) { |
|
| 277 | - return [ |
|
| 278 | - 'code' => $code, |
|
| 279 | - 'msg_banco' => $obj['message'], |
|
| 280 | - 'voucher' => html_entity_decode($obj['voucher']), |
|
| 281 | - 'id_pago' => $obj['id'], |
|
| 282 | - 'reference' => $obj['reference'], |
|
| 283 | - 'original_response' => $obj, |
|
| 284 | - ]; |
|
| 285 | - } |
|
| 254 | + if ($code == 400) { |
|
| 255 | + throw new Exceptions\InvalidInputException( |
|
| 256 | + 'Error al validar los datos enviados.' |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | + if ($code == 401) { |
|
| 260 | + throw new Exceptions\AuthException( |
|
| 261 | + 'Error de autenticación, ha ocurrido un error' |
|
| 262 | + .' con las llaves utilizadas.'); |
|
| 263 | + } elseif ($code == 403) { |
|
| 264 | + throw new Exceptions\BankRejectException( |
|
| 265 | + 'Pago Rechazado por el banco.' |
|
| 266 | + ); |
|
| 267 | + } elseif ($code == 500) { |
|
| 268 | + throw new Exceptions\InstapagoException( |
|
| 269 | + 'Ha Ocurrido un error interno dentro del servidor.' |
|
| 270 | + ); |
|
| 271 | + } elseif ($code == 503) { |
|
| 272 | + throw new Exceptions\InstapagoException( |
|
| 273 | + 'Ha Ocurrido un error al procesar los parámetros de entrada.' |
|
| 274 | + .' Revise los datos enviados y vuelva a intentarlo.' |
|
| 275 | + ); |
|
| 276 | + } elseif ($code == 201) { |
|
| 277 | + return [ |
|
| 278 | + 'code' => $code, |
|
| 279 | + 'msg_banco' => $obj['message'], |
|
| 280 | + 'voucher' => html_entity_decode($obj['voucher']), |
|
| 281 | + 'id_pago' => $obj['id'], |
|
| 282 | + 'reference' => $obj['reference'], |
|
| 283 | + 'original_response' => $obj, |
|
| 284 | + ]; |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - throw new \Exception('Not implemented yet'); |
|
| 287 | + throw new \Exception('Not implemented yet'); |
|
| 288 | 288 | } |
| 289 | 289 | } |
@@ -36,64 +36,64 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class Validator |
| 38 | 38 | { |
| 39 | - protected $validations = []; |
|
| 39 | + protected $validations = []; |
|
| 40 | 40 | |
| 41 | - public function payment() |
|
| 42 | - { |
|
| 43 | - $this->validations = [ |
|
| 44 | - 'amount' => [FILTER_VALIDATE_FLOAT], |
|
| 45 | - 'description' => [FILTER_VALIDATE_REGEXP, '/^(.{0,140})$/'], |
|
| 46 | - 'card_holder' => [FILTER_VALIDATE_REGEXP, '/^([a-zA-ZáéíóúñÁÉÍÓÚÑ\ ]+)$/'], |
|
| 47 | - 'card_holder_id' => [FILTER_VALIDATE_REGEXP, '/^(\d{5,8})$/'], |
|
| 48 | - 'card_number' => [FILTER_VALIDATE_REGEXP, '/^(\d{16})$/'], |
|
| 49 | - 'cvc' => [FILTER_VALIDATE_INT], |
|
| 50 | - 'expiration' => [FILTER_VALIDATE_REGEXP, '/^(\d{2})\/(\d{4})$/'], |
|
| 51 | - 'ip' => [FILTER_VALIDATE_IP], |
|
| 52 | - ]; |
|
| 41 | + public function payment() |
|
| 42 | + { |
|
| 43 | + $this->validations = [ |
|
| 44 | + 'amount' => [FILTER_VALIDATE_FLOAT], |
|
| 45 | + 'description' => [FILTER_VALIDATE_REGEXP, '/^(.{0,140})$/'], |
|
| 46 | + 'card_holder' => [FILTER_VALIDATE_REGEXP, '/^([a-zA-ZáéíóúñÁÉÍÓÚÑ\ ]+)$/'], |
|
| 47 | + 'card_holder_id' => [FILTER_VALIDATE_REGEXP, '/^(\d{5,8})$/'], |
|
| 48 | + 'card_number' => [FILTER_VALIDATE_REGEXP, '/^(\d{16})$/'], |
|
| 49 | + 'cvc' => [FILTER_VALIDATE_INT], |
|
| 50 | + 'expiration' => [FILTER_VALIDATE_REGEXP, '/^(\d{2})\/(\d{4})$/'], |
|
| 51 | + 'ip' => [FILTER_VALIDATE_IP], |
|
| 52 | + ]; |
|
| 53 | 53 | |
| 54 | - return $this; |
|
| 55 | - } |
|
| 54 | + return $this; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public function release() |
|
| 58 | - { |
|
| 59 | - $this->validations = [ |
|
| 60 | - 'amount' => [FILTER_VALIDATE_FLOAT], |
|
| 61 | - 'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'], |
|
| 62 | - ]; |
|
| 57 | + public function release() |
|
| 58 | + { |
|
| 59 | + $this->validations = [ |
|
| 60 | + 'amount' => [FILTER_VALIDATE_FLOAT], |
|
| 61 | + 'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'], |
|
| 62 | + ]; |
|
| 63 | 63 | |
| 64 | - return $this; |
|
| 65 | - } |
|
| 64 | + return $this; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - public function query() |
|
| 68 | - { |
|
| 69 | - $this->validations = [ |
|
| 70 | - 'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'], |
|
| 71 | - ]; |
|
| 67 | + public function query() |
|
| 68 | + { |
|
| 69 | + $this->validations = [ |
|
| 70 | + 'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'], |
|
| 71 | + ]; |
|
| 72 | 72 | |
| 73 | - return $this; |
|
| 74 | - } |
|
| 73 | + return $this; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - public function validate($fields) |
|
| 77 | - { |
|
| 78 | - foreach ($this->validations as $key => $filters) { |
|
| 79 | - if (!$this->_validation($fields[$key], $filters)) { |
|
| 80 | - throw new Exceptions\ValidationException("Error {$key}: {$fields[$key]}"); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - } |
|
| 76 | + public function validate($fields) |
|
| 77 | + { |
|
| 78 | + foreach ($this->validations as $key => $filters) { |
|
| 79 | + if (!$this->_validation($fields[$key], $filters)) { |
|
| 80 | + throw new Exceptions\ValidationException("Error {$key}: {$fields[$key]}"); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - private function _validation($value, $filters) |
|
| 86 | - { |
|
| 87 | - $filter = $filters[0]; |
|
| 88 | - $flags = []; |
|
| 89 | - if ($filter === FILTER_VALIDATE_REGEXP) { |
|
| 90 | - $flags = [ |
|
| 91 | - 'options' => [ |
|
| 92 | - 'regexp'=> $filters[1], |
|
| 93 | - ], |
|
| 94 | - ]; |
|
| 95 | - } |
|
| 85 | + private function _validation($value, $filters) |
|
| 86 | + { |
|
| 87 | + $filter = $filters[0]; |
|
| 88 | + $flags = []; |
|
| 89 | + if ($filter === FILTER_VALIDATE_REGEXP) { |
|
| 90 | + $flags = [ |
|
| 91 | + 'options' => [ |
|
| 92 | + 'regexp'=> $filters[1], |
|
| 93 | + ], |
|
| 94 | + ]; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - return filter_var($value, $filter, $flags); |
|
| 98 | - } |
|
| 97 | + return filter_var($value, $filter, $flags); |
|
| 98 | + } |
|
| 99 | 99 | } |