API of Mercadopago

Asked

Viewed 1,518 times

5

I’m making a trolley system here to test the API of the marketplace. Everything is working, except the part about creating the payment that I have no idea how to get started.

API Mercadopago

I’m not very good at messing with arrays, and I’m here for help. As seen on the API website, it uses array.

$preference_data = array(
    "items" => array(
        array(
            "title" => "API do Mercado Pago",
            "quantity" => 1,
            "currency_id" => "BRL",                             "unit_price" => 10.00
        )
    )
);

Then I have an array (from the cart) that I want to pass to that of the MP payment request.

Cart, I’m doing by SESSION:

$_SESSION['carrinho'][$produto] = $preco; //Produto:Teste;Preço:20
$_SESSION['carrinho'][$produto2] = $preco2; //Produto:Teste2;Preço:15

(I used the Pagseguro API on it and managed to make it work)

Now I want to build that Mercadopago array with those Stand products that I can so:

foreach($_SESSION['carrinho'] as $produto => $preco){

Anyway, how do I convert the cart array to the mercadopago array?

  • What is stored in your cart array? The quantity is also stored?

  • Quantity and always 1

2 answers

1

Good first you must pass the data from your cart to the way the marketPago asks.

<?php 
foreach($_SESSION['carrinho'] as $produto){
$_SESSION['items']['title']=$produto['Produto'];
$_SESSION['items']['quantity']=1;
$_SESSION['items']['currency_id']='BRL';
$_SESSION['items']['unit_price']=$produto['Preço'];                             
}

?>

Ready in case what I did there, was to transform your cart and pass to the array of the way that mercadopago wants, an array items, with title, quantity... Now just go to $preference_data.

$preference_data=$_SESSION['items'];

And that’s it, oh the rest of the process, I believe it’s no secret... just follow like the API there. I hope I helped!! (I didn’t test here kk so it might not be that right)

  • tal error: Uncaught exception 'MercadoPagoException' with message 'items needed' in /home/u995146358/public_html/sistemas/mercadopago/lib/mercadopago.php:528 Stack trace: #0 /home/u995146358/public_html/sistemas/mercadopago/lib/mercadopago.php(557): MPRestClient::exec(Array) #1 /home/u995146358/public_html/sistemas/mercadopago/lib/mercadopago.php(215): Mprestclient::post(Array) #2 /home/u995146358/public_html/sistemas/pagar.php(76): MP->create_preference(Array) #3 {main} thrown in /home/u995146358/public_html/sistemas/mercadopago/lib/mercadopago.php on line 528

  • Help there, have Skype?

  • Didn’t work out ?

0

I had the same problem. I solved it as follows:

$itensMP = array(array());

foreach ($this->cart->contents() as $items) {

    $itemMP = array(
        "id" => $items['id'],
        "title" => $items['name'],
        "quantity" => intval($items['qty']),
        "currency_id" => "BRL", 
        "unit_price" => floatval($items['price'])
    );

    array_push($itensMP, $itemMP);
}

$mp = new MP('seu_id', 'seu_token');

$preference_data = array("items" => $itensMP);

$preference = $mp->create_preference($preference_data);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.