php - PayPal IPN Listener failing on postback -
first time here , stumped after days of trying resolve problem. here's background problem...
i have paypal ipn listener script. in fact have 2 versions of because first didn't work tried didn't work either. both of ipn listener scripts obtained paypal...
paypal_notifya.php
from... developer . paypal . com /docs/classic/ipn/gs_ipn/
the script stops at...
fputs($fp, $header . $req);
<?php require_once("../lib/phpmailer/phpmailerautoload.php"); ?> <?php // paypal ipn // send empty http 200 ok response acknowledge receipt of notification header('http/1.1 200 ok'); // assign payment notification values local variables $invoiceid = $_post['invoice']; $paymentstatus = $_post['payment_status']; $productid = $_post['item_number']; $paymentamount = $_post['mc_gross']; $paymentcurrency = $_post['mc_currency']; $txnid = $_post['txn_id']; $receiveremail = $_post['receiver_email']; $payeremail = $_post['payer_email']; $response = $_post['response']; // build required acknowledgement message out of notification received $req = 'cmd=_notify-validate'; // add 'cmd=_notify-validate' beginning of acknowledgement foreach ($_post $key => $value) { // loop through notification nv pairs $value = urlencode(stripslashes($value)); // encode these values $req .= "&$key=$value"; // add nv pairs acknowledgement } // set acknowledgement request headers $header = "post /cgi-bin/webscr http/1.1\r\n"; // http post request $header .= "content-type: application/x-www-form-urlencoded\r\n"; $header .= "content-length: " . strlen($req) . "\r\n\r\n"; // paypal ipn simulator fails on line below... // open socket acknowledgement request $fp = fsockopen('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // paypal.com (live) - sandbox.paypal.com (test) // send http post request paypal validation fputs($fp, $header . $req); while (!feof($fp)) { // while not eof $res = fgets($fp, 1024); // acknowledgement response if (strcmp ($res, "verified") == 0) { // response contains verified - process notification $teststage = "got past verified!"; require_once("../emails/ipn_test.php"); // own verified stuff here // ***** invalid response ***** } else if (strcmp ($res, "invalid") == 0) { // response contains invalid - reject notification // own invalid stuff here } } fclose($fp); // close file ?>
paypal_notifyb.php
from... github . com /paypal/ipn-code-samples/blob/master/paypal_ipn.php
the script stops at...
$res = curl_exec($ch);
and reports following in error log...
can't connect paypal validate ipn message: error:14077410:ssl routines:ssl23_get_server_hello:sslv3 alert handshake failure
<?php require_once("../lib/phpmailer/phpmailerautoload.php"); ?> <?php // paypal notify // start session session_start(); // config: enable debug mode. means we'll log requests 'ipn.log' in same directory. // useful if encounter network errors or other intermittent problems ipn (validation). // set 0 once go live or don't require logging. define("debug", 1); // set 0 once you're ready go live define("use_sandbox", 1); define("log_file", "./ipn.log"); // read post data // reading posted data directly $_post causes serialization // issues array data in post. reading raw post data input stream instead. $raw_post_data = file_get_contents('php://input'); $raw_post_array = explode('&', $raw_post_data); $mypost = array(); foreach ($raw_post_array $keyval) { $keyval = explode ('=', $keyval); if (count($keyval) == 2) $mypost[$keyval[0]] = urldecode($keyval[1]); } // read post paypal system , add 'cmd' $req = 'cmd=_notify-validate'; if(function_exists('get_magic_quotes_gpc')) { $get_magic_quotes_exists = true; } foreach ($mypost $key => $value) { if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { $value = urlencode(stripslashes($value)); } else { $value = urlencode($value); } $req .= "&$key=$value"; } // post ipn data paypal validate ipn data genuine // without step can fake ipn data if(use_sandbox == true) { $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; $livetestemail = "info-facilitator@flysoftware.com"; } else { $paypal_url = "https://www.paypal.com/cgi-bin/webscr"; $livetestemail = "info@flysoftware.com"; } $ch = curl_init($paypal_url); if ($ch == false) { return false; } curl_setopt($ch, curlopt_http_version, curl_http_version_1_1); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_returntransfer,1); curl_setopt($ch, curlopt_postfields, $req); curl_setopt($ch, curlopt_ssl_verifypeer, 1); curl_setopt($ch, curlopt_ssl_verifyhost, 2); curl_setopt($ch, curlopt_forbid_reuse, 1); if(debug == true) { curl_setopt($ch, curlopt_header, 1); curl_setopt($ch, curlinfo_header_out, 1); } // set tcp timeout 30 seconds curl_setopt($ch, curlopt_connecttimeout, 30); curl_setopt($ch, curlopt_httpheader, array('connection: close')); // config: please download 'cacert.pem' "http://curl.haxx.se/docs/caextract.html" , set directory path // of certificate shown below. ensure file readable webserver. // mandatory environments. $cert = __dir__ . "./cacert.pem"; curl_setopt($ch, curlopt_cainfo, $cert); // paypal ipn simulator fails on line below , reports following error... // can't connect paypal validate ipn message: error:14077410:ssl routines:ssl23_get_server_hello:sslv3 alert handshake failure $res = curl_exec($ch); if (curl_errno($ch) != 0) { // curl error if(debug == true) { error_log(date('[y-m-d h:i e] '). "can't connect paypal validate ipn message: " . curl_error($ch) . php_eol, 3, log_file); } curl_close($ch); exit; } else { // log entire http response if debug switched on. if(debug == true) { error_log(date('[y-m-d h:i e] '). "http request of validation request:". curl_getinfo($ch, curlinfo_header_out) ." ipn payload: $req" . php_eol, 3, log_file); error_log(date('[y-m-d h:i e] '). "http response of validation request: $res" . php_eol, 3, log_file); } curl_close($ch); } // inspect ipn validation result , act accordingly // split response headers , payload, better way strcmp $tokens = explode("\r\n\r\n", trim($res)); $res = trim(end($tokens)); if (strcmp ($res, "verified") == 0) { // response verified $teststage = "got past verified!"; require_once("../emails/ipn_test.php"); // own verified stuff here // ***** invalid response ***** } else if (strcmp ($res, "invalid") == 0) { // response contains invalid - reject notification // own invalid stuff here if(debug == true) { error_log(date('[y-m-d h:i e] '). "invalid ipn: $req" . php_eol, 3, log_file); } } ?>
when try both scripts in paypal ipn simulator, both report...
ipn sent , handshake verified.
however, both scripts don't communicate paypal , "if verified" condition never reached. therefore irrelevant there not yet backend code after condition - trying listener work now!
i have talked web host , stated disabled sslv3 in hope causing problem. made no difference , both scripts continue fail @ point communicate paypal. host claims not "their" fault , problem paypal. despite pointing host in direction of relevant threads (that can't post because not have reputation).
stackoverflow . com / questions/35030756/paypal-ipn-openssl-error14077410ssl-routinesssl23-get-server-hellosslv3-aler
stackoverflow . com / questions/34955346/paypal-ipn-handshake-issues
and page paypal...
paypal-knowledge . com / infocenter/index?page=content&id=faq1766&expand=true&locale=en_us
i hope can me resolve or @ least confirm "my" scripts @ fault or server @ fault, because i'm getting nowhere!!
p.s. i'm new php.
faced same problem here using java servlet catch ipn notification. after receive lots of parameters, response status 200 ok sent back. then, new http request formed keeping parameters received @ first step , adding command 'cmd' -- things quite similar php code.
as see, fsockopen tries connect 'tls://www.sandbox.paypal.com', whereas using 'https://www.sandbox.paypal.com/cgi-bin/webscr'. may follow tutorial quite helpful http://www.geekality.net/2011/05/28/php-tutorial-paypal-instant-payment-notification-ipn
verified received finally. 1 thing note that, due https, there certificate expire warning had disable ignore cookies in apache http client.
hope post eases of pain.
Comments
Post a Comment