Brian Moreau

Search my site


Plasma
   Home   
    Solutions    
    Articles   
    Projects   
    Blog   
    Contact   
    Maker Portfolio   
 
 
 

PayPal Buy Now Buttons
Sending custom variables
using the IPN

A practical guide in 4 easy steps.

June
2013

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.
https://www.paypal.com/cgi-bin/webscr?cmd=p/pdn/howto_checkout-outside

NOTE*
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.
 
NOTE*
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.

Examples:
http://www.mydomain.com/paymentcanceled.php
http://www.mydomain.com/paymentmade.php

NOTE*
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="https://www.paypal.com/cgi-bin/webscr" 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="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>

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.
Example:
http://www.mydomain.com/paypal_ipn.php

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.
<?PHP
$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.

WARNING:
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
https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNIntro/

 
 
 
 

Readers comments >

Date:  10-06-2013 15:38:13
From:  Chris Toon
Location: Portsmouth
Thank you, you have saved me a lot of time. I tried a lot of other examples and nothing worked. This solution is easy to understand and works.

Date:  24-07-2013 18:19:42
From:  Cam Little
Location:
Thank you, I knew there must be a way to pass variables trough a button created on Paypal's site. Unfortunately all of Paypal's documentation is a mess so this saved me much frustration.

Date:  31-10-2013 13:42:52
From:  Bruce Harrison
Location: Milan, TN
Can the content of the custom variable be included in the email that paypal sends out to the email addresses on the account? I want to include some registration info on the email that the financial person gets when a payment occurs.

Date:  14-01-2014 00:12:49
From:  David
Location:
Thank you! Thank you! Your post made this crystal clear. The PayPal docs have gotten better, but now sadly they are just junk instead of pure trash.

Date:  30-01-2014 04:18:11
From:  Yogesh
Location: India
This is very useful for me but How do I testing it before going to live.Because when I M created my button in sandbox that time paypal will not asked me "after payment redirect URL"?.Plz Help me its very urgnet.

Date:  03-02-2014 06:24:11
From:  Thomas Knopke
Location: Hameln, Germany
Pt.4: The custom variable used when sending TO PayPal, will be returned FROM PayPal as 'cm', (renamed for unknown reasons) so easily type: $userID = $_GET['cm']; Works perfect in my codes ...

Date:  22-02-2014 05:53:49
From:  Ray Wilson
Location: Hereford
Is there any way of adding the button reference (sent as os0) to the return URL in the Cancel option of the button. Thanks There is a box for advanced variables but I can't find any information on it.

Date:  28-03-2014 13:17:07
From:  JASMEET SINGH
Location:
HEY I JUST FOUND OUT THAT THIS CAN ALSO BE DONE IN A MUCH BETTER WAY USING SOMETHINF CALLED PDT (PAYMENT DATA TRANSFER)

Date:  31-03-2014 12:21:16
From:  Brian
Location: London
Thanks for that Jasmeet. Just reading about it, apparently PDT is not meant to be used with credit card payments.

Date:  20-08-2014 15:23:09
From:  Dave
Location:
PayPal has the URL where to send someone after they complete their transaction which you highlight. Then as I read into this you also have a serverside page that PayPal sends info to for updating your DB. I need to return an event ID to the static page. I see how to send it but how can I get it back to fill in some fields on my 'thank you' page? Can the listener be the same as the static page?

Date:  20-08-2014 15:23:42
From:  Dave
Location:
PayPal has the URL where to send someone after they complete their transaction which you highlight. Then as I read into this you also have a serverside page that PayPal sends info to for updating your DB. I need to return an event ID to the static page. I see how to send it but how can I get it back to fill in some fields on my 'thank you' page? Can the listener be the same as the static page?

Date:  13-08-2015 09:59:50
From:  Reena
Location:
Can i use a single payment button to pay invoice with different customer?

Date:  13-08-2015 11:46:24
From:  Reena
Location:
how to i get IPN messages in a separate file in my project? Can u please explain this step by step

Date:  07-09-2015 06:30:27
From:  Nancy Knox
Location: Madison, AL
Brian, this is a wonderful post. Only problem is that paypal has changed the way they do buttons, and now we have to enter something like: custom=value in the form. I can't figure out how to make it take my php code.

Date:  07-09-2015 07:37:18
From:  Nancy Knox
Location: Madison, AL
Hi Brian, I just added the custom input tag in the middle of the button code paypal generated. Not sure if that's what I'm supposed to do, but it worked. :)

Date:  29-09-2015 19:05:13
From:  Nancy Knox
Location: Madison, AL
Hi Brian, My paypal buttons (see last comment)seem to be unstable. Sometimes work fine, sometimes call up an ad for Paypal credit. Is this due to my adding the custom input tag in the middle of the code, and is there a better way to do it? Thanks.

Date:  23-01-2016 00:17:46
From:  Rambo
Location:
Thanks much for the explanation and step by step process. I has really helped me in understanding.

Date:  07-02-2016 13:27:33
From:  John
Location: scotland
Thanks for the explanation of the IPN, really useful Just a couple of questions just now which i hope aren't to stupid ! If you use the IPN variables "subscr_payment" & "subscr_failed" what values do you receive back? Does an IPN get sent if a user starts a transaction but then cancels? thanks john

Date:  08-09-2016 20:37:19
From:  Colin Steele-Perkins
Location: Llanrwst UK
This is the code I need but does Pay Pal send me an email with the persons address to post items to once paid? Thanks Colin

Date:  12-09-2016 18:35:11
From:  Brian
Location:
Colin, This option exists in the button setup options on the PayPal website. Just tick the box to require customer address.

Date:  18-12-2016 17:33:49
From:  William
Location: Atlanta
I cannot get this to work either by adding it to the code as noted or by adding custom as an advanced variable (custom=XX) in the PayPal button setup. After the customer pays and RETURNS TO MERCHANT (my url for the next step in the purchase process) and I view the page source from within the brownser, the hidden field CUSTOM is not there.

Date:  15-01-2017 16:08:39
From:  Brian
Location:
William, the custom variable is passed to paypal in the button code. It is then passed back to you via your IPN Listener page / code. You will not be able to view the code PayPal send back to you unless you capture it and place it into a database or file using the code in the IPN Listener page / URL I have updated the instructions above as PayPal have moved and changed some pages.

Date:  27-01-2017 15:17:05
From:  Ryan Bowman
Location: Chicago
Thanks for the post. I included this link in the comments of my video on Paypal Buy Now Buttons since a commenter was asking about adding info to a database. The video is here in case anyone is interested. It covers the basics of Buy Now buttons: https://www.youtube.com/watch?v=AnUbfBCsXs4

Date:  07-02-2017 08:25:59
From:  Zherdev Denis
Location: Russia
Thank you for this useful article, bro!

Date:  25-02-2017 05:00:21
From:  Paul Wiens
Location: Vancouver, BC
Thank you for this wonderful post. Is it possible to send the custom variable to the button using the "email URL" (ie. not the html code)?

Date:  02-06-2017 07:38:51
From:  Liam Labuschagne
Location: Te Awamutu
Hi I would just like to say thanks so much for this article I have been trying to figure out how to do this for days now! So many thanks!

Date:  11-06-2018 17:33:43
From:  Joaquim Llobet
Location: Mataró
Thank you very much Bryan. God bless you

Date:  15-07-2018 21:40:14
From:  Marcus Robbins
Location:
2018 and this is still helping people! Thank you! Honestly paypal, you're a mess.

Date:  20-09-2018 00:08:24
From:  Col Madden
Location: Darwin Australia
Thank you so much for this article - so helpful and easy to understand. I have to agree with other people who have left comments that the PayPal documentation is a joke. I thought it just me finding it difficult. What really bugs me are the contradictions.

Date:  07-06-2019 00:14:52
From:  Mark Chen
Location: Taiwan
Thanks for the article. I have 1 question. So in the IPN listener page, I can just assume that Paypal has successfully processed the payment so that I just start to process the 'custom' variable? or do I need to check if the payment is successful first? in other words, what do Paypal do if they decline the credit card transaction and will the 'custom' value still be sent to my IPN listener page? thanks.

Date:  17-08-2022 18:10:43
From:  Tony Drewry
Location: Bristol
Is there anyway to have one PayPal button that receives a variable value? NOT one that the user enters - one that receives a total of items selected. Eg 1. Item cost (3 possible), 2. Delivery cost from a range of options. So far I have 30 possible buttons each called by a particular combination. Should be a more sensible way of doing things Tony

 
 
 

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!