Jump to content

Modulo MercadoPago parou de funcionar depois da 7.6


thalesgodoi

Recommended Posts

Colegas, boa noite.

 

Percebi que o módulo parou de funcionar já alguns dias, eu consegui resolver o problema de uma maneira bem simples.

Aparentemente a função GetStatus do módulo envia requisições POST, não sei o motivo sendo que a API trabalha com GET. Efetuei alguns testes via curl e cheguei a conclusão que o protocolo estava incorreto e que a requisição deveria ser realmente via GET.

 

Enfim, fiz a alteração no arquivo chamado via função "include_once", no meu sistema o nome do arquivo era "lib.php".

Procurem a linha "$retorno = $this->DoPost($opt = null, $url, $header, "200", "none", "post");" e alterem para "$retorno = $this->DoPost($opt = null, $url, $header, "200", "none", "get");" dentro da public "GetStatus($id)".

 

Função GetStatus já com a correção:

public function GetStatus($id) {
    $this->getAccessToken();
    logTransaction("MERCADOPAGO LIB", $this->accesstoken, "Unsuccessful");
    $url = "https://api.mercadolibre.com/collections/notifications/" . $id . "?access_token=" . $this->accesstoken;
    $header = array( "Accept: application/json", "Content-Type: application/x-www-form-urlencoded" );
    $retorno = $this->DoPost($opt = null, $url, $header, "200", "none", "get");
    return $retorno;
}

 

 

Me contem se resolveu para vocês, aqui já está funcionando o retorno e inserção nas transações.

Ressaltando que o meu problema não estava relacionado a versão 7.6 e sim ao retorno automático após o dia 26.

Vocês podem remover a linha "logTransaction("MERCADOPAGO LIB", $this->accesstoken, "Unsuccessful");", havia adicionado apenas para debug e não consegui editar o post anterior.

Link to comment
Share on other sites

Parabéns Fernando

Aqui funcionou também  vou colar aqui todo o código lib.php do modulo do mercado pago !

Arquivo editado /public_html/modules/gateways/mercadopago/lib.php

<?php 

class mpAuth extends mpCall
{
    public $client_id = NULL;
    public $client_secret = NULL;
    public $refresh = NULL;
    public $newrefresh = null;
    public $accesstoken = NULL;
    public $error = NULL;
    protected $date = NULL;
    protected $expired = NULL;
    protected $b2b2c = false;

    public function GeAuthCore($url)
    {
        $link = "https://auth.mercadolibre.com.ar/authorization?client_id=" . $this->client_id . "&response_type=code&platform_id=mp&redirect_uri=" . $url;
        return $link;
    }

    public function GetRefreshToken($auth, $redict_url)
    {
        $url = "https://api.mercadolibre.com/oauth/token";
        $header = array( "Accept: application/json" );
        $post = array( "grant_type" => "authorization_code", "client_id" => $this->client_id, "client_secret" => $this->client_secret, "code" => $auth, "redirect_uri" => $redict_url );
        $dados = $this->DoPost($post, $url, $header, "200", "post", "post");
        return $dados;
    }

    public function getAccessToken()
    {
        $data = getdate();
        $time = $data[0];
        if( isset($this->accesstoken) && isset($this->date) ) 
        {
            $timedifference = $time - $this->date;
            if( $timedifference < $this->expired ) 
            {
                return $this->accesstoken;
            }

        }

        if( $this->refresh != null ) 
        {
            $post = array( "client_id" => $this->client_id, "client_secret" => $this->client_secret, "grant_type" => "refresh_token", "refresh_token" => $this->refresh );
            $this->b2b2c = true;
            $header = array( "Accept: application/json", "Content-Type: application/x-www-form-urlencoded" );
            $url = "https://api.mercadolibre.com/oauth/token";
            $dados = $this->DoPost($post, $url, $header, "200", "post", "post");
            $this->accesstoken = $dados["access_token"];
            $this->newrefresh = $dados["refresh_token"];
            $this->date = $time;
            $this->expired = $dados["expires_in"];
            return $dados["access_token"];
        }

        $post = array( "client_id" => $this->client_id, "client_secret" => $this->client_secret, "grant_type" => "client_credentials" );
        $header = array( "Accept: application/json", "Content-Type: application/x-www-form-urlencoded" );
        $url = "https://api.mercadolibre.com/oauth/token";
        $dados = $this->DoPost($post, $url, $header, "200", "post", "post");
        $this->accesstoken = $dados["access_token"];
        $this->date = $time;
        $this->expired = $dados["expires_in"];
        return $dados["access_token"];
    }

}


class mpCore extends mpAuth
{
    public function __construct($client_id, $client_secret)
    {
        $this->client_id = $client_id;
        $this->client_secret = $client_secret;
    }

    public function GetMethods($country_id)
    {
        $url = "https://api.mercadolibre.com/sites/" . $country_id . "/payment_methods";
        $header = array( "Content-Type:application/json" );
        $methods = $this->DoPost($opt = null, $url, $header, "200", "none", "get");
        return $methods;
    }

    public function GetCheckout($data, $excludes, $method = "lightbox")
    {
        if( $excludes != "" ) 
        {
            $methods_excludes = preg_split("/[\\s,]+/", $excludes);
            foreach( $methods_excludes as $exclude ) 
            {
                $excludemethods[] = array( "id" => $exclude );
            }
            if( !isset($data["mkfee"]) ) 
            {
                $data["mkfee"] = "";
            }

            $data["mkfee"] = "";
            $opt = array( "external_reference" => $data["external_reference"], "items" => array( array( "id" => $data["external_reference"], "title" => $data["title"], "description" => $data["quantity"] . " x " . $data["title"], "quantity" => $data["quantity"], "unit_price" => round($data["amount"], 2), "currency_id" => $data["currency"], "picture_url" => $data["image"] ) ), "payer" => array( "name" => $data["payment_firstname"], "surname" => $data["payment_lastname"], "email" => $data["email"] ), "back_urls" => array( "pending" => $data["pending"], "success" => $data["approved"] ), "payment_methods" => array( "excluded_payment_methods" => $excludemethods ) );
        }
        else
        {
            $opt = array( "external_reference" => $data["external_reference"], "items" => array( array( "id" => $data["external_reference"], "title" => $data["title"], "description" => $data["quantity"] . " x " . $data["title"], "quantity" => $data["quantity"], "unit_price" => round($data["amount"], 2), "currency_id" => $data["currency"], "picture_url" => $data["image"] ) ), "payer" => array( "name" => $data["payment_firstname"], "surname" => $data["payment_lastname"], "email" => $data["email"] ), "back_urls" => array( "pending" => $data["pending"], "success" => $data["approved"] ) );
        }

        $this->getAccessToken();
        $url = "https://api.mercadolibre.com/checkout/preferences?access_token=" . $this->accesstoken;
        $header = array( "Content-Type:application/json", "Accept: application/json" );
        $dados = $this->DoPost($opt, $url, $header, "201", "json", "post");
        $link = $dados["init_point"];
        switch( $method ) 
        {
            case "lightbox":
                $bt = "<a href=\"" . $link . "\" name=\"MP-payButton\" class=\"blue-l-rn-ar\">Comprar</a>\n    <script type=\"text/javascript\" src=\"https://www.mercadopago.com/org-img/jsapi/mptools/buttons/render.js\"></script>";
                break;
            case "Iframe":
                $bt = "<iframe id=\"MP-Checkout-IFrame\" frameborder=\"0\" style=\"width: 740px; height: 480px;\" src=\"" . $link . "\">";
                break;
            case "initpoint":
                $bt = $link;
                break;
            default:
                $bt = "<a href=\"" . $link . "\" name=\"MP-payButton\" class=\"blue-l-rn-ar\">Comprar</a>\n    <script type=\"text/javascript\" src=\"https://www.mercadopago.com/org-img/jsapi/mptools/buttons/render.js\"></script>";
                break;
        }
        if( $this->b2b2c ) 
        {
            $return = array( $this->newrefresh, $bt );
            return $return;
        }

        return $bt;
    }

    public function GetStatus($id) {
    $this->getAccessToken();
    logTransaction("MERCADOPAGO LIB", $this->accesstoken, "Unsuccessful");
    $url = "https://api.mercadolibre.com/collections/notifications/" . $id . "?access_token=" . $this->accesstoken;
    $header = array( "Accept: application/json", "Content-Type: application/x-www-form-urlencoded" );
    $retorno = $this->DoPost($opt = null, $url, $header, "200", "none", "get");
    return $retorno;
    }

    public function SearchPayment($dados = array(  ), $limit = null, $offset = null, $sort = null, $order = null)
    {
        $field = "";
        foreach( $dados as $key => $value ) 
        {
            if( $key == "date_created" || $key == "date_approved" || $key == "last_modified" ) 
            {
                $field .= "range=" . $key . "&";
                foreach( $value as $keys => $dates ) 
                {
                    if( $keys == "start" ) 
                    {
                        $field .= "begin_date=" . $dates . "&";
                    }
                    else
                    {
                        if( $keys == "end" ) 
                        {
                            $field .= "end_date=" . $dates . "&";
                        }

                    }

                }
            }
            else
            {
                $field .= $key . "=" . $value . "&";
            }

        }
        $this->getAccessToken();
        if( $limit != null ) 
        {
            $field .= "limit=" . $limit . "&";
        }

        if( $offset != null ) 
        {
            $field .= "offset=" . $offset . "&";
        }

        if( $sort != null ) 
        {
            $field .= "sort=" . $sort . "&";
        }

        if( $order != null ) 
        {
            $field .= "order=" . $order . "&";
        }

        $header = array( "Accept: application/json", "Content-Type: application/x-www-form-urlencoded" );
        $url = "https://api.mercadolibre.com/collections/search?access_token=" . $this->accesstoken . "&" . $field;
        var_dump($url);
        $retorno = $this->DoPost($opt = null, $url, $header, "none", "none", "get");
        return $retorno;
    }

    public function RefundPayment($id)
    {
        $this->getAccessToken();
        $header = array( "Content-Type:application/json", "Accept: application/json" );
        $opt = "{\"status\":\"cancelled\"}";
        $url = "https://api.mercadolibre.com/collections/" . $id . "?access_token=" . $this->accesstoken;
        $teste = $this->DoPost($opt, $url, $header, "200", "none", "put");
        return $teste;
    }

    public function create_preapproval_payment($preapproval_payment, $method = "lightbox")
    {
        $this->getAccessToken();
        $header = array( "Content-Type:application/json", "Accept: application/json" );
        $url = "https://api.mercadolibre.com/preapproval?access_token=" . $this->accesstoken;
        $preapproval_payment_result = $this->DoPost($preapproval_payment, $url, $header, "201", "json", "post");
        $link = $preapproval_payment_result["init_point"];
        switch( $method ) 
        {
            case "lightbox":
                $bt = "<a href=\"" . $link . "\" name=\"MP-payButton\" class=\"blue-l-rn-ar\">Comprar</a>\n<script type=\"text/javascript\" src=\"https://www.mercadopago.com/org-img/jsapi/mptools/buttons/render.js\"></script>";
                break;
            case "Iframe":
                $bt = "<iframe id=\"MP-Checkout-IFrame\" frameborder=\"0\" style=\"width: 740px; height: 480px;\" src=\"" . $link . "\">";
                break;
            case "initpoint":
                $bt = $link;
                break;
            default:
                $bt = "<a href=\"" . $link . "\" name=\"MP-payButton\" class=\"blue-l-rn-ar\">Comprar</a>\n    <script type=\"text/javascript\" src=\"https://www.mercadopago.com/org-img/jsapi/mptools/buttons/render.js\"></script>";
                break;
        }
        if( $this->b2b2c ) 
        {
            $return = array( $this->newrefresh, $bt );
            return $return;
        }

        return $bt;
    }

}


class mpCall
{
    public static function debug($error)
    {
        echo "<br>Retorno error<br><pre>";
        print_r($error);
        echo "</pre><br><br>";
    }

    public function DoPost($fields, $url, $heads, $codeexpect, $type, $method)
    {
        if( $type == "json" ) 
        {
            $posts = json_encode($fields);
        }
        else
        {
            if( $type == "none" ) 
            {
                $posts = $fields;
            }
            else
            {
                $posts = http_build_query($fields);
            }

        }

        switch( $method ) 
        {
            case "get":
                $options = array( CURLOPT_RETURNTRANSFER => "1", CURLOPT_HTTPHEADER => $heads, CURLOPT_SSL_VERIFYPEER => "false", CURLOPT_URL => $url, CURLOPT_POSTFIELDS => $posts, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_CONNECTTIMEOUT => 0 );
                break;
            case "put":
                $options = array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_HTTPHEADER => $heads, CURLOPT_SSL_VERIFYPEER => "false", CURLOPT_URL => $url, CURLOPT_POSTFIELDS => $posts, CURLOPT_CUSTOMREQUEST => "PUT", CURLOPT_HEADER => 1, CURLOPT_CONNECTTIMEOUT => 0 );
                break;
            case "post":
                $options = array( CURLOPT_RETURNTRANSFER => "1", CURLOPT_HTTPHEADER => $heads, CURLOPT_SSL_VERIFYPEER => "false", CURLOPT_URL => $url, CURLOPT_POSTFIELDS => $posts, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_CONNECTTIMEOUT => 0 );
                break;
            case "delete":
                $options = array( CURLOPT_RETURNTRANSFER => "1", CURLOPT_HTTPHEADER => $heads, CURLOPT_SSL_VERIFYPEER => "false", CURLOPT_URL => $url, CURLOPT_POSTFIELDS => $posts, CURLOPT_CUSTOMREQUEST => "DELETE", CURLOPT_CONNECTTIMEOUT => 0 );
                break;
            default:
                $options = array( CURLOPT_RETURNTRANSFER => "1", CURLOPT_HTTPHEADER => $heads, CURLOPT_SSL_VERIFYPEER => "false", CURLOPT_URL => $url, CURLOPT_POSTFIELDS => $posts, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_CONNECTTIMEOUT => 0 );
                break;
        }
        $options[CURLOPT_SSLVERSION] = TLSv1;
        $call = curl_init();
        curl_setopt_array($call, $options);
        $dados = curl_exec($call);
        $status = curl_getinfo($call);
        curl_close($call);
        if( $status["http_code"] != $codeexpect && $codeexpect != "none" ) 
        {
            $this->debug($dados);
            return false;
        }

        return json_decode($dados, true);
    }

}

Muito Obrigado e parabéns pela iniciativa de compartilhar !!!

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.

×
×
  • Create New...

Important Information

Do you agree with our terms?