38

How to use Instant Payment Notification Simulator in local machine? What will be provided in IPN handler URL? I don't have access to our router.

Thanks

JR Galia
  • 17,229
  • 19
  • 92
  • 144
  • I know this is old, but you can test sandbox cases on a local host without any tunneling or ngrok. See my answer here and just point your browser to this php file. It will simulate an IPN and sandbox will automatically validate it. https://stackoverflow.com/questions/11469636/paypal-sandbox-test-tool-ipn-simulator-in-localhost/18476477#18476477 – user6972 Dec 10 '20 at 06:36

6 Answers6

51

You can test on localhost using ngrok.

Simply run ngrok locally then paste test url that ngrok gives you (something like http://1bc7d09d.ngrok.com/)

It provides a tunnel to your localhost.

atahan
  • 706
  • 1
  • 10
  • 11
35

Paypal's IPN simulator will not work with localhost. However, you can simulate the simulator :-). To do this you need to install a browser plugin/extension such as poster for firefox or Advanced Rest Client for google chrome.

Open the app and put in your url you are listening on for IPN responses:

http://localhost/ipn

Put the following as your POST data and submit the request:

residence_country=US&invoice=abc1234&address_city=San+Jose&first_name=John&payer_id=TESTBUYERID01&mc_fee=0.44&txn_id=421462822&receiver_email=seller%40paypalsandbox.com&custom=xyz123+CUSTOMHASH&payment_date=12%3A40%3A25+27+Aug+2013+PDT&address_country_code=US&address_zip=95131&item_name1=something&mc_handling=2.06&mc_handling1=1.67&tax=2.02&address_name=John+Smith&last_name=Smith&receiver_id=seller%40paypalsandbox.com&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31AgAAjEU7A5rthY2aP4j1jOIrjuGx&address_country=United+States&payment_status=Completed&address_status=confirmed&business=seller%40paypalsandbox.com&payer_email=buyer%40paypalsandbox.com&notify_version=2.4&txn_type=cart&test_ipn=1&payer_status=unverified&mc_currency=USD&mc_gross=12.34&mc_shipping=3.02&mc_shipping1=1.02&item_number1=AK-1234&address_state=CA&mc_gross1=9.34&payment_type=instant&address_street=123%2C+any+street

UPDATE: I just started using another option that is easier, https://localtunnel.me. To install and run the module do the following. It will show your public URL. All requests to that public URL will be forwarded to your localhost.

npm install -g localtunnel
lt --port 80
Matt
  • 925
  • 2
  • 9
  • 19
  • 4
    I also use [localtunnel](http://localtunnel.me/) and I find it easier than `ngrok` as I don't have to sign up. One tip, if your site is found on a different host (not locahost) use the `-l` option. Ex. `lt -p 80 -l local.dev` hope that helps. – JohnnyQ Sep 14 '15 at 11:57
26

You cannot test IPN on your localhost, as IPN is all about PayPal's server initiating a server-side POST to a URL you define.
As a result, your IPN script must be accessible by the outside world (or you can use a tunnel such as ngrok.me/localtunnel.me).

Robert
  • 19,326
  • 3
  • 58
  • 59
  • 4
    Good addition. Yeah, you could use localtunnel if you're using Rails. Note that PayPal recommends port 80/443 (HTTP/HTTPS), though. The 'IPN validator' won't work on non-HTTP(S) ports. – Robert Dec 21 '12 at 12:08
  • You can also fake the IPN data for testing on your local host. See my script below. You'll need a network connection for the listener to contact the sandbox and php with curl installed. – user6972 Aug 27 '13 at 22:26
  • Yes, assuming it's :8002 on a non-localhost host. – Robert Apr 23 '15 at 06:55
11

You can simulate PayPal's IPN post by running the script below I made on your localhost (you'll need curl installed). As long as your listener is pointed to the sandbox it should validate. Change your listener to the live paypal and it should fail to validate.

<?php
// SIMULATE PAYPAL IPN LOCALLY
//
// Sometimes you need to test on your local host and this can be difficult due
// to IP routing issues.  Use this code on your local machine to simulate the
// same process that the sandbox IPN simulator does when posting to your URL.
//
// Run this code in command line or via the browser.  It will post IPN data just
// like Paypal would.  If the code you've written to process your IPN data
// posts back to the sandbox, it should come back as valid.

// Put the full url to test in $paypal_url, include file extensions if necessary
$paypal_url = 'http://localhost/paypal_ipn/process';   // IPN listener to test

//example posted data from paypal IPN
$test = 'residence_country=US&invoice=abc1234&address_city=San+Jose&first_name=John&payer_id=TESTBUYERID01&mc_fee=0.44&txn_id=421462822&receiver_email=seller%40paypalsandbox.com&custom=xyz123+CUSTOMHASH&payment_date=12%3A40%3A25+27+Aug+2013+PDT&address_country_code=US&address_zip=95131&item_name1=something&mc_handling=2.06&mc_handling1=1.67&tax=2.02&address_name=John+Smith&last_name=Smith&receiver_id=seller%40paypalsandbox.com&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31AgAAjEU7A5rthY2aP4j1jOIrjuGx&address_country=United+States&payment_status=Completed&address_status=confirmed&business=seller%40paypalsandbox.com&payer_email=buyer%40paypalsandbox.com&notify_version=2.4&txn_type=cart&test_ipn=1&payer_status=unverified&mc_currency=USD&mc_gross=12.34&mc_shipping=3.02&mc_shipping1=1.02&item_number1=AK-1234&address_state=CA&mc_gross1=9.34&payment_type=instant&address_street=123%2C+any+street';
/*
 * More detailed breakout of the raw data
  _POST EXAMPLE ARRAY FROM PAYPAL:
  Array
  (
  [residence_country] => US
  [invoice] => abc1234
  [address_city] => San Jose
  [first_name] => John
  [payer_id] => TESTBUYERID01
  [mc_fee] => 0.44
  [txn_id] => 421462822
  [receiver_email] => seller@paypalsandbox.com
  [custom] => xyz123 CUSTOMHASH
  [payment_date] => 12:40:25 27 Aug 2013 PDT
  [address_country_code] => US
  [address_zip] => 95131
  [item_name1] => something
  [mc_handling] => 2.06
  [mc_handling1] => 1.67
  [tax] => 2.02
  [address_name] => John Smith
  [last_name] => Smith
  [receiver_id] => seller@paypalsandbox.com
  [verify_sign] => AFcWxV21C7fd0v3bYYYRCpSSRl31AgAAjEU7A5rthY2aP4j1jOIrjuGx
  [address_country] => United States
  [payment_status] => Completed
  [address_status] => confirmed
  [business] => seller@paypalsandbox.com
  [payer_email] => buyer@paypalsandbox.com
  [notify_version] => 2.4
  [txn_type] => cart
  [test_ipn] => 1
  [payer_status] => unverified
  [mc_currency] => USD
  [mc_gross] => 12.34
  [mc_shipping] => 3.02
  [mc_shipping1] => 1.02
  [item_number1] => AK-1234
  [address_state] => CA
  [mc_gross1] => 9.34
  [payment_type] => instant
  [address_street] => 123, any street
  )
 */

//#
// PayPal IPN processor in PHP
// fake paypal post to test scripts
//#
//----------------------------------------------------------
// Create FAKE post from PayPal.
//----------------------------------------------------------

$req = $test; // use test data

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $paypal_url);
curl_setopt($ch,CURLOPT_POST, substr_count($req,'&')+1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $req);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
?>
user6972
  • 851
  • 1
  • 15
  • 32
  • you need to change txn_id for every test to avoid duplicate transaction re-use error. – khaled_webdev May 26 '14 at 14:19
  • @khaled_webdev does sandbox really prevent duplicate txnid's? That wasn't a problem when I last used this. Your listener can check this and reject. The live paypal system would also detect and reject. – user6972 May 26 '14 at 19:36
  • it's not sandbox but a log generated by orderly bundle symfony2-paypal-ipn and my listener doesn't work second time. First time 'Sucess' and when I try again I got Error. – khaled_webdev May 27 '14 at 08:34
  • @khaled_webdev This method is for testing your listener via sandbox. If your listener blocks repeat txnid's then that's a property of your listener not paypal or the test method. – user6972 May 27 '14 at 16:02
  • when I have used my own ipn (picked from ipn hsitory of my test account) , SUCCESS always. Maybe the type of payment have influence and my listener maybe but I have to thank you for this solution. – khaled_webdev May 27 '14 at 16:22
  • @khaled_webdev it is common and recommended that listeners be designed not to process duplicate txnid's so it wouldn't surprise me that the package you're using blocks it. – user6972 May 27 '14 at 17:20
  • add ```curl_setopt($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=1'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);``` to xdebug script and use ssl – 8ctopus Jun 08 '20 at 05:14
5

I do test on localhost. You can use a service like dyn.com or noip.com or point a subdomain to your local address if it's static.

Brian Keith
  • 318
  • 3
  • 14
4
/ngrok http -host-header=yourwebsite.com 80

will tunnel to localhost with the right host header

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
garyconstable
  • 309
  • 3
  • 15