1
votes

I'm stuck at the problem with paypal IPN and it is getting always invalid when i use paypal buy now button, however it works perfectly with IPN simulator.

I know there are many questions and answers at stackoverflow about this to help but i have tried lots of things from here and also from other sites, none of them worked.

Here is the button code i'm using,

<form name="_xclick" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick"><input type="hidden" name="business" value="[email protected]">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="item_name" value="Teddy Bear">
<input type="hidden" name="amount" value="12.99">
<input type="hidden" name="notify_url" value="http://mysite/ipn">
<input type="hidden" name="custom" value="2">
<input type="hidden" name="return" value="http://mysite/success">
<input type="hidden" name="cancel_return" value="http://mysite/cancel">
<input type="image" src="http://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>

And here is the IPN Listener Code,

<?php
    $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    $postFields = 'cmd=_notify-validate';
    foreach($_POST as $key => $value){
        $value = urlencode(stripslashes($value));
        $postFields .= "&$key=".$value;
    }

    $ch = curl_init();
    curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postFields
    ));
    $result = curl_exec($ch);
    curl_close($ch);
    if($result == "VERIFIED"){
        $handle = fopen('ipn.txt','w');
        fwrite($handle,$result.'----'.$postFields);
        fclose($handle);
    }else{
        $handle = fopen('ipn.txt','w');
        fwrite($handle,$result.'----'.$postFields);
        fclose($handle);
    }

And here is the response i saved into ipn.txt from simulator,

VERIFIED----cmd=_notify-validate&residence_country=US&invoice=abc1234&address_city=San+Jose&first_name=John&payer_id=TESTBUYERID01&shipping=3.04&mc_fee=0.44&txn_id=332239535&receiver_email=seller%40paypalsandbox.com&quantity=1&custom=xyz123&payment_date=17%3A48%3A59+9+Feb+2015+PST&address_country_code=US&address_zip=95131&tax=2.02&item_name=something&address_name=John+Smith&last_name=Smith&receiver_id=seller%40paypalsandbox.com&item_number=AK-1234&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31Aryx.id7DMD.5oDzhJYQOgjfGK2z&address_country=United+States&payment_status=Completed&address_status=confirmed&business=seller%40paypalsandbox.com&payer_email=buyer%40paypalsandbox.com&notify_version=2.1&txn_type=web_accept&test_ipn=1&payer_status=verified&mc_currency=USD&mc_gross=12.34&address_state=CA&mc_gross1=9.34&payment_type=instant&address_street=123%2C+any+street

And finally this response text by pressing button and after making payment through paypal sandbox account.

INVALID----cmd=_notify-validate&mc_gross=12.99&protection_eligibility=Eligible&address_status=unconfirmed&payer_id=K95EGBDGNUN8S&tax=0.00&address_street=Flat+no.+507+Wing+A+Raheja+Residency%0AFilm+City+Road%2C+Goregaon+East&payment_date=17%3A53%3A25+Feb+09%2C+2015+PST&payment_status=Completed&charset=UTF-8&address_zip=400097&first_name=Test&mc_fee=0.81&address_country_code=IN&address_name=Test+Buyer&notify_version=3.8&custom=46&payer_status=verified&business=xxx-facilitator%40gmail.com&address_country=India&address_city=Mumbai&quantity=1&verify_sign=AlLjZyUxzvtq5pK4-AOSkTM98NV-AIiAPCkBL.N4R.UZINnl-aty.OZT&payer_email=xxx-buyer%40gmail.com&txn_id=4LG750650N267953C&payment_type=instant&last_name=Buyer&address_state=Maharashtra&receiver_email=xxx-facilitator%40gmail.com&payment_fee=0.81&receiver_id=HYW9VPL8HWUYA&txn_type=web_accept&item_name=Teddy+Bear&mc_currency=USD&item_number=&residence_country=IN&test_ipn=1&handling_amount=0.00&transaction_subject=46&payment_gross=12.99&shipping=0.00&ipn_track_id=a7a2c1d3e08a2

What I have done so far,

Made sure that i'm only working with sandbox and not with live things with paypal

Changed charset of both accounts (buyer and seller at sandbox)

Changed listener code multiple times, but got same result

searched stackoverflow and other sites with google with no luck.

already have looked these ones,

Paypal SandBox IPN always returns INVALID

PayPal IPN returning INVALID

1

1 Answers

0
votes

I'm pretty sure it was just problem with order of post data.

Used code from https://github.com/orderly/codeigniter-paypal-ipn and it works like charm!

Hope this answer will help someone and will save time.