|
| 1 | +<?php |
| 2 | + |
| 3 | +class PIXPDV { |
| 4 | + private $homologacao; |
| 5 | + private $cnpj; |
| 6 | + private $token; |
| 7 | + private $secret; |
| 8 | + private $instacia; |
| 9 | + |
| 10 | + public function __construct($cnpj, $token, $secret, $homologacao = true) { |
| 11 | + $this->homologacao = $homologacao; |
| 12 | + $this->cnpj = $this->homologacao === false ? $cnpj : '00641418000188'; |
| 13 | + $this->token = $this->homologacao === false ? $token : 'tk-ezI0OTgwMzRDLUE1MzctNDM3QS1CQTk0LUZFODlFMEE0MzIyNn0'; |
| 14 | + $this->secret = $this->homologacao === false ? $secret : 'sk-e0JBNTFGRTY0LTczMkYtNDYxNC1CQ0Q1LUI0OTVDODgxOTUwRX0'; |
| 15 | + |
| 16 | + $this->instacia = curl_init(); |
| 17 | + curl_setopt($this->instacia, CURLOPT_RETURNTRANSFER, 1); |
| 18 | + curl_setopt($this->instacia, CURLOPT_POST, 1); |
| 19 | + curl_setopt($this->instacia,CURLOPT_ENCODING, ''); |
| 20 | + curl_setopt($this->instacia,CURLOPT_MAXREDIRS, 10); |
| 21 | + curl_setopt($this->instacia,CURLOPT_TIMEOUT, 0); |
| 22 | + curl_setopt($this->instacia,CURLOPT_FOLLOWLOCATION, true); |
| 23 | + curl_setopt($this->instacia,CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
| 24 | + curl_setopt($this->instacia,CURLOPT_USERAGENT, 'PHP'); |
| 25 | + } |
| 26 | + |
| 27 | + public function statusToken() { |
| 28 | + $body = json_encode(["cnpj" => $this->cnpj]); |
| 29 | + $headers = [ |
| 30 | + 'Content-Type: application/json', |
| 31 | + 'Json-Hash: ' . $this->gerarHmac($body), |
| 32 | + 'Authorization: Basic ' . base64_encode($this->cnpj . ':' . $this->token) |
| 33 | + ]; |
| 34 | + |
| 35 | + curl_setopt($this->instacia,CURLOPT_CUSTOMREQUEST, 'POST'); |
| 36 | + curl_setopt($this->instacia, CURLOPT_URL, $this->homologacao === false ? "https://pixpdv.com.br/api/v1/statustoken" : "https://pixpdv.com.br/api-h/v1/statustoken"); |
| 37 | + curl_setopt($this->instacia, CURLOPT_POSTFIELDS, $body); |
| 38 | + curl_setopt($this->instacia, CURLOPT_HTTPHEADER, $headers); |
| 39 | + |
| 40 | + $response = curl_exec($this->instacia); |
| 41 | + |
| 42 | + return json_decode($response, true); |
| 43 | + } |
| 44 | + |
| 45 | + public function gerarQRDinamico($valor, $minutos, $msg, $imagem = false) { |
| 46 | + $body = json_encode([ |
| 47 | + "valor" => $valor, |
| 48 | + "minutos" => $minutos, |
| 49 | + "mensagem" => $msg, |
| 50 | + "imagem" => $imagem |
| 51 | + ]); |
| 52 | + |
| 53 | + $headers = [ |
| 54 | + 'Content-Type: application/json', |
| 55 | + 'Json-Hash: ' . $this->gerarHmac($body), |
| 56 | + 'Authorization: Basic ' . base64_encode($this->cnpj . ':' . $this->token) |
| 57 | + ]; |
| 58 | + |
| 59 | + curl_setopt($this->instacia,CURLOPT_CUSTOMREQUEST, 'POST'); |
| 60 | + curl_setopt($this->instacia, CURLOPT_URL, $this->homologacao === false ? "https://pixpdv.com.br/api/v1/qrdinamico" : "https://pixpdv.com.br/api-h/v1/qrdinamico"); |
| 61 | + curl_setopt($this->instacia, CURLOPT_POSTFIELDS, $body); |
| 62 | + curl_setopt($this->instacia, CURLOPT_HTTPHEADER, $headers); |
| 63 | + |
| 64 | + $response = curl_exec($this->instacia); |
| 65 | + if ($response === false) { |
| 66 | + return curl_error($this->instacia); |
| 67 | + } |
| 68 | + |
| 69 | + return json_decode($response, true); |
| 70 | + } |
| 71 | + |
| 72 | + public function gerarQRCobranca(string $valor, string $vencimento, int $expira, string $msg, array $pagador, array $juros, array $multa, array $desconto, bool $img = false, string $documento = "") { |
| 73 | + $body = json_encode([ |
| 74 | + "valor" => $valor, |
| 75 | + "vencimento" => $vencimento, |
| 76 | + "expira" => $expira, |
| 77 | + "mensagem" => $msg, |
| 78 | + "imagem" => $img, |
| 79 | + "documento" => $documento, |
| 80 | + "pagador" => $pagador, |
| 81 | + "juros" => $juros, |
| 82 | + "multa" => $multa, |
| 83 | + "desconto" => $desconto |
| 84 | + ]); |
| 85 | + |
| 86 | + $headers = [ |
| 87 | + 'Content-Type: application/json', |
| 88 | + 'Json-Hash: ' . $this->gerarHmac($body), |
| 89 | + 'Authorization: Basic ' . base64_encode($this->cnpj . ':' . $this->token) |
| 90 | + ]; |
| 91 | + |
| 92 | + curl_setopt($this->instacia,CURLOPT_CUSTOMREQUEST, 'POST'); |
| 93 | + curl_setopt($this->instacia, CURLOPT_URL, $this->homologacao === false ? "https://pixpdv.com.br/api/v1/qrcobranca" : "https://pixpdv.com.br/api-h/v1/qrcobranca"); |
| 94 | + curl_setopt($this->instacia, CURLOPT_POSTFIELDS, $body); |
| 95 | + curl_setopt($this->instacia, CURLOPT_HTTPHEADER, $headers); |
| 96 | + |
| 97 | + $response = curl_exec($this->instacia); |
| 98 | + if ($response === false) { |
| 99 | + return curl_error($this->instacia); |
| 100 | + } |
| 101 | + |
| 102 | + return json_decode($response, true); |
| 103 | + } |
| 104 | + |
| 105 | + public function statusQRCode(string $qrcodeid) { |
| 106 | + $headers = [ |
| 107 | + 'Content-Type: application/json', |
| 108 | + 'Authorization: Basic ' . base64_encode($this->cnpj . ':' . $this->token) |
| 109 | + ]; |
| 110 | + |
| 111 | + curl_setopt($this->instacia,CURLOPT_CUSTOMREQUEST, 'GET'); |
| 112 | + curl_setopt($this->instacia, CURLOPT_URL, $this->homologacao === false ? "https://pixpdv.com.br/api/v1/qrstatus?qrcodeid=" . $qrcodeid : "https://pixpdv.com.br/api-h/v1/qrstatus?qrcodeid=" . $qrcodeid); |
| 113 | + curl_setopt($this->instacia, CURLOPT_HTTPHEADER, $headers); |
| 114 | + |
| 115 | + $response = curl_exec($this->instacia); |
| 116 | + if ($response === false) { |
| 117 | + return curl_error($this->instacia); |
| 118 | + } |
| 119 | + |
| 120 | + return json_decode($response, true); |
| 121 | + } |
| 122 | + |
| 123 | + public function devolverPagamento(string $qrcodeid) { |
| 124 | + $body = json_encode(["qrcodeid" => $qrcodeid]); |
| 125 | + |
| 126 | + $headers = [ |
| 127 | + 'Content-Type: application/json', |
| 128 | + 'Json-Hash: ' . $this->gerarHmac($body), |
| 129 | + 'Authorization: Basic ' . base64_encode($this->cnpj . ':' . $this->token) |
| 130 | + ]; |
| 131 | + |
| 132 | + curl_setopt($this->instacia,CURLOPT_CUSTOMREQUEST, 'POST'); |
| 133 | + curl_setopt($this->instacia, CURLOPT_URL, $this->homologacao === false ? "https://pixpdv.com.br/api/v1/qrrefund" : "https://pixpdv.com.br/api-h/v1/qrrefund"); |
| 134 | + curl_setopt($this->instacia, CURLOPT_POSTFIELDS, $body); |
| 135 | + curl_setopt($this->instacia, CURLOPT_HTTPHEADER, $headers); |
| 136 | + |
| 137 | + $response = curl_exec($this->instacia); |
| 138 | + if ($response === false) { |
| 139 | + return curl_error($this->instacia); |
| 140 | + } |
| 141 | + |
| 142 | + return json_decode($response, true); |
| 143 | + } |
| 144 | + |
| 145 | + public function resumo(string $inicio, string $fim, string $tipo) { |
| 146 | + $headers = [ |
| 147 | + 'Authorization: Basic ' . base64_encode($this->cnpj . ':' . $this->token) |
| 148 | + ]; |
| 149 | + |
| 150 | + curl_setopt($this->instacia,CURLOPT_CUSTOMREQUEST, 'GET'); |
| 151 | + curl_setopt($this->instacia, CURLOPT_URL, $this->homologacao === false ? "https://pixpdv.com.br/api/v1/qrresumo?inicio=$inicio&fim=$fim&tipo=$tipo" : "https://pixpdv.com.br/api-h/v1/qrresumo?inicio=$inicio&fim=$fim&tipo=$tipo"); |
| 152 | + curl_setopt($this->instacia, CURLOPT_HTTPHEADER, $headers); |
| 153 | + |
| 154 | + $response = curl_exec($this->instacia); |
| 155 | + if ($response === false) { |
| 156 | + return curl_error($this->instacia); |
| 157 | + } |
| 158 | + |
| 159 | + return json_decode($response, true); |
| 160 | + } |
| 161 | + |
| 162 | + public function saldo() { |
| 163 | + $headers = [ |
| 164 | + 'Authorization: Basic ' . base64_encode($this->cnpj . ':' . $this->token) |
| 165 | + ]; |
| 166 | + |
| 167 | + curl_setopt($this->instacia,CURLOPT_CUSTOMREQUEST, 'GET'); |
| 168 | + curl_setopt($this->instacia, CURLOPT_URL, $this->homologacao === false ? "https://pixpdv.com.br/api/v1/saldo" : "https://pixpdv.com.br/api-h/v1/saldo"); |
| 169 | + curl_setopt($this->instacia, CURLOPT_HTTPHEADER, $headers); |
| 170 | + |
| 171 | + $response = curl_exec($this->instacia); |
| 172 | + if ($response === false) { |
| 173 | + return curl_error($this->instacia); |
| 174 | + } |
| 175 | + |
| 176 | + return json_decode($response, true); |
| 177 | + } |
| 178 | + |
| 179 | + public function retirarSaldo(float $valor) { |
| 180 | + $body = json_encode(["valor" => $valor]); |
| 181 | + |
| 182 | + $headers = [ |
| 183 | + 'Content-Type: application/json', |
| 184 | + 'Json-Hash: ' . $this->gerarHmac($body), |
| 185 | + 'Authorization: Basic ' . base64_encode($this->cnpj . ':' . $this->token) |
| 186 | + ]; |
| 187 | + |
| 188 | + curl_setopt($this->instacia,CURLOPT_CUSTOMREQUEST, 'POST'); |
| 189 | + curl_setopt($this->instacia, CURLOPT_URL, $this->homologacao === false ? "https://pixpdv.com.br/api/v1/retirada" : "https://pixpdv.com.br/api-h/v1/retirada"); |
| 190 | + curl_setopt($this->instacia, CURLOPT_POSTFIELDS, $body); |
| 191 | + curl_setopt($this->instacia, CURLOPT_HTTPHEADER, $headers); |
| 192 | + |
| 193 | + $response = curl_exec($this->instacia); |
| 194 | + if ($response === false) { |
| 195 | + return curl_error($this->instacia); |
| 196 | + } |
| 197 | + |
| 198 | + return json_decode($response, true); |
| 199 | + } |
| 200 | + |
| 201 | + public function extrato(string $inicio, string $fim) { |
| 202 | + $headers = [ |
| 203 | + 'Authorization: Basic ' . base64_encode($this->cnpj . ':' . $this->token) |
| 204 | + ]; |
| 205 | + |
| 206 | + curl_setopt($this->instacia,CURLOPT_CUSTOMREQUEST, 'GET'); |
| 207 | + curl_setopt($this->instacia, CURLOPT_URL, $this->homologacao === false ? "https://pixpdv.com.br/api/v1/extrato?inicio=$inicio&fim=$fim" : "https://pixpdv.com.br/api-h/v1/extrato?inicio=$inicio&fim=$fim"); |
| 208 | + curl_setopt($this->instacia, CURLOPT_HTTPHEADER, $headers); |
| 209 | + |
| 210 | + $response = curl_exec($this->instacia); |
| 211 | + if ($response === false) { |
| 212 | + return curl_error($this->instacia); |
| 213 | + } |
| 214 | + |
| 215 | + return json_decode($response, true); |
| 216 | + } |
| 217 | + |
| 218 | + private function gerarHmac($body) { |
| 219 | + $hmac = hash_hmac('sha256', $body, $this->secret); |
| 220 | + return $hmac; |
| 221 | + } |
| 222 | + |
| 223 | + public function __destruct() { |
| 224 | + curl_close($this->instacia); |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +?> |
0 commit comments