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);
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.
– Inkeliz
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 :)
– MarceloBoni