├── README.md ├── examples └── payments │ ├── confirm.php │ ├── create.php │ ├── createEmail.php │ ├── getStatusByCommerceId.php │ └── result.php └── lib ├── Config.class.php └── FlowApi.class.php /README.md: -------------------------------------------------------------------------------- 1 | # PHP-API-CLIENT 2 | Cliente PHP para consumir el API de Flow. 3 | 4 | **Flow.cl** es una pasarela de pagos para comercio electrónico. Este cliente le permite integrar su ecommerce para recibir pagos online. 5 | 6 | ## Requerimientos 7 | * PHP 5.2 o superior 8 | * php_curl activado 9 | * php_openssl activado 10 | 11 | ## Instalación 12 | Baje la última versión y copie los archivos en su servidor. 13 | 14 | ## Documentación 15 | La documentación completa del API REST de Flow la encuentrá aquí: https://www.flow.cl/docs/api.html 16 | 17 | ## Comenzando 18 | ### Configurando el cliente 19 | Configure correctamente el cliente en el archivo **lib/Config.class.php**. 20 | Lo primero que debe configurar es su apiKey y secretKey del comercio registrado en Flow. Esto lo obtiene en la sección **Mis datos** acceda a https://www.flow.cl, una vez autenticado con su cuenta Flow, seleccione Mis datos y recupere su apiKey y secretKey desde la sección Seguridad. 21 | - **APIKEY** el apiKey obtenida desde su cuenta Flow 22 | - **SECRETKEY** el secretKey obtenida desde su cuenta Flow 23 | - **APIURL** la URL del endpoint del API de Flow, Aquí podrá configurar el endpoint de producción o del sandbox. Esta información se obtiene en la documentación del API https://www.flow.cl/docs/api.html 24 | - **BASEURL** La URL base donde instaló el cliente PHP en su servidor 25 | 26 | 27 | ```php 28 | $COMMERCE_CONFIG = array( 29 | "APIKEY" => "1F90971E-8276-4713-97FF-2BLF5091EE3B", // Registre aquí su apiKey 30 | "SECRETKEY" => "f8b45f9b8bcdb5702dc86a1b894492303741c405", // Registre aquí su secretKey 31 | "APIURL" => "https://www.flow.cl/api", // Producción EndPoint o Sandbox EndPoint 32 | "BASEURL" => "https://www.misitio/apiFlow" //Registre aquí la URL base en su página donde instaló el cliente 33 | ); 34 | ``` 35 | ### Llamando a un servicio 36 | En este ejemplo crearemos una Orden de Cobro y redireccionaremos el browser del pagador para efectuar el pago 37 | ```php 38 | "9999999-9", 48 | "otroDato" => "otroDato" 49 | ); 50 | $optional = json_encode($optional); 51 | 52 | //Prepara el arreglo de datos 53 | $params = array( 54 | "commerceOrder" => rand(1100,2000), 55 | "subject" => "Pago de prueba", 56 | "currency" => "CLP", 57 | "amount" => 5000, 58 | "email" => "cliente@gmail.com", 59 | "paymentMethod" => 9, 60 | "urlConfirmation" => Config::get("BASEURL") . "/examples/payments/confirm.php", 61 | "urlReturn" => Config::get("BASEURL") ."/examples/payments/result.php", 62 | "optional" => $optional 63 | ); 64 | //Define el metodo a usar 65 | $serviceName = "payment/create"; 66 | 67 | try { 68 | // Instancia la clase FlowApi 69 | $flowApi = new FlowApi; 70 | // Ejecuta el servicio 71 | $response = $flowApi->send($serviceName, $params,"POST"); 72 | //Prepara url para redireccionar el browser del pagador 73 | $redirect = $response["url"] . "?token=" . $response["token"]; 74 | header("location:$redirect"); 75 | } catch (Exception $e) { 76 | echo $e->getCode() . " - " . $e->getMessage(); 77 | } 78 | ?> 79 | ``` 80 | Otros ejemplos los podrá ver en la carpeta **examples** de este cliente. 81 | -------------------------------------------------------------------------------- /examples/payments/confirm.php: -------------------------------------------------------------------------------- 1 | $token 15 | ); 16 | $serviceName = "payment/getStatus"; 17 | $flowApi = new FlowApi(); 18 | $response = $flowApi->send($serviceName, $params, "GET"); 19 | 20 | //Actualiza los datos en su sistema 21 | 22 | print_r($response); 23 | 24 | 25 | } catch (Exception $e) { 26 | echo "Error: " . $e->getCode() . " - " . $e->getMessage(); 27 | } 28 | ?> -------------------------------------------------------------------------------- /examples/payments/create.php: -------------------------------------------------------------------------------- 1 | "9999999-9", 11 | "otroDato" => "otroDato" 12 | ); 13 | $optional = json_encode($optional); 14 | 15 | //Prepara el arreglo de datos 16 | $params = array( 17 | "commerceOrder" => rand(1100,2000), 18 | "subject" => "Pago de prueba", 19 | "currency" => "CLP", 20 | "amount" => 5000, 21 | "email" => "cliente@gmail.com", 22 | "paymentMethod" => 9, 23 | "urlConfirmation" => Config::get("BASEURL") . "/examples/payments/confirm.php", 24 | "urlReturn" => Config::get("BASEURL") ."/examples/payments/result.php", 25 | "optional" => $optional 26 | ); 27 | //Define el metodo a usar 28 | $serviceName = "payment/create"; 29 | 30 | try { 31 | // Instancia la clase FlowApi 32 | $flowApi = new FlowApi; 33 | // Ejecuta el servicio 34 | $response = $flowApi->send($serviceName, $params,"POST"); 35 | //Prepara url para redireccionar el browser del pagador 36 | $redirect = $response["url"] . "?token=" . $response["token"]; 37 | header("location:$redirect"); 38 | } catch (Exception $e) { 39 | echo $e->getCode() . " - " . $e->getMessage(); 40 | } 41 | 42 | ?> -------------------------------------------------------------------------------- /examples/payments/createEmail.php: -------------------------------------------------------------------------------- 1 | rand(1100,2000), 11 | "subject" => "pago prueba cobro Email", 12 | "currency" => "CLP", 13 | "amount" => 2000, 14 | "email" => "cliente@gmail.com", 15 | "paymentMethod" => 9, 16 | "urlConfirmation" => Config::get("BASEURL") . "/examples/payments/confirm.php", 17 | "urlReturn" => Config::get("BASEURL") ."/examples/payments/result.php", 18 | "forward_days_after" => 1, 19 | "forward_times" => 2 20 | ); 21 | 22 | $serviceName = "payment/createEmail"; 23 | 24 | try { 25 | $flowApi = new FlowApi; 26 | 27 | $response = $flowApi->send($serviceName, $params,"POST"); 28 | 29 | print_r($response); 30 | 31 | 32 | } catch (Exception $e) { 33 | echo $e->getCode() . " - " . $e->getMessage(); 34 | } 35 | 36 | ?> -------------------------------------------------------------------------------- /examples/payments/getStatusByCommerceId.php: -------------------------------------------------------------------------------- 1 | "1306" 10 | ); 11 | $serviceName = "payment/getStatusByCommerceId"; 12 | $flowApi = new FlowApi(); 13 | $response = $flowApi->send($serviceName, $params, "GET"); 14 | 15 | print_r($response); 16 | 17 | 18 | } catch (Exception $e) { 19 | echo "Error: " . $e->getCode() . " - " . $e->getMessage(); 20 | } 21 | 22 | ?> -------------------------------------------------------------------------------- /examples/payments/result.php: -------------------------------------------------------------------------------- 1 | $token 18 | ); 19 | //Indica el servicio a utilizar 20 | $serviceName = "payment/getStatus"; 21 | $flowApi = new FlowApi(); 22 | $response = $flowApi->send($serviceName, $params, "GET"); 23 | 24 | print_r($response); 25 | 26 | } catch (Exception $e) { 27 | echo "Error: " . $e->getCode() . " - " . $e->getMessage(); 28 | } 29 | 30 | ?> -------------------------------------------------------------------------------- /lib/Config.class.php: -------------------------------------------------------------------------------- 1 | "mi apiKey", // Registre aquí su apiKey 15 | "SECRETKEY" => "mi secretKey", // Registre aquí su secretKey 16 | "APIURL" => "https://www.flow.cl/api", // Producción EndPoint o Sandbox EndPoint 17 | "BASEURL" => "https://www.micomercio.cl/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 | } 30 | -------------------------------------------------------------------------------- /lib/FlowApi.class.php: -------------------------------------------------------------------------------- 1 | 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 | * @throws Exception 35 | */ 36 | public function send( $service, $params, $method = "GET") { 37 | $method = strtoupper($method); 38 | $url = Config::get("APIURL") . "/" . $service; 39 | $params = array("apiKey" => $this->apiKey) + $params; 40 | $params["s"] = $this->sign($params); 41 | if($method == "GET") { 42 | $response = $this->httpGet($url, $params); 43 | } else { 44 | $response = $this->httpPost($url, $params); 45 | } 46 | 47 | if(isset($response["info"])) { 48 | $code = $response["info"]["http_code"]; 49 | if (!in_array($code, array("200", "400", "401"))) { 50 | throw new Exception("Unexpected error occurred. HTTP_CODE: " .$code , $code); 51 | } 52 | } 53 | $body = json_decode($response["output"], true); 54 | return $body; 55 | } 56 | 57 | /** 58 | * Funcion para setear el apiKey y secretKey y no usar los de la configuracion 59 | * @param string $apiKey apiKey del cliente 60 | * @param string $secretKey secretKey del cliente 61 | */ 62 | public function setKeys($apiKey, $secretKey) { 63 | $this->apiKey = $apiKey; 64 | $this->secretKey = $secretKey; 65 | } 66 | 67 | 68 | /** 69 | * Funcion que firma los parametros 70 | * @param string $params Parametros a firmar 71 | * @return string de firma 72 | * @throws Exception 73 | */ 74 | private function sign($params) { 75 | $keys = array_keys($params); 76 | sort($keys); 77 | $toSign = ""; 78 | foreach ($keys as $key) { 79 | $toSign .= $key . $params[$key]; 80 | } 81 | if(!function_exists("hash_hmac")) { 82 | throw new Exception("function hash_hmac not exist", 1); 83 | } 84 | return hash_hmac('sha256', $toSign , $this->secretKey); 85 | } 86 | 87 | 88 | /** 89 | * Funcion que hace el llamado via http GET 90 | * @param string $url url a invocar 91 | * @param array $params los datos a enviar 92 | * @return array el resultado de la llamada 93 | * @throws Exception 94 | */ 95 | private function httpGet($url, $params) { 96 | $url = $url . "?" . http_build_query($params); 97 | $ch = curl_init(); 98 | curl_setopt($ch, CURLOPT_URL, $url); 99 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 100 | $output = curl_exec($ch); 101 | if($output === false) { 102 | $error = curl_error($ch); 103 | throw new Exception($error, 1); 104 | } 105 | $info = curl_getinfo($ch); 106 | curl_close($ch); 107 | return array("output" =>$output, "info" => $info); 108 | } 109 | 110 | /** 111 | * Funcion que hace el llamado via http POST 112 | * @param string $url url a invocar 113 | * @param array $params los datos a enviar 114 | * @return array el resultado de la llamada 115 | * @throws Exception 116 | */ 117 | private function httpPost($url, $params ) { 118 | $ch = curl_init(); 119 | curl_setopt($ch, CURLOPT_URL, $url); 120 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 121 | curl_setopt($ch, CURLOPT_POST, TRUE); 122 | curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 123 | $output = curl_exec($ch); 124 | if($output === false) { 125 | $error = curl_error($ch); 126 | throw new Exception($error, 1); 127 | } 128 | $info = curl_getinfo($ch); 129 | curl_close($ch); 130 | return array("output" =>$output, "info" => $info); 131 | } 132 | 133 | 134 | } 135 | --------------------------------------------------------------------------------