![Avatar discord do usuario guilherme_efi](https://cdn.discordapp.com/avatars/775350441965649951/4701d75a22d7b2eb0ab1b7fed94c1d29.webp)
Bom dia, @roxzin! Como vai?
Aqui está um código que funciona:
php
$homolog = true; // false para produção
$config = [
"certificado" => realpath(__DIR__ . "/certificado.pem"), // certificado em .pem de produção ou homologação
"client_id" => "Client_Id",
"client_secret" => "Client_Secret"
];
$autorizacao = base64_encode($config["client_id"] . ":" . $config["client_secret"]);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $homolog ? "https://pix-h.api.efipay.com.br/oauth/token" : "https://pix.api.efipay.com.br/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 8,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{"grant_type": "client_credentials"}',
CURLOPT_SSLCERT => $config["certificado"], // Caminho do certificado
CURLOPT_SSLCERTPASSWD => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $autorizacao",
"Content-Type: application/json"
),
));
var_dump(curl_exec($curl));
$returnAuth = json_decode(curl_exec($curl), true);
$access_token = $returnAuth['access_token'];
curl_close($curl);
//Criação da cobrança
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $homolog ? 'https://pix-h.api.efipay.com.br/v2/cob/' : 'https://pix.api.efipay.com.br/v2/cob/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"calendario": {
"expiracao": 3600
},
"devedor": {
"cpf": "12345678909",
"nome": "Francisco da Silva"
},
"valor": {
"original": "0.45"
},
"chave": "[email protected]"
}',
CURLOPT_SSLCERT => $config["certificado"], // Caminho do certificado
CURLOPT_SSLCERTPASSWD => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $access_token",
"Content-Type: application/json"
),
));
$responsePix = json_decode(curl_exec($curl), true);
curl_close($curl);
echo '
if ($responsePix['loc']['id']) {
$idlocationPix = $responsePix['loc']['id'];
// Obtêm o Pix Copia e Cola e QR Code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $homolog ? 'https://pix-h.api.efipay.com.br/v2/loc/' . $idlocationPix . '/qrcode' : 'https://pix.api.efipay.com.br/v2/loc/' . $idlocationPix . '/qrcode',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_SSLCERT => $config["certificado"], // Caminho do certificado
CURLOPT_SSLCERTPASSWD => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $access_token",
),
));
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
$PixCopiaCola = $response['qrcode'];
$imagemQrcode = $response['imagemQrcode'];
echo 'QR Code:';
echo '
echo 'Imagem:
';
echo '
';
}
Também temos nossa SDK de PHP (https://github.com/efipay/sdk-php-apis-efi), que usa o Guzzle.$homolog = true; // false para produção
$config = [
"certificado" => realpath(__DIR__ . "/certificado.pem"), // certificado em .pem de produção ou homologação
"client_id" => "Client_Id",
"client_secret" => "Client_Secret"
];
$autorizacao = base64_encode($config["client_id"] . ":" . $config["client_secret"]);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $homolog ? "https://pix-h.api.efipay.com.br/oauth/token" : "https://pix.api.efipay.com.br/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 8,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{"grant_type": "client_credentials"}',
CURLOPT_SSLCERT => $config["certificado"], // Caminho do certificado
CURLOPT_SSLCERTPASSWD => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $autorizacao",
"Content-Type: application/json"
),
));
var_dump(curl_exec($curl));
$returnAuth = json_decode(curl_exec($curl), true);
$access_token = $returnAuth['access_token'];
curl_close($curl);
//Criação da cobrança
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $homolog ? 'https://pix-h.api.efipay.com.br/v2/cob/' : 'https://pix.api.efipay.com.br/v2/cob/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"calendario": {
"expiracao": 3600
},
"devedor": {
"cpf": "12345678909",
"nome": "Francisco da Silva"
},
"valor": {
"original": "0.45"
},
"chave": "[email protected]"
}',
CURLOPT_SSLCERT => $config["certificado"], // Caminho do certificado
CURLOPT_SSLCERTPASSWD => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $access_token",
"Content-Type: application/json"
),
));
$responsePix = json_decode(curl_exec($curl), true);
curl_close($curl);
echo '
' . json_encode($responsePix, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '';
if ($responsePix['loc']['id']) {
$idlocationPix = $responsePix['loc']['id'];
// Obtêm o Pix Copia e Cola e QR Code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $homolog ? 'https://pix-h.api.efipay.com.br/v2/loc/' . $idlocationPix . '/qrcode' : 'https://pix.api.efipay.com.br/v2/loc/' . $idlocationPix . '/qrcode',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_SSLCERT => $config["certificado"], // Caminho do certificado
CURLOPT_SSLCERTPASSWD => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $access_token",
),
));
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
$PixCopiaCola = $response['qrcode'];
$imagemQrcode = $response['imagemQrcode'];
echo 'QR Code:';
echo '
' . json_encode($PixCopiaCola, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '';
echo 'Imagem:
';
echo '
}