Using PayPal’s IPN with ColdFusion
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.
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
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’ –>
<CFLOOP INDEX="TheField" list="#Form.FieldNames#">
<CFSET str = str & "&#LCase(TheField)#=#URLEncodedFormat(Evaluate(TheField))#">
<CFSET str = str & "&payment_date=#URLEncodedFormat(Form.payment_date)#">
<CFSET str = str & "&subscr_date=#URLEncodedFormat(Form.subscr_date)#">
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">
<!– 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 "email@example.com">
<!– process payment –>
<cfquery name="qInsertOrder" datasource="MyDSN">
INSERT INTO Orders(
<!— let’s log all errors —>
output="Error order info">
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)#">
<cfquery name="qInsertCustomer" datasource="YourDSN">
INSERT INTO Customers(
<!— log any errors when inserting this customer —>
output="Error inserting customer">
Now send the customer an email, thanking them for their order and with further information about their purchase:
<!— send user an email —>
<cfmail from="firstname.lastname@example.org" 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:
Your username is: #payer_email#
Your password is: #password#
If you have questions or need help, please go to:
Thanks again for your purchase!
<!— Now send yourself an email alerting that there was a sale done —>
<cfmail from="email@example.com" to="firstname.lastname@example.org" subject="Someone Purchased From The Website!">
An order was placed by: #FORM.first_name#’ #FORM.last_name#
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:
<!— Log Any Errors Sending Emails —>
output="Error sending email">
<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.
<!– error –>
This usually means that something went wrong along the way, you can use this area to log it and keep for your records.
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!
Article first appearead at EasyCFM.com
EasyCFM.Com introduces at least three new tutorials each week, written by the webmaster (Pablo Varando) and also from individual people who post their own tutorials for visitors to learn from. For more information please visit: http://www.easycfm.com [EasyCFM is Hosted by Colony One On-Line – http://www.colony1.net]