Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.71 KB | None | 0 0
  1. <?php
  2.  
  3. define('IPN_CERTIFICATE_PATH', __DIR__ . '/cert/cacert.pem');
  4. class ipnlistener
  5. {
  6.     public $use_curl = true;
  7.  
  8.     public $force_ssl_v3 = false;
  9.  
  10.     public $follow_location = false;
  11.  
  12.     public $use_ssl = false;
  13.  
  14.     public $use_sandbox = false;
  15.  
  16.     public $timeout = 30;
  17.     private $post_data = [];
  18.     private $post_uri = '';
  19.     private $response_status = '';
  20.     private $response = '';
  21.  
  22.     const PAYPAL_HOST = 'ipnpb.paypal.com';
  23.     const SANDBOX_HOST = 'ipnpb.sandbox.paypal.com';
  24.    
  25.     protected function curlPost($encoded_data)
  26.     {
  27.         if ($this->use_ssl) {
  28.             $uri = 'https://'.$this->getPaypalHost().'/cgi-bin/webscr';
  29.             $this->post_uri = $uri;
  30.         } else {
  31.             $uri = 'https://'.$this->getPaypalHost().'/cgi-bin/webscr';
  32.             $this->post_uri = $uri;
  33.         }
  34.         $ch = curl_init();
  35.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  36.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  37.         curl_setopt($ch, CURLOPT_CAINFO, IPN_CERTIFICATE_PATH);
  38.         curl_setopt($ch, CURLOPT_URL, $uri);
  39.         curl_setopt($ch, CURLOPT_POST, true);
  40.         curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_data);
  41.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
  42.         curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
  43.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44.         curl_setopt($ch, CURLOPT_HEADER, true);
  45.         if ($this->force_ssl_v3) {
  46.             curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  47.         }
  48.         $this->response = curl_exec($ch);
  49.         $this->response_status = strval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  50.         if ($this->response === false || $this->response_status == '0') {
  51.             $errno = curl_errno($ch);
  52.             $errstr = curl_error($ch);
  53.             throw new Exception("cURL error: [$errno] $errstr");
  54.         }
  55.     }
  56.    
  57.     protected function fsockPost($encoded_data)
  58.     {
  59.         if ($this->use_ssl) {
  60.             $uri = 'ssl://'.$this->getPaypalHost();
  61.             $port = '443';
  62.             $this->post_uri = $uri.'/cgi-bin/webscr';
  63.         } else {
  64.             $uri = $this->getPaypalHost();
  65.             $port = '80';
  66.             $this->post_uri = 'https://'.$uri.'/cgi-bin/webscr';
  67.         }
  68.         $fp = fsockopen($uri, $port, $errno, $errstr, $this->timeout);
  69.         if (!$fp) {
  70.             throw new Exception("fsockopen error: [$errno] $errstr");
  71.         }
  72.         $header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
  73.         $header .= 'Host: '.$this->getPaypalHost()."\r\n";
  74.         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  75.         $header .= 'Content-Length: '.strlen($encoded_data)."\r\n";
  76.         $header .= "Connection: Close\r\n\r\n";
  77.         fputs($fp, $header.$encoded_data."\r\n\r\n");
  78.         while (!feof($fp)) {
  79.             if (empty($this->response)) {
  80.                 $this->response .= $status = fgets($fp, 1024);
  81.                 $this->response_status = trim(substr($status, 9, 4));
  82.             } else {
  83.                 $this->response .= fgets($fp, 1024);
  84.             }
  85.         }
  86.         fclose($fp);
  87.     }
  88.    
  89.     private function getPaypalHost()
  90.     {
  91.         if ($this->use_sandbox) {
  92.             return self::SANDBOX_HOST;
  93.         } else {
  94.             return self::PAYPAL_HOST;
  95.         }
  96.     }
  97.    
  98.     public function getPostUri()
  99.     {
  100.         return $this->post_uri;
  101.     }
  102.    
  103.     public function getResponse()
  104.     {
  105.         return $this->response;
  106.     }
  107.    
  108.     public function getResponseStatus()
  109.     {
  110.         return $this->response_status;
  111.     }
  112.    
  113.     public function getTextReport()
  114.     {
  115.         $r = '';
  116.         for ($i = 0; $i < 80; ++$i) {
  117.             $r .= '-';
  118.         }
  119.         $r .= "\n[".date('m/d/Y g:i A').'] - '.$this->getPostUri();
  120.         if ($this->use_curl) {
  121.             $r .= " (curl)\n";
  122.         } else {
  123.             $r .= " (fsockopen)\n";
  124.         }
  125.         for ($i = 0; $i < 80; ++$i) {
  126.             $r .= '-';
  127.         }
  128.         $r .= "\n{$this->getResponse() }\n";
  129.         for ($i = 0; $i < 80; ++$i) {
  130.             $r .= '-';
  131.         }
  132.         $r .= "\n";
  133.         foreach ($this->post_data as $key => $value) {
  134.             $r .= str_pad($key, 25)."$value\n";
  135.         }
  136.         $r .= "\n\n";
  137.  
  138.         return $r;
  139.     }
  140.    
  141.     public function processIpn($post_data = null)
  142.     {
  143.         $encoded_data = 'cmd=_notify-validate';
  144.         if ($post_data === null) {
  145.             if (!empty($_POST)) {
  146.                 $this->post_data = $_POST;
  147.                 $encoded_data .= '&'.file_get_contents('php://input');
  148.             } else {
  149.                 throw new Exception('No POST data found.');
  150.             }
  151.         } else {
  152.             $this->post_data = $post_data;
  153.             foreach ($this->post_data as $key => $value) {
  154.                 $encoded_data .= "&$key=".urlencode($value);
  155.             }
  156.         }
  157.         if ($this->use_curl) {
  158.             $this->curlPost($encoded_data);
  159.         } else {
  160.             $this->fsockPost($encoded_data);
  161.         }
  162.         if (strpos($this->response_status, '200') === false) {
  163.             throw new Exception('Invalid response status: '.$this->response_status);
  164.         }
  165.         if (strpos($this->response, 'VERIFIED') !== false) {
  166.             return true;
  167.         } elseif (strpos($this->response, 'INVALID') !== false) {
  168.             return false;
  169.         } else {
  170.             throw new Exception('Unexpected response from PayPal.');
  171.         }
  172.     }
  173.    
  174.     public function requirePostMethod()
  175.     {
  176.         if ($_SERVER['REQUEST_METHOD'] && $_SERVER['REQUEST_METHOD'] != 'POST') {
  177.             header('Allow: POST', true, 405);
  178.             throw new Exception('Invalid HTTP request method.');
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement