Solution: Nice PayPal Downloads & Buyer Specified Pricing

Tech


Joomla!

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

Version: 1.5-2.5
Developer: Open Source Matters, Inc.
Link: http://www.joomla.org

 

Nice PayPal Downloads

The Nice PayPal Downloads extension is a robust download manager that allows you to distribute files for free or for paid download. 

Version: 1.92
Developer: TriniTronic
Link: http://trinitronic.com/index.php/Downloads/nice-paypal-downloads.html

 

Background


In this article you're going to be learning how to modify the Nice Paypal Downloads extension to allow buyers to specify their own purchase amounts for any particular download of your choosing. Now, you need to be careful, because the Nice Paypal Downloads extension has security features that would normally not allow for variable pricing. Normally, these features will prohibit would-be scammers from specifying their own amounts and getting downloads for less than the seller wanted to charge. So, in moving forward with this modification, it's important that you maintain the security features for downloads that do not allow for variable pricing.

 

Solution


Before you get started, it is important to understand that many of the files and folders for this extension are named similarly and it's easy to accidentally modify the wrong file. So, pay careful attention to the file names and file paths cited in this tutorial.

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

The first thing that you want to do is establish a flag that the admin can set when they want to sell an item and allow the buyer to set their own pricing.  For the purposes of this demonstration, you'll use an item price of -$1.00 as this flag.  So, when the system sees a price of -$1.00, it will know that the buyer is allowed to set their own price. Using -$1.00 as a flag works well, because there is never going to be an occasion that a seller will need to use a negative price. Plus, the extension already accepts negative numbers. So, there are no code modifications needed.

 

Altering the PayPal Button Generation

Now that you have established a flag you want to modify the button generator to have the correct behavior when it encounters the flag. For this you need to modify the Nice PayPal Downloads Button plugin. Do the following.

 

1. FTP to your site and download the file found here plugins/system/nicepaypaldownloadsbtn/nicepaypaldownloadsbtn.php

2. Make a local copy of this file and name it nicepaypaldownloadsbtn-backup.php

3. Open the file in a simple text editor.

4. Around line 165, you should see a block of code that looks like the following one.

{codecitation class="brush:php;first-line:165;wrap-lines:false;" width="550px" }

$insert = '<form action="'.$this->paypal_url.'/cgi-bin/webscr" method="post">

<input type="hidden" name="cmd" value="_xclick">  

<input type="hidden" name="business" value="'.$this->paypal_email.'">  

<input type="hidden" name="item_name" value="'.$this->_data->item_name.'">  

<input type="hidden" name="item_number" value="'.$this->_data->id.'">

<input type="hidden" name="quantity" value="1">

<input type="hidden" name="amount" value="'.$this->_data->amount.'">

<input type="hidden" name="tax" value="'.$this->_data->tax.'">

<input type="hidden" name="shipping" value="0">

<input type="hidden" name="currency_code" value="'.$this->currency_code.'"> 

<input type="hidden" name="lc" value="'.$this->country_code.'">

<input type="hidden" name="mrb" value="YRDKF6S68Y7DS"/>

<input type="hidden" name="return" value="'.JURI::base().'index.php?option=com_nicepaypaldownloads&amp;nd=r&amp;itm='.$this->_data->id.'">    

<input type="hidden" name="cancel_return" value="'.JURI::base().'index.php?option=com_nicepaypaldownloads&amp;nd=c&amp;itm='.$this->_data->id.'">    

<input type="hidden" name="notify_url" value="'.JURI::base().'index.php?option=com_nicepaypaldownloads&amp;controller=Ipn&amp;task=ipn">  

<input type="image" border="0" name="submit" src="'.$btnimg.'" alt="PayPal - The safer, easier way to pay online">

</form>';

{/codecitation}

 

5. Note the line that sets the amount input for the form, about line 171. This is the line that you need to modify.

{codecitation class="brush:php;first-line:171;wrap-lines:false;" width="550px" }

<input type="hidden" name="amount" value="'.$this->_data->amount.'">

{/codecitation}

 

6. Recognize that the entire block is one long string. So, you need to break the block up into three strings before make our modification.

{codecitation class="brush:php;first-line:165;wrap-lines:false;" width="550px" }

$insert = '<form action="'.$this->paypal_url.'/cgi-bin/webscr" method="post">

<input type="hidden" name="cmd" value="_xclick">  

<input type="hidden" name="business" value="'.$this->paypal_email.'">  

<input type="hidden" name="item_name" value="'.$this->_data->item_name.'">  

<input type="hidden" name="item_number" value="'.$this->_data->id.'">

<input type="hidden" name="quantity" value="1">';

 

$insert .= '<input type="hidden" name="amount" value="'.$this->_data->amount.'">';

 

$insert .= '<input type="hidden" name="tax" value="'.$this->_data->tax.'">

<input type="hidden" name="shipping" value="0">

<input type="hidden" name="currency_code" value="'.$this->currency_code.'"> 

<input type="hidden" name="lc" value="'.$this->country_code.'">

<input type="hidden" name="mrb" value="YRDKF6S68Y7DS"/>

<input type="hidden" name="return" value="'.JURI::base().'index.php?option=com_nicepaypaldownloads&amp;nd=r&amp;itm='.$this->_data->id.'">    

<input type="hidden" name="cancel_return" value="'.JURI::base().'index.php?option=com_nicepaypaldownloads&amp;nd=c&amp;itm='.$this->_data->id.'">    

<input type="hidden" name="notify_url" value="'.JURI::base().'index.php?option=com_nicepaypaldownloads&amp;controller=Ipn&amp;task=ipn">  

<input type="image" border="0" name="submit" src="'.$btnimg.'" alt="PayPal - The safer, easier way to pay online">

</form>';

{/codecitation}

 

7. Now, you can freely change the amount field. You need to add an exception that looks for the -1.00 value. Once your finished the modified block of code should look something like the one below.

{codecitation class="brush:php;first-line:165;wrap-lines:false;" width="550px" }

$insert = '<form action="'.$this->paypal_url.'/cgi-bin/webscr" method="post">

<input type="hidden" name="cmd" value="_xclick">  

<input type="hidden" name="business" value="'.$this->paypal_email.'">  

<input type="hidden" name="item_name" value="'.$this->_data->item_name.'">  

<input type="hidden" name="item_number" value="'.$this->_data->id.'">

<input type="hidden" name="quantity" value="1">';

 

if ( $this->_data->amount == -1 ){

 

  $insert .= 'Enter Price<br /><input type="text" name="amount" size="10" value=""><br /><br />';

 

}else {

 

  $insert .= '<input type="hidden" name="amount" value="'.$this->_data->amount.'">';

 

}

 

$insert .= '<input type="hidden" name="tax" value="'.$this->_data->tax.'">

<input type="hidden" name="shipping" value="0">

<input type="hidden" name="currency_code" value="'.$this->currency_code.'"> 

<input type="hidden" name="lc" value="'.$this->country_code.'">

<input type="hidden" name="mrb" value="YRDKF6S68Y7DS"/>

<input type="hidden" name="return" value="'.JURI::base().'index.php?option=com_nicepaypaldownloads&amp;nd=r&amp;itm='.$this->_data->id.'">    

<input type="hidden" name="cancel_return" value="'.JURI::base().'index.php?option=com_nicepaypaldownloads&amp;nd=c&amp;itm='.$this->_data->id.'">    

<input type="hidden" name="notify_url" value="'.JURI::base().'index.php?option=com_nicepaypaldownloads&amp;controller=Ipn&amp;task=ipn">  

<input type="image" border="0" name="submit" src="'.$btnimg.'" alt="PayPal - The safer, easier way to pay online">

</form>';

{/codecitation}

 

8. Save your changes and upload your modifed script back to its original location on your site.

That is it for modifying the button plugin. All items with a price of -1.00 will generate a button like the one below.

Modifying the PayPal IPN Validation Script

Now, you need to edit the extensions IPN validation process. You need to do this so that the extension will except IPN messages from PayPal that have a variable purchase amount.

 

1. Open the file found at components/com_nicepaypaldownloads/models/ipn.php

2. Find the function named "function itemValidate()" around line 449.

3. You need to add just a little bit of code to your MySQL query. This exta bit of code tells MySQL to look for the item record with either an price amount that matchs the IPN message amount, or that matches the -1.00 flag.

 

DB Query, Line 473

{codecitation class="brush:php;first-line:473;wrap-lines:false;" width="550px" }

if ($testmode == 1){

 

        $query = 'SELECT * FROM #__nicedloads_items AS itm

        JOIN #__nicedloads_fsettings AS fset

        WHERE itm.id = "'.$itmid.'"

        AND itm.amount = "'.number_format($amount, 2, '.', '').'"

        AND fset.paypal_testemail = "'.$paypalemail.'"

        AND fset.paypal_currencycode = "'.$currencycode.'"

        LIMIT 0, 1;';

 

}else {

 

        $query = 'SELECT * FROM #__nicedloads_items AS itm

        JOIN #__nicedloads_fsettings AS fset

        WHERE itm.id = "'.$itmid.'"

        AND itm.amount = "'.number_format($amount, 2, '.', '').'"

        AND fset.paypal_email = "'.$paypalemail.'"

        AND fset.paypal_currencycode = "'.$currencycode.'"

        LIMIT 0, 1;';

 

}

{/codecitation}

 

Change to the following, note lines 478 and 488

{codecitation class="brush:php;first-line:473;wrap-lines:false;" width="550px" }

if ($testmode == 1){

 

    $query = 'SELECT * FROM #__nicedloads_items AS itm

    JOIN #__nicedloads_fsettings AS fset

    WHERE itm.id = "'.$itmid.'" 

    AND (itm.amount = "'.number_format($amount, 2, '.', '').'" 

        OR itm.amount = "-1.00")

    AND fset.paypal_testemail = "'.$paypalemail.'" 

    AND fset.paypal_currencycode = "'.$currencycode.'" 

    LIMIT 0, 1;';

 

}else {

 

    $query = 'SELECT * FROM #__nicedloads_items AS itm

    JOIN #__nicedloads_fsettings AS fset

    WHERE itm.id = "'.$itmid.'" 

    AND (itm.amount = "'.number_format($amount, 2, '.', '').'" 

        OR itm.amount = "-1.00")

    AND fset.paypal_email = "'.$paypalemail.'" 

    AND fset.paypal_currencycode = "'.$currencycode.'" 

    LIMIT 0, 1;';

 

}

{/codecitation}

 

4. Save your changes and upload your modified script back to its original location on your site.

That should do it. No more modifications are needed to make buyer specified prices work. Keep in mind that we did not make any changes to the IPN Log.

~ Enjoy success!