Skip to content

Commit 5fa37c4

Browse files
committed
Agrega componentes lib
1 parent 0451aa7 commit 5fa37c4

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

lib/Config.class.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Clase para Configurar el cliente
4+
* @Filename: Config.class.php
5+
* @version: 2.0
6+
* @Author: flow.cl
7+
* @Email: csepulveda@tuxpan.com
8+
* @Date: 28-04-2017 11:32
9+
* @Last Modified by: Carlos Sepulveda
10+
* @Last Modified time: 28-04-2017 11:32
11+
*/
12+
13+
$COMMERCE_CONFIG = array(
14+
"APIKEY" => "1F90971E-8276-4713-97FF-2BLF5091EE3B", // Registre aquí su apiKey
15+
"SECRETKEY" => "f8b45f9b8bcdb5702dc86a1b894492303741c405", // Registre aquí su secretKey
16+
"APIURL" => "https://www.flow.cl/api", // Producción EndPoint o Sandbox EndPoint
17+
"BASEURL" => "http://localhost/apiFlow" //Registre aquí la URL base en su página donde instalará el cliente
18+
);
19+
20+
class Config {
21+
22+
static function get($name) {
23+
global $COMMERCE_CONFIG;
24+
if(!isset($COMMERCE_CONFIG[$name])) {
25+
throw new Exception("The configuration element thas not exist", 1);
26+
}
27+
return $COMMERCE_CONFIG[$name];
28+
}
29+
}

lib/FlowApi.class.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
/**
4+
* Clase cliente del Api2 de Flow
5+
* @Filename: FlowApi.class.php
6+
* @version: 2.0
7+
* @Author: flow.cl
8+
* @Email: csepulveda@tuxpan.com
9+
* @Date: 28-04-2017 11:32
10+
* @Last Modified by: Carlos Sepulveda
11+
* @Last Modified time: 28-04-2017 11:32
12+
*/
13+
14+
require(__DIR__ . "/Config.class.php");
15+
16+
class FlowApi {
17+
18+
protected $apiKey;
19+
protected $secretKey;
20+
21+
22+
public function __construct() {
23+
$this->apiKey = Config::get("APIKEY");
24+
$this->secretKey = Config::get("SECRETKEY");
25+
}
26+
27+
28+
/**
29+
* Funcion que invoca un servicio del Api de Flow
30+
* @param string $service Nombre del servicio a ser invocado
31+
* @param array $params datos a ser enviados
32+
* @param string $method metodo http a utilizar
33+
* @return string en formato JSON
34+
*/
35+
public function send( $service, $params, $method = "GET") {
36+
$method = strtoupper($method);
37+
$url = Config::get("APIURL") . "/" . $service;
38+
$params = array("apiKey" => $this->apiKey) + $params;
39+
$data = $this->getPack($params, $method);
40+
$sign = $this->sign($params);
41+
if($method == "GET") {
42+
$response = $this->httpGet($url, $data, $sign);
43+
} else {
44+
$response = $this->httpPost($url, $data, $sign);
45+
}
46+
47+
if(isset($response["info"])) {
48+
$code = $response["info"]["http_code"];
49+
$body = json_decode($response["output"], true);
50+
if($code == "200") {
51+
return $body;
52+
} elseif(in_array($code, array("400", "401"))) {
53+
throw new Exception($body["message"], $body["code"]);
54+
} else {
55+
throw new Exception("Unexpected error occurred. HTTP_CODE: " .$code , $code);
56+
}
57+
} else {
58+
throw new Exception("Unexpected error occurred.");
59+
}
60+
}
61+
62+
/**
63+
* Funcion para setear el apiKey y secretKey y no usar los de la configuracion
64+
*/
65+
public function setKeys($apiKey, $secretKey) {
66+
$this->apiKey = $apiKey;
67+
$this->secretKey = $secretKey;
68+
}
69+
70+
/**
71+
* Funcion que empaqueta los datos de parametros para ser enviados
72+
* @param array $params datos a ser empaquetados
73+
* @param string $method metodo http a utilizar
74+
*/
75+
private function getPack($params, $method) {
76+
$keys = array_keys($params);
77+
sort($keys);
78+
$data = "";
79+
foreach ($keys as $key) {
80+
if($method == "GET") {
81+
$data .= "&" . rawurlencode($key) . "=" . rawurlencode($params[$key]);
82+
} else {
83+
$data .= "&" . $key . "=" . $params[$key];
84+
}
85+
}
86+
return substr($data, 1);
87+
}
88+
89+
/**
90+
* Funcion que firma los parametros
91+
* @param string $params Parametros a firmar
92+
* @return string de firma
93+
*/
94+
private function sign($params) {
95+
$keys = array_keys($params);
96+
sort($keys);
97+
$toSign = "";
98+
foreach ($keys as $key) {
99+
$toSign .= "&" . $key . "=" . $params[$key];
100+
}
101+
$toSign = substr($toSign, 1);
102+
if(!function_exists("hash_hmac")) {
103+
throw new Exception("function hash_hmac not exist", 1);
104+
}
105+
return hash_hmac('sha256', $toSign , $this->secretKey);
106+
}
107+
108+
109+
/**
110+
* Funcion que hace el llamado via http GET
111+
* @param string $url url a invocar
112+
* @param array $data datos a enviar
113+
* @param string $sign firma de los datos
114+
* @return string en formato JSON
115+
*/
116+
private function httpGet($url, $data, $sign) {
117+
$url = $url . "?" . $data . "&s=" . $sign;
118+
$ch = curl_init();
119+
curl_setopt($ch, CURLOPT_URL, $url);
120+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
121+
$output = curl_exec($ch);
122+
if($output === false) {
123+
$error = curl_error($ch);
124+
throw new Exception($error, 1);
125+
}
126+
$info = curl_getinfo($ch);
127+
curl_close($ch);
128+
return array("output" =>$output, "info" => $info);
129+
}
130+
131+
/**
132+
* Funcion que hace el llamado via http POST
133+
* @param string $url url a invocar
134+
* @param array $data datos a enviar
135+
* @param string $sign firma de los datos
136+
* @return string en formato JSON
137+
*/
138+
private function httpPost($url, $data, $sign ) {
139+
$ch = curl_init();
140+
curl_setopt($ch, CURLOPT_URL, $url);
141+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
142+
curl_setopt($ch, CURLOPT_POST, TRUE);
143+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data . "&s=" . $sign);
144+
$output = curl_exec($ch);
145+
if($output === false) {
146+
$error = curl_error($ch);
147+
throw new Exception($error, 1);
148+
}
149+
$info = curl_getinfo($ch);
150+
curl_close($ch);
151+
return array("output" =>$output, "info" => $info);
152+
}
153+
154+
155+
}

0 commit comments

Comments
 (0)