4

I am integrating paypal with IPN, (i have done this on many websites and never had this issue before.) I am using PHP Paypal IPN Integration Class - Micah Carrick.

I am testing it on sandbox account. All the data i am receiving back from paypal is fine. But always IPN verifications fails.

I have used same class on many projects but never got this issue, please guide me, what can be the reason for this issue.

This is what i am getting in log file:

[10/29/2012 5:02 AM] - FAIL: IPN Validation Failed.
IPN POST Vars from Paypal:
mc_gross=10.00, protection_eligibility=Eligible, address_status=confirmed, payer_id=LZ3J5RXT7ZRSW, tax=0.00, address_street=1 Main St, payment_date=03:02:41 Oct 29, 2012 PDT, payment_status=Completed, charset=windows-1252, address_zip=95131, first_name=AProfessionals, mc_fee=0.59, address_country_code=US, address_name=AProfessionals Inc, notify_version=3.7, custom=, payer_status=verified, business=brian_1351496665_biz@a1professionals.com, address_country=United States, address_city=San Jose, quantity=1, verify_sign=AV0bkFkV43dlmXuqlWjyHTfWE.SBANTBgLiHNABcsVQsMvyhdLQg8mTi, payer_email=harry_1351496900_per@a1professionals.com, txn_id=9ES74523RB953760X, payment_type=instant, last_name=Inc, address_state=CA, receiver_email=brian_1351496665_biz@a1professionals.com, payment_fee=0.59, receiver_id=NEV59MNUMBET6, txn_type=web_accept, item_name=Paypal Test Transaction, mc_currency=USD, item_number=, residence_country=US, test_ipn=1, handling_amount=0.00, transaction_subject=Paypal Test Transaction, payment_gross=10.00, shipping=0.00, ipn_track_id=74d5b2446aded,

IPN Response from Paypal Server:
HTTP/1.0 302 Found
Location: https://www.sandbox.paypal.com
Server: BigIP
Connection: close
Content-Length: 0
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ankush Kalia
  • 122
  • 4
  • 15
  • I tried IPN Simulator to check the problem and i got this error in my log file: [29-Oct-2012 11:24:51 UTC] cURL error: [77] Problem with the SSL CA cert (path? access rights?) – Ankush Kalia Oct 29 '12 at 11:27
  • 1
    Are you using a "Host" header in your request? Have you seen this notice from PayPal? https://www.x.com/developers/community/blogs/pp_mts_robertg/action-required-update-your-ipn-pdt-scripts – dbellizzi Nov 04 '12 at 23:42
  • Just faced same issue today. Solved as this post: http://stackoverflow.com/questions/11746644/paypal-sandbox-ipn-always-returns-invalid/37630831#37630831 – Naguissa Jul 19 '16 at 08:19

2 Answers2

3

From my experience these are 3 common reasons why IPN fails validation

1) cmd=_notify-validate is being appended rather then prepended... this can at times cause problems. So instead of:

  $post_string = '';    
  foreach ($_POST as $field=>$value) { 
     $this->ipn_data["$field"] = $value;
     $post_string .= $field.'='.urlencode(stripslashes($value)).'&'; 
  }
  $post_string.="cmd=_notify-validate"; // append ipn command

Follow the code on this page: https://www.x.com/developers/PayPal/documentation-tools/code-sample/216623

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
   $get_magic_quotes_exists = true;
} 
foreach ($myPost as $key => $value) {        
   if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
        $value = urlencode(stripslashes($value)); 
   } else {
        $value = urlencode($value);
   }
   $req .= "&$key=$value";
}

2) Address is multi lined.

// read post data from PayPal and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
    // put line feeds back to CR+LF as that's how PayPal sends them out
    // otherwise multi-line data will be rejected as INVALID
     $value = str_replace("\n", "\r\n", $value);
        $this->ipn_data_arr[$key] = $value;
        $req .= '&'.$key.'='.urlencode(stripslashes($value)); 
    }

you can test this yourself by going to your paypal acc and changing from 1 line to 2 lines and using your acc to transact. 2 lines fail tragically without $value = str_replace("\n", "\r\n", $value); we all wonder why paypal didn't have that line in their sample code...

3) User has non-english characters in their name, address, etc (probably not applicable to you but since we're on the topic)

see: http://blog.tentaclesoftware.com/archive/2010/04/09/87.aspx the main point is:

Log into PayPal
Click the ‘Profile’ link in the menu bar under ‘My Account’
Click the ‘Language Encoding’ link under the ‘Selling Preferences’ column
Click ‘More Options’
Set ‘Encoding’ to ‘UTF-8’
Click ‘Save’

If you noticed there is a common theme in all 3: order of the data, encoding of the data, etc if just one small thing is slightly different it will fail. So if it isn't this 3 reasons think along these lines. Look for and check the fields which its data will likely cause problems... non-standard characters, hidden meta data, etc

PK.
  • 2,471
  • 3
  • 24
  • 30
  • code samples moved to http://paypal.github.io/sdk/sample-apps/ see espacially https://github.com/paypal/ipn-code-samples – staabm Mar 20 '14 at 08:32
2

changed one thing in paypal library

// open the connection to paypal

 $fp = fsockopen(‘ssl://’.$url_parsed[host],“443”,$err_num,$err_str,30);

//$fp = fsockopen($url_parsed[host],“80”,$err_num,$err_str,30); 
Ballu Malav
  • 130
  • 2
  • 13