Search code examples
phpvalidationcodeigniterpaypal-ipn

PHP/CodeIgniter PayPal IPN Invalid Validation


I'm having a website build on CodeIgniter 2 and I'm using the CodeIgniter PayPal Lib. I have done everything neccessary and I'm now able to proceed payments. I receive an IPN data an I have it send to my e-mail. I have read the PayPal IPN Guide, but I couldn't find a solution there.

Evetything fine untill here and I'm happy with the result, but I'm concerned, because the PayPal IPN verification fails and I cannot understand where is the problem.

When I send an IPN test from the sandbox test site I receive a valid IPN, but when I make a payment from my website the IPN validation fails.

I'm logging all the data and in the both cases (valid or invalid) the payment is successful and i have a "SUCCESS!" message from PayPal.

Things I have tryed

  1. Change the CSRF protection on/off
  2. Change encoding (utf-8 | windows-1252)
  3. Adding|Removing fields from my PayPal request

Code I'm using

The fields I'm using

$this->paypal_lib->add_field('business', '[email protected]');
$this->paypal_lib->add_field('return', site_url('paypal/success'));
$this->paypal_lib->add_field('cancel_return', site_url('paypal/cancel'));
$this->paypal_lib->add_field('notify_url', site_url('contest/receive_ipn'));
$this->paypal_lib->add_field('item_name', 'Contest Subscribtion Payment (Test)');
$this->paypal_lib->add_field('amount', '30');
$this->paypal_lib->add_field('item_number', Y11-1329469079-12); // Reference number
$this->paypal_lib->add_field('quantity', '1');
$this->paypal_lib->add_field('charset', 'utf8');
$this->paypal_lib->add_field('custom', 1723); //This is an id that I need.  

The post to PayPal for validation

$post_string.="cmd=_notify-validate";

if (isset($_POST)){
    foreach ($_POST as $field=>$value){
         $value = str_replace("\n", "\r\n", $value);
         $value = urlencode(stripslashes($value));
         $post_string .= "&$field=$value";

         $this->ipn_data[$field] = $value; //this is part of the library
    }
}

$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($post_string) . "\r\n\r\n";

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

The code to verify the IPN

fputs ($fp, $header . $post_string);
while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) {
        // Send me e-mail - Verified
    } else if (strcmp ($res, "INVALID") == 0) {
        // Send me e-mail - Invalid
    }
}
fclose($fp);

Also I'm posting the response I receive

This one is the INVALID (then send from my website)

cmd=_notify-validate&mc_gross=30.00&protection_eligibility=Ineligible&address_status=unconfirmed&payer_id=LD9UMUWHD44GY&tax=0.00&address_street=Via+Unit+d%27Italia%2C+5783296&payment_date=02%3A19%3A29+Feb+17%2C+2012+PST&payment_status=Completed&charset=windows-1252&address_zip=80127&first_name=Alexander&mc_fee=1.37&address_country_code=IT&address_name=Alexander+Videnov&notify_version=3.4&custom=1721&payer_status=verified&business=aviden_1321715512_biz%40gmail.com&address_country=Italy&address_city=Napoli&quantity=1&verify_sign=AF1YwvTycK97c-VCwQnfsdzArWAcAqZjskElh-FsmZ0s9HqL9BjFUKVH&payer_email=aviden_1329133801_per%40gmail.com&txn_id=32Y96385XJ0686735&payment_type=instant&last_name=Videnov&address_state=Napoli&receiver_email=aviden_1321715512_biz%40gmail.com&payment_fee=&receiver_id=TQVQ3ASRDBW28&txn_type=web_accept&item_name=Yicca+Contest+Subscribtion+Payment+%28Test%29&mc_currency=EUR&item_number=Y11-1329473936-12&residence_country=IT&test_ipn=1&handling_amount=0.00&transaction_subject=1721&payment_gross=&shipping=0.00&ipn_track_id=35382e1887f00

And this is the VERIFIED (when send from PayPal test site)

cmd=_notify-validate&test_ipn=1&payment_type=echeck&payment_date=02%3A28%3A24+Feb+17%2C+2012+PST&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John+Smith&address_country=United+States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San+Jose&address_street=123%2C+any+street&business=seller%40paypalsandbox.com&receiver_email=seller%40paypalsandbox.com&receiver_id=TESTSELLERID1&residence_country=US&item_name=something&item_number=AK-1234&quantity=1&shipping=3.04&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&txn_type=web_accept&txn_id=242171028&notify_version=2.1&custom=xyz123&invoice=abc1234&charset=windows-1252&verify_sign=An5ns1Kso7MWUdW4ErQKJJJ4qi4-Arge8MWjXZSo7fPSQf3xaqAOjrSH

Two things I have noticed

  1. The order of the fields (the request) is different between IPN request send by my website and the one send from sandbox test site.
  2. There is difference in the notify_version field. From my website (3.4) | From PayPal (2.1)

Did anybody expirienced the same problem with the validation. Is there something that I'm missing, or some way that I can debug more?


Solution

  • Not sure if you're using adaptive payments or not, but I used this library: http://www.binpress.com/app/paypal-adaptive-payments-pro-codeigniter-library/140 and it works awesome and the author is very helpful.

    See my question here: Paypal IPN Issue with parallel payment. I've posted my paypal IPN code in the answer.