Skip to content

Commit 36810f9

Browse files
committed
Revert "Update FlowApi.class.php"
This reverts commit ace67f3.
1 parent 8738bb6 commit 36810f9

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

lib/FlowApi.class.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Clase cliente del Api2 de Flow
55
* @Filename: FlowApi.class.php
6-
* @version: 2.1
6+
* @version: 2.0
77
* @Author: flow.cl
88
* @Email: csepulveda@tuxpan.com
99
* @Date: 28-04-2017 11:32
@@ -36,21 +36,27 @@ public function send( $service, $params, $method = "GET") {
3636
$method = strtoupper($method);
3737
$url = Config::get("APIURL") . "/" . $service;
3838
$params = array("apiKey" => $this->apiKey) + $params;
39-
$params["s"] = $this->sign($params);
39+
$data = $this->getPack($params, $method);
40+
$sign = $this->sign($params);
4041
if($method == "GET") {
41-
$response = $this->httpGet($url, $params);
42+
$response = $this->httpGet($url, $data, $sign);
4243
} else {
43-
$response = $this->httpPost($url, $params);
44+
$response = $this->httpPost($url, $data, $sign);
4445
}
4546

4647
if(isset($response["info"])) {
4748
$code = $response["info"]["http_code"];
48-
if (!in_array($code, array("200", "400", "401"))) {
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 {
4955
throw new Exception("Unexpected error occurred. HTTP_CODE: " .$code , $code);
5056
}
57+
} else {
58+
throw new Exception("Unexpected error occurred.");
5159
}
52-
$body = json_decode($response["output"], true);
53-
return $body;
5460
}
5561

5662
/**
@@ -61,6 +67,24 @@ public function setKeys($apiKey, $secretKey) {
6167
$this->secretKey = $secretKey;
6268
}
6369

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+
}
6488

6589
/**
6690
* Funcion que firma los parametros
@@ -72,8 +96,9 @@ private function sign($params) {
7296
sort($keys);
7397
$toSign = "";
7498
foreach ($keys as $key) {
75-
$toSign .= $key . $params[$key];
99+
$toSign .= "&" . $key . "=" . $params[$key];
76100
}
101+
$toSign = substr($toSign, 1);
77102
if(!function_exists("hash_hmac")) {
78103
throw new Exception("function hash_hmac not exist", 1);
79104
}
@@ -88,8 +113,8 @@ private function sign($params) {
88113
* @param string $sign firma de los datos
89114
* @return string en formato JSON
90115
*/
91-
private function httpGet($url, $params) {
92-
$url = $url . "?" . http_build_query($params);
116+
private function httpGet($url, $data, $sign) {
117+
$url = $url . "?" . $data . "&s=" . $sign;
93118
$ch = curl_init();
94119
curl_setopt($ch, CURLOPT_URL, $url);
95120
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
@@ -110,12 +135,12 @@ private function httpGet($url, $params) {
110135
* @param string $sign firma de los datos
111136
* @return string en formato JSON
112137
*/
113-
private function httpPost($url, $params ) {
138+
private function httpPost($url, $data, $sign ) {
114139
$ch = curl_init();
115140
curl_setopt($ch, CURLOPT_URL, $url);
116141
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
117142
curl_setopt($ch, CURLOPT_POST, TRUE);
118-
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
143+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data . "&s=" . $sign);
119144
$output = curl_exec($ch);
120145
if($output === false) {
121146
$error = curl_error($ch);

0 commit comments

Comments
 (0)