我正在使用PHP模块MercadoPago。这是我的代码:
$mp = new MP("CLIENT_ID", "CLIENT_SCRET");
foreach ($_SESSION["carrito"] as $value){
$itemCode = $value['tagme'];
$itemDesc = get_pedido($itemCode);
$itemQty = $value['cant']; // it comes from $_POST['cant']
$unitPrice = $value['unit_price'];
$items[] = array(
"title" => $itemDesc,
"quantity" => $itemQty,
"currency_id" => "ARS",
"unit_price" => $unitPrice
);
}
$preference = array(
"items" => $items,
"payer" => array(
'name' => $name,
'email' => $email
),
"back_urls" => array(
'success' => 'http://example.com/success.php',
'pending' => 'http://example.com/pending.php'
)
);
echo '<pre>';print_r($items); echo '</pre>';
$mp->sandbox_mode(TRUE);
$preferenceResult = $mp->create_preference($preference);$items输出如下:
Array
(
[0] => Array
(
[title] => Test Product
[quantity] => 1
[currency_id] => ARS
[unit_price] => 36
)
[1] => Array
(
[title] => Shipping Cost
[quantity] => 1
[currency_id] => ARS
[unit_price] => 42
)
)但我得到了以下错误:
致命错误:在/home/..../public_html/mercadopago-sdk/mercadopago.php:227堆栈跟踪:#0 /home/..../public_html/mercadopago-sdk/mercadopago.php(240):MPRestClient::exec('POST',‘//prefe.’,数组,‘’application/jso.‘) #1 /home/..../public_html/mercadopago-sdk/mercadopago.php(126):MPRestClient::post(’/checkout/prefe.‘,Array) #2 /home/./public_html/concerm.php(140):MP->create_prefe #3 {main} {main}抛入第227行的/home/..../public_html/mercadopago-sdk/mercadopago.php中
有什么想法吗?
发布于 2013-07-29 09:05:46
我通过将数量输入到INT来解决这个问题。
foreach ($_SESSION["carrito"] as $value){
$itemCode = $value['tagme'];
$itemDesc = get_pedido($itemCode);
$itemQty = $value['cant']; // it comes from $_POST['cant']
$unitPrice = $value['unit_price'];
$items[] = array(
"title" => $itemDesc,
"quantity" => intval($itemQty), // this solved the error
"currency_id" => "ARS",
"unit_price" => $unitPrice
);
}我的数量似乎是字符串,它来自于会话中的隐藏和存储:
<input type="hidden" name="cant" value="1" />但是,MercadoPago API需要该字段中的整数。所以,我不得不使用intval()。
https://stackoverflow.com/questions/16454635
复制相似问题