I have this api in which it works with array and I want to pass a value that is defined in a variable and it does not take it, but neither does it throw me any error:
<?php
include 'include/cont_db.php';
require_once ('sdk-mercadopago/lib/mercadopago.php');
$code = '5LGIm61xtGES';
$ress = $conn->query("SELECT * FROM reservas WHERE code = '".$code."'");
$row = $ress->fetch();
$valor = (float)$row['valor']; //covierte dato de valor string a num
$cliente_id = "numero";
$cliente_secret = "numeros";
$mp = new MP($cliente_id, $cliente_secret);
$preference_data = array(
"items" => array(
array(
"title" => "Pago de Reserva",
"quantity" => 1,
"currency_id" => "ARS",
"unit_price" => $valor //velor en pesos
)
)
);
$preference = $mp->create_preference($preference_data);
?>
<h3><a href="<?php echo $preference['response']['init_point']; ?>">Pay</a></h3>
If I create a variable with the name titulo
and replace the text, it works fine, but the same does not happen to me with the value.
[Solution]
Thanks a friend, I understood that the number that the value that was happening was of value string
and "unit_price"
must be a numerical value, that's why the payment link was not generated correctly. The solution was to convert the value string
to num
of the variable $valor
making a typecast to (float)
:
$valor = (float) $row['valor']; //covierte dato de valor string a num