Setting PayPal return URL to localhost

16,407

Solution 1

What if you try to specify your IP address instead of localhost?

Local host cannot be resolved on distant machines. It's only your local DNS which knows localhost statement and is 127.0.0.1.

http://your-IP-address:8888/paypal/success.php

Solution 2

You can create some URL alias for your website and add it to your hosts file (http://en.wikipedia.org/wiki/Hosts_(file)). Then pass that URL as your return URL to paypal. They just trigger a re-direct within the browser to that location at which point your host file will resolve it to your local development server.

For Example: Add a line to your hosts file like 127.0.0.1 local.mywebdomain.com

Then in your PayPal button, pass that same URL for the return parameter (e.g. <input type="hidden" name="return" value="http://local.mywebdomain.com/success.php">)

Solution 3

It is worth noting, that when working with the Facebook authorization API's (and probably others if not all) you can put https://localhost:xxxx/my-redirect in as a redirect URL and it will work.

Thus, the answers here that basically say "duh..... paypal can't reach localhost on your PC" are a little harsh and off base.

A remote API doesn't need to know how localhost resolves on their end, but in the context of a client side redirect they do not care and presume that the client resolves localhost to where the developer intends.

That said, I will be honest.... the first time I worked with Facebook API's I was a little puzzled as to how localhost can work. But it was in the process of that that I developed a fuller understanding of how these types of redirects happen and that they are client side.

And that makes a lot of sense because in the far majority of cases, an API that provides authentication, or in this case payment, is going to redirect the client to a url with some type of token that proves their indentity/payment.

Yes, paypal also has webhooks, but that is a different beast. Webhooks are when the 3rd party service you are working with will have its back end send a POST to your back end, giving you the official result of some transaction.

When I develop things of this nature, I put more weight on these server-to-server communications because I can be sure that if paypal's backend is telling me something happened, then it really did in fact happen and isn't possibly a client side hack.

No one working with webhooks is going to try to put 'localhost'in as a webhook URL, because clearly that makes no sense and couldn't work for server-to-server communication.

But when a service redirects a web browser client following a transaction, some developers will assume that localhost can work since it should be a client side redirect and isn't a webhook.

When a developer that is new to any given API is using 'localhost' in what seems to be a client side redirect to land the user back on the merchant's site, it isn't a "stupid" question at all.

Share:
16,407
Techie
Author by

Techie

Remembering that I'll be dead soon, is the most important tool I've ever encountered to help me make the big choices in Life. Because almost everything - all external expectations, all pride, all fear of embarrassment or failure, these things just fall away in the face of death, leaving only what is truly important. Remembering that you are going to die, is the best way I know to avoid the trap of thinking you have something to lose. You are already naked, there is no reason not to follow your heart. -- Steve Jobs

Updated on June 05, 2022

Comments

  • Techie
    Techie almost 2 years

    I'm trying to integrate Paypal and I'm using sandbox in the process. I follow the step of the accepted answer in the below question. Setting PayPal return URL and making it auto return?

    But when I try to enter the URL, Paypal return the below error.

    We were unable to validate the URL you have entered. Please check your entry and try again.
    

    URL I'm trying to set is http://localhost:8888/paypal/success.php.

    Also I tried sending the return url with the form as below.

    <input type="hidden" name="return" value="http://localhost:8888/paypal/success.php">
    

    Both methods does not work for me.

    Full Form

    <form method="post" action="https://www.sandbox.paypal.com/cgi-bin/webscr">
    <!-- Identify your business so that you can collect the payments. -->
    <input type="hidden" value="[email protected]" name="business">
    <!-- Specify a Buy Now button. -->
    <input type="hidden" value="_xclick" name="cmd">
    <!-- Specify details about the item that buyers will purchase. -->
    <input type="hidden" value="AM Test Item" name="item_name">
    <input type="hidden" value="22.16" name="amount">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" value="item_number" name="item_number">
    <input type="hidden" name="return" value="http://localhost:8888/paypal/success.php">
    <!-- Display the payment button. -->
    <input type="image" name="submit" border="0" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">
    <img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
    </form> 
    

    How can I test this on my development pc ?

  • Techie
    Techie over 11 years
    I had the same idea at first but the above answer helped me out. Thanks anyway
  • Ryan Szrama
    Ryan Szrama over 11 years
    Also, this is incorrect. I do all of my testing locally, and PayPal has no problem whatsoever using a localhost address for the return URL. It's only when testing IPNs that you need to be on a public web server.
  • YodasMyDad
    YodasMyDad over 10 years
    The question is 'Auto Return' and this DOES NOT work when using localhost. You can however set the return url as localhost, and when during the paypal steps you click return to (Whatever name). It will return to the localhost url, however this is a problem when actually trying to debug and test auto return!
  • mjn42
    mjn42 about 5 years
    A developer machine usually is not reachable from external systems. Providing the local IP address does not work unless it is in the same network as PayPal.
  • Mangesh Sathe
    Mangesh Sathe about 5 years
    Did anyone succeeded in this?
  • BoomShaka
    BoomShaka about 3 years
    The ReturnURL is a client side redirect, the client browser is sent to localhost, paypal servers themselves don't access it. So in theory localhost should be fine. Although paypal IS refusing to accept it SetExpressCheckout is giving me a wierd error about shipping nonsense that isn't returned if i use a domain.
  • Honk der Hase
    Honk der Hase over 2 years
    That is actually a pretty precise summary of the difference between redirect and callback ;)