|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BMorais\Pix; |
|
4
|
|
|
|
|
5
|
|
|
use Mpdf\QrCode\Output; |
|
6
|
|
|
use Mpdf\QrCode\QrCode; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* CLASSE PIX |
|
10
|
|
|
* Esta classe é responsavel por gerar o pix |
|
11
|
|
|
* |
|
12
|
|
|
* @author Bruno Morais <[email protected]> |
|
13
|
|
|
* @copyright GPL © 2022, bmorais.com |
|
14
|
|
|
* @package bmorais\pix |
|
15
|
|
|
* @subpackage class |
|
16
|
|
|
* @access private |
|
17
|
|
|
*/ |
|
18
|
|
|
class Pix { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* IDs do Payload do Pix |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
const ID_PAYLOAD_FORMAT_INDICATOR = '00'; |
|
25
|
|
|
const ID_MERCHANT_ACCOUNT_INFORMATION = '26'; |
|
26
|
|
|
|
|
27
|
|
|
const ID_MERCHANT_ACCOUNT_INFORMATION_GUI = '00'; |
|
28
|
|
|
const ID_MERCHANT_ACCOUNT_INFORMATION_CHAVE = '01'; |
|
29
|
|
|
const ID_MERCHANT_ACCOUNT_INFORMATION_DESCRIPTION = '02'; |
|
30
|
|
|
|
|
31
|
|
|
const ID_MERCHANT_CATEGORY_CODE = '52'; |
|
32
|
|
|
const ID_TRANSACTION_CURRENCY = '53'; |
|
33
|
|
|
const ID_TRANSACTION_AMOUNT = '54'; |
|
34
|
|
|
const ID_COUNTRY_CODE = '58'; |
|
35
|
|
|
const ID_MERCHANT_NAME = '59'; |
|
36
|
|
|
const ID_MERCHANT_CITY = '60'; |
|
37
|
|
|
|
|
38
|
|
|
const ID_ADDITIONAL_DATA_FIELD_TEMPLATE = '62'; |
|
39
|
|
|
const ID_ADDITIONAL_DATA_FIELD_TEMPLATE_TXID = '05'; |
|
40
|
|
|
|
|
41
|
|
|
const ID_CRC16 = '63'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Chave Pix |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $pixKey; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Descrição do pagamento |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $description; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Nome do titular da conta |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private $merchantName; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Cidade do titular da conta |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
private $merchantCity; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* ID de transação Pix |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
private $txid; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Valor da transação |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
private $amount; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Método responsável por definir o valor de $pixKey |
|
81
|
|
|
* @param string $pixKey |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setPixKey($pixKey) { |
|
84
|
|
|
$this->pixKey = $pixKey; |
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Método responsável por definir o valor de $description |
|
90
|
|
|
* @param string $description |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setDescription($description) { |
|
93
|
|
|
$this->description = $description; |
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Método responsável por definir o valor de $merchantName |
|
99
|
|
|
* @param string $merchantName |
|
100
|
|
|
*/ |
|
101
|
|
|
public function setMerchantName($merchantName) { |
|
102
|
|
|
$this->merchantName = $merchantName; |
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Método responsável por definir o valor de $merchantCity |
|
108
|
|
|
* @param string $merchantCity |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setMerchantCity($merchantCity) { |
|
111
|
|
|
$this->merchantCity = $merchantCity; |
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Método responsável por definir o valor de $txid |
|
117
|
|
|
* @param string $txid |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setTxid($txid) { |
|
120
|
|
|
$this->txid = $txid; |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param $amount |
|
126
|
|
|
* @return $this |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setAmount($amount) { |
|
129
|
|
|
$this->amount = number_format($amount, 2, ',', '.'); |
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Método responsável por retornar o valor completo de um objeto pauload |
|
135
|
|
|
* @param string $id |
|
136
|
|
|
* @param string $value |
|
137
|
|
|
* @return string $id.$size.$value |
|
138
|
|
|
*/ |
|
139
|
|
|
private function getValue($id, $value) { |
|
140
|
|
|
$size = str_pad(strlen($value), 2, '0', STR_PAD_LEFT); |
|
141
|
|
|
|
|
142
|
|
|
return $id.$size.$value; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Método responsável por retornar valores completos da informação da conta |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
private function getMerchantAccountInformation() { |
|
150
|
|
|
// Domínio do Banco Central |
|
151
|
|
|
$gui = $this->getValue(self::ID_MERCHANT_ACCOUNT_INFORMATION_GUI, 'br.gov.bcb.pix'); |
|
152
|
|
|
|
|
153
|
|
|
// Chave Pix |
|
154
|
|
|
$key = $this->getValue(self::ID_MERCHANT_ACCOUNT_INFORMATION_CHAVE, $this->pixKey); |
|
155
|
|
|
|
|
156
|
|
|
// Descrição do pagamento |
|
157
|
|
|
$description = |
|
158
|
|
|
strlen($this->description) ? |
|
159
|
|
|
$this->getValue(self::ID_MERCHANT_ACCOUNT_INFORMATION_DESCRIPTION, $this->description) : |
|
160
|
|
|
''; |
|
161
|
|
|
|
|
162
|
|
|
// Retorna o valor completo da conta |
|
163
|
|
|
return $this->getValue(self::ID_MERCHANT_ACCOUNT_INFORMATION, $gui.$key.$description); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Método responsável por retornar os valores completos do campo adicional do pix ($txid) |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
private function getAdditionalDataFieldTemplate() { |
|
171
|
|
|
// txid |
|
172
|
|
|
$txid = $this->getValue(self::ID_ADDITIONAL_DATA_FIELD_TEMPLATE_TXID, $this->txid); |
|
173
|
|
|
|
|
174
|
|
|
// Retorna o valor completo |
|
175
|
|
|
return $this->getValue(self::ID_ADDITIONAL_DATA_FIELD_TEMPLATE, $txid); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Método responsável por calcular o valor da hash de validação do código pix |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
private function getCRC16($payload) { |
|
183
|
|
|
// Adiciona dados gerais do payload |
|
184
|
|
|
$payload .= self::ID_CRC16.'04'; |
|
185
|
|
|
|
|
186
|
|
|
// Dados definidos pelo Bacen |
|
187
|
|
|
$polinomio = 0x1021; |
|
188
|
|
|
$resultado = 0xFFFF; |
|
189
|
|
|
|
|
190
|
|
|
// Checksum |
|
191
|
|
|
if (($length = strlen($payload)) > 0) { |
|
192
|
|
|
for ($offset = 0; $offset < $length; $offset++) { |
|
193
|
|
|
$resultado ^= (ord($payload[$offset]) << 8); |
|
194
|
|
|
for ($bitwise = 0; $bitwise < 8; $bitwise++) { |
|
195
|
|
|
if (($resultado <<= 1) & 0x10000) $resultado ^= $polinomio; |
|
196
|
|
|
$resultado &= 0xFFFF; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// Retorna o código CRC16 de 4 caractéres |
|
202
|
|
|
return self::ID_CRC16.'04'.strtoupper(dechex($resultado)); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Método responsável por gerar o código completo do payload Pix |
|
207
|
|
|
* @return string |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getPayload() { |
|
210
|
|
|
// Cria o $payload |
|
211
|
|
|
$payload = |
|
212
|
|
|
$this->getValue(self::ID_PAYLOAD_FORMAT_INDICATOR, '01') . |
|
213
|
|
|
$this->getMerchantAccountInformation() . |
|
214
|
|
|
$this->getValue(self::ID_MERCHANT_CATEGORY_CODE, '0000') . |
|
215
|
|
|
$this->getValue(self::ID_TRANSACTION_CURRENCY, '986') . |
|
216
|
|
|
$this->getValue(self::ID_TRANSACTION_AMOUNT, $this->amount) . |
|
217
|
|
|
$this->getValue(self::ID_COUNTRY_CODE, 'BR') . |
|
218
|
|
|
$this->getValue(self::ID_MERCHANT_NAME, $this->merchantName) . |
|
219
|
|
|
$this->getValue(self::ID_MERCHANT_CITY, $this->merchantCity) . |
|
220
|
|
|
$this->getAdditionalDataFieldTemplate(); |
|
221
|
|
|
|
|
222
|
|
|
// Retorna o payload + CRC16 |
|
223
|
|
|
return $payload.$this->getCRC16($payload); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param $payload |
|
228
|
|
|
* @param $size |
|
229
|
|
|
* @return string |
|
230
|
|
|
* @throws \Mpdf\QrCode\QrCodeException |
|
231
|
|
|
*/ |
|
232
|
|
|
public function qrcode($payload, $size=400){ |
|
233
|
|
|
$objQrcode = new QrCode($payload); |
|
234
|
|
|
return (new Output\Png)->output($objQrcode, $size); |
|
235
|
|
|
} |
|
236
|
|
|
} |