Reading list Switch to dark mode

    PayPal IPN Integration WordPress

    Updated 25 August 2017

    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.

    Searching for an experienced
    WordPress Company ?
    Find out More

    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!

     

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    7 comments

  • Shiv
    • Varun Kumar (Moderator)
  • Andrew McCombe
    • Amit Chauhan (Moderator)
  • Ashu Sachdev
    • Varun Kumar
  • Ashu Sachdev
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content