2

I need to add Paypal support to PHP-based website.

  1. What data I need to send to PayPal via POST to receive a payment of X dollars sent to a specified Paypal account?
  2. What kind of response I get back from Paypal and how?

TIA

flag

1 Answer

1

The simplest way is to display "Buy now" button on your website. Here is the sample code.

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="text" name="amount" value="" />
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="business name">
<input type="hidden" name="lc" value="JP">
<input type="hidden" name="item_name" value="Some services">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="rm" value="1">
<input type="hidden" name="return" value="http://somesite.com/success.php">
<input type="hidden" name="cancel_return" value="http://somesite.com/cancel.php">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-BuyNowBF:button_green.gif:NonHosted">
<input type="hidden" name="notify_url" value="https://somesite.com/ipn.php">
<input type="image" src="http://somesite.com/button_green.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

You can specify both 'success' and 'cancel' page URLs.

If you need Paypal to ping your website once payment is processed use IPN (Instant Paypal Notification). URL of IPN page needs to be passed as notify_url parameter. Here is the sample PHP code of ipn.php file.

<?php
include("include/dbcommon.php");

set_error_handler("ipn_error_handler");

//------------------------------------------------------------------
// Open log file (in append mode) and write the current time into it.
// Open the DB Connection. Open the actual database.
//-------------------------------------------------------------------
$log = fopen("ipn.log", "a");
fwrite($log, "\n\nipn - " . gmstrftime ("%b %d %Y %H:%M:%S", time()) . "\n");

//------------------------------------------------
// Read post from PayPal system and create reply
// starting with: 'cmd=_notify-validate'...
// then repeating all values sent - VALIDATION.
//------------------------------------------------

$req = "cmd=_notify-validate";
foreach($_POST as $key=>$val)
{
  $req.= "&".$key."=".urlencode($val);
  fwrite($log,$key."=".$val."\n");
}


//--------------------------------------------
// Create message to post back to PayPal...
// Open a socket to the PayPal server...
//--------------------------------------------
$header = "POST http://www.paypal.com/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";
$fp = fsockopen ("www.paypal.com", 80, $errno, $errstr, 30);

//----------------------------------------------------------------------
// Check HTTP connection made to PayPal OK, If not, print an error msg
//----------------------------------------------------------------------
if (!$fp)
{
  echo "$errstr ($errno)";
  fwrite($log, "Failed to open HTTP connection!\n");
  fwrite($log, $errstr." ".$errno);
  fclose ($log);
  return;
}

//--------------------------------------------------------
// If connected OK, write the posted values back, then...
//--------------------------------------------------------
fputs ($fp, $header . $req);
//-------------------------------------------
// ...read the results of the verification...
// If VERIFIED = continue to process the TX...
//-------------------------------------------
$res="";
while (!feof($fp))
  $res .= fgets ($fp, 1024);
fclose ($fp);

if (strpos($res, "VERIFIED")===FALSE)
{
  fwrite($log,"ERROR - UnVERIFIIED payment\r\nPayPal response:");
  fwrite($log,$res);
  fclose($log);
  return;
}

fwrite($log,"payment VERIFIIED\r\n");


if ($_POST["payment_status"]!="Completed")
{
  fwrite($log,"ERROR - payment status is not Completed\r\n");
  fclose($log);
  return;
}

//update order status

$conn=db_connect();
$sql = "update shopsales_order_main set status='PAYMENT RECEIVED' where Order_no=".$_POST["item_number"];
db_exec($sql,$conn);
db_close($conn);

fwrite($log,"OK - payment received.\r\n");
fclose($log);

function ipn_error_handler($errno, $errstr, $errfile, $errline)
{
  global $log;
  fwrite($log,"PHP ERROR \r\n".$errstr);
  fclose($log);
  exit(0);
}

?>

You can open an account at developer.paypal.com to test your apps in sandbox.

Paypal also provides Website Payments Pro - more advanced payment processing option which is more like having your own merchant account. Having Website Payments Pro account (from $30 per month) you can accept credit cards on your website passing credit card info to Paypal in background via API.

link|flag

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.