File "Consultar_saldo_doacoes.php"

Full path: C:/xampp/htdocs/SITE FUNCIONANDO (STRING_NOTFOUND_837_111)/Consultar_saldo_doacoes.php
File size: 1.56 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor &nnbsp; Back

<?php

// Carregar o SDK do Mercado Pago
require_once 'vendor/autoload.php';

use MercadoPago\Client\Payment\PaymentClient;
use MercadoPago\MercadoPagoConfig;
use MercadoPago\Exceptions\MPApiException;
use MercadoPago\Client\Common\RequestOptions;

try {
    // Configurar o Access Token
    $accessToken = getenv('MERCADOPAGO_ACCESS_TOKEN'); // Pegando o token do ambiente
    if (!$accessToken) {
        throw new Exception("Access Token não configurado.");
    }
    MercadoPagoConfig::setAccessToken($accessToken);

    // URL da API do Mercado Pago para consultar saldo
    $url = "https://api.mercadopago.com/v1/account/balance";

    // Configurar cURL para chamada manual
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer $accessToken"
    ]);

    // Executar requisição e capturar resposta
    $response = curl_exec($ch);

    // Verificar erros na requisição
    if (curl_errno($ch)) {
        throw new Exception("Erro na requisição: " . curl_error($ch));
    }

    curl_close($ch);

    // Decodificar a resposta JSON
    $saldo = json_decode($response, true);
    if (isset($saldo['available_balance'])) {
        echo "Saldo disponível: R$ " . number_format($saldo['available_balance'], 2, ',', '.');
    } else {
        echo "Erro ao obter saldo: " . $response;
    }

} catch (MPApiException $e) {
    echo "Erro na API do Mercado Pago: " . $e->getMessage();
} catch (Exception $e) {
    echo "Erro: " . $e->getMessage();
}

?>