MarketPay API

5

I'm making a cart system here to test the MercadoPago API. Everything is working except for the part of creating the payment that I have no idea how to start.

MercadoPago API

I'm not very good at messing with arrays, and I'm here to ask for help. As it turns out there on the API site, it uses array.

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

So I have an array (of the cart) that I want to pass to the MP's payment request.

Cart, so doing by SESSION:

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

(I used the PayPal API on it and got it to work)

Now I want to build that MercadoPago array with these Cart products that I get like this:

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

Finally, how do I convert the cart array to the marketplace array?

    
asked by anonymous 11.12.2016 / 15:05

2 answers

1

Well first you must pass the data of your cart to the form that the marketPago asks for.

<?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'];                             
}

?>

In the case what I did there, it was to transform your cart and move to the array in the way that marketplace wants, an array items, with title, quantity ... Now just move on $ preference_data.

$preference_data=$_SESSION['items'];

And that's it, there the rest of the process, I believe it's no secret ... just follow the API there. Hope this helps!! (I have not tested here kk so it might not be right)

    
11.12.2016 / 15:36
0

I had the same problem. I have decided 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);
    
04.11.2017 / 23:12