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/
Related
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?
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;
I have been stuck for days! I am trying to get the details from PayPal IPN from a parallel payment.
I need the two email addresses that money was sent to and the amount, also the status.
I am looking here:
https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_APIPN
These are the lines in my code where I try and get email and amount:
$Email1 = $_POST['transaction[0].receiver'];
$Email2 = $_POST['transaction[1].receiver'];
And amount part:
$Amount1 = $_POST['transaction[0].amount'];
$Amount2 = $_POST['transaction[1].amount'];
I am use the IPN code from this page.
https://cms.paypal.com/cms_content/AU/en_AU/files/developer/IPN_PHP_41.txt
Any help? The POST variables come up blank?
Update: I Have found this:
<source lang="php">
<?php
error_reporting(E_ALL ^ E_NOTICE);
// By Gleb Esman, gleb#memberwing.com, http://www.memberwing.com/
//
// Pull raw POST data.
// We need to pull raw data and build our own copy of $_POST in order to
// workaround of invalid POST keys that Adaptive IPN request uses.
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$_YOUR_POST = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode('=', $keyval);
if (count($keyval) == 2)
$_YOUR_POST[$keyval[0]] = urldecode($keyval[1]);
}
if (count($_YOUR_POST) < 3) {
$_YOUR_POST = $_POST;
$original_post_used = TRUE;
}
else
$original_post_used = FALSE;
// Build final $_req postback request
// Paypal's IPN Sample
// read the post from PayPal system and add 'cmd'
if ($original_post_used) {
$_req = 'cmd=_notify-validate';
foreach ($_YOUR_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$_req .= "&$key=$value";
}
}
else
$_req = $raw_post_data . '&cmd=_notify-validate';
// $_req is ready for postback to Paypal here...
$req = $_req;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// 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";
//}
// 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);
// throw all this junk into the local error log so we can see what happened!
function log_arr($item, $key) {
$p .= "$key = $item";
error_log(urldecode($p));
}
array_walk_recursive($_YOUR_POST, 'log_arr');
error_log(urldecode($req));
// 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);
}
?>
</source>
which produces this in log file:
[10-Dec-2011 07:13:50] transaction[0].id_for_sender_txn = xxxxx
[10-Dec-2011 07:13:50] log_default_shipping_address_in_transaction = xxx
[10-Dec-2011 07:13:50] transaction[0].receiver = xxxxx
[10-Dec-2011 07:13:50] action_type = xxxx
[10-Dec-2011 07:13:50] transaction[1].paymentType = xxx
[10-Dec-2011 07:13:50] transaction[0].amount = xxxx
[10-Dec-2011 07:13:50] charset = xxx
Can anyone tell me how to get these into variables as this is what is printed in log file?
Try $_POST['transaction']['0']['receiver'] and so on. Also, enable E_ALL error messages so you see notices about things like this. If that were enabled, and you checked your error log, you would have seen an undefined index notice. Also, if you're ever desperate to debug $_POST/$_GET/whatever stuff and it's not you controlling the request, you can always write the content to a file and examine it.
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
I'm trying to figure out how to use PayPal's IPN and I've run into a wall.
I want a buyer to be forwarded to a success page after making a purchase, and I want that page to show the details of their transaction. I choose IPN instead of the PDT because I also want to do some other behind the scenes stuff with their data.
Anyway, here's the code I'm using -- I'm testing in sandbox mode -- but it returns "FAIL" every time.
$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.sandbox.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) {
// PAYMENT VALIDATED & VERIFIED!
echo "Validated!";
}
else if (strcmp ($res, "INVALID") == 0) {
// PAYMENT INVALID & INVESTIGATE MANUALY!
echo "Invalid!";
}
}
fclose ($fp);
}
i got the same problem in the test enviroment because my item_name has especial character,
then i change item_name to only english word and number. it works fine.
but in real enviroment i still find this problem,i read the https://www.x.com/docs/DOC-1551, but i still don't know why
I didn't realize that the sandbox account I set up was Unverified. I re-made the pre-configured sandbox account and then it started to work perfectly.