Skip to content

Instantly share code, notes, and snippets.

@pablomoretti
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablomoretti/e0cd3a3b85b5ff2c8268 to your computer and use it in GitHub Desktop.
Save pablomoretti/e0cd3a3b85b5ff2c8268 to your computer and use it in GitHub Desktop.
MercadoLibre notifications example
<?php
include 'access_token.php';
include 'meekrodb.2.2.class.php';
DB::$error_handler = false;
DB::$throw_exception_on_error = true;
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE );
if($input['topic'] == 'orders'){
$order = json_decode(file_get_contents('https://api.mercadolibre.com' . $input['resource'] . "?access_token=" . get_access_token()),TRUE);
if($order == NULL){
exit('error obtener la orden');
}
$orderId = $order['id'];
$rows = DB::query("SELECT * FROM my_orders WHERE meli_order_id = %i ", $order['id']);
//new order - array_merge
if(count($rows) == 0){
DB::insert('my_orders', array(
'meli_order_id' => $order['id'],
'product' => $order['order_items'][0]['item']['title'],
'status' => $order['status'],
'customer' => ('email:' . $order['buyer']['email'] . '/ tel :' . $order['buyer']['phone']['area_code'] . $order['buyer']['phone']['number'] )
));
DB::insertId();
} else{
DB::update('my_orders', array(
'product' => $order['order_items'][0]['item']['title'],
'status' => $order['status'],
'customer' => ('email:' . $order['buyer']['email'] . '/ tel :' . $order['buyer']['phone']['area_code'] . $order['buyer']['phone']['number'] )
), "meli_order_id=%i", $order['id']);
}
}else{
exit('Request MALO :(');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment