how to read return variables using json curl and php

4

My problem refers to the integration with the sky and I've rolled everything, but without success.

I read this post: Request in Cielo's PHP API using cURL

"I understood" what I said in the post and did so:

<?
//string json contendo os dados de um funcionário
$request = '{
    "MerchantOrderId":"2014111703",
    "Customer":{
    "Name":"Comprador crédito simples"
},
"Payment":{
    "Type":"CreditCard",
    "Amount":15700,
    "Installments":1,
    "SoftDescriptor":"123456789ABCD",
    "CreditCard":{
        "CardNumber":"1234123412341231",
        "Holder":"Teste Holder",
        "ExpirationDate":"12/2030",
        "SecurityCode":"123",
        "Brand":"Visa"
    }
 }
}';

$data_string = json_encode($request, true);

$MerchantID="meuid";
$MerchantKey="meukey";

$ch = curl_init("https://apisandbox.cieloecommerce.cielo.com.br/1/sales");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'MerchantId: ' . $MerchantID,
    'MerchantKey: ' . $MerchantKey,
    'Content-Length: ' . strlen($data_string))
);

$result = curl_exec($ch);
$result = json_decode($result, true);

echo "$result->ProofOfSale";
echo "$result->Tid";
echo "$result->AuthorizationCode";
echo "$result->SoftDescriptor";
echo "$result->PaymentId";
echo "$result->ECI";
echo "$result->Status";
echo "$result->ReturnCode";
echo "$result->ReturnMessage";
?>

The final part refers to the return variables that Heaven informs this manual: link

But it does not return anything. My intention is for the customer who purchases the product to make the purchase directly from within the site and not be directed to the sky environment.

I really do not understand anything about json and how it works in php.

If you could help me, I would be very grateful.

    
asked by anonymous 27.05.2017 / 12:39

2 answers

1

See this answer. The problem is in json_decode($result, true) , after CURL you have two options:

  • Change to json_decode($result) and keep the rest as is; or
  • Keep json_decode($result, true) and change $result->ProofOfSale to $result['ProofOfSale'] , just like others, for example.
  • Also make a var_dump($result) to know how the data is and if it really is "coming".

        
    28.05.2017 / 20:53
    0

    Try submitting your json in array format. You are using json_encode , its function is to transform an array into json, so try to use json_decode :

    <?php
    //string json contendo os dados de um funcionário
    $request = '{
        "MerchantOrderId":"2014111703",
        "Customer":{
        "Name":"Comprador crédito simples"
    },
    "Payment":{
        "Type":"CreditCard",
        "Amount":15700,
        "Installments":1,
        "SoftDescriptor":"123456789ABCD",
        "CreditCard":{
            "CardNumber":"1234123412341231",
            "Holder":"Teste Holder",
            "ExpirationDate":"12/2030",
            "SecurityCode":"123",
            "Brand":"Visa"
        }
     }
    }';
    
    $data_string = json_decode($request, true);
    
    $MerchantID="meuid";
    $MerchantKey="meukey";
    
    $ch = curl_init("https://apisandbox.cieloecommerce.cielo.com.br/1/sales");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'MerchantId: ' . $MerchantID,
        'MerchantKey: ' . $MerchantKey,
        'Content-Length: ' . strlen($data_string))
    );
    
    $result = curl_exec($ch);
    $result = json_decode($result, true);
    
    echo "$result->ProofOfSale";
    echo "$result->Tid";
    echo "$result->AuthorizationCode";
    echo "$result->SoftDescriptor";
    echo "$result->PaymentId";
    echo "$result->ECI";
    echo "$result->Status";
    echo "$result->ReturnCode";
    echo "$result->ReturnMessage";
    ?>
    

    Check this line too $result = json_decode($result, true); , because if cielo is sending you an array, you have to use the json_encode function because below that line you are displaying the return as an object.

        
    27.05.2017 / 16:19