Advertisement
Guest User

pagseguro.php

a guest
May 10th, 2016
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. <?php
  2.     header('Content-Type: text/html; charset=ISO-8859-1');
  3.     define('TOKEN', 'Digite seu TOKEN aqui');
  4.    
  5.     $servername = "localhost";
  6.     $username = " ";
  7.     $password = " ";
  8.     $dbname = "pagseguro";
  9.    
  10.     $conn = new mysqli($servername, $username, $password, $dbname);
  11.     if ($conn->connect_error)
  12.     {
  13.         die("Connection failed: " . $conn->connect_error);
  14.     }
  15.    
  16.     mysqli_query($conn, "SET NAMES 'utf8'"); // Essa Query foi a solução que eu encontrei para extrair caracteres especiais sem problemas.
  17.  
  18.     class PagSeguroNpi
  19.     {
  20.         private $timeout = 20;
  21.        
  22.         public function notificationPost() {
  23.             $postdata = 'Comando=validar&Token='.TOKEN;
  24.             foreach ($_POST as $key => $value) {
  25.                 $valued    = $this->clearStr($value);
  26.                 $postdata .= "&$key=$valued";
  27.             }
  28.             return $this->verify($postdata);
  29.         }
  30.        
  31.         private function clearStr($str) {
  32.             if (!get_magic_quotes_gpc()) {
  33.                 $str = addslashes($str);
  34.             }
  35.             return $str;
  36.         }
  37.        
  38.         private function verify($data) {
  39.             $curl = curl_init();
  40.             curl_setopt($curl, CURLOPT_URL, "https://pagseguro.uol.com.br/pagseguro-ws/checkout/NPI.jhtml");
  41.             curl_setopt($curl, CURLOPT_POST, true);
  42.             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  43.             curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  44.             curl_setopt($curl, CURLOPT_HEADER, false);
  45.             curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
  46.             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  47.             $result = trim(curl_exec($curl));
  48.             curl_close($curl);
  49.             return $result;
  50.         }
  51.     }
  52.  
  53.     if (count($_POST) > 0)
  54.     {
  55.         $npi = new PagSeguroNpi();
  56.         $result = $npi->notificationPost();
  57.         if ($result === "VERIFICADO")
  58.         {
  59.             if($_POST['StatusTransacao'] === "Aprovado")
  60.             {
  61.                 $Code = $_POST['TransacaoID'];
  62.                 $Produto_ID = $_POST['ProdID_1'];
  63.                
  64.                 if($Produto_ID == 1) $Cash = 1000;
  65.                 else if($Produto_ID == 2) $Cash = 2000; //Aqui você irá definir quanto de cash vale cada produto
  66.                
  67.                 $sql = "INSERT INTO pagseguro (Code, Cash) VALUES ('$Code', '$Cash')";
  68.                 $conn->query($sql);
  69.             }
  70.         }
  71.     }
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement