@@ -36,260 +36,260 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class Api |
| 38 | 38 | { |
| 39 | - protected $keyId; |
|
| 40 | - protected $publicKeyId; |
|
| 39 | + protected $keyId; |
|
| 40 | + protected $publicKeyId; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Crear un nuevo objeto de Instapago. |
|
| 44 | - * |
|
| 45 | - * @param string $keyId llave privada |
|
| 46 | - * @param string $publicKeyId llave publica |
|
| 47 | - * Requeridas. |
|
| 48 | - */ |
|
| 49 | - public function __construct($keyId, $publicKeyId) |
|
| 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; |
|
| 56 | - } |
|
| 42 | + /** |
|
| 43 | + * Crear un nuevo objeto de Instapago. |
|
| 44 | + * |
|
| 45 | + * @param string $keyId llave privada |
|
| 46 | + * @param string $publicKeyId llave publica |
|
| 47 | + * Requeridas. |
|
| 48 | + */ |
|
| 49 | + public function __construct($keyId, $publicKeyId) |
|
| 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; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Crear un pago directo. |
|
| 60 | - * |
|
| 61 | - * @param array<string> $fields Los campos necesarios |
|
| 62 | - * para procesar el pago. |
|
| 63 | - * |
|
| 64 | - * @throws Exceptions\InstapagoException |
|
| 65 | - * |
|
| 66 | - * @return array<string> Respuesta de Instapago |
|
| 67 | - */ |
|
| 68 | - public function directPayment($fields) |
|
| 69 | - { |
|
| 70 | - return $this->payment('2', $fields); |
|
| 71 | - } |
|
| 58 | + /** |
|
| 59 | + * Crear un pago directo. |
|
| 60 | + * |
|
| 61 | + * @param array<string> $fields Los campos necesarios |
|
| 62 | + * para procesar el pago. |
|
| 63 | + * |
|
| 64 | + * @throws Exceptions\InstapagoException |
|
| 65 | + * |
|
| 66 | + * @return array<string> Respuesta de Instapago |
|
| 67 | + */ |
|
| 68 | + public function directPayment($fields) |
|
| 69 | + { |
|
| 70 | + return $this->payment('2', $fields); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Crear un pago diferido o reservado. |
|
| 75 | - * |
|
| 76 | - * @param array<string> $fields Los campos necesarios |
|
| 77 | - * para procesar el pago. |
|
| 78 | - * |
|
| 79 | - * @throws Exceptions\InstapagoException |
|
| 80 | - * |
|
| 81 | - * @return array<string> Respuesta de Instapago |
|
| 82 | - */ |
|
| 83 | - public function reservePayment($fields) |
|
| 84 | - { |
|
| 85 | - return $this->payment('1', $fields); |
|
| 86 | - } |
|
| 73 | + /** |
|
| 74 | + * Crear un pago diferido o reservado. |
|
| 75 | + * |
|
| 76 | + * @param array<string> $fields Los campos necesarios |
|
| 77 | + * para procesar el pago. |
|
| 78 | + * |
|
| 79 | + * @throws Exceptions\InstapagoException |
|
| 80 | + * |
|
| 81 | + * @return array<string> Respuesta de Instapago |
|
| 82 | + */ |
|
| 83 | + public function reservePayment($fields) |
|
| 84 | + { |
|
| 85 | + return $this->payment('1', $fields); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Crear un pago. |
|
| 90 | - * |
|
| 91 | - * @param string $type tipo de pago ('1' o '0') |
|
| 92 | - * @param array<string> $fields Los campos necesarios |
|
| 93 | - * para procesar el pago. |
|
| 94 | - * |
|
| 95 | - * @throws Exceptions\InstapagoException |
|
| 96 | - * |
|
| 97 | - * @return array<string> Respuesta de Instapago |
|
| 98 | - */ |
|
| 99 | - private function payment($type, $fields) |
|
| 100 | - { |
|
| 101 | - (new Validator())->payment()->validate($fields); |
|
| 88 | + /** |
|
| 89 | + * Crear un pago. |
|
| 90 | + * |
|
| 91 | + * @param string $type tipo de pago ('1' o '0') |
|
| 92 | + * @param array<string> $fields Los campos necesarios |
|
| 93 | + * para procesar el pago. |
|
| 94 | + * |
|
| 95 | + * @throws Exceptions\InstapagoException |
|
| 96 | + * |
|
| 97 | + * @return array<string> Respuesta de Instapago |
|
| 98 | + */ |
|
| 99 | + private function payment($type, $fields) |
|
| 100 | + { |
|
| 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; |
|
| 121 | - } |
|
| 120 | + return $result; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Completar Pago |
|
| 125 | - * Este método funciona para procesar un bloqueo o pre-autorización |
|
| 126 | - * para así procesarla y hacer el cobro respectivo. |
|
| 127 | - * |
|
| 128 | - * @param array<string> $fields Los campos necesarios |
|
| 129 | - * para procesar el pago. |
|
| 130 | - * |
|
| 131 | - * @throws Exceptions\InstapagoException |
|
| 132 | - * |
|
| 133 | - * @return array<string> Respuesta de Instapago |
|
| 134 | - */ |
|
| 135 | - public function continuePayment($fields) |
|
| 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 | - ]; |
|
| 123 | + /** |
|
| 124 | + * Completar Pago |
|
| 125 | + * Este método funciona para procesar un bloqueo o pre-autorización |
|
| 126 | + * para así procesarla y hacer el cobro respectivo. |
|
| 127 | + * |
|
| 128 | + * @param array<string> $fields Los campos necesarios |
|
| 129 | + * para procesar el pago. |
|
| 130 | + * |
|
| 131 | + * @throws Exceptions\InstapagoException |
|
| 132 | + * |
|
| 133 | + * @return array<string> Respuesta de Instapago |
|
| 134 | + */ |
|
| 135 | + public function continuePayment($fields) |
|
| 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 | + ]; |
|
| 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; |
|
| 149 | - } |
|
| 148 | + return $result; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Información/Consulta de Pago |
|
| 153 | - * Este método funciona para procesar un bloqueo o pre-autorización |
|
| 154 | - * para así procesarla y hacer el cobro respectivo. |
|
| 155 | - * |
|
| 156 | - * @param string $id_pago ID del pago a consultar |
|
| 157 | - * |
|
| 158 | - * @throws Exceptions\InstapagoException |
|
| 159 | - * |
|
| 160 | - * @return array<string> Respuesta de Instapago |
|
| 161 | - */ |
|
| 162 | - public function query($id_pago) |
|
| 163 | - { |
|
| 164 | - (new Validator())->query()->validate([ |
|
| 165 | - 'id' => $id_pago, |
|
| 166 | - ]); |
|
| 151 | + /** |
|
| 152 | + * Información/Consulta de Pago |
|
| 153 | + * Este método funciona para procesar un bloqueo o pre-autorización |
|
| 154 | + * para así procesarla y hacer el cobro respectivo. |
|
| 155 | + * |
|
| 156 | + * @param string $id_pago ID del pago a consultar |
|
| 157 | + * |
|
| 158 | + * @throws Exceptions\InstapagoException |
|
| 159 | + * |
|
| 160 | + * @return array<string> Respuesta de Instapago |
|
| 161 | + */ |
|
| 162 | + public function query($id_pago) |
|
| 163 | + { |
|
| 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; |
|
| 178 | - } |
|
| 177 | + return $result; |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * Cancelar Pago |
|
| 182 | - * Este método funciona para cancelar un pago previamente procesado. |
|
| 183 | - * |
|
| 184 | - * @param string $id_pago ID del pago a cancelar |
|
| 185 | - * |
|
| 186 | - * @throws Exceptions\InstapagoException |
|
| 187 | - * |
|
| 188 | - * @return array<string> Respuesta de Instapago |
|
| 189 | - */ |
|
| 190 | - public function cancel($id_pago) |
|
| 191 | - { |
|
| 192 | - (new Validator())->query()->validate([ |
|
| 193 | - 'id' => $id_pago, |
|
| 194 | - ]); |
|
| 180 | + /** |
|
| 181 | + * Cancelar Pago |
|
| 182 | + * Este método funciona para cancelar un pago previamente procesado. |
|
| 183 | + * |
|
| 184 | + * @param string $id_pago ID del pago a cancelar |
|
| 185 | + * |
|
| 186 | + * @throws Exceptions\InstapagoException |
|
| 187 | + * |
|
| 188 | + * @return array<string> Respuesta de Instapago |
|
| 189 | + */ |
|
| 190 | + public function cancel($id_pago) |
|
| 191 | + { |
|
| 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; |
|
| 206 | - } |
|
| 205 | + return $result; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * Realiza Transaccion |
|
| 210 | - * Efectúa y retornar una respuesta a un metodo de pago. |
|
| 211 | - * |
|
| 212 | - * @param $url string endpoint a consultar |
|
| 213 | - * @param $fields array datos para la consulta |
|
| 214 | - * @param $method string verbo http de la consulta |
|
| 215 | - * |
|
| 216 | - * @return array resultados de la transaccion |
|
| 217 | - */ |
|
| 218 | - public function curlTransaccion($url, $fields, $method) |
|
| 219 | - { |
|
| 220 | - $client = new Client([ |
|
| 221 | - 'base_uri' => 'https://api.instapago.com/', |
|
| 222 | - ]); |
|
| 208 | + /** |
|
| 209 | + * Realiza Transaccion |
|
| 210 | + * Efectúa y retornar una respuesta a un metodo de pago. |
|
| 211 | + * |
|
| 212 | + * @param $url string endpoint a consultar |
|
| 213 | + * @param $fields array datos para la consulta |
|
| 214 | + * @param $method string verbo http de la consulta |
|
| 215 | + * |
|
| 216 | + * @return array resultados de la transaccion |
|
| 217 | + */ |
|
| 218 | + public function curlTransaccion($url, $fields, $method) |
|
| 219 | + { |
|
| 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 | - } |
|
| 241 | - } |
|
| 237 | + return $obj; |
|
| 238 | + } catch (\GuzzleHttp\Exception\ConnectException $e) { |
|
| 239 | + throw new Exceptions\TimeoutException('Cannot connect to api.instapago.com'); |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * Verifica y retornar el resultado de la transaccion. |
|
| 245 | - * |
|
| 246 | - * @param $obj datos de la consulta |
|
| 247 | - * |
|
| 248 | - * @return array datos de transaccion |
|
| 249 | - */ |
|
| 250 | - public function checkResponseCode($obj) |
|
| 251 | - { |
|
| 252 | - $code = $obj['code']; |
|
| 243 | + /** |
|
| 244 | + * Verifica y retornar el resultado de la transaccion. |
|
| 245 | + * |
|
| 246 | + * @param $obj datos de la consulta |
|
| 247 | + * |
|
| 248 | + * @return array datos de transaccion |
|
| 249 | + */ |
|
| 250 | + public function checkResponseCode($obj) |
|
| 251 | + { |
|
| 252 | + $code = $obj['code']; |
|
| 253 | 253 | |
| 254 | - switch ($code) { |
|
| 255 | - case 400: |
|
| 256 | - throw new Exceptions\InvalidInputException( |
|
| 257 | - 'Error al validar los datos enviados.' |
|
| 258 | - ); |
|
| 259 | - break; |
|
| 260 | - case 401: |
|
| 261 | - throw new Exceptions\AuthException( |
|
| 262 | - 'Error de autenticación, ha ocurrido un error con las llaves utilizadas.' |
|
| 263 | - ); |
|
| 264 | - break; |
|
| 265 | - case 403: |
|
| 266 | - throw new Exceptions\BankRejectException( |
|
| 267 | - 'Pago Rechazado por el banco.' |
|
| 268 | - ); |
|
| 269 | - break; |
|
| 270 | - case 500: |
|
| 271 | - throw new Exceptions\InstapagoException( |
|
| 272 | - 'Ha Ocurrido un error interno dentro del servidor.' |
|
| 273 | - ); |
|
| 274 | - break; |
|
| 275 | - case 503: |
|
| 276 | - throw new Exceptions\InstapagoException( |
|
| 277 | - 'Ha Ocurrido un error al procesar los parámetros de entrada. Revise los datos enviados y vuelva a intentarlo.' |
|
| 278 | - ); |
|
| 279 | - break; |
|
| 280 | - case 201: |
|
| 281 | - return [ |
|
| 282 | - 'code' => $code, |
|
| 283 | - 'msg_banco' => $obj['message'], |
|
| 284 | - 'voucher' => html_entity_decode($obj['voucher']), |
|
| 285 | - 'id_pago' => $obj['id'], |
|
| 286 | - 'reference' => $obj['reference'], |
|
| 287 | - 'original_response' => $obj, |
|
| 288 | - ]; |
|
| 289 | - break; |
|
| 290 | - default: |
|
| 291 | - throw new \Exception('Not implemented yet'); |
|
| 292 | - break; |
|
| 293 | - } |
|
| 294 | - } |
|
| 254 | + switch ($code) { |
|
| 255 | + case 400: |
|
| 256 | + throw new Exceptions\InvalidInputException( |
|
| 257 | + 'Error al validar los datos enviados.' |
|
| 258 | + ); |
|
| 259 | + break; |
|
| 260 | + case 401: |
|
| 261 | + throw new Exceptions\AuthException( |
|
| 262 | + 'Error de autenticación, ha ocurrido un error con las llaves utilizadas.' |
|
| 263 | + ); |
|
| 264 | + break; |
|
| 265 | + case 403: |
|
| 266 | + throw new Exceptions\BankRejectException( |
|
| 267 | + 'Pago Rechazado por el banco.' |
|
| 268 | + ); |
|
| 269 | + break; |
|
| 270 | + case 500: |
|
| 271 | + throw new Exceptions\InstapagoException( |
|
| 272 | + 'Ha Ocurrido un error interno dentro del servidor.' |
|
| 273 | + ); |
|
| 274 | + break; |
|
| 275 | + case 503: |
|
| 276 | + throw new Exceptions\InstapagoException( |
|
| 277 | + 'Ha Ocurrido un error al procesar los parámetros de entrada. Revise los datos enviados y vuelva a intentarlo.' |
|
| 278 | + ); |
|
| 279 | + break; |
|
| 280 | + case 201: |
|
| 281 | + return [ |
|
| 282 | + 'code' => $code, |
|
| 283 | + 'msg_banco' => $obj['message'], |
|
| 284 | + 'voucher' => html_entity_decode($obj['voucher']), |
|
| 285 | + 'id_pago' => $obj['id'], |
|
| 286 | + 'reference' => $obj['reference'], |
|
| 287 | + 'original_response' => $obj, |
|
| 288 | + ]; |
|
| 289 | + break; |
|
| 290 | + default: |
|
| 291 | + throw new \Exception('Not implemented yet'); |
|
| 292 | + break; |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | 295 | } |
@@ -222,7 +222,7 @@ |
||
| 222 | 222 | ]); |
| 223 | 223 | |
| 224 | 224 | $args = []; |
| 225 | - if (! in_array($method, ['GET', 'POST', 'DELETE'])) { |
|
| 225 | + if ( ! in_array($method, ['GET', 'POST', 'DELETE'])) { |
|
| 226 | 226 | throw new Exception('Not implemented yet', 1); |
| 227 | 227 | } |
| 228 | 228 | $key = ($method == 'GET') ? 'query' : 'form_params'; |