christiansalazarh

paypal

Aug 22nd, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.22 KB | None | 0 0
  1. <?php
  2.  /**
  3.      Esta es una clase que escribi mucho tiempo atras, con errores en cuanto a modelado y no es orientada a OOP pero funciona bien.
  4.  
  5.      Uso:
  6.  
  7.      en un action solo llamar a:
  8.      $ppal =  new PayPal();
  9.      $ppal->handler();
  10.      este action es lo que llamamos el "callback" en paypal, cuya url deberea ser registrada en la pagina de paypal para que al pagar se nos notifique con este callback.
  11.  
  12.  
  13.      en una vista crear un formulario de pago paypal con:  
  14.      PayPal::createInstantPaymentForm($formid,$custom,$itemname,$itemnumber,$qty
  15.         ,$subtotal,$discount,$total);
  16.  
  17.      requiere configuracion en el array de parametros en config main.
  18.  
  19.      Christian Salazar <christiansalazarh@gmail.com>
  20.  */
  21.  class PayPal {
  22.  
  23.     // variables a leer tras una llamada exitosa a paypalinst->handler();
  24.     //
  25.     public $p_txn_type;
  26.     public $p_payer_id;
  27.     public $p_txn_id;
  28.     public $p_payment_date;
  29.     public $p_custom;
  30.     public $p_data;
  31.     public $p_payment_status;
  32.     public $p_verified;
  33.     public $p_mc_gross_1;  // en realidad presenta a mc_gross,
  34.     public $p_mc_fee;
  35.  
  36.     public function clear(){
  37.         $this->p_txn_type="";
  38.         $this->p_payer_id="";
  39.         $this->p_txn_id="";
  40.         $this->p_payment_date="";
  41.         $this->p_custom="";
  42.         $this->p_data="";
  43.         $this->p_payment_status="";
  44.         $this->p_verified="";
  45.         $this->p_mc_gross_1="";
  46.         $this->p_mc_fee="";
  47.     }
  48.  
  49.     public static function createInstantPaymentForm($formid,$custom,$itemname,$itemnumber,$qty
  50.         ,$subtotal,$discount,$total,$onsubmit_jsfunction_name=null)
  51.     {
  52.         $return_url = Yii::app()->params['paypal_returnurl'];
  53.         $cancel_url = Yii::app()->params['paypal_cancelurl'];
  54.         $email = Yii::app()->params['paypal_business'];
  55.         $url = Yii::app()->params['paypal_url'];
  56.         $notifyurl = Yii::app()->params['paypal_ipnhandler'];
  57.  
  58.         if($onsubmit_jsfunction_name == null)
  59.         {
  60.             $onsubmit = "";
  61.         }
  62.         else
  63.         {
  64.             $onsubmit = "onsubmit = 'return {$onsubmit_jsfunction_name}()';";
  65.         }
  66.  
  67.  
  68.         echo "\n\n<!-- paypal form -->\n";
  69.         echo "<form action='$url' method='post' id='$formid' class='paypalform' {$onsubmit}>\n";
  70.         echo \t<input type='hidden' name='business' value='$email'>\n";
  71.         echo \t<input type='hidden' name='notify_url' value='$notifyurl'>\n";
  72.         echo \t<input type='hidden' name='return' value='$return_url'>\n";
  73.         echo \t<input type='hidden' name='cancel_url' value='$cancel_url'>\n";
  74.         echo \t<input type='hidden' name='currency_code' value='USD'>\n";
  75.         echo \t<input type='hidden' name='address_override' value='1'>\n";
  76.         echo \t<input type='hidden' name='no_shipping' value='1'>\n";
  77.         //echo "    \t<input type='hidden' name='tax' value='0'>\n";
  78.         echo \t<input type='hidden' name='cmd' value='_xclick'>\n";
  79.         echo \t<input type='hidden' name='custom' value='$custom' />\n";
  80.         echo \t<input type='hidden' name='item_name' value='$itemname'>\n";
  81.         //echo "    \t<input type='hidden' name='item_number' value='$itemnumber'>\n";
  82.         //echo "    \t<input type='hidden' name='quantity' value='$qty'>\n";
  83.         echo \t<input type='hidden' name='amount' value='$total'>\n";
  84.         echo \t<input type='submit' value='' class='paypalbutton'>\n";
  85.         echo "</form>\n\n\n";
  86.     }
  87.  
  88.  
  89.  
  90.     /** este handler debe ir en un action expuesto a paypal
  91.      *
  92.      *
  93.      */
  94.     public function handler()
  95.     {
  96.         Yii::log("inicia handler","info");
  97.  
  98.         // url para enviar de vuelta el paquete ipn enviado para ser verificado por paypal
  99.         $verifier = Yii::app()->params['paypal_verifier'];
  100.         error_reporting(E_ALL ^ E_NOTICE);
  101.  
  102.         $this->p_txn_type = $_POST['txn_type'];
  103.         $this->p_payer_id = $_POST['payer_id'];
  104.         $this->p_txn_id = $_POST['txn_id'];
  105.         $this->p_payment_date = $_POST['payment_date'];
  106.         $this->p_custom = $_POST['custom'];
  107.         $this->p_payment_status = $_POST['payment_status'];
  108.         $this->p_mc_gross_1 = $_POST['mc_gross'];
  109.         $this->p_mc_fee = $_POST['mc_fee'];
  110.  
  111.         $this->p_data = "";
  112.         foreach($_POST as $k=>$v)
  113.             $this->p_data .= "{$k}={$v}\r\n";
  114.  
  115.         Yii::log("handler data:\r\n".$this->p_data,"info");
  116.  
  117.         // Read the post from PayPal and add 'cmd'
  118.         $req = 'cmd=_notify-validate';
  119.         if(function_exists('get_magic_quotes_gpc'))
  120.             $get_magic_quotes_exits = true;
  121.  
  122.         foreach ($_POST as $key => $value)
  123.         {
  124.             if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1)
  125.             {
  126.                 $value = urlencode(stripslashes($value));
  127.             }
  128.             else
  129.             {
  130.                 $value = urlencode($value);
  131.             }
  132.             $req .= "&$key=$value";
  133.         }
  134.  
  135.         // Post back to PayPal to validate
  136.         $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
  137.         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  138.         $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  139.  
  140.         Yii::log("handler verifier:\r\n".$verifier,"info");
  141.  
  142.         $fp = fsockopen ($verifier, 443, $errno, $errstr, 30);
  143.         if (!$fp)
  144.         { // HTTP ERROR
  145.             $reception = "HTTP-ERROR";
  146.         }
  147.         else
  148.         {
  149.             // NO HTTP ERROR
  150.             fputs ($fp, $header . $req);
  151.             while (!feof($fp))
  152.             {
  153.                 $res = fgets ($fp, 1024);
  154.  
  155.                 if (strcmp ($res, "VERIFIED") == 0)
  156.                 {
  157.                     $reception = "VERIFIED";
  158.                 }
  159.                 else
  160.                 if (strcmp ($res, "INVALID") == 0)
  161.                 {
  162.                     $reception = "INVALID";
  163.                 }
  164.             }
  165.         }
  166.         fclose ($fp);
  167.         Yii::log("handler verifier result:\r\n".$reception,"info");
  168.  
  169.         $this->p_verified = $reception;
  170.         return ($reception == "VERIFIED");
  171.     }
  172.  }
Add Comment
Please, Sign In to add comment