Return to Snippet

Revision: 50273
at August 17, 2011 21:16 by mafhh14


Initial Code
<?php
// for live
// $url = "https://www.paypal.com/cgi-bin/webscr";
// for sandbox
$url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
?>
<form name="paypal_ipn" action="<?php echo $url ?>" method="POST">
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="cmd" value="_cart" />
<!-- The business email address, where you want to receive the payment -->
<input type="hidden" name="business" value="[email protected]" />
<!-- The customer email address -->
<input type="hidden" name="receiver_email" value="[email protected]" />
<input type="hidden" name="item_name_1" value="test product" />
<input type="hidden" name="quantity_1" value="1" />
<input type="hidden" name="amount_1" value="25.58" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="invoice" value="12345" />
<input type="hidden" name="amount" value="25.58" /> 
<!-- Where you want to return after PayPal Payment -->
<input type="hidden" name="return" value="http://www.yoursite.com/finished.php" />
<!-- A back-end notification send to the specific page after successful payment  -->
<input type="hidden" name="notify_url" value="https://www.yoursite.com/finished_secure.php" />
<!-- Where you want to return after cancel the PayPal Payment  -->
<input type="hidden" name="cancel_return" value="http://www.yoursite.com" />
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" border=0 />
</form>

============================ Start finished_secure.php file ===============================
<?php
$req = 'cmd=_notify-validate';
foreach($_POST as $key => $value) {
   $value = urlencode(stripslashes($value));
   $req  .= "&$key=$value";
}
$header .= "POST /cgi-bin/webscr HTTP/1.0
";
$header .= "Content-Type: application/x-www-form-urlencoded
";
$header .= "Content-Length: " . strlen($req) . "
";
//$url = "ssl://www.paypal.com"; // for live
$url = "ssl://www.sandbox.paypal.com"; // for sandbox
$fp = fsockopen ("$url", 443, $errno, $errstr, 30);

$paypal_payment_amount = $_POST['mc_gross'];

//print_r($_POST); To print all the variables get form PayPal

if(!$fp){
   echo "PayPal socket not opened.";
}
else{
  fputs ($fp, $header . $req);
  while(!feof($fp)){
    $res = fgets ($fp, 1024);
    if(strcmp($res, "VERIFIED") == 0){
      // write any code to here
    }
   elseif(strcmp ($res, "INVALID") == 0){
      // error
   }
  } // end while
}
fclose($fp);
?>
============================== End finished_secure.php file =================================

Initial URL
http://www.phpmoot.com/php-paypal-ipn-payment-gateway/

Initial Description


Initial Title
PayPal IPN Payment Gateway

Initial Tags
php

Initial Language
PHP