Can I test paypal api's from localhost
Asked Answered
V

8

23

UPDATE 1:

According to this tutorial on Using PayPal's Instant Payment Notification with PHP, PayPal cannot access locally hosted websites unless certain router ports are opened. Is this because the website is about IPN or is this true for all PayPal APIs?

ORIGINAL QUESTION:

On my laptop, I have a LAMP environment setup, when I use the http://localhost to create website before going live.

For a new project, I need to use the PayPal APIs. Will I be able to use the localhost to test the PayPal APIs if I connect my laptop to the internet? Or will I have to upload my website to a LAMP host elsewhere?

You're probably thinking, what a stupid question, just try it to see if it works. I have tried it and it's not working and I wanted to rule out this question before going onto the next step.

Vanhorn answered 1/4, 2011 at 8:42 Comment(3)
Do you receive an error code from paypal? Which API-set are you using, ExpressCheckout?Vlissingen
I get an apache type message saying that the website (my localhost) is down for maintenance...Vanhorn
You can run a script to fake IPN's locally that will validate on paypal's sandbox. It is the same as running their simulator: #11470136Oxidate
M
6

It should work. I have made a payment integration with paypal last year, and it worked on localhost without problems.

Are you using the paypal sandbox for development? https://developer.paypal.com/

Merriott answered 1/4, 2011 at 8:53 Comment(9)
Yes, I have setup a sandbox account and used the credentials from the sandbox account into the code generated from the paypal adaptive api integration wizard.Vanhorn
Please see UPDATE 1: in the original question.Vanhorn
make sure that paypal can access the site on your localhost. this means that your site must be accessible from other machines.Merriott
@Mihai Frentiu, are you sure this is neccessary?Vanhorn
Yes, because in the paypal sandbox(for example) you must specify an IPN handler URL. Paypal is using this URL to call the php file on your website that handles the IPN call. So you cannot pass into that url localhost/yourwebsite/ipn_handler.php. You will need something like your_computer_public_ip/yourwebsite/ipn_handler.phpMerriott
I copied and pasted the files to my live server, and they started working perfectly. So this means that I cannot use my localhost :(.Vanhorn
Apparently it depends on the API methods you use. I make direct card payments calls (DoDirectPayment method) and it works fine on localhost.Millican
This doesn't work for me on localhost (XAMPP). I'm using express checkout method. However, it did work when I put it on a hosted server such as 1&1.Nicodemus
You might be having a routing problem due to your ISP blocking ports. Try my script solution here #11470136Oxidate
T
21

If you want to debug your IPN code, you'll need to make your server publicly available in some way. This is so PayPal can asynchronously post back to your server at a later time after you've submitted your request. Usually this is pretty quick I think (within 15 seconds) but it could be longer.

One easy way I've found to make a development server available publicly is to use a tunneling solution from this list, like boringproxy. This allows you to continue to develop in your IDE like normal, running your code in debug mode. When PayPal posts back to your endpoint, you can debug it right there in your IDE. These services wrap this up for you, so it's very easy to do without any technical know how.

From my understanding, this is done using a "reverse SSH tunnel" which allows your site to be made public by proxying it through a server that's already publicly available. Note that before you do this, you have to consider that not just PayPal can hit your site once it's made public, but anyone can, so please take that into consideration first.

Also, if you've got your own public facing domain and don't mind playing around in a terminal with SSH, you can supposedly do something like this shell script (copied from this gist)

# Usage: show <local-port> <subdomain>
function show() {
    DOMAIN=".yourdomain.com"
    REMOTE="$2$DOMAIN"
    ssh -tR 1080:127.0.0.1:$1 vps "sudo ssh -Nl \$USER -L $REMOTE:80:127.0.0.1:1080 localhost"
}

To get this to work as above, you'd need to put the following in your ~/.ssh/config file:

Host vps
    HostName <server address>
    User <server username>

If you don't want to (or can't) do this, then the following will work:

SERVERUSER="<server username>"
ssh -l $SERVERUSER -tR 1080:127.0.0.1:$1 <server address> "sudo ssh -Nl \$SERVERUSER -L $REMOTE:80:127.0.0.1:1080 localhost"
Threonine answered 17/7, 2012 at 12:8 Comment(0)
G
8

One simple solution is described in the official developers page of PayPal:

developer.paypal.com - Local IPN Testing

The trick consists on writing a small HTML file with this content:

<form target="_new" method="post" action="https://www.YourDomain.com/Path/YourIPNHandler.php">

<!-- start example variables: -->

<input type="hidden" name="SomePayPalVar" value="SomeValue1"/>
<input type="hidden" name="SomeOtherPPVar" value="SomeValue2"/>
    
<!-- /end example variables -->

<input type="submit"/>
	
</form>

To get the real results you need to copy all of the IPN variables which PayPal sends. These real variables can be found into the PayPal account, under IPN History:

IPNs History

You need to click on the relative Message ID and then copy the "IPN Message" content (it will be something like mc_gross=27.00&invoice=Test-1&protection_eligibility=Ineligible&...) which must be converted into HTML hidden input fields. For example:

<input type="hidden" name="mc_gross" value="27.00"/>
<input type="hidden" name="invoice" value="Test-1"/>
<input type="hidden" name="protection_eligibility" value="Ineligible"/>

....

After setting up all of these variables and changing the action URL, you can open the file with a browser and then submit this form.

Gobi answered 1/4, 2011 at 8:42 Comment(0)
U
8

If http://localhost doesn't validate use http://127.0.0.1

Usufruct answered 6/5, 2012 at 9:14 Comment(4)
Why this is not marked up is beyond me. Guess people prefer useless wild overly complex work arounds over something simple. Thanks, I know I had this going at one point and forgot to try this one.Mallard
Hi Shawn, I haven't tried this yet, but curious. So when we use paypal's sandbox urn for testing PayPal payments, are you saying PayPal sandbox's IPN should be able to reach 127.0.0.1 i.e my local machine? Isn't 127.0.0.1 common Home machine IP?Cincinnatus
In the meantime, I will try this though.Cincinnatus
This should be the correct answer (at least for Express Checkout). Works perfectly.Regardful
M
6

It should work. I have made a payment integration with paypal last year, and it worked on localhost without problems.

Are you using the paypal sandbox for development? https://developer.paypal.com/

Merriott answered 1/4, 2011 at 8:53 Comment(9)
Yes, I have setup a sandbox account and used the credentials from the sandbox account into the code generated from the paypal adaptive api integration wizard.Vanhorn
Please see UPDATE 1: in the original question.Vanhorn
make sure that paypal can access the site on your localhost. this means that your site must be accessible from other machines.Merriott
@Mihai Frentiu, are you sure this is neccessary?Vanhorn
Yes, because in the paypal sandbox(for example) you must specify an IPN handler URL. Paypal is using this URL to call the php file on your website that handles the IPN call. So you cannot pass into that url localhost/yourwebsite/ipn_handler.php. You will need something like your_computer_public_ip/yourwebsite/ipn_handler.phpMerriott
I copied and pasted the files to my live server, and they started working perfectly. So this means that I cannot use my localhost :(.Vanhorn
Apparently it depends on the API methods you use. I make direct card payments calls (DoDirectPayment method) and it works fine on localhost.Millican
This doesn't work for me on localhost (XAMPP). I'm using express checkout method. However, it did work when I put it on a hosted server such as 1&1.Nicodemus
You might be having a routing problem due to your ISP blocking ports. Try my script solution here #11470136Oxidate
V
3

It should work without a problem, however it might get picky if you send in "invalid URLs" for return urls and IPN message urls. Meaning, sending in http://localhost/cancelpaypal.php as cancelURL might tell you that it is an invalid url.

I do however don't think it should.

Having to open up router ports would only be needed for IPN, because the redirect in the normal flow is a regular "Location:" header hence it is your browser that needs to be able to access the site (localhost)

Vlissingen answered 1/4, 2011 at 9:10 Comment(0)
S
0

you need to register your local address so it could be opened from internet. you can use ngrok service for that.

Saldana answered 31/3, 2023 at 15:45 Comment(0)
F
-1

Well, it most works, but also, you can setup a temporary local dns entry. All you have to do is:

  1. open your /etc/hosts
  2. add a new entry: yourwebsite.com 127.0.0.1

So when your browser query for the website will be fetched from your 127.0.0.1, somethings your need to flush dns( /etc/init.d/nscd restart).

and that is it all, but remember to remove the entry when you are ready for production.

Furmark answered 9/1, 2017 at 7:52 Comment(3)
This makes localhost known as yourwebsite.com on YOUR computer, but PayPal still doesn't know where to find yourwebsite.com. In other words: It doesn't solve the problem.Pursuance
for the checkout flow, used in the owner example, Paypal doesn't do anything with the redirectUrl, It will process the request, and return the user to the redirectUrl, nothing else.Furmark
If client flow is used, then you don't need this tmp domain name. Can as well use the ip address.Anthozoan
H
-4

To use IPN your localhost has to be accessed from the web. One solution that definitly works is to use a virtual Machine, install VPN-Server, connect your Clinet via VPN and manage virtual host to redirect to your local IP-adress. That way, if you turn on VPN your server can be accessed from outside and IPN can be sent.

Hahnert answered 7/1, 2012 at 10:9 Comment(1)
There is absolutely no reason at all to use a virtual machine or VPN.Gurias

© 2022 - 2024 — McMap. All rights reserved.