ulvis.paste.net

Paste Search Dynamic
Recent pastes
Codigo dentro de Pages
  1. <?php
  2.  if (!defined('INITIALIZED'))
  3.     exit;
  4.  
  5. require_once "mercadopago.php";
  6.  
  7.  // Pega em https://www.mercadopago.com/mlb/account/credentials > Checkout Básico
  8. $mp = new MP("CLIENT ID", "CLIENT_SECRET");
  9.  
  10. /*
  11. SQL :
  12.  CREATE TABLE mercado_pago(
  13.     `id` INT(11) NOT NULL AUTO_INCREMENT,
  14.     `account_id` int(11) DEFAULT NULL,
  15.     `item_count` int(11) NOT NULL,
  16.     `date` BIGINT(15),
  17.     `payment_amount` float DEFAULT '0',
  18.     `status` varchar(100) NOT NULL,
  19.     `code` varchar(100) NOT NULL,
  20.     `email` varchar(100) NOT NULL,
  21.     PRIMARY KEY (`id`)
  22. );
  23.  
  24. */
  25.        
  26. if (!isset($_GET["id"]) || !ctype_digit($_GET["id"])) {
  27.     http_response_code(400);
  28.     return;
  29. }else{
  30.  
  31. $payment_info = $mp->get_payment_info($_GET["id"]);
  32.  
  33.  
  34. if ($payment_info["status"] == 200) {
  35.  
  36.     if($payment_info['response']['status'] == 'approved')
  37.     {
  38.         // print_r($payment_info['response']);
  39.         // Alterar o $payment_info para o codigo da transação
  40.  
  41.         if(!isset($payment_info['response']['description']))
  42.             $errors[] = "Não existe Description";
  43.  
  44.        
  45.  
  46.         if(empty($errors)){
  47.  
  48.         if(isset($payment_info['response']['payer']['email']) && strlen($payment_info['response']['payer']['email']) > 5 ){
  49.             $email = $payment_info['response']['payer']['email'];
  50.         }elseif(isset($payment_info['response']['collector']['email']) && strlen($payment_info['response']['collector']['email']) > 5){
  51.             $email = $payment_info['response']['collector']['email'];
  52.         }else
  53.             $email = "Not Found";
  54.  
  55.        
  56.         $code = $_GET["id"];
  57.         $desc = $payment_info['response']['description'];
  58.         $status = $payment_info['response']['status'];    
  59.         $amount = $payment_info['response']['transaction_amount'];
  60.         $steps = explode(" ", $desc);
  61.         $coins = (int)$steps[0];        
  62.        
  63.         $findQ = $SQL->query("SELECT * FROM `mercado_pago` WHERE code = ".$code)->fetchAll();
  64.  
  65.               if(count($findQ) == 0)
  66.                 {
  67.            $add = $SQL->query("INSERT INTO `mercado_pago`(`item_count`, `date`, `payment_amount`, `status`, `code`, `email`) VALUES ('$coins', '".time()."', '$amount', '$status', '$code', '$email')");
  68.                 }else
  69.             $main_content .= 'This payment already exist.';
  70.  
  71.         }else{
  72.             $main_content .= "Os seguintes erros aconteceram: <br />";
  73.             foreach ($errors as $err) {
  74.                 $main_content .= $errors.'<br>';
  75.             }
  76.             }
  77.  
  78.     }
  79.  
  80. }
  81. http_response_code(200);
  82.  
  83. }
Parsed in 0.051 seconds