0

我正在使用PHP MercadoPago SDK 模块。这是我的代码:

$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 中带有消息“数量必须是数字”的未捕获异常“异常” :227 堆栈跟踪:#0 /home/..../ public_html/mercadopago-sdk/mercadopago.php(240): MPRESTClient::exec('POST', '/checkout/prefe...', Array, 'application/jso...') #1 /home/.. ../public_html/mercadopago-sdk/mercadopago.php(126): MPRESTClient::post('/checkout/prefe...', Array) #2 /home/../public_html/confirm.php(140 ): MP->create_preference(Array) #3 {main} 在第 227 行的 /home/..../public_html/mercadopago-sdk/mercadopago.php 中抛出

任何想法?

4

1 回答 1

2

I solved the problem by typecasting the quantity to 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
    ); 
}

My quantity seems to be string which comes from hidden and stores in session:

<input type="hidden" name="cant" value="1" />

But, MercadoPago API expects an integer in that field. So, I had to use intval().

于 2013-07-29T09:05:46.283 回答