php - IPN message returning INVALID -


i'm trying make website on need facilitate paypal's ipn technology. however, though use sample code implementing ipn listener, cannot seem make work. sample code is:

<?php // 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"; } else {     $paypal_url = "https://www.paypal.com/cgi-bin/webscr"; } $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); } // config: optional proxy configuration //curl_setopt($ch, curlopt_proxy, $proxy); //curl_setopt($ch, curlopt_httpproxytunnel, 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); $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) {     // check whether payment_status completed     // check txn_id has not been processed     // check receiver_email paypal email     // check payment_amount/payment_currency correct     // process payment , mark item paid.     // assign posted variables 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'];      echo "working";        if(debug == true) {         error_log(date('[y-m-d h:i e] '). "verified ipn: $req ". php_eol, 3, log_file);     } } else if (strcmp ($res, "invalid") == 0) {      echo "not working";     // log manual investigation     // add business logic here deals invalid ipn messages     if(debug == true) {         error_log(date('[y-m-d h:i e] '). "invalid ipn: $req" . php_eol, 3, log_file);     } } ?> 

i have https on server website. appreciated.

update:

the paypal-form looks this:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top" id="paypal_section">    <!--  indsæt sandbox før paypal ved test  -->                                     <input type="hidden" name="cmd" value="_xclick">                                     <input type="hidden" name="business" value="test_company@hotmail.com">    <!--  Ændre denne til testmail ved test  -->                                     <input type="hidden" name="item_name" value="pixel-blocks;">                                     <input type="hidden" name="button_subtype" value="services">                                     <input type="hidden" name="currency_code" value="usd">                                     <input type="hidden" name="return" value=""> <!-- skal Ændres til successide -->                                     <input type="hidden" name="amount" value="<?php echo $_session["payment"];?>">                                                                       <input type="hidden" name="invoice" value="<?php echo $_session["order_id"];?>">                                     <input type="hidden" name="custom" value="<?php echo $_session["customer_id"];?>">                                     <input type="hidden" name="bn" value="pp-buynowbf:btn_buynowcc_lg.gif:nonhosted">                                     <input type="image" src="https://www.paypal.com/en_us/i/btn/btn_buynowcc_lg.gif" border="0" name="submit" alt="paypal - safer, easier way pay online!">                                     <img alt="" border="0" src="https://www.paypal.com/en_us/i/scr/pixel.gif" width="1" height="1">                                 </form> 

update log:

invalid [2016-04-19 21:50 europe/copenhagen] invalid ipn: cmd=_notify-validate [2016-04-20 08:26 europe/copenhagen] http request of validation request:post /cgi-bin/webscr http/1.1 host: www.sandbox.paypal.com accept: */* connection: close content-length: 991 content-type: application/x-www-form-urlencoded  

double check account configured or not sandbox. have set ipn url?


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -