Note: Mike Daugherty has written a more recent tutorial on this topic that you might also want to review. It can be seen here: http://tutorial468.easycfm.com/ (Pablo Varando - 07.20.2006)
If you've ever wanted to have a shopping cart
solution that works in REAL time, but didn't want to (or couldn't) get a
merchant account, your alternative is PayPal. PayPal is the leading money
processing site on the web, with millions of transactions each day! A short time
ago, PayPal introduced Instant Payment Notification ("IPN") which allowed vendors
the ability to sell products from their web sites and then update their
databases in real time when customers checked out. Let me warn you that
using PayPal's IPN system is a tough thing to do, it's not as easy as they make
it appear. In order to make this task less difficult I decided to write a tutorial on how to implement it for your
website. This is probably the only tutorial online showing you how to implement
the PayPal IPN, as most people
would rather have you pay them to implement it for you! So let's begin with
the tutorial.
The first thing you will need to do is to get a
FREE PayPal account. Now keep in mind that to accept payments you will have to
put a credit card and a bank account on record. This is good for you (if you're like me, you will want to get the Paypal MasterCard Debit Card which allows you to access your Paypal funds instantly through
MasterCard). Once you have those steps in place, the next thing is to begin
your IPN modifications. That will be the first part of this tutorial.
Go to www.paypal.com
Log into your account and then click on the
"Sell" tab on the upper right hand corner:

Next click on the Instant Payment Notification
link:

Then click on the Technical Overview section:

Next you will click on the link titled
"Start Using IPN".

Then click on the "EDIT" button:

Now you will need to enter two values:
1) Make sure this is "checked".
2) Enter the URL to the file that you will create in this tutorial, it's usually
recommended you place it in the CART folder under the name ipn.cfm.
3) Click the Save Button.

Note: Click the "read the
instructions" link above the "SAVE" button for further
information.
That's it, you have now enabled PayPal's IPN
and are ready to use it! Now I'll show you the back-end code you'll need to
begin accessing real time payments.
The first thing you need to understand is that
PayPal will send you information to the link you place above (The file you will
now create) as POST form submission. That data will be available for you to do as
you wish.... this tutorial will demonstrate how you use that data to finalize an
order and allow the customer to download the software they've purchased.
The first thing you need to do is to create a
string with values you will pass back to PayPal for verification.
<!-- read post from
PayPal system and add 'cmd' -->
<CFSET str="cmd=_notify-validate">
<CFLOOP INDEX="TheField"
list="#Form.FieldNames#">
<CFSET str = str & "&#LCase(TheField)#=#URLEncodedFormat(Evaluate(TheField))#">
</CFLOOP>
<CFIF IsDefined("FORM.payment_date")>
<CFSET str = str & "&payment_date=#URLEncodedFormat(Form.payment_date)#">
</CFIF>
<CFIF IsDefined("FORM.subscr_date")>
<CFSET str = str & "&subscr_date=#URLEncodedFormat(Form.subscr_date)#">
</CFIF>
Next we will post that information back to
PayPal to verify that this is a VALID order and that the values we have are
correct.
<!-- post back to PayPal system to validate -->
<CFHTTP URL="https://www.paypal.com/cgi-bin/webscr?#str#"
METHOD="GET"
RESOLVEURL="false">
</CFHTTP>
<!-- check notification validation -->
<CFIF #CFHTTP.FileContent# is "VERIFIED">
<!-- check that
payment_status=Completed -->
<cfif FORM.payment_status eq "Completed">
<!-- check that receiver_email is your email address
-->
<cfif
#FORM.RECEIVER_EMAIL# eq "mypaypalemail@mysite.com">
<!-- process payment -->
<cftry>
<cfquery
name="qInsertOrder"
datasource="MyDSN">
INSERT INTO Orders(
receiver_email,
item_number,
quantity,
payment_status,
payment_date,
payment_gross,
payment_fee,
txn_id,
txn_type,
payment_type,
payer_id
)
VALUES(
'#FORM.RECEIVER_EMAIL#',
'#FORM.ITEM_NUMBER#',
'#FORM.QUANTITY#',
'#FORM.PAYMENT_STATUS#',
#CreateODBCDateTime(Now())#,
'#FORM.PAYMENT_GROSS#',
'#FORM.PAYMENT_FEE#',
'#FORM.TXN_ID#',
'#FORM.TXN_TYPE#',
'#FORM.PAYMENT_TYPE#',
'#FORM.PAYER_ID#'
)
</cfquery>
<cfcatch>
<!---
let's log all errors --->
<cffile
action="append"
file="D:\paypal_logs\paypal_log.txt"
output="Error order info">
</cfcatch>
</cftry>
Now let's generate a password for this
customer, this will be used to allow them to download their software, you can
also (as shown) generate serial numbers for their software at this step, to make
sure that they get a serial number when registering their software!
<cfset password = #Evaluate("#RandRange(1,10000)#
* #RandRange(1,10000)#")#>
<cfset serial_number = "#RandRange(1,10000)#-0-#Left(first_name,1)#">
<cftry>
<cfquery name="qInsertCustomer"
datasource="YourDSN">
INSERT INTO Customers(
first_name,
last_name,
email,
serial_number,
address,
city,
state,
zip,
serial_used,
payer_id,
password
)
VALUES(
'#FORM.first_name#',
'#FORM.last_name#',
'#FORM.payer_email#',
'#serial_number#',
'#FORM.address_street#',
'#FORM.address_city#',
'#FORM.address_state#',
'#FORM.address_zip#',
0,
'#FORM.payer_id#',
'#password#'
)
</cfquery>
<cfcatch>
<!--- log any errors when inserting this customer --->
<cffile
action="append"
file="D:\paypal_logs\paypal_log.txt"
output="Error inserting customer">
</cfcatch>
</cftry>
Now send the customer an email, thanking them
for their order and with further information about their purchase:
<!--- send user an email --->
<cftry>
<cfmail from="you@yoursite.com"
to="#FORM.payer_email#"
subject="MySite.Com Software Purchase!">
Thank you for
purchasing software from MySite.Com
You may
download your software by going to:
http://www.yoursite.com/download/
Your username
is: #payer_email#
Your password
is: #password#
If you have
questions or need help, please go to:
http://www.mysite.com/support/
Thanks again
for your purchase!
MySite.Com,
Inc.
http://www.mysite.com
</cfmail>
<!--- Now send yourself an
email alerting that there was a sale done --->
<cfmail from="me@mysite.com"
to="me@mysite.com"
subject="Someone Purchased From The
Website!">
An order was placed by: #FORM.first_name#'
#FORM.last_name#
Item Purchased:
Item Number: #FORM.ITEM_NUMBER#
Purchase Price:
#DOLLARFORMAT(FORM.PAYMENT_GROSS)#
PayPal Fee:
#DollarFORMAT(FORM.PAYMENT_FEE)#
==========================
Profit Fee:
#DollarFormat(Evaluate("#FORM.PAYMENT_GROSS# - #FORM.PAYMENT_FEE#"))#
A Reminder Email:
http://www.mysite.com
</cfmail>
<cfcatch>
<!--- Log
Any Errors Sending Emails --->
<cffile
action="append"
file="D:\paypal_logs\paypal_log.txt"
output="Error sending email">
</cfcatch>
</cftry>
</cfif>
</cfif>
<CFELSEIF
#CFHTTP.FileContent# is "INVALID">
<!-- log for investigation -->
Something that was
purchased was invalid, either the order or the information provided. This is
usually good to log in case someone is trying to purchase with stolen card
numbers, etc. Here simply place a QUERY tag that insert the data above into a
database.
<CFELSE>
<!-- error -->
This usually means that
something went wrong along the way, you can use this area to log it and keep for
your records.
</CFIF>
That's pretty much it, with this tutorial you
can now begin taking instant credit card payments via PayPal securely and
process the orders on the fly!
Questions? Comments? Email Me: webmaster@easycfm.com
Date added: Wed. September 25, 2002
Posted by: Pablo Varando | Views: 60570 | Tested Platforms: CF5 | Difficulty: Advanced
Ecommerce
Full Applications
 |
Makes it look difficult.
PayPal integration with coldfusion can be made in a much simpler code, it is not very easy to follow the code above, however thanks for your time in posting your tutorial.
Posted by: Surfer
Posted on: 05/17/2004 10:03 AM
|
Verification works
For all those who suffer Try this it worked for me!!! I tried to make it as simple as possible
<!-------------------------------------------------------------------------------------------------- Remember, this is not a graphic interface - The IPN.CFM file should run in the background. It is only an interface between PayPal and your transaction complete, invalid or error steps. ---------------------------------------------------------------------------------------------------> <cfset x = GetHttpRequestData()> <cfset str="cmd=_notify-validate&" & x.content> <!---post back to PayPal for validation---> <cfhttp url="https://www.paypal.com/cgi-bin/webscr?#str#" method="GET" resolveurl="no"> </cfhttp> <!-------------------------------------------------------------------------------------------------- Use the following to test your IPN.CFM file - The idea is to record the PayPal encoded and hidden 'message string' as posted by PayPal apon completion of each transaction. I record the message to a .txt file called IPN_log.txt - Make sure you change the path to the relative path of your own web direcory on the server. ---------------------------------------------------------------------------------------------------> <cffile action="append" file="d:\yourWebDirectory\IPN_log.txt" output=" ------------------------------------------------------------- Paypal Says the transaction is: #CFHTTP.FileContent# Transaction status: #FORM.payment_status# Seller account: #FORM.RECEIVER_EMAIL# ------------------------------------------------------------- " addnewline="yes">
<CFLOOP INDEX="TheField" list="#Form.FieldNames#"> <CFSET str = str & "LCase(TheField)#=#URLEncodedFormat(Form[TheField])#"> <cffile action="append" file="d:\yourWebDirectory\IPN_log.txt" output="#LCase(TheField)#---->#(Form[TheField])#" addnewline="yes"> </CFLOOP>
<!--- Payment verified ---> <CFIF #CFHTTP.FileContent# is "VERIFIED"> <!-- check that payment_status=Completed --> <cfif #FORM.payment_status# eq "Completed"> <!-- check that receiver_email is your email address (that money went into your account --> <cfif #FORM.RECEIVER_EMAIL# eq "name@email.com"> Paypal says the funds went into your account<br> <!-- Everything checked ok, add your action here (typically you would want to update your database here --> </cfif> </cfif>
<CFELSEIF #CFHTTP.FileContent# is "INVALID"> <!-- The information is not valid, please investigate and trace invalid info --> Invalid Info<br> <!---Something that was purchased was invalid, either the order or the information provided. This is usually good to log in case someone is trying to purchase with stolen card numbers, etc. Here simply place a QUERY tag that insert the data above into a database.---> <CFELSE> <!-- ERROR --> <!---This usually means that something went wrong along the way, you can use this area to log it and keep for your records.---> </CFIF>
Posted by: Chris van den Heever
Posted on: 06/17/2005 09:04 PM
|
How to insert QUERY tag to insert data in the database
Thanks for your great tutorial "Using Paypal's IPN with ColdFusion" One thing that is not quite clear to me is:
towards the end of the tutorial you write that "..... <CFELSEIF #CFHTTP.FileContent# is "INVALID"> <!-- log for investigation -->
Something that was purchased was invalid, either the order or the information provided. This is usually good to log in case someone is trying to purchase with stolen card numbers, etc. Here simply place a QUERY tag that insert the data above into a database.
What's the exact procedure of placing the QUERY tag from Paypal IPN message so that it inserts the data into a database?
Great many thanks Radek
Posted by: radek
Posted on: 08/29/2005 10:53 PM
|
Changes in paypal
Please Pablo, update your paypal tutorial, some changes has been ocurred in the page...
Posted by: Erick
Posted on: 09/19/2005 11:00 AM
|
RE: Changes in paypal
I am working on a new tuorial that will contain subscriptions, single and multiple item checkouts, etc..
I will leave this one as it (gives you the general idea) until that one is complete.
Posted by: Pablo Varando
Posted on: 10/05/2005 12:57 PM
|
Anxiously awaiting your new tutorial
Hi Pablo, your tutorial is great. Do you know when you'll have your new ones ready for posting?
Thanks, Doug
Posted by: Doug
Posted on: 10/07/2005 09:18 PM
|
RE: Anxiously awaiting your new tutorial
I am currently working on it and making it into a multi-tutorial series... The first installment should be ready in a bout a week or two (sorry, delayed by Macromedia MAX conference I am attending right now).. then they will (hopefully) come out weekly or so!
Posted by: Pablo Varando
Posted on: 10/19/2005 02:31 PM
|
I Am Also Anxiously Awaiting this new Tutorial!
I am also anxiously awaiting this tutorial! I am doing SUBSCRIPTIONS and trying to get ColdFusion and IPN to work together using MySQL as my database to process Subscriptions!
Bless You Man! I have been EVERYWHERE and I can't find any info on ColdFusion, IPN and Subscriptions!
Thank You in Advance! Doug (the Novice!) :)
Posted by: Doug S. the Novice
Posted on: 11/08/2005 05:33 PM
|
I Am Also Anxiously Awaiting this new Tutorial!
Me TOO! I am trying the SAME THING (subscriptions) but can't seem to get any response at all. I have tried everything here and neither solution works.
Posted by: Sergio Vazquez II
Posted on: 11/09/2005 02:26 AM
|
Still working on this...
I just wanted to let you all know I am working on this.. I had to fly to China to finalize a project I was working on and now with the Thanksgiving holiday, things have been a little behind... Rest assured I am working to get this completed! :)
Posted by: Pablo Varando
Posted on: 11/26/2005 12:36 AM
|
Thanks - Helped me alot
Thaks a lot I have been working on the IPN and CFM - and have been going around in circles. This has helped smoth out the process.
Posted by: Chris
Posted on: 11/29/2005 07:18 PM
|
Kewl!
Kewl! I've been checking back about every other day to see if there had been any more updates. Thanks for posting this update... I am looking forward to reading and studying your wonderful tutorial on how to use Paypal's IPN with ColdFusion while doing SUBSCRIPTIONS! Thanks again in advance! I am loving your website! I am constantly referring to it.
Posted by: Doug (the Novice!)
Posted on: 11/29/2005 11:18 PM
|
Great tut
Pablo, good tutorial man. Have you written the new updated paypal tutorial?
Posted by: Ketan
Posted on: 12/30/2005 10:04 PM
|
Waiting with Anxious Anticipation!
Hey... Thought I'd check in again with this Tutorial. I'm actually flying out of town tomorrow to visit a cousin and stay with them to "GET AWAY" in order to Program! (My phone rings alot!) I'll be programming for 8 days... Jan. 22nd-30th.
I could really use this tutorial right now. :)
Posted by: Doug (the Novice!)
Posted on: 01/22/2006 12:23 AM
|
Almost done...
Sorry to have gotten delayed... I have been a bit delayed since I am getting a new service for the community in place (www.freecoldfusionhosting.com) so have been a litle delayed, but I am working on it... I will let you know all know when I am almost done.
Pablo
Posted by: Pablo
Posted on: 01/23/2006 04:48 PM
|
Desperately awaiting Paypal Tutorial :)
Well, today is my birthday, April 4th. :) I was hoping to log on and find the tutorial for using Paypal IPN for SUBSCRIPTIONS with ColdFusion.
I really don't know where else to turn for help. I would of thought that Paypal would help to provide updated examples on their website, but I have not found any... especially for Coldfusion and Subscriptions.
Any ETA on the Paypal Tutorial?
Posted by: Doug (the Novice!)
Posted on: 04/04/2006 11:46 AM
|
db file attributes for CF ipn ???
Dear Pablo:
Could you please let me know what the file attributes are for the db fields ... i don't want to guess at them so please let me know asap what they are and any updates as noted above for the IPN file .... thanks a bunch ...i would also appreciate it greatly if you could tell how to "pass" the newly generated user name and password to another program to automatically set them up ....
thanks
Scott Steele
Posted by: Scott Steele
Posted on: 05/22/2006 11:34 PM
|
Updated IPN Tutorial Posted
Mike Daugherty has written a more recent tutorial on this topic that you might also want to review. It can be seen here: http://tutorial468.easycfm.com/
Posted by: Pablo Varando
Posted on: 07/20/2006 12:46 PM
|
Finally, a working tutorial!
I finally got IPN to work using Mike Daugherty's tutorial!! I've been trying to get this to work for months. Thanks to both you guys for leading me to a solution.
Posted by: Glen Ochoa
Posted on: 08/10/2006 04:47 PM
|
why CFHTTP.FileContent is "INVALID
I try many time.I get CFHTTP.FileContent "INVALID" why??
Thanks
Posted by: mark
Posted on: 06/28/2007 11:29 PM
|
Listen to Chris.
I struggled with IPN for a while, reading tutorials and tips that were often out of date. As of October 2007, what Chris has above works. The best way to test you IPN scripts is at http://www.eliteweaver.co.uk/testing/ipntest.php
(don't forget to change your script to point to the eliteweaver URL while you test it here).
Good luck. Neal
Posted by: Neal
Posted on: 10/06/2007 11:59 PM
|
Form fields and auto-return
If I use this IPN thing, do I still need to use the "return", "cancel_return" and "rm" input fields in the subscribe form? Also, what about auto-return - do I need to use that too - or should I not use that if I am using IPN?
Posted by: Matt
Posted on: 01/10/2008 12:33 AM
|
|