-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStaticPix.php
53 lines (47 loc) · 1.73 KB
/
StaticPix.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace PixPhp;
class StaticPix
{
public static function formatField($id, $value)
{
return $id . str_pad(strlen($value), 2, '0', STR_PAD_LEFT) . $value;
}
public static function calculateCRC16($data)
{
$result = 0xFFFF;
for ($i = 0; $i < strlen($data); $i++) {
$result ^= (ord($data[$i]) << 8);
for ($j = 0; $j < 8; $j++) {
if ($result & 0x8000) {
$result = ($result << 1) ^ 0x1021;
} else {
$result <<= 1;
}
$result &= 0xFFFF;
}
}
return strtoupper(str_pad(dechex($result), 4, '0', STR_PAD_LEFT));
}
public static function generatePix($key, $idTx = '', $amount = 0.00, $description = '')
{
$result = "000201";
$result .= self::formatField("26", "0014br.gov.bcb.pix" . self::formatField("01", PixKey::formatKey($key)));
$result .= "52040000"; // Fixed code
$result .= "5303986"; // Currency (Real)
if ($amount > 0) {
$result .= self::formatField("54", number_format($amount, 2, '.', ''));
}
$result .= "5802BR"; // Country
$result .= "5901N"; // Name
$result .= "6001C"; // City
// Description field (if provided)
if (!empty($description)) {
$result .= self::formatField("62", self::formatField("05", $idTx ?: '***') . self::formatField("50", $description));
} else {
$result .= self::formatField("62", self::formatField("05", $idTx ?: '***'));
}
$result .= "6304"; // Start of CRC16
$result .= self::calculateCRC16($result); // Add CRC16 at the end
return $result;
}
}