Paypal Sandbox Test Tool IPN Simulator in Localhost
Asked Answered
O

6

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

Octofoil answered 13/7, 2012 at 11:29 Comment(1)
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. #11470136Diaphanous
O
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).

Oolite answered 13/7, 2012 at 17:29 Comment(3)
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.Oolite
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.Diaphanous
Yes, assuming it's :8002 on a non-localhost host.Oolite
S
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.

Siouan answered 7/6, 2014 at 11:7 Comment(1)
Wow - this is something I was looking for a long time! This is answer!Pentecost
G
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
Guru answered 12/11, 2013 at 22:44 Comment(1)
I also use localtunnel 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.Cloudlet
O
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).

Oolite answered 13/7, 2012 at 17:29 Comment(3)
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.Oolite
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.Diaphanous
Yes, assuming it's :8002 on a non-localhost host.Oolite
D
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] => [email protected]
  [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] => [email protected]
  [verify_sign] => AFcWxV21C7fd0v3bYYYRCpSSRl31AgAAjEU7A5rthY2aP4j1jOIrjuGx
  [address_country] => United States
  [payment_status] => Completed
  [address_status] => confirmed
  [business] => [email protected]
  [payer_email] => [email protected]
  [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);
?>
Diaphanous answered 27/8, 2013 at 22:25 Comment(7)
you need to change txn_id for every test to avoid duplicate transaction re-use error.Lent
@Lent 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.Diaphanous
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.Lent
@Lent 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.Diaphanous
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.Lent
@Lent 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.Diaphanous
add curl_setopt($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=1'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); to xdebug script and use sslFlorina
C
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.

Cameroncameroon answered 13/6, 2013 at 4:15 Comment(0)
A
4
/ngrok http -host-header=yourwebsite.com 80

will tunnel to localhost with the right host header

Airsickness answered 7/4, 2016 at 16:22 Comment(1)
Thank you very much for this very simple code snippet. :)Enthronement

© 2022 - 2024 — McMap. All rights reserved.