I have a problem when trying to execute a CURL passing parameters with the POST method, the CURL that I want to execute is this:
curl -X POST \
'https://api.mercadopago.com/v1/payments?access_token=ACCESS_TOKEN_ENV' \
-d '{
"token":"b3a7dbec3eb0d71798c4f19fec445795",
"installments":1,
"transaction_amount":58.80,
"description":"Point Mini a maquininha que dá o dinheiro de suas vendas na hora",
"payment_method_id":"visa",
"payer":{
"email":"[email protected]"
}
These are the minimum data that Mercado Pago asks me to be able to make a record of a new payment, I am trying to execute this query from PHP, I am trying the following:
<?php
$curl = curl_init();
$token = "5ad1c4126a25bbabc068961e69e7d5ce";
$installments = 1;
$transaction_amount = 100;
$description = "Pago desde cURL";
$payment_method_id = "master";
$payer = array(
"email" => "[email protected]"
);
curl_setopt($curl, CURLOPT_URL,'https://api.mercadopago.com/v1/payments?access_token=TEST-1234-56789-101112-456789');
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "{\"transaction_amount\" : \"$transaction_amount\",
\"token\" : \"$token\",
\"description\" : \"$description\",
\"installments\" : \"$installments\",
\"payment_method_id\" : \"$payment_method_id\",
\"payer\" : \"$payer\"}");
$data = curl_exec($curl);
curl_close($curl);
echo $data;
?>
But when executing in postman it gives me the following two errors:
<br />
<b>Notice</b>: Array to string conversion in <b>C:\xampp20\htdocs\Apoya pagos\Curl\pago_curl.php</b> on line <b>24</b><br />
{
"message": "transaction_amount attribute must be numeric",
"error": "bad_request",
"status": 400,
"cause": [
{
"code": 4003,
"description": "transaction_amount attribute must be numeric",
"data": null
}
]
}
I don't know what I'm doing wrong when sending my payer field as an array and I don't understand why it tells me that transaction_amount must be numeric when I'm passing it as a number.
In your PHP code you have
resulting in
while in original CURL it is
no quotes around the number.
There may be other errors, it is one that the server tells you:
transaction_amount attribute must be numeric
, expects a number and receives a string.I also recommend that you form JSON with a special function, it will be more comfortable and with fewer errors