Advertisement
Guest User

pagseguro.php

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