Flipkart

Tuesday, December 22, 2009

Paypal sandbox form elements

// First create 3 files in root of site called
// paypal_return.php,cancel_return.php,notify_return.php
// Replace business@domain.com with your test business account email id
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" name="frm" id="frm" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="business@domain.com">
<input type="hidden" name="item_name" value="Deposite Balance">
<input type="hidden" name="amount" value="100">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="charset" value="utf-8">
<input type="hidden" name="rm" value="2" >
<input type="hidden" name="return" id="return" value="http://yoursite/paypal_return.php" />
<input type="hidden" name="cancel_return" value="http://yoursite/cancel_return.php">
<input type="hidden" name="notify_url" value="http://yoursite/notify_return.php">
<input type="hidden" name="first_name" value="Vasim" />
<input type="hidden" name="last_name" value="Padhiyar" />
<input type="submit" value="Paypal" name="btn" />
</form>

// After Payment Complete , Paypal will return you Token in request
// on paypal_return.php
//Check This Token Transation Record on paypal server as below

$order_status = check_paypal($_REQUEST['tx']);

if($order_status=='SUCCESS')
{
// Update Database and return to main page
}
else
{
// Error msg
}


// Replace $auth_token with your appropriate token
function check_paypal($paypal_tx)
{
$status = "FAIL";

$req = 'cmd=_notify-synch';

$tx_token = $paypal_tx;

$auth_token = "03suYLK9KVmKHZMETOKW4Ad0g2uDM7R2mS0-IUd4B2tFr6E0SlDZRUVSX-G";

$req .= "&tx=$tx_token&at=$auth_token";

$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";
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);

if (!$fp)
{
$status = "FAIL";
}
else
{
fputs ($fp, $header . $req);

$res = '';
$headerdone = false;
while (!feof($fp))
{
$line = fgets ($fp, 1024);

if (strcmp($line, "\r\n") == 0)
{
$headerdone = true;
}
else if ($headerdone)
{
$res .= $line;
}
}

$lines = explode("\n", $res);

$keyarray = array();

if (strcmp ($lines[0], "SUCCESS") == 0)
{
$status = "SUCCESS";
}
else if (strcmp ($lines[0], "FAIL") == 0)
{
$status = "FAIL";

}

}

fclose ($fp);

return $status;
}