Untitled
unknown
php
2 years ago
3.8 kB
6
Indexable
<?php
namespace App\Service;
use Exception;
class Flow
{
const URL_DEV = 'https://sandbox.flow.cl/api';
const URL_PROD = 'https://www.flow.cl/api';
const SECRET_KEY_DEV = 'e97bee0d27e7189e60f63d1946ab66d4b547e530';
const API_KEY_DEV = '32DF11A7-A099-4EA0-B926-22L912E75A91';
public static function getSignature($params)
{
$secretKey = self::SECRET_KEY_DEV;
$default = array(
"apiKey" => self::API_KEY_DEV
);
$final = array_merge($default, $params);
$keys = array_keys($final);
sort($keys);
$toSign = "";
foreach ($keys as $key) {
$toSign .= $key . $final[$key];
};
return hash_hmac('sha256', $toSign, $secretKey);
}
public static function generateUrl($product)
{
$response = false;
$secretKey = self::SECRET_KEY_DEV;
$default = array(
"apiKey" => self::API_KEY_DEV
);
$params = array_merge($default, $product);
$keys = array_keys($params);
sort($keys);
$toSign = "";
foreach ($keys as $key) {
$toSign .= $key . $params[$key];
};
$signature = hash_hmac('sha256', $toSign, $secretKey);
$url = self::URL_DEV;
$url = $url . '/payment/create';
$params["s"] = $signature;
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
throw new Exception($error, 1);
}
$info = curl_getinfo($ch);
if (!in_array($info['http_code'], array('200', '400', '401'))) {
throw new Exception('Unexpected error occurred. HTTP_CODE: ' . $info['http_code'], $info['http_code']);
}
} catch (Exception $e) {
echo 'Error: ' . $e->getCode() . ' - ' . $e->getMessage();
}
return json_decode($response);
}
public static function getStatus($token)
{
$response = false;
$secretKey = self::SECRET_KEY_DEV;
$params = array(
"apiKey" => self::API_KEY_DEV,
'token' => $token
);
$keys = array_keys($params);
sort($keys);
$toSign = "";
foreach ($keys as $key) {
$toSign .= $key . $params[$key];
};
$signature = hash_hmac('sha256', $toSign, $secretKey);
$url = self::URL_DEV;
$url = $url . '/payment/getStatus';
$params["s"] = $signature;
$url = $url . "?" . http_build_query($params);
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
throw new Exception($error, 1);
}
$info = curl_getinfo($ch);
if (!in_array($info['http_code'], array('200', '400', '401'))) {
throw new Exception('Unexpected error occurred. HTTP_CODE: ' . $info['http_code'], $info['http_code']);
}
} catch (Exception $e) {
echo 'Error: ' . $e->getCode() . ' - ' . $e->getMessage();
}
return json_decode($response);
}
}Editor is loading...