Solution: Patch Nice Memberships' IPN Code for Future PayPal Compatibility

Tech


Joomla!

Joomla is one of the world’ s most popular open source CMS (content management system).

Version: 1.6-2.5
Developer: Open Source Matters, Inc.
Linkhttp://www.joomla.org

 

Nice Memberships

The Nice Memberships extension manages and enrolls subscribed users into specific Joomla user groups. 

Version: 1.10 or less
Developer: TriniTronic
Linkhttp://trinitronic.com/joomla/nice-memberships-extension

 

Background


PayPal has upgraded their IPN service to HTTP 1.1. This change will require all site's that use PayPal's IPN services to comply with the new change by February 1, 2013. Nice Memberships extension for Joomla uses PayPal's IPN service and will be effected by PayPal's change.

According to PayPal, "Starting February 1, 2013, we will require all incoming requests to have a “Host” header which complies with HTTP 1.1 specifications. This header was not required under HTTP 1.0. IPN and PDT scripts using HTTP 1.0 may start failing with “HTTP/1.1 400 Bad Request” errors after February 1, 2013, which will result in IPN messages not being validated successfully, or PDT scripts not being able to retrieve transaction information."

If you are using Nice Memberships 1.11, no need to worry. Otherwise you need to upgrade to the latest version of the extension, follow the steps below to patch your version of the extension.

 

Solution


IMPORTANT: Before you proceed make a full back up of your web site files and database.

 

1. FTP into your Joomla website

2. Go to components/com_nicememberships/controllers/

3. Download the file named Ipn.php

4. Make a local copy of the file, name it Ipn-backup.php (the file name is case-sensitive, so keep that "I" capitalized).

5. Open the Ipn.php file in a simple text editor (like MS Notepad, Notepad++, Sublime Text etc).

6. Find the block of code that looks like the following, around line 51.

{codecitation class="brush:php;first-line:51;" width="550px" }// post back to PayPal system to validate

$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";

$header .= "Content-Type: application/x-www-form-urlencoded\r\n";

$header .= "Content-Length: " . strlen($req) . "\r\n";

 

//live paypal account or sandbox account

if ($this->_test_ipn == 0){

      $header .= "Host: www.paypal.com\r\n\r\n";

$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

}else {

      $header .= "Host: www.sandbox.paypal.com\r\n\r\n";

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

}

 

if (!$fp) {

// HTTP ERROR

}else {

    

fputs ($fp, $header . $req);

      

while (!feof($fp)) {

      

        $res = fgets ($fp, 1024);

        

        if (strcmp ($res, "VERIFIED") == 0) {

{/codecitation}

 

7. Alter the block of code to look like the one below.

{codecitation class="brush:php;first-line:51;" width="550px" }// post back to PayPal system to validate

$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";

$header .= "Content-Type: application/x-www-form-urlencoded\r\n";

$header .= "Content-Length: " . strlen($req) . "\r\n";

    

//live paypal account or sandbox account

if ($this->_test_ipn == 0){

      $header .= "Host: www.paypal.com\r\n";

      $header .= "Connection: close\r\n\r\n";

$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

}else {

      $header .= "Host: www.sandbox.paypal.com\r\n";

      $header .= "Connection: close\r\n\r\n";

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

}

    

if (!$fp) {

// HTTP ERROR

}else {

  fputs ($fp, $header . $req);

  while (!feof($fp)) {

    $res = fgets ($fp, 1024);

    if (strpos($res, "VERIFIED") !== false) {

{/codecitation}

 

8. Save your changes.

9. Upload the altered Ipn.php file back to its original location.

10. Test your changes by making a purchase through PayPal.

 

Remember, if something goes wrong during the alteration, you can simply rename Ipn-backup.php and use it to roll-back any changes you made. After which, you can re-attempt the alteration.

 

~ Enjoy success!