Integrate with Payu - How to read data individually

Asked

Viewed 67 times

1

Hello!

I am integrating with an online payment feature called Payu. I am not being able to read the return data individually. The code I’m using:

$url = "https://sandbox.api.payulatam.com/payments-api/4.0/service.cgi";    

  if(!function_exists('curl_init')) {
    die('cURL not available!');
  }  

  $dados = array(
        "language" => "es",
        "command" => "SUBMIT_TRANSACTION",

            "merchant" => array(
                "apiKey" => "4Vj8eK4rloUd272L48hsrarnUA",
                "apiLogin" => "pRRXKOl8ikMmt9u",
            ),

        "transaction" => array(
            "order" => array(

                "accountId"=> "512327",
                 "referenceCode"=> "payment_test_00000001",
                 "description"=> "payment test",
                 "language"=> "es",
                 "signature"=> "31eba6f397a435409f57bc16b5df54c3",
                 "notifyUrl"=> "http=>//www.tes.com/confirmation",

                   "additionalValues"=>array(
                        "TX_VALUE"=>array(  

                            "value"=> 100,
                            "currency"=> "BRL"

                        )//end array TX_VALUE
                    ),//end array additionalValues

                    "buyer"=>array(
                        "fullName"=> "First name and second buyer  name",
                        "emailAddress"=> "[email protected]",
                        "dniNumber"=> "811.807.405-64",
                        "cnpj"=> "24.481.827",

                        "shippingAddress"=>array(
                           "street1"=> "calle 100",
                           "street2"=> "5555487",
                           "city"=> "Sao paulo",
                           "state"=> "SP",
                           "country"=> "BR",
                           "postalCode"=> "01019-030"
                        )//end array shippingAddress

                    )//end array buyer

            ),//end array order     

              "type"=> "AUTHORIZATION_AND_CAPTURE",
              "paymentMethod"=> "BOLETO_BANCARIO",
              "paymentCountry"=> "BR",
              "expirationDate"=> "2018-12-28T00:00:00",
              "ipAddress"=> "127.0.0.1" 

        ),//end array transaction

        "test"=>false

  );//end array principal

  //var_dump($dados);

  /*
  foreach($dados as $d_show) {
    echo $d_show, '<br>';
  }
  */

  $json = json_encode($dados);


    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);   
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                                                       
        //'Authorization: xxzY789jKlPwWwW',                                                         
        //'Accept: application/json',                                       
        'Content-Type: application/json',                                                               
    )); 

    $output = curl_exec($curl);

        if ($output === FALSE) {
            echo 'An error has occurred: ' . curl_error($curl) . PHP_EOL;
        }
        else {

            echo $output;                                               

            echo "<br>";

            $retorno = json_decode($output, true);                                  

            //$code = $retorno->code;
            //$orderId = $retorno->orderId;

            $URL_BOLETO_BANCARIO = $retorno->URL_BOLETO_BANCARIO;

            /*
            echo "code: ".$code[0];
            echo "\n";
            echo "orderId: ".$orderId[0];
            */

            echo "URL_BOLETO_BANCARIO: ".$URL_BOLETO_BANCARIO[2];

        }

With the above code, I can send the data via json/php/Curl to the Payu url.

With this section, I can read the return data cluster via json:

$output = curl_exec($curl);
echo $output;

appears like this on the screen:

SUCCESS844619692f570a54f-a499-405b-b1d5-21b72f00831fPENDINGAWAITING_NOTIFICATIONPENDING_TRANSACTION_CONFIRMATIONEXPIRATION_DATE2018-12-27T19:00:00URL_PAYMENT_RECEIPT_PDFhttps://sandbox.checkout.payulatam.com/ppp-web-gateway-payu/boletoReceipt/pdf/vid/844619692Yf570a54fa499405Ya97caf8259aecd7.pdfBAR_CODE34191.11129 56160.131118 11111.160005 8 77510000010000URL_PAYMENT_RECEIPT_HTMLhttps://sandbox.checkout.payulatam.com/ppp-web-gateway-payu/blv?vid=844619692Yf570a54fa499405Ya97caf8259aecd7URL_BOLETO_BANCARIOhttps://sandbox.checkout.payulatam.com/ppp-web-gateway-payu/blv?vid=844619692Yf570a54fa499405Ya97caf8259aecd7

Now the problem is to be able to read this data individually. For example, read only to the link that generates the boleto, URL_BOLETO_BANCARIO. I am trying so(excerpt present in the code above):

$retorno = json_decode($output, true);                                  
$URL_BOLETO_BANCARIO = $retorno->URL_BOLETO_BANCARIO;
echo "URL_BOLETO_BANCARIO: ".$URL_BOLETO_BANCARIO[2];

...this way did not work, appears this message on the screen:

Notice: Trying to get property 'URL_BOLETO_BANCARIO' of non-object in C:\xampp\htdocs\

Does anyone know how to solve?

1 answer

1


Spin it and see if it goes right.

I removed the comment from

'//'Accept: application/json' 

why it was returning in XML format, and verified that the return data is in array format and not object

You can check all data by giving a print_r in 'return' just below this snippet:

$retorno = json_decode($output, true);  

If you can’t let me know please so I can check in with you.

<?php

$url = "https://sandbox.api.payulatam.com/payments-api/4.0/service.cgi";    

  if(!function_exists('curl_init')) {
    die('cURL not available!');
  }  

  $dados = array(
        "language" => "es",
        "command" => "SUBMIT_TRANSACTION",

            "merchant" => array(
                "apiKey" => "4Vj8eK4rloUd272L48hsrarnUA",
                "apiLogin" => "pRRXKOl8ikMmt9u",
            ),

        "transaction" => array(
            "order" => array(

                "accountId"=> "512327",
                 "referenceCode"=> "payment_test_00000001",
                 "description"=> "payment test",
                 "language"=> "es",
                 "signature"=> "31eba6f397a435409f57bc16b5df54c3",
                 "notifyUrl"=> "http=>//www.tes.com/confirmation",

                   "additionalValues"=>array(
                        "TX_VALUE"=>array(  

                            "value"=> 100,
                            "currency"=> "BRL"

                        )//end array TX_VALUE
                    ),//end array additionalValues

                    "buyer"=>array(
                        "fullName"=> "First name and second buyer  name",
                        "emailAddress"=> "[email protected]",
                        "dniNumber"=> "811.807.405-64",
                        "cnpj"=> "24.481.827",

                        "shippingAddress"=>array(
                           "street1"=> "calle 100",
                           "street2"=> "5555487",
                           "city"=> "Sao paulo",
                           "state"=> "SP",
                           "country"=> "BR",
                           "postalCode"=> "01019-030"
                        )//end array shippingAddress

                    )//end array buyer

            ),//end array order     

              "type"=> "AUTHORIZATION_AND_CAPTURE",
              "paymentMethod"=> "BOLETO_BANCARIO",
              "paymentCountry"=> "BR",
              "expirationDate"=> "2018-12-28T00:00:00",
              "ipAddress"=> "127.0.0.1" 

        ),//end array transaction

        "test"=>false

  );//end array principal

  //var_dump($dados);

  /*
  foreach($dados as $d_show) {
    echo $d_show, '<br>';
  }
  */

  $json = json_encode($dados);


    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);   
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                                                       
        //'Authorization: xxzY789jKlPwWwW',                                                         
        'Accept: application/json',                                       
        'Content-Type: application/json',                                                               
    )); 

    $output = curl_exec($curl);

        if ($output === FALSE) {
            echo 'An error has occurred: ' . curl_error($curl) . PHP_EOL;
        }
        else {

            //echo $output;                                               

            echo "<br>";

            $retorno = json_decode($output, true);                                  

            //$code = $retorno->code;
            //$orderId = $retorno->orderId;

            //$URL_BOLETO_BANCARIO = $retorno->URL_BOLETO_BANCARIO;
            $URL_BOLETO_BANCARIO = $retorno['transactionResponse']['extraParameters']['URL_BOLETO_BANCARIO'];

            /*
            echo "code: ".$code[0];
            echo "\n";
            echo "orderId: ".$orderId[0];
            */

            echo "URL_BOLETO_BANCARIO: ".$URL_BOLETO_BANCARIO;

        }
  • Hi @Rodrigo Beloyanis, I realized that when enabling Accept: application/json, the data came in a more appropriate format. But when trying to visualize a specific data like this: $return = json_decode($output, true);$URL_BOLETO_BANCARIO = $return->URL_BOLETO_BANCARIO;echo "URL_BOLETO_BANCARIO: ". $URL_BOLETO_BANCARIO; ...I don’t get the data yet.. continue showing:Trying to get Property 'URL_BOLETO_BANCARIO' of non-object in C: xampp htdocs\

  • The format correct is: $URL_BOLETO_BANCARIO = $retorno['transactionResponse']['extraParameters']['URL_BOLETO_BANCARIO']; The wrong format: $URL_BOLETO_BANCARIO = $retorno->URL_BOLETO_BANCARIO;

  • Neo, check these excerpts... note that $retorno->URL_BOLETO_BANCARIO does not work, because this variable $retorno is not an object but an array

  • perfect! solved. I thank you. Have you ever done integration with a payment feature like this? I’ve tried with pagseguro, but it’s very complicated... this Payu seems to be a good alternative... if you can leave a contact to share more on this subject...

  • I’ve worked with Pagseguro yes, give me your question I help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.