MercadoPago PHP SDK数量错误

时间:2013-05-09 04:53:51

标签: php api payment-gateway

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

$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 Stack   追踪:#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}引自/home/..../public_html/mercadopago-sdk/mercadopago.php on第227行

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我通过将数量转换为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()