Search code examples
phppaypalpaypal-ipn

Paypal return url not working with php and html form


I have payments set up (using php) so that when a customer returns to the success.php file (after payment process on Paypal using an IPN listener) they are added to the db with a new membership number, which is also generated in the success.php file. The process works fine if the customer pays as a guest, they are returned to the success.php page as they should be. However, if the customer logs in to paypal rather than paying as a guest they are redirected to the Paypal user account page instead of back to the success.php page on my site. This means the payment has been taken but their membership number is not created or added to the db.

Is there a way to force ALL customers back to my success page or should the code to create the new member be added to the ipn listener (ipn.php) file so it wouldn't matter if they didn't come back to the success page?

This is the code I use in the checkout page to set the return url.

<form action="<?php echo $paypalURL; ?>" method="post">
           <!-- Identify your business so that you can collect the payments. -->
           <input type="hidden" name="business" value="<?php echo $paypalID; ?>">

           <!-- Specify a Buy Now button. -->
           <input type="hidden" name="cmd" value="_xclick">

           <!-- Specify details about the item that buyers will purchase. -->
           <input type="hidden" name="item_name" value="<?php echo $item_name; ?>">
           <input type="hidden" name="item_number" value="<?php echo $item_number; ?>">
           <input type="hidden" name="amount" value="<?php echo $price; ?>">
           <input type="hidden" name="currency_code" value="GBP">

           <!-- Specify URLs -->
           <input type='hidden' name='cancel_return' value='http://example.com/payment-cancelled'>
                 <input type='hidden' name='return' value='http://example.com/thanks-for-joining/'>

           <!-- Display the payment button. -->
           <input type="submit" name="submit" class="button" value="Pay Now">
       </form>

I should add that I have only tested this in sandbox mode so far, so if anyone knows if this is a sandbox only issue, please let me know.

UPDATE: Further testing shows that the return url no longer works with a guest check out either. This has only started happening since sandbox payments are going through the new payment pages (screenshot attached).

Has paypal changed the method of requesting a return url?

Paypal payment screen shot


Solution

  • I fought with a few ways of integrating paypal's payments into my site. From what I've read here on stackoverflow, on paypal's site, and all over the web, it is probably best to put all of that backend work into your listener. You could set up something on the front-end to prep your DB for the customer, but the major problem with using the success page for this information is that:

    1) your customers could just enter the URL of your success page if known
    2) A customer can choose not to be redirected after paypal and may not return to your site at all (this is the best reason as I can see it).
    3) Sometimes paypal redirects, but the listener may not have received paypals response of completed, pending, .etc (this is why they wait 10sec before redirection), so you do not want the user to go elsewhere or have been verified prematurely.

    Honestly, placing all the code in your listener is quite simple as well and reduces miscommunication between your success page and paypal. As for testing, I just used the IPN simulator to test my code and it was fine.

    As for the redirect URL, there are a few redudancies in paypal, if you use buttons, the option 3 (I believe) will provide a return URL that overrides the others. I'm not sure about using sandbox, but make sure you have .sandbox.paypal in your code to ensure it works (https://gist.github.com/xcommerce-gists/3440401#file-completelistener-php).

    I hope this helps.