Tom
Tom

Reputation: 3847

Paypal multiple IPN setup

I am not 100% clear about this when using IPN for let's say 3 websites, and if someone with the knowledge could explain this to me according to my scenario, I will appreciate it.

I have setup my sanbox test business account to use the IPN listener : site1.com/listener.php (Working just fine).

I am wondering about how to setup more listeners for my other sites, using the same paypal account.

In my scenario, I am working with only subscription payments for all sites.

Question 1: The ipn_notification_url variable. Will this variable, if set, tell paypal when the payment is made, to always use this as the listener ipn url? Example; if the subscription fail or something like that when next montly payment is made?

So when paypal have the need to IPN my listener, it will use the listener from the variable and not the set url in account profile settings? Or will this variable ONLY be used for the actual payment when beeing processed?

Question 2: Is it possible to distinct the websites apart, if need to have a master listener, that handles the forwarding to the correct listener url? Example: custom = userId , receiver_id = Sitename

Question 2 is actually similar to the question 1. Will the POSTED variables from the initial payment, STICK to the payments that are going automaticly in the future for my subscriptions. So that when paypal need to send me IPN updates, it will always use the url from my variable set on initial payment??

Thanks for any enlightment on this.

Upvotes: 1

Views: 3674

Answers (2)

jesus g_force Harris
jesus g_force Harris

Reputation: 534

I found this script from http://codeseekah.com/ which will enable you to set-up multiple PayPal IPN listeners. It allows you to filter the notifications which means that you can send to different listeners depending on conditions that you set (so useful!):

<?php

    ini_set( 'max_execution_time', 0 ); // Do not abort with timeouts
    ini_set( 'display_errors', 'Off' ); // Do not display any errors to anyone
    $urls = array(); // The broadcast session queue

    // List of IPN listener points ** ADJUST THESE TO POINT TO YOUR LISTENERS
    $ipns = array(
            'first' => 'http://www.yourwebsite1.co.uk//paypal/ipn.php',
            'second' => 'http://www.yourwebsite2.co.uk//paypal/ipn.php',
            'third' => 'http://www.yourwebsite3.co.uk//paypal/ipn.php'
        );

    // ** ADJUST THESE CONDITIONS TO FILTER 
    if($_POST['txn_type']!='cart') $urls []= $ipns['first']; // Choose this IPN URL if all conditions have been met
    if(isset($_POST['auction_buyer_id'])) $urls []= $ipns['second']; // Choose this IPN URL if all conditions have been met
    $urls []= $ipns['third']; // maybe this one is always sent to

    // Broadcast
    if ( !sizeof($urls) ) $urls = $ipns; // No URLs have been matched
    $urls = array_unique( $urls ); // Unique, just in case

    // Broadcast (excluding IPNs from the list according to filter is possible
    foreach ( $urls as $url ) broadcast( $url );

    header( 'HTTP/1.1 200 OK', true, 200 );
    exit();

    // Perform a simple cURL-powered proxy request to broadcast
    function broadcast( $url ) {

        // Format POST data accordingly
        $data = array();
        foreach ($_POST as $key => $value) $data []= urlencode($key).'='.urlencode($value);
        $data = implode('&', $data);

        // Log the broadcast
        file_put_contents('_logs/'.time().'.'.reverse_lookup( $url ).'-'.rand(1,100), $data);

        $ch = curl_init(); // Initialize

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($data));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_exec($ch); // Execute HTTP request
        curl_close($ch); // Close
    }

    function reverse_lookup( $url ) {
        global $ipns;
        foreach ( $ipns as $tag => $_url ) {
            if ( $url == $_url ) return $tag;
        }
        return 'unknown';
    }
?>

Just customise the parts you need to (with **s), save it in a file, for example called "multiple-paypal-ipn.php", then put it on one of your servers. Then in your PayPal IPN URL setting (in PayPal) put in the full URL where you have just placed it, e.g. http://www.yourwebsite/paypal/multiple-paypal-ipn.php

This saved me BIG time so hopefully will for others!_g

Upvotes: 3

user207421
user207421

Reputation: 311054

There are three URLs you can provide via hidden input elements in the form, with these names:

  1. notify_url: this is the URL to which all notifications for this purchase will be sent.
  2. return: this is the URL to which the user will be returned on successfully completing checkout.
  3. cancel_return: this is the URL to which the user will be returned on cancelling checkout at paypal.

So if you use notify_url in the button, it can be different per site, per button, and even per button render, if you have a use for such a thing.

Note that notify_url overrides whatever you set in Payment Preferences.

Regarding (2), you can also provide an rm variable as follows:

0: User is returned via GET 1: User is returned via GET with no payment variables. 2. User is returned via PUT with all payment variables returned, i.e. as an echo of what the button sent to PayPal.

I'm not clear on the difference between 0 and 1.

See PayPal HTML Variables.

EDIT In another answer at SO which I cannot now find, a PayPal person states that you can pass arbitrary arguments in the notify_url itself, which will come back the same way you sent them.

Upvotes: 3

Related Questions