-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mais classes utilitárias adicionadas
- Loading branch information
Paulo Elias
committed
Feb 7, 2019
1 parent
3ee1659
commit 91a9577
Showing
3 changed files
with
250 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
namespace Pekit; | ||
|
||
class IfThenPay | ||
{ | ||
static function format_number ($number) | ||
{ | ||
$verifySepDecimal = number_format (99, 2); | ||
|
||
$valorTmp = $number; | ||
|
||
$sepDecimal = substr ($verifySepDecimal, 2, 1); | ||
|
||
$hasSepDecimal = true; | ||
|
||
$i = (strlen ($valorTmp) - 1); | ||
|
||
for ($i; $i != 0; $i -= 1) { | ||
if (substr ($valorTmp, $i, 1) == "." || substr ($valorTmp, $i, 1) == ",") { | ||
$hasSepDecimal = true; | ||
$valorTmp = trim (substr ($valorTmp, 0, $i)) . "@" . trim (substr ($valorTmp, 1 + $i)); | ||
break; | ||
} | ||
} | ||
|
||
if ($hasSepDecimal != true) { | ||
$valorTmp = number_format ($valorTmp, 2); | ||
|
||
$i = (strlen ($valorTmp) - 1); | ||
|
||
for ($i; $i != 1; $i--) { | ||
if (substr ($valorTmp, $i, 1) == "." || substr ($valorTmp, $i, 1) == ",") { | ||
$hasSepDecimal = true; | ||
$valorTmp = trim (substr ($valorTmp, 0, $i)) . "@" . trim (substr ($valorTmp, 1 + $i)); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
for ($i = 1; $i != (strlen ($valorTmp) - 1); $i++) { | ||
if (substr ($valorTmp, $i, 1) == "." || substr ($valorTmp, $i, 1) == "," || substr ($valorTmp, $i, 1) == " ") { | ||
$valorTmp = trim (substr ($valorTmp, 0, $i)) . trim (substr ($valorTmp, 1 + $i)); | ||
break; | ||
} | ||
} | ||
|
||
if (strlen (strstr ($valorTmp, '@')) > 0) { | ||
$valorTmp = trim (substr ($valorTmp, 0, strpos ($valorTmp, '@'))) . trim ($sepDecimal) . | ||
trim (substr ($valorTmp, strpos ($valorTmp, '@') + 1)); | ||
} | ||
|
||
return $valorTmp; | ||
} | ||
|
||
static function GenerateMbRef ($ent_id, $subent_id, $order_id, $order_value) | ||
{ | ||
$chk_val = 0; | ||
$msg = ""; | ||
$order_id = "0000" . $order_id; | ||
|
||
if (strlen ($ent_id) < 5) | ||
$msg .= "Lamentamos mas tem de indicar uma entidade válida"; | ||
else if (strlen ($ent_id) > 5) | ||
$msg .= "Lamentamos mas tem de indicar uma entidade válida"; | ||
|
||
if (strlen ($subent_id) == 0) | ||
$msg .= "Lamentamos mas tem de indicar uma subentidade válida"; | ||
|
||
$order_value = sprintf ("%01.2f", $order_value); | ||
|
||
$order_value = self::format_number ($order_value); | ||
|
||
if ($order_value < 1) | ||
$msg .= "Lamentamos mas é impossível gerar uma referência MB para valores inferiores a 1 Euro"; | ||
|
||
if ($order_value >= 1000000) | ||
$msg .= "Pagamento fraccionado por exceder o valor limite para pagamentos no sistema Multibanco"; | ||
|
||
if (strlen ($subent_id) == 1) { | ||
//Apenas sao considerados os 6 caracteres mais a direita do order_id | ||
$order_id = substr ($order_id, (strlen ($order_id) - 6), strlen ($order_id)); | ||
$chk_str = sprintf ('%05u%01u%06u%08u', $ent_id, $subent_id, $order_id, round ($order_value * 100)); | ||
} | ||
else if (strlen ($subent_id) == 2) { | ||
//Apenas sao considerados os 5 caracteres mais a direita do order_id | ||
$order_id = substr ($order_id, (strlen ($order_id) - 5), strlen ($order_id)); | ||
$chk_str = sprintf ('%05u%02u%05u%08u', $ent_id, $subent_id, $order_id, round ($order_value * 100)); | ||
} | ||
else { | ||
//Apenas sao considerados os 4 caracteres mais a direita do order_id | ||
$order_id = substr ($order_id, (strlen ($order_id) - 4), strlen ($order_id)); | ||
$chk_str = sprintf ('%05u%03u%04u%08u', $ent_id, $subent_id, $order_id, round ($order_value * 100)); | ||
} | ||
|
||
//cálculo dos check digits | ||
|
||
$chk_array = [3, 30, 9, 90, 27, 76, 81, 34, 49, 5, 50, 15, 53, 45, 62, 38, 89, 17, 73, 51]; | ||
|
||
for ($i = 0; $i < 20; $i++) { | ||
$chk_int = substr ($chk_str, 19 - $i, 1); | ||
$chk_val += ($chk_int % 10) * $chk_array[$i]; | ||
} | ||
|
||
$chk_val %= 97; | ||
|
||
$chk_digits = sprintf ('%02u', 98 - $chk_val); | ||
|
||
if ($msg) | ||
return ['message' => $msg]; | ||
|
||
$data_mb = [ | ||
'entidade' => $ent_id, | ||
'referencia' => substr ($chk_str, 5, 3) . substr ($chk_str, 8, 3) . substr ($chk_str, 11, 1) . $chk_digits, | ||
'valor' => number_format ($order_value, 2, ',', ' ') | ||
]; | ||
return $data_mb; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Pekit; | ||
|
||
class UUID | ||
{ | ||
public static function v3($namespace, $name) | ||
{ | ||
if (!self::is_valid($namespace)) return false; | ||
|
||
// Get hexadecimal components of namespace | ||
$nhex = str_replace(array('-', '{', '}'), '', $namespace); | ||
|
||
// Binary Value | ||
$nstr = ''; | ||
|
||
// Convert Namespace UUID to bits | ||
for ($i = 0; $i < strlen($nhex); $i += 2) | ||
{ | ||
$nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); | ||
} | ||
|
||
// Calculate hash value | ||
$hash = md5($nstr . $name); | ||
|
||
return sprintf('%08s-%04s-%04x-%04x-%12s', | ||
|
||
// 32 bits for "time_low" | ||
substr($hash, 0, 8), | ||
|
||
// 16 bits for "time_mid" | ||
substr($hash, 8, 4), | ||
|
||
// 16 bits for "time_hi_and_version", | ||
// four most significant bits holds version number 3 | ||
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, | ||
|
||
// 16 bits, 8 bits for "clk_seq_hi_res", | ||
// 8 bits for "clk_seq_low", | ||
// two most significant bits holds zero and one for variant DCE1.1 | ||
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, | ||
|
||
// 48 bits for "node" | ||
substr($hash, 20, 12) | ||
); | ||
} | ||
|
||
public static function v4() | ||
{ | ||
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', | ||
|
||
// 32 bits for "time_low" | ||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), | ||
|
||
// 16 bits for "time_mid" | ||
mt_rand(0, 0xffff), | ||
|
||
// 16 bits for "time_hi_and_version", | ||
// four most significant bits holds version number 4 | ||
mt_rand(0, 0x0fff) | 0x4000, | ||
|
||
// 16 bits, 8 bits for "clk_seq_hi_res", | ||
// 8 bits for "clk_seq_low", | ||
// two most significant bits holds zero and one for variant DCE1.1 | ||
mt_rand(0, 0x3fff) | 0x8000, | ||
|
||
// 48 bits for "node" | ||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) | ||
); | ||
} | ||
|
||
public static function v5($namespace, $name) | ||
{ | ||
if (!self::is_valid($namespace)) return false; | ||
|
||
// Get hexadecimal components of namespace | ||
$nhex = str_replace(array('-', '{', '}'), '', $namespace); | ||
|
||
// Binary Value | ||
$nstr = ''; | ||
|
||
// Convert Namespace UUID to bits | ||
for ($i = 0; $i < strlen($nhex); $i += 2) | ||
{ | ||
$nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); | ||
} | ||
|
||
// Calculate hash value | ||
$hash = sha1($nstr . $name); | ||
|
||
return sprintf('%08s-%04s-%04x-%04x-%12s', | ||
|
||
// 32 bits for "time_low" | ||
substr($hash, 0, 8), | ||
|
||
// 16 bits for "time_mid" | ||
substr($hash, 8, 4), | ||
|
||
// 16 bits for "time_hi_and_version", | ||
// four most significant bits holds version number 5 | ||
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, | ||
|
||
// 16 bits, 8 bits for "clk_seq_hi_res", | ||
// 8 bits for "clk_seq_low", | ||
// two most significant bits holds zero and one for variant DCE1.1 | ||
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, | ||
|
||
// 48 bits for "node" | ||
substr($hash, 20, 12) | ||
); | ||
} | ||
|
||
public static function is_valid($uuid) | ||
{ | ||
return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . | ||
'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters