Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- Esta es una clase que escribi mucho tiempo atras, con errores en cuanto a modelado y no es orientada a OOP pero funciona bien.
- Uso:
- en un action solo llamar a:
- $ppal = new PayPal();
- $ppal->handler();
- 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.
- en una vista crear un formulario de pago paypal con:
- PayPal::createInstantPaymentForm($formid,$custom,$itemname,$itemnumber,$qty
- ,$subtotal,$discount,$total);
- requiere configuracion en el array de parametros en config main.
- Christian Salazar <christiansalazarh@gmail.com>
- */
- class PayPal {
- // variables a leer tras una llamada exitosa a paypalinst->handler();
- //
- public $p_txn_type;
- public $p_payer_id;
- public $p_txn_id;
- public $p_payment_date;
- public $p_custom;
- public $p_data;
- public $p_payment_status;
- public $p_verified;
- public $p_mc_gross_1; // en realidad presenta a mc_gross,
- public $p_mc_fee;
- public function clear(){
- $this->p_txn_type="";
- $this->p_payer_id="";
- $this->p_txn_id="";
- $this->p_payment_date="";
- $this->p_custom="";
- $this->p_data="";
- $this->p_payment_status="";
- $this->p_verified="";
- $this->p_mc_gross_1="";
- $this->p_mc_fee="";
- }
- public static function createInstantPaymentForm($formid,$custom,$itemname,$itemnumber,$qty
- ,$subtotal,$discount,$total,$onsubmit_jsfunction_name=null)
- {
- $return_url = Yii::app()->params['paypal_returnurl'];
- $cancel_url = Yii::app()->params['paypal_cancelurl'];
- $email = Yii::app()->params['paypal_business'];
- $url = Yii::app()->params['paypal_url'];
- $notifyurl = Yii::app()->params['paypal_ipnhandler'];
- if($onsubmit_jsfunction_name == null)
- {
- $onsubmit = "";
- }
- else
- {
- $onsubmit = "onsubmit = 'return {$onsubmit_jsfunction_name}()';";
- }
- echo "\n\n<!-- paypal form -->\n";
- echo "<form action='$url' method='post' id='$formid' class='paypalform' {$onsubmit}>\n";
- echo " \t<input type='hidden' name='business' value='$email'>\n";
- echo " \t<input type='hidden' name='notify_url' value='$notifyurl'>\n";
- echo " \t<input type='hidden' name='return' value='$return_url'>\n";
- echo " \t<input type='hidden' name='cancel_url' value='$cancel_url'>\n";
- echo " \t<input type='hidden' name='currency_code' value='USD'>\n";
- echo " \t<input type='hidden' name='address_override' value='1'>\n";
- echo " \t<input type='hidden' name='no_shipping' value='1'>\n";
- //echo " \t<input type='hidden' name='tax' value='0'>\n";
- echo " \t<input type='hidden' name='cmd' value='_xclick'>\n";
- echo " \t<input type='hidden' name='custom' value='$custom' />\n";
- echo " \t<input type='hidden' name='item_name' value='$itemname'>\n";
- //echo " \t<input type='hidden' name='item_number' value='$itemnumber'>\n";
- //echo " \t<input type='hidden' name='quantity' value='$qty'>\n";
- echo " \t<input type='hidden' name='amount' value='$total'>\n";
- echo " \t<input type='submit' value='' class='paypalbutton'>\n";
- echo "</form>\n\n\n";
- }
- /** este handler debe ir en un action expuesto a paypal
- *
- *
- */
- public function handler()
- {
- Yii::log("inicia handler","info");
- // url para enviar de vuelta el paquete ipn enviado para ser verificado por paypal
- $verifier = Yii::app()->params['paypal_verifier'];
- error_reporting(E_ALL ^ E_NOTICE);
- $this->p_txn_type = $_POST['txn_type'];
- $this->p_payer_id = $_POST['payer_id'];
- $this->p_txn_id = $_POST['txn_id'];
- $this->p_payment_date = $_POST['payment_date'];
- $this->p_custom = $_POST['custom'];
- $this->p_payment_status = $_POST['payment_status'];
- $this->p_mc_gross_1 = $_POST['mc_gross'];
- $this->p_mc_fee = $_POST['mc_fee'];
- $this->p_data = "";
- foreach($_POST as $k=>$v)
- $this->p_data .= "{$k}={$v}\r\n";
- Yii::log("handler data:\r\n".$this->p_data,"info");
- // Read the post from PayPal and add 'cmd'
- $req = 'cmd=_notify-validate';
- if(function_exists('get_magic_quotes_gpc'))
- $get_magic_quotes_exits = true;
- foreach ($_POST as $key => $value)
- {
- if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1)
- {
- $value = urlencode(stripslashes($value));
- }
- else
- {
- $value = urlencode($value);
- }
- $req .= "&$key=$value";
- }
- // Post back to PayPal to validate
- $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
- $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
- $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
- Yii::log("handler verifier:\r\n".$verifier,"info");
- $fp = fsockopen ($verifier, 443, $errno, $errstr, 30);
- if (!$fp)
- { // HTTP ERROR
- $reception = "HTTP-ERROR";
- }
- else
- {
- // NO HTTP ERROR
- fputs ($fp, $header . $req);
- while (!feof($fp))
- {
- $res = fgets ($fp, 1024);
- if (strcmp ($res, "VERIFIED") == 0)
- {
- $reception = "VERIFIED";
- }
- else
- if (strcmp ($res, "INVALID") == 0)
- {
- $reception = "INVALID";
- }
- }
- }
- fclose ($fp);
- Yii::log("handler verifier result:\r\n".$reception,"info");
- $this->p_verified = $reception;
- return ($reception == "VERIFIED");
- }
- }
Add Comment
Please, Sign In to add comment