Brian Moreau

Search my site

    Maker Portfolio   

PayPal Buy Now Buttons
Sending custom variables
using the IPN

A practical guide in 4 easy steps.


PayPal_IPN_Buy_Now_ButtonOne of the most common questions when using PayPal Buy Now Buttons is how do I know who has paid.
For example if it is for a website subscription then your members will have a user ID so you can send that ID to PayPal and have it returned when payment is received.
You can then instantly update your database and mark that member as paid.

Now PayPal is fairly well inline with twitter when it comes to technical documents.
There are so many of them and all quite confusing and sometimes conflicting.
I did try their technical support but the guy had to admit it was beyond his knowledge.
They did promise to call me back with a solution but #FAIL

They even suggested I ask my question on a third party web developers forum like Stackoverflow, I did and there were 100’s of other people with the same problem!
So after many frustrating hours I managed to solve the problem myself.

So if you want to send a custom variable from your website to PayPal via a pay now button and have that variable returned to you this is a simple way to do it.
Now to start with I do not believe you can choose your own variable names you have to use the pre defined variables as defined in the link below.

Sending variables already defined when creating your payment button on PayPal will be overiden. For example when creating your button on PayPal you call the product “ProductX” but in your website button code you send a variable called item_name this will change the product name.
The variable called custom is to be used named “custom”, you cannot rename it.
So I would suggest for sending unique customer ID’s or your site member ID’s to use the field CUSTOM.

1) Create a Pay Now Button

The first thing to do is create your pay now button.
If you have a business account go to 'TOOLS .. ALL TOOLS .. PAYPAL BUTTONS'
FOPR NON BUSINESS ACCOUNTS Go to, Profile, My selling preferences then click UPDATE on PayPal Buttons.
Then click Create New Button.
The minimum information required is Name and Price, (I used a penny to test).

Then click on Step 3 of the button creation tool.
Here you should enter the URL where the customer is to be taken after payment or if the payment is cancelled or fails.


No variables are sent to these URL’s by PayPal they are simply static landing and notification pages for the user who just clicked on your button. Also note you must use specific wording on the payment received page as per PayPal's terms and conditions.
Ignore the Add advanced variables option.

Now Copy and Paste the button HTML code into the page where you want the payment button to appear.
2) Edit Button to add custom variable

You now need to add a hidden field that will contain the custom variable you want to send.
Example 1 For sending a PHP varaible
<input type="hidden" name="custom" value="<?=$twitId;?>">
Example 2 For sending a fixed variable
<input type="hidden" name="custom" value="mycustomvalue">
Example 3
<input type="hidden" name="custom" value="<3862463">

Your complete Pay Now button code will now look like this.

<form action="" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="1234567890">
<input type="hidden" name="custom" value="<?=$mycustomvar;?>">
<input type="image" src="" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
<img alt="" border="0" src="" width="1" height="1">

3) Setup IPN

The next thing to do is turn on and set a URL for the Instant Payment Notification on PayPal
On PayPal go to Profile, My selling preferences, Instant payment notifications.
IF you have a business account go to 'PROFILE .. PROFILE AND SETTINGS .. My selling preferences ..
On that page enter the URL where notification is to be sent or click 'UPDATE' on Instant payment notifications.
Enter the URL of the page on your website that will capture the returned data from PayPal.
Ensure 'Receive IPN messages or Message delivery is selected or enabled.
This will be your IPN Listener page.

If you have already processed payments or are testing then you can view the data sent to the IPN URL by clicking on history, IPN history and then click on the payment you wish to view.
Your see there is a vast amount of data sent including the payees email address.

4) Create IPN Listener

The last thing to do is to create your IPN listener page. This will collect and process the data received so you can automatically send email notification or update your database.

The data is sent in POST format and thus can easily be retrieved using PHP thus.
$payer_email = $_POST[payer_email];
$userID = $_POST[custom];

What you do with that data is your choice.

Want to send more variables.
I recommend using the custom field for user id’s as this is hidden, you can if you wish use some of the other pre defined variables to send additional data such as item size and colour options. For these PayPal recommend you use variables on0, on1, os0 and os1. These will appear on the PayPal payment page.

Your IPN URL is also called by PayPal if you issue a refund, so your IPN Listener script should check the payment_status and reason_code to determine why the IPN has been sent.
Possible values for the payment_status are Completed or Refunded and the reason_code will be set to Refunded if a refund is made and no value when a payment is completed.
I would also suggest you collect the date just in case there is delay in receiving the notification.

PayPal IPN Integration Guide


Readers comments >

Sorry there are no comments, be the first to leave a comment.

Leave a comment or ask me a question >

You don’t need to register to leave a comment because I feel people should not be forced to register to have their say.
All comments are checked prior to publishing to prevent spam.
Donít worry this wont take long.
If you supply your email address below you will automatically be notified when I approve your comment.

Full name > *
eMail address > (not published)
Website > (http://...)
Location > (town or city)
Comment or question > *
Human *
  * feilds required  
PayPal Buy Now Button, Sending custom variables
© 2008 - 2021 - Brian Moreau

Valid XHTML 1.0 Transitional Valid CSS!