Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    PayPal IPN Integration WordPress

    PayPal IPN Integration WordPress – Instant Payment Notification (IPN)  is a message service that automatically notifies merchants of events related to PayPal transactions.

    Merchants can use it to automate back-office and administrative functions, including automatically fulfilling orders and providing customers with order status.

    We are discussing here for subscription based transactions using PayPal recurring payment. IPN becomes important when new transaction occurred to your account and you don’t know about transaction details that who buy the plan, etc.

    So, IPN is the message which contains all the information regarding transaction.

    To Setup IPN , enable IPN from PayPal site in user profile selling tools section. You have to set notification URL at PayPal site. PayPal will send IPN to that particular action in URL.

    Lets’s start coding…

    First set notification URL, like http://example.com/?action=IPN_Handler

    In functions.php file, write functions to get IPN message,

    <?php
    
    add_action( 'init', 'paypal_ipn' );
    
    ?>
    

    In the above line of code, we describe action for paypal_ipn function with init hook, now define function,

    <?php
    
    function paypal_ipn() {
    		
        global $wp;
    	
        if (isset($_GET['action']) && $_GET['action']=='IPN_Handler') {
    						      	
            if(check_ipn()) {
    		
                ipn_request($IPN_status = true);
            
            } else {
            
                ipn_request($IPN_status = false);
            }
    	
         }
    
    }
    
    ?>

     

    Now, define function check_ipn used in above function,

    <?php
    
    function check_ipn() {
    
         $ipn_response = !empty($_POST) ? $_POST : false;
    
         if ($ipn_response == false) {
            
             return false;
            
         }
    
         if ($ipn_response && check_ipn_valid($ipn_response)) {
    
             header('HTTP/1.1 200 OK');
    
             return true;
         }
    }
    
    ?>

    In above function, we check for the ipn response whether it is valid or not using check_ipn_valid function. If it is valid, we send back OK message to PayPal. Let’s come to the check_ipn_valid function,

     

    <?php
    
    function check_ipn_valid($ipn_response) {
    
         $paypal_adr = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr'; // sandbox mode
         
         // use https://ipnpb.paypal.com/cgi-bin/webscr for live
         
         // Get received values from post data
         
         $validate_ipn = array('cmd' => '_notify-validate');
      
         $validate_ipn += stripslashes_deep($ipn_response);
    
         // Send back post vars to paypal
    
         $params = array(
             'body' => $validate_ipn,
             'sslverify' => false,
             'timeout' => 60,
             'httpversion' => '1.1',
             'compress' => false,
             'decompress' => false,
             'user-agent' => 'paypal-ipn/'
          );
    
          // Post back to get a response
    
          $response = wp_safe_remote_post($paypal_adr, $params);
    
          // check to see if the request was valid
    
          if (!is_wp_error($response) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && strstr($response['body'], 'VERIFIED')) {
    
              return true;
    
          }
    
          return false;
    
    }
    
    ?>

     

    In above function, we post back the data recieved with ‘cmd’=_notify_validate appended in the response to validate the response.

    Now, if everything goes fine then we get ipn status true for ipn_request function otherwise false. So. lets’s come to the ipn_request function,

     

    <?php
    
    function ipn_request($IPN_status) {
    		
         $ipn_response = !empty($_POST) ? $_POST : false;
    
         $ipn_response['IPN_status'] = ( $IPN_status == true ) ? 'Verified' : 'Invalid';
    
         $posted = stripslashes_deep($ipn_response);
    
    }
    
    ?>

    Now, you can do what you want with data in $posted, like save into database or mail it to concerned person. To know about the data contained in $posted, you can check here.

    Support

    Still have any issue feel free to add a ticket and let us know your views to make the code better https://webkul.uvdesk.com/en/customer/create-ticket/

    Thanks for Your Time! Have a Good Day!

     

    . . .

    Comments (5)

    Add Your Comment

  • Andrew McCombe
    Thanks. This information is hard to find on the internet.
    • Amit Chauhan
      Yes indeed 🙂
  • Ashu Sachdev
    add_action( ‘init’, ‘paypal_ipn’ ); and Function name are different how is call
    • Varun Kumar
      Hi Ashu, thanks for your suggestion. Now we have corrected the issue and it will work fine now. Thanks.
  • Ashu Sachdev
    add_action( ‘init’, ‘paypal_ipn’ ); and Function name are different how is call
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close

    Table of Content

    Hide Index