@@ -38,54 +38,54 @@ |
||
38 | 38 | protected $validations = []; |
39 | 39 | |
40 | 40 | public function payment() { |
41 | - $this->validations = [ |
|
42 | - 'amount' => [FILTER_VALIDATE_FLOAT], |
|
43 | - 'description' => [FILTER_VALIDATE_REGEXP, '/^(.{0,140})$/'], |
|
44 | - 'card_holder' => [FILTER_VALIDATE_REGEXP, '/^([a-zA-Z\ ]+)$/'], |
|
45 | - 'card_holder_id' => [FILTER_VALIDATE_REGEXP, '/^(\d{5,8})$/'], |
|
46 | - 'card_number' => [FILTER_VALIDATE_REGEXP, '/^(\d{16})$/'], |
|
47 | - 'cvc' => [FILTER_VALIDATE_INT], |
|
48 | - 'expiration' => [FILTER_VALIDATE_REGEXP, '/^(\d{2})\/(\d{4})$/'], |
|
49 | - 'ip' => [FILTER_VALIDATE_IP], |
|
50 | - ]; |
|
51 | - return $this; |
|
41 | + $this->validations = [ |
|
42 | + 'amount' => [FILTER_VALIDATE_FLOAT], |
|
43 | + 'description' => [FILTER_VALIDATE_REGEXP, '/^(.{0,140})$/'], |
|
44 | + 'card_holder' => [FILTER_VALIDATE_REGEXP, '/^([a-zA-Z\ ]+)$/'], |
|
45 | + 'card_holder_id' => [FILTER_VALIDATE_REGEXP, '/^(\d{5,8})$/'], |
|
46 | + 'card_number' => [FILTER_VALIDATE_REGEXP, '/^(\d{16})$/'], |
|
47 | + 'cvc' => [FILTER_VALIDATE_INT], |
|
48 | + 'expiration' => [FILTER_VALIDATE_REGEXP, '/^(\d{2})\/(\d{4})$/'], |
|
49 | + 'ip' => [FILTER_VALIDATE_IP], |
|
50 | + ]; |
|
51 | + return $this; |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | public function release(){ |
55 | - $this->validations = [ |
|
56 | - 'amount' => [FILTER_VALIDATE_FLOAT], |
|
57 | - 'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'], |
|
58 | - ]; |
|
59 | - return $this; |
|
55 | + $this->validations = [ |
|
56 | + 'amount' => [FILTER_VALIDATE_FLOAT], |
|
57 | + 'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'], |
|
58 | + ]; |
|
59 | + return $this; |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | public function query(){ |
63 | - $this->validations = [ |
|
64 | - 'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'], |
|
65 | - ]; |
|
66 | - return $this; |
|
63 | + $this->validations = [ |
|
64 | + 'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'], |
|
65 | + ]; |
|
66 | + return $this; |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | public function validate($fields){ |
70 | - foreach ($this->validations as $key => $filters) { |
|
71 | - if ( !$this->_validation($fields[$key], $filters) ){ |
|
72 | - throw new Exceptions\ValidationException("Error {$key}: {$fields[$key]}"); |
|
73 | - } |
|
74 | - } |
|
70 | + foreach ($this->validations as $key => $filters) { |
|
71 | + if ( !$this->_validation($fields[$key], $filters) ){ |
|
72 | + throw new Exceptions\ValidationException("Error {$key}: {$fields[$key]}"); |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | 76 | } |
77 | 77 | |
78 | 78 | private function _validation ($value, $filters) |
79 | 79 | { |
80 | - $filter = $filters[0]; |
|
81 | - $flags = []; |
|
82 | - if ($filter === FILTER_VALIDATE_REGEXP) { |
|
83 | - $flags = [ |
|
84 | - "options" => [ |
|
85 | - "regexp"=> $filters[1], |
|
86 | - ], |
|
87 | - ]; |
|
88 | - } |
|
89 | - return filter_var($value, $filter, $flags); |
|
80 | + $filter = $filters[0]; |
|
81 | + $flags = []; |
|
82 | + if ($filter === FILTER_VALIDATE_REGEXP) { |
|
83 | + $flags = [ |
|
84 | + "options" => [ |
|
85 | + "regexp"=> $filters[1], |
|
86 | + ], |
|
87 | + ]; |
|
88 | + } |
|
89 | + return filter_var($value, $filter, $flags); |
|
90 | 90 | } |
91 | 91 | } |
92 | 92 | \ No newline at end of file |
@@ -47,11 +47,11 @@ discard block |
||
47 | 47 | * Requeridas. |
48 | 48 | */ |
49 | 49 | public function __construct($keyId, $publicKeyId) { |
50 | - if ( empty($keyId) || empty($publicKeyId) ) { |
|
51 | - throw new Exceptions\InstapagoException('Los parámetros "keyId" y "publicKeyId" son requeridos para procesar la petición.'); |
|
52 | - } |
|
53 | - $this->publicKeyId = $publicKeyId; |
|
54 | - $this->keyId = $keyId; |
|
50 | + if ( empty($keyId) || empty($publicKeyId) ) { |
|
51 | + throw new Exceptions\InstapagoException('Los parámetros "keyId" y "publicKeyId" son requeridos para procesar la petición.'); |
|
52 | + } |
|
53 | + $this->publicKeyId = $publicKeyId; |
|
54 | + $this->keyId = $keyId; |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function directPayment($fields) |
66 | 66 | { |
67 | - return $this->payment('direct', $fields); |
|
67 | + return $this->payment('direct', $fields); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | */ |
78 | 78 | public function reservePayment($fields) |
79 | 79 | { |
80 | - return $this->payment('reserve', $fields); |
|
80 | + return $this->payment('reserve', $fields); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | /** |
@@ -91,31 +91,31 @@ discard block |
||
91 | 91 | */ |
92 | 92 | |
93 | 93 | private function payment($paymentType, $fields) { |
94 | - $type = '2'; |
|
95 | - if ($paymentType == 'reserve') { |
|
96 | - $type = '1'; |
|
97 | - } |
|
94 | + $type = '2'; |
|
95 | + if ($paymentType == 'reserve') { |
|
96 | + $type = '1'; |
|
97 | + } |
|
98 | 98 | |
99 | - (new Validator())->payment()->validate($fields); |
|
99 | + (new Validator())->payment()->validate($fields); |
|
100 | 100 | |
101 | - $fields = [ |
|
102 | - 'KeyID' => $this->keyId, |
|
103 | - 'PublicKeyId' => $this->publicKeyId, |
|
104 | - 'amount' => $fields['amount'], |
|
105 | - 'description' => $fields['description'], |
|
106 | - 'cardHolder' => $fields['card_holder'], |
|
107 | - 'cardHolderId' => $fields['card_holder_id'], |
|
108 | - 'cardNumber' => $fields['card_number'], |
|
109 | - 'cvc' => $fields['cvc'], |
|
110 | - 'expirationDate' => $fields['expiration'], |
|
111 | - 'statusId' => $type, |
|
112 | - 'IP' => $fields['ip'], |
|
113 | - ]; |
|
101 | + $fields = [ |
|
102 | + 'KeyID' => $this->keyId, |
|
103 | + 'PublicKeyId' => $this->publicKeyId, |
|
104 | + 'amount' => $fields['amount'], |
|
105 | + 'description' => $fields['description'], |
|
106 | + 'cardHolder' => $fields['card_holder'], |
|
107 | + 'cardHolderId' => $fields['card_holder_id'], |
|
108 | + 'cardNumber' => $fields['card_number'], |
|
109 | + 'cvc' => $fields['cvc'], |
|
110 | + 'expirationDate' => $fields['expiration'], |
|
111 | + 'statusId' => $type, |
|
112 | + 'IP' => $fields['ip'], |
|
113 | + ]; |
|
114 | 114 | |
115 | - $obj = $this->curlTransaccion('payment', $fields, 'POST'); |
|
116 | - $result = $this->checkResponseCode($obj); |
|
115 | + $obj = $this->curlTransaccion('payment', $fields, 'POST'); |
|
116 | + $result = $this->checkResponseCode($obj); |
|
117 | 117 | |
118 | - return $result; |
|
118 | + return $result; |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
@@ -129,17 +129,17 @@ discard block |
||
129 | 129 | * @throws Exceptions\InstapagoException |
130 | 130 | */ |
131 | 131 | public function continuePayment($fields){ |
132 | - (new Validator())->release()->validate($fields); |
|
133 | - $fields = [ |
|
134 | - 'KeyID' => $this->keyId, //required |
|
135 | - 'PublicKeyId' => $this->publicKeyId, //required |
|
136 | - 'id' => $fields['id'], //required |
|
137 | - 'amount' => $fields['amount'], //required |
|
138 | - ]; |
|
132 | + (new Validator())->release()->validate($fields); |
|
133 | + $fields = [ |
|
134 | + 'KeyID' => $this->keyId, //required |
|
135 | + 'PublicKeyId' => $this->publicKeyId, //required |
|
136 | + 'id' => $fields['id'], //required |
|
137 | + 'amount' => $fields['amount'], //required |
|
138 | + ]; |
|
139 | 139 | |
140 | - $obj = $this->curlTransaccion('complete', $fields, 'POST'); |
|
141 | - $result = $this->checkResponseCode($obj); |
|
142 | - return $result; |
|
140 | + $obj = $this->curlTransaccion('complete', $fields, 'POST'); |
|
141 | + $result = $this->checkResponseCode($obj); |
|
142 | + return $result; |
|
143 | 143 | } |
144 | 144 | |
145 | 145 | /** |
@@ -152,19 +152,19 @@ discard block |
||
152 | 152 | * @throws Exceptions\InstapagoException |
153 | 153 | */ |
154 | 154 | public function query($id_pago) { |
155 | - (new Validator())->query()->validate([ |
|
156 | - 'id' => $id_pago |
|
157 | - ]); |
|
155 | + (new Validator())->query()->validate([ |
|
156 | + 'id' => $id_pago |
|
157 | + ]); |
|
158 | 158 | |
159 | - $fields = [ |
|
160 | - 'KeyID' => $this->keyId, //required |
|
161 | - 'PublicKeyId' => $this->publicKeyId, //required |
|
162 | - 'id' => $id_pago, //required |
|
163 | - ]; |
|
159 | + $fields = [ |
|
160 | + 'KeyID' => $this->keyId, //required |
|
161 | + 'PublicKeyId' => $this->publicKeyId, //required |
|
162 | + 'id' => $id_pago, //required |
|
163 | + ]; |
|
164 | 164 | |
165 | - $obj = $this->curlTransaccion('payment', $fields, 'GET'); |
|
166 | - $result = $this->checkResponseCode($obj); |
|
167 | - return $result; |
|
165 | + $obj = $this->curlTransaccion('payment', $fields, 'GET'); |
|
166 | + $result = $this->checkResponseCode($obj); |
|
167 | + return $result; |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -176,19 +176,19 @@ discard block |
||
176 | 176 | * @throws Exceptions\InstapagoException |
177 | 177 | */ |
178 | 178 | public function cancel($id_pago) { |
179 | - (new Validator())->query()->validate([ |
|
180 | - 'id' => $id_pago |
|
181 | - ]); |
|
179 | + (new Validator())->query()->validate([ |
|
180 | + 'id' => $id_pago |
|
181 | + ]); |
|
182 | 182 | |
183 | - $fields = [ |
|
184 | - 'KeyID' => $this->keyId, //required |
|
185 | - 'PublicKeyId' => $this->publicKeyId, //required |
|
186 | - 'id' => $id_pago, //required |
|
187 | - ]; |
|
183 | + $fields = [ |
|
184 | + 'KeyID' => $this->keyId, //required |
|
185 | + 'PublicKeyId' => $this->publicKeyId, //required |
|
186 | + 'id' => $id_pago, //required |
|
187 | + ]; |
|
188 | 188 | |
189 | - $obj = $this->curlTransaccion('payment', $fields, 'DELETE'); |
|
190 | - $result = $this->checkResponseCode($obj); |
|
191 | - return $result; |
|
189 | + $obj = $this->curlTransaccion('payment', $fields, 'DELETE'); |
|
190 | + $result = $this->checkResponseCode($obj); |
|
191 | + return $result; |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
@@ -203,32 +203,32 @@ discard block |
||
203 | 203 | */ |
204 | 204 | public function curlTransaccion($url, $fields, $method) |
205 | 205 | { |
206 | - $client = new Client([ |
|
207 | - 'base_uri' => 'https://api.instapago.com/', |
|
208 | - ]); |
|
206 | + $client = new Client([ |
|
207 | + 'base_uri' => 'https://api.instapago.com/', |
|
208 | + ]); |
|
209 | 209 | |
210 | - $args = []; |
|
210 | + $args = []; |
|
211 | 211 | |
212 | - if ($method == 'GET') { |
|
213 | - $args = [ |
|
214 | - 'query' => $fields |
|
215 | - ]; |
|
216 | - }else if ($method == 'POST' || $method == 'DELETE') { |
|
217 | - $args = [ |
|
218 | - 'form_params' => $fields |
|
219 | - ]; |
|
220 | - }else{ |
|
221 | - throw new Exception("Not implemented yet", 1); |
|
222 | - } |
|
212 | + if ($method == 'GET') { |
|
213 | + $args = [ |
|
214 | + 'query' => $fields |
|
215 | + ]; |
|
216 | + }else if ($method == 'POST' || $method == 'DELETE') { |
|
217 | + $args = [ |
|
218 | + 'form_params' => $fields |
|
219 | + ]; |
|
220 | + }else{ |
|
221 | + throw new Exception("Not implemented yet", 1); |
|
222 | + } |
|
223 | 223 | |
224 | - try { |
|
225 | - $request = $client->request($method, $url, $args); |
|
226 | - $body = $request->getBody()->getContents(); |
|
227 | - $obj = json_decode($body); |
|
228 | - return $obj; |
|
229 | - } catch (\GuzzleHttp\Exception\ConnectException $e) { |
|
230 | - throw new Exceptions\TimeoutException("Cannot connect to api.instapago.com"); |
|
231 | - } |
|
224 | + try { |
|
225 | + $request = $client->request($method, $url, $args); |
|
226 | + $body = $request->getBody()->getContents(); |
|
227 | + $obj = json_decode($body); |
|
228 | + return $obj; |
|
229 | + } catch (\GuzzleHttp\Exception\ConnectException $e) { |
|
230 | + throw new Exceptions\TimeoutException("Cannot connect to api.instapago.com"); |
|
231 | + } |
|
232 | 232 | } |
233 | 233 | |
234 | 234 | /** |
@@ -240,40 +240,40 @@ discard block |
||
240 | 240 | */ |
241 | 241 | public function checkResponseCode($obj) |
242 | 242 | { |
243 | - $code = $obj->code; |
|
243 | + $code = $obj->code; |
|
244 | 244 | |
245 | - if ($code == 400) { |
|
246 | - throw new Exceptions\InvalidInputException( |
|
247 | - 'Error al validar los datos enviados.' |
|
248 | - ); |
|
249 | - }else if ($code == 401) { |
|
250 | - throw new Exceptions\AuthException( |
|
251 | - 'Error de autenticación, ha ocurrido un error' |
|
252 | - . ' con las llaves utilizadas.'); |
|
253 | - }else if ($code == 403) { |
|
254 | - throw new Exceptions\BankRejectException( |
|
255 | - 'Pago Rechazado por el banco.' |
|
256 | - ); |
|
257 | - }else if ($code == 500) { |
|
258 | - throw new Exceptions\InstapagoException( |
|
259 | - 'Ha Ocurrido un error interno dentro del servidor.' |
|
260 | - ); |
|
261 | - }else if ($code == 503) { |
|
262 | - throw new Exceptions\InstapagoException( |
|
263 | - 'Ha Ocurrido un error al procesar los parámetros de entrada.' |
|
264 | - . ' Revise los datos enviados y vuelva a intentarlo.' |
|
265 | - ); |
|
266 | - }else if ($code == 201) { |
|
267 | - return [ |
|
268 | - 'code' => $code, |
|
269 | - 'msg_banco' => $obj->message, |
|
270 | - 'voucher' => html_entity_decode($obj->voucher), |
|
271 | - 'id_pago' => $obj->id, |
|
272 | - 'reference' => $obj->reference, |
|
273 | - ]; |
|
274 | - }else { |
|
275 | - throw new \Exception('Not implemented yet'); |
|
276 | - } |
|
245 | + if ($code == 400) { |
|
246 | + throw new Exceptions\InvalidInputException( |
|
247 | + 'Error al validar los datos enviados.' |
|
248 | + ); |
|
249 | + }else if ($code == 401) { |
|
250 | + throw new Exceptions\AuthException( |
|
251 | + 'Error de autenticación, ha ocurrido un error' |
|
252 | + . ' con las llaves utilizadas.'); |
|
253 | + }else if ($code == 403) { |
|
254 | + throw new Exceptions\BankRejectException( |
|
255 | + 'Pago Rechazado por el banco.' |
|
256 | + ); |
|
257 | + }else if ($code == 500) { |
|
258 | + throw new Exceptions\InstapagoException( |
|
259 | + 'Ha Ocurrido un error interno dentro del servidor.' |
|
260 | + ); |
|
261 | + }else if ($code == 503) { |
|
262 | + throw new Exceptions\InstapagoException( |
|
263 | + 'Ha Ocurrido un error al procesar los parámetros de entrada.' |
|
264 | + . ' Revise los datos enviados y vuelva a intentarlo.' |
|
265 | + ); |
|
266 | + }else if ($code == 201) { |
|
267 | + return [ |
|
268 | + 'code' => $code, |
|
269 | + 'msg_banco' => $obj->message, |
|
270 | + 'voucher' => html_entity_decode($obj->voucher), |
|
271 | + 'id_pago' => $obj->id, |
|
272 | + 'reference' => $obj->reference, |
|
273 | + ]; |
|
274 | + }else { |
|
275 | + throw new \Exception('Not implemented yet'); |
|
276 | + } |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | } |