I am having doubts on how to implement PayPal IPN. I have created a button, and it has the variable notify_url defined:
<input type="hidden" name="notify_url" value="./ipn.php" />
In ipn.php file I have put this code (from Integrate paypal):
<?php
// 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.sandbox.paypal.com', 443, $errno, $errstr, 30);
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is "completed"
// check that txn_id has not been previously processed
// check that receiver_email is your primary paypal email
// check that payment_amount/payment_currency are correct
// process payment
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>
The question is how do I receive the notifications in the user's account? What kind of code do I need additionally?
My code stop working, I have made some changes like $res=trim($res); as I saw in another question but still not working.
The code was perfectly working including automatic post in Facebook and Tweeter.
Please, could someone check my code and help me to fix it?
<?php
// Revision Notes
// 11/04/11 - changed post back url from https://www.paypal.com/cgi-bin/webscr to https://ipnpb.paypal.com/cgi-bin/webscr
// For more info see below:
// https://www.x.com/content/bulletin-ip-address-expansion-paypal-services
// "ACTION REQUIRED: if you are using IPN (Instant Payment Notification) for Order Management and your IPN listener script is behind a firewall that uses ACL (Access Control List) rules which restrict outbound traffic to a limited number of IP addresses, then you may need to do one of the following:
// To continue posting back to https://www.paypal.com to perform IPN validation you will need to update your firewall ACL to allow outbound access to *any* IP address for the servers that host your IPN script
// OR Alternatively, you will need to modify your IPN script to post back IPNs to the newly created URL https://ipnpb.paypal.com using HTTPS (port 443) and update firewall ACL rules to allow outbound access to the ipnpb.paypal.com IP ranges (see end of message)."
/////////////////////////////////////////////////
/////////////Begin Script below./////////////////
/////////////////////////////////////////////////
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.paypal.com\r\n";
//$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
// If testing on Sandbox use:
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
$fp = fsockopen ('ssl://ipnpb.paypal.com', 443, $errno, $errstr, 30);
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$business = $_POST['business'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$mc_gross = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$receiver_id = $_POST['receiver_id'];
$quantity = $_POST['quantity'];
$num_cart_items = $_POST['num_cart_items'];
$payment_date = $_POST['payment_date'];
$first_name = $_POST['fir st_name'];
$last_name = $_POST['last_name'];
$payment_type = $_POST['payment_type'];
$payment_status = $_POST['payment_status'];
$payment_gross = $_POST['payment_gross'];
$payment_fee = $_POST['payment_fee'];
$settle_amount = $_POST['settle_amount'];
$memo = $_POST['memo'];
$payer_email = $_POST['payer_email'];
$txn_type = $_POST['txn_type'];
$payer_status = $_POST['payer_status'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_state = $_POST['address_state'];
$address_zip = $_POST['address_zip'];
$address_country = $_POST['address_country'];
$address_status = $_POST['address_status'];
$item_number = $_POST['item_number'];
$tax = $_POST['tax'];
$option_name1 = $_POST['option_name1'];
$option_selection1 = $_POST['option_selection1'];
$option_name2 = $_POST['option_name2'];
$option_selection2 = $_POST['option_selection2'];
$for_auction = $_POST['for_auction'];
$invoice = $_POST['invoice'];
$custom = $_POST['custom'];
$notify_version = $_POST['notify_version'];
$verify_sign = $_POST['verify_sign'];
$payer_business_name = $_POST['payer_business_name'];
$payer_id =$_POST['payer_id'];
$mc_currency = $_POST['mc_currency'];
$mc_fee = $_POST['mc_fee'];
$exchange_rate = $_POST['exchange_rate'];
$settle_currency = $_POST['settle_currency'];
$parent_txn_id = $_POST['parent_txn_id'];
$pending_reason = $_POST['pending_reason'];
$reason_code = $_POST['reason_code'];
// subscription specific vars
$subscr_id = $_POST['subscr_id'];
$subscr_date = $_POST['subscr_date'];
$subscr_effective = $_POST['subscr_effective'];
$period1 = $_POST['period1'];
$period2 = $_POST['period2'];
$period3 = $_POST['period3'];
$amount1 = $_POST['amount1'];
$amount2 = $_POST['amount2'];
$amount3 = $_POST['amount3'];
$mc_amount1 = $_POST['mc_amount1'];
$mc_amount2 = $_POST['mc_amount2'];
$mc_amount3 = $_POST['mcamount3'];
$recurring = $_POST['recurring'];
$reattempt = $_POST['reattempt'];
$retry_at = $_POST['retry_at'];
$recur_times = $_POST['recur_times'];
$username = $_POST['username'];
$password = $_POST['password'];
//auction specific vars
$for_auction = $_POST['for_auction'];
$auction_closing_date = $_POST['auction_closing_date'];
$auction_multi_item = $_POST['auction_multi_item'];
$auction_buyer_id = $_POST['auction_buyer_id'];
//DB connect creds and email
$notify_email = "xxxxxx#gmail.com"; //email address to which debug emails are sent to
$DB_Server = "localhost"; //your MySQL Server
$DB_Username = "xxxxx"; //your MySQL User Name
$DB_Password = "xxxxx"; //your MySQL Password
$DB_DBName = "xxxxx"; //your MySQL Database Name
if (!$fp)
{
// HTTP ERROR
}
else
{
fputs ($fp, $header . $req);
while (!feof($fp))
{
$res = fgets ($fp, 1024);
$res=trim($res);
if (strcmp ($res, "VERIFIED") == 0)
//if (strcmp ($res, "VERIFIED\r\n") == 0)
{
//create MySQL connection
$Connect = #mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database
$Db = #mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
$fecha = date("m")."/".date("d")."/".date("Y");
$fecha = date("Y").date("m").date("d");
//check if transaction ID has been processed before
$checkquery = "select txnid from paypal_payment_info where txnid='".$txn_id."'";
$sihay = mysql_query($checkquery) or die("Duplicate txn id check query failed:<br>" . mysql_error() . "<br>" . mysql_errno());
$nm = mysql_num_rows($sihay);
if ($nm == 0)
{
//execute query
$strQuery = "insert into paypal_payment_info(paymentstatus,buyer_email,receiver_email,firstname,lastname,street,city,state,zipcode,country,mc_gross,mc_fee,quantity,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecreation) values ('".$payment_status."','".$payer_email."','".$receiver_email."','".$first_name."','".$last_name."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','".$address_country."','".$mc_gross."','".$mc_fee."','".$quantity."','".$memo."','".$payment_type."','".$payment_date."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fecha."')";
$result = mysql_query("insert into paypal_payment_info(paymentstatus,buyer_email,receiver_email,firstname,lastname,street,city,state,zipcode,country,mc_gross,mc_fee,quantity,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecreation) values ('".$payment_status."','".$payer_email."','".$receiver_email."','".$first_name."','".$last_name."','".$address_street."','".$address_city."','".$address_state."','".$address_zip."','".$address_country."','".$mc_gross."','".$mc_fee."','".$quantity."','".$memo."','".$payment_type."','".$payment_date."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fecha."')")
or die("Default - paypal_payment_info, Query failed:<br>" . mysql_error() . "<br>" . mysql_errno());
{
$consumerKey = 'ccYgLrtBUrgIPJDQ';
$consumerSecret = '4qXLLXdQPcKqAqKscYQH9p9m3cznyVWYc4v8';
$oAuthToken = 'uVgkQTC0BLNT9V9vTpc25poxDgIomCgMbw';
$oAuthSecret = 'L8rfc6A1Ii5cMeuYEhf6hwkc3aATyo';
require_once('twitteroauth.php');
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
$tweet->post('statuses/update', array('status' => 'Last donation US$ '.$mc_gross.' to '.$item_name.' '));
}
mail($notify_email, "VERIFIED IPN", "$res\n $req\n $strQuery\n $struery\n $strQuery2");
$sql = mysql_query(" UPDATE cadastros SET amount='$mc_gross'-'$mc_fee'+amount, mc_gross = '$mc_gross' , mc_fee ='$mc_fee', payment_date = '$payment_date',pay_date = now() WHERE receiver_email='$receiver_email'");
$result = mysql_query($sql) or die( mysql_error() );
}
else
{
// send an email
mail($notify_email, "VERIFIED DUPLICATED TRANSACTION", "$res\n $req\n $strQuery\n $struery\n $strQuery2");
}
// if the IPN POST was 'INVALID'...do this
}
else if (strcmp ($res, "INVALID") == 0)
{
// log for manual investigation
mail($notify_email, "INVALID IPN", "$res\n $req");
}
}
fclose ($fp);
}
?>
The HOST tag in the header has to match the URL you are using with fsockopen.
Try this:
$sandbox = 1;
$paypalURL = $sandbox ? 'www.sandbox.paypal.com' : 'www.paypal.com';
// Create http header
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Host: $paypalURL\r\n";
$header .= "Connection: close\r\n\r\n";
// Post back to PayPal system to verify
$fp = fsockopen('ssl://'.$paypalURL, 443, $errno, $errstr, 30);
I know this has been covered quite a lot on here but I've spent most of the day trying to fix this error and don't seem to be getting anywhere.
When I use the PayPals IPN simulator to send a Simulation IPN I keep receiving the response Live-INVALID IPN
Now I thought that this was due to the fact that I'm sending it as a test from sandbox so I tried using the sandbox url as you can see below but if I do this I don't receive a responce at all.
Can someone tell me where I'm going wrong please?
<?php // PHP 4.1
// 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";
}
$email = ****;
// 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);
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
mail($email, "Live-VERIFIED IPN", $res . "\n\n" . $req);
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
mail($email, "Live-INVALID IPN", $res . "\n\n" . $req);
}
}
fclose ($fp);
}
?>
After posting this question I found the following example on paypal developers which uses curl instead, this worked first time for me and I received a response of verified.
Please find the link below for anyone who encounters the same issue:
https://developer.paypal.com/webapps/developer/docs/classic/ipn/ht_ipn/
I am doing Paypal IPN with Zend Framework and I would like to sent the verification url of ipn. How can I submit the back end request in Zend?
For example:
$this->_helper->layout()->disableLayout();
$formData = $this->getRequest()->getParams();
$url="www.sandbox.paypal.com?cmd=_notify-validate&transaction_subject=Zhopenemr Plan Subscription&txn_type=subscr_payment&payment_date=04:02:32 Mar 16, 2012 PDT&subscr_id=I-XFD23RR8DT6G&last_name=T P&residence_country=US&pending_reason=echeck&item_name=Zhopenemr Plan Subscription&payment_gross=4169.90&mc_currency=USD&business=tpprad_1211426184_biz#hotmail.com&payment_type=echeck&protection_eligibility=Ineligible&verify_sign=AGkW.2d.KC8Af-bSXQHFoo4g-LvfAmXI1BIIEMPHZZem9-oQOwopoG4i&payer_status=verified&test_ipn=1&payer_email=tpprad_1211426414_per#hotmail.com&txn_id=98J32260FL8930534&receiver_email=tpprad_1211426184_biz#hotmail.com&first_name=Pradeep&payer_id=LVQGVA8WRESRN&receiver_id=U6AEZTRXA6L4U&payment_status=Pending&mc_gross=4169.90&charset=windows-1252¬ify_version=3.4&ipn_track_id=ff7d0a9a6f2d2";
$request = new zHTTPRequest($url, HTTP_METH_GET);
// $request->setRawPostData($xml);
$request->send();
$response = $request->getResponseBody();
$ipnval="";
foreach($formData as $key=>$value){
$ipnval.= "".$key." = ".$value." <br>";
}
I want to submit the $url to paypal and to verify the status. How can I do this in the back end?
$request = new zHTTPRequest($url, HTTP_METH_GET);
// $request->setRawPostData($xml);
$request->send();
$response = $request->getResponseBody();
This statement can do it with simple php. How can this work with Zend?
Thanks in advance.
// 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 ('www.paypal.com', 80, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
} else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
exit;
My handler and paypal had communicated for a few days ago then something happened which made the latter stopped sending IPN anymore. I tried simulating the IPN sending via my own script without the post back capability for validation using cURL. My handler is perfectly working. I also place a simple line of code(already tested via cURL) before the lines that post back to paypal so I could check paypal's response. That line of code simply records the IPN into the database. It didn't record either. It should right? Because paypal sends IPN twice. I made sure IPN is turned on in my merchant account and the link points to my handler. Now I am beginning to suspect paypal isn't sending me anything. Here's my handler:
<?php
require_once 'classes/Mysql.php';
require_once 'classes/Email.php';
$mysql = new Mysql();
$myemail = new Email();
// 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";
}
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$payment_status = $_POST['txn_type'];
$payment_amount = $_POST['mc_amount3'];
$payment_currency = $_POST['mc_currency'];
$subscr_id = $_POST['subscr_id'];
$receiver_email = urldecode($_POST['receiver_email']);
$payer_email = $_POST['payer_email'];
$subscr_type = $_POST['option_selection1'];
$username = $_POST['custom'];
//Save a copy of $req to the database. If paypal sends IPN, this should at least grab the first one before validation.
$memberID = $mysql->get_member_id($username);
$mysql->test_message($req.$payment_amount.$payment_currency, $memberID, $username, $payment_amount, $payment_currency);
// 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) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
//This if block does the payment processing. If I place this before the validation/post back occurs, it doesn't work either. But it works when testing using my cURL script--- before the validation.
if($memberID)
{
if($payment_status=='subscr_signup' && $payment_currency=='USD' && !$mysql->check_if_transactionID_exists($subscr_id))
{
$mysql->activate_subscription($memberID, $subscr_id, $subscr_type, $payment_amount);
}
}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
I heard so many stories paypal is so frustrating to set. Could it be that paypal is just isn't sending IPN or there's something wrong with my handler?
I've run into a similar issue-- if you dump the var $res from the line $res = fgets ($fp, 1024); you'll see HTTP/1.1 200 OK, not the VERIFIED/INVALID that you're looking for.
Try checking out this answer for more info: Paypal IPN returning HTTP/1.1 200 OK