Request in the Cielo API in PHP using Curl

Asked

Viewed 2,535 times

0

I’m trying to make a REQUEST the way below, I’m not getting:

public function subscribe(){   
$json_url = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';  

$json_string  = json_encode(array(     

    "MerchantOrderId"=>"2014113245231706",
     "Customer" => array(
        "Name" => "Comprador rec programada"
     ),

     "Payment" => array(
        "Type" => "CreditCard",
        "Amount" => 1500,
        "Installments" => 1,
         "SoftDescriptor" => "Assinatura Fraldas"  
     )    


));


$ch = curl_init($json_url);

$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array('Content-type: application/json'), CURLOPT_POSTFIELDS => $json_string
    );

    curl_setopt_array( $ch, $options );
    $result = curl_exec($ch); // Getting jSON result string

    print_r($result);                

    }
  • The headers are missing, as documented in http://developercielo.github.io/Webservice-3.0/? shell#creating-a-sale-with-authentication. See the error returned and check http://developercielo.github.io/Webservice-3.0/? shell#http-status-code, the reason.

  • It would be interesting for you to explain why you are not succeeding, what error appears, what happens, besides, I see no reason for -1 in the question :)

2 answers

2

I am ONLY basing myself on the documentation provided on http://developercielo.github.io/Webservice-3.0/? shell#creating-a-sale-with-authentication, because it uses the SAME endpoint, but this was not mentioned in your question.

curl
--request POST "https://apisandbox.cieloecommerce.cielo.com.br/1/sales/"
--header "Content-Type: application/json"
--header "MerchantId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
--header "MerchantKey: 0123456789012345678901234567890123456789"
--header "RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
--data-binary
{  
   "MerchantOrderId":"2014111903",
   "Customer":{  
      "Name":"Comprador crédito autenticação"
   },
   "Payment":{  
      "Type":"CreditCard",
      "Amount":15700,
      "Installments":1,
      "Authenticate":true,
      "ReturnUrl":"http://www.cielo.com.br",
      "SoftDescriptor":"123456789ABCD",
      "CreditCard":{  
         "CardNumber":"4551870000000183",
         "Holder":"Teste Holder",
         "ExpirationDate":"12/2015",
         "SecurityCode":"123",
         "Brand":"Visa"
      }
   }
}
--verbose

Let’s translate that into human language:

  • --request POST : Indicates that it is a POST method (strange... but ok).
  • --header <conteudo> : Sets the request headers to <conteudo>.
  • --data-binary <conteudo> : Sets the body of the request to <conteudo>.

In PHP then do exactly the same thing, in the same order, to make it easier:

First of all set the request body and headers:

$cabeçalhos = [
    "Content-Type: application/json",
    'MerchantId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    'MerchantKey: 0123456789012345678901234567890123456789',
    'RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

$corpo = [
    "MerchantOrderId" => "2014111903",
    "Customer" => [
        "Name" => "Comprador crédito autenticação"
    ],
    "Payment" => [
        "Type" => "CreditCard",
        "Amount" => 15700,
        "Installments" => 1,
        "Authenticate" => true,
        "ReturnUrl" => "http => //www.cielo.com.br",
        "SoftDescriptor" => "123456789ABCD",
        "CreditCard" => [
            "CardNumber" => "4551870000000183",
            "Holder" => "Teste Holder",
            "ExpirationDate" => "12/2015",
            "SecurityCode" => "123",
            "Brand" => "Visa"
        ]
    ]
];

Now comes the CURL part, which is the main:

$ch = curl_init();

curl_setopt_array($ch, [

    // Define o método POST:
    CURLOPT_CUSTOMREQUEST => 'POST',

    /* Uma outra opção é utilizar:
    CURLOPT_POST => true,
    */

    // Define o URL:
    CURLOPT_URL => 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/',

    // Define os cabeçalhos:    
    CURLOPT_HTTPHEADER => $cabeçalhos,

    // Define corpo, em JSON:
    CURLOPT_POSTFIELDS => json_encode($corpo),

    // Habilita o retorno
    CURLOPT_RETURNTRANSFER => true

]);

// Executa:
$resposta = curl_exec($ch);

// Encerra CURL:
curl_close($ch);

Now read the answer, can example:

var_dump($resposta);
  • Thanks so much even for the help!!!! ball show ....

0

Follow how I made the request via POST in my integration.

$request is an array with card information, client, etc... as it is in the same documentation.

$data_string = json_encode($request, true);                                                                                   

$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);

Browser other questions tagged

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