PHP

PayPal’s Instant Payment Notification With PHP

Mostly You encounter a lot of problems that PayPal IPN (Notify URL) is not working for sandbox accounts. Here I described about how we can use a sandbox business account to notify IPN service.

Initially we have to create a sadbox testing paypal account using. https://developer.paypal.com/
A personal or business test account can also be created on sandbox.paypal.com.
https://developer.paypal.com/developer/accounts/create.

Next we have to login to created sandbox Paypal Account using sandbox URL
and go for IPN settings to follow URL
https://www.sandbox.paypal.com/cgi-bin/customerprofileweb?cmd=_profile-ipn-notify.

Instant Payment Notification

Now enable IPN service and add yousite URL where you want get Instant Payment Notification, Edit Instant Payment Notification (IPN) settings.

Instant Payment Notification

Now the paypal transaction data would be sent to the notification URL. Now create a pay now button using available scope on developer account on PayPal.

After added button, we need a handler file for get transation information on our database.

Now create a php file to get IPN verification, File name is: get-ipn.php
mysql_connect("localhost", "db_user", "db_password") or die(mysql_error());
mysql_select_db("PayPal") or die(mysql_error());

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
 $value = urlencode(stripslashes($value));
 $req .= "&$key=$value";
}
// post back to PayPal system 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";

$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);


if (!$fp) {
// HANDLE HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

// PAYMENT VALIDATED & VERIFIED!

}

else if (strcmp ($res, "INVALID") == 0) {

// PAYMENT INVALID & INVESTIGATE MANUALY!

}
}
fclose ($fp);
}

 

Thank you for being here, Please share your feedback in below comment section.

Being Idea

Being Idea is a web platform of programming tutorials to make better programming skills and provides Software Development Solutions.

This website uses cookies.