Skip to content

Instantly share code, notes, and snippets.

@andresams
Created March 8, 2016 13:59
Show Gist options
  • Save andresams/c7362d1e8862c1de9d22 to your computer and use it in GitHub Desktop.
Save andresams/c7362d1e8862c1de9d22 to your computer and use it in GitHub Desktop.
Código do envio de dados para API do Mercado Pago.
<?php
#Código de seleção destes dados do cliente e inserção do pedido acima
$data['client_id']=Configuration::get('MERCADOC_CLIENT_ID');
$data['client_secret']=Configuration::get('MERCADOC_CLIENT_SECRET');
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'MercadoPago Prestashop Brasil v2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: '.'application/json'));
curl_setopt($ch, CURLOPT_URL, "https://api.mercadolibre.com/oauth/token?client_id=". $data['client_id']."&grant_type=client_credentials&client_secret=".$data['client_secret']);
$login=json_decode(curl_exec($ch));
$reason=urlencode('Pedido-'.$id_compra);
$uri='https://api.mercadolibre.com/checkout/custom/create_payment?access_token='.$login->access_token;
$data['amount']=(float) $_POST['amount'];
$data['reason']=$reason;
$data['currency_id']='BRL';
$data['payment_method_id']=strtolower($_POST['bandeira_cartao']);
$data['payer_email']=$endereco->email;
$data['external_reference']=$cart->id;
$data['installments']= (int)$_POST['parcelas'];
$data['access_token']=$login->access_token;
$data['card_token_id']=$_POST['card_token_id'];
#Insere os items comprados no vetor
$j=0;
foreach($order_products as $i => $produto)
{
$item['id']= $produto['product_id'];
$item['title'] = substr($produto['product_name'], 0, 250);
$item['category_id'] = 'fashion';
$item['quantity'] = $produto['product_quantity'];
$item['currency_id'] = 'BRL';
$item['unity_price'] = number_format($produto['price'], 2);
#Obtém URL da imagem deste produto
$image = new Image($produto['image']->id_image);
$item['picture_url'] = _PS_BASE_URL_._THEME_PROD_DIR_.$image->getExistingImgPath().".jpg";
$items[]=$item;
unset($item);
}
$data['items'] = $items;
#Dados do comprador
$datetime = new DateTime(date('Y-m-d H:i:s'));
$date_created =$datetime->format(DateTime::ISO8601);
$cliente['name']=trim($_POST['fatura_nome']);
$cliente['surname']=trim($_POST['fatura_sobrenome']);
$cliente['email']=trim($endereco->email);
$cliente['phone']['area_code']=trim($_POST['cartao_telefone_ddd']);
$cliente['phone']['number']=trim($_POST['cartao_telefone']);
$cliente['identification']['type']='CPF';
$cliente['identification']['number']=preg_replace('/\D/', '', $_POST['cartao_cpf']);
$cliente['address']['zip_code']=trim($_POST['fatura_cep']);
$cliente['address']['street_name']=trim($_POST['fatura_endereco']);
$cliente['address']['street_number']=trim($_POST['fatura_numero']);
$cliente['date_created']= $date_created;
$data['payer'] = $cliente;
$data['notification_url'] ='https://www.donnashape.com/mercadoc/retornoc.php';
$data['external_reference'] = $id_compra;
$data_string=json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $uri);
$pagamento=(json_decode(curl_exec($ch)));
#abaixo disso é feito o tratamento com os dados retornados pelo Mercado Pago,
#como por exemplo mudar o status do pedido, etc
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment