Integrating PayPal’s IPN with ColdFusion
Ok, everyone seems to really need this one so I sat down and cranked this out. Hope this helps you. It’s pretty simple once you get the hang of it. Currently, the tutorial is designed for items that have a set price, not a calculated one.
Let me explain: When the user clicks the button to go to PayPal, this tutorial assumes the item price is always constant like a book for $19.95 or 5 books for $49.95. If you use a shopping cart system that adds several items together for a variable price, you’ll have to wait for the nest tutorial in this series.
Good luck with all this. I have included demo files that you can use as a way to get started. Get them here.
The PayPal Side of Things
Step One:
Create a PayPal account. Go to http://www.paypal.com and setup an account.
Step Two:
On the Profile Tab (under My Account) we need to enable a few things. First, go to Instant Payment Notification Preferences. Turn this on and specify your notification URL. For my site, I use http://www.yourdomain.com/PayPal/ipn.cfm. We will create this file later, so don’t worry if it doesn’t exist yet. Save your settings and go back the profile summary page.
Now, we need to adjust some Website Payment Preferences. So, go to the Website Payment Preferences link below the Instant Payment Notification Preferences link. First, turn Auto Return ON and set the Return URL to the same one you set above, http://www.yourdomain.com/PayPal/ipn.cfm
Next, turn ON Payment Data Transfer. Once you do this, you will see an Identity Token. Copy and paste this to a text file for later. You will need this, very important!!!. I also turn on the PayPal Account Optional feature as well.
Step Three:
Create your invoice page that the customer will see. This will be where we transfer them over to PayPal to make the payment. You may create this page however you’d like, but we will use PayPal to create the form that actually sends the customer to PayPal’s website will your details already specified. You can use the one I included if you’d like.
Once your page is ready, we need to use the Button Factory under Merchant Tools at PayPal. Go to PayPal, login to your account and click Merchant Tools. Look toward the bottom of the page. Find the Buy Now Button Factory link, click it, then click Get Started.
Fill in the information such as Item name, item ID, and price. Don’t try using #form.itemprice# or #form.itemname# cause that doesn’t seem to work. These need to be static names and prices. Choose a button style and hit Create Button when you are finished.
Copy the HTML that is generated and paste in onto your Invoice Page in the appropriate spot. This will show the button you choose to your users and then transfer them to PayPal to make the payment.
If you have several items or different prices for different “packages” or “levels” of product, simply create more buttons to accommodate your needs.
The ColdFusion Side of Things
Step Four:
Ok, now we have an invoice page with a button on it (the one you created in Step 3) that will send the user PayPal. Once the user makes a payment, they will be transferred back to the Page we specified in Step 2, http://www.yourdomain.com/PayPal/ipn.cfm.
Let’s create that page now:
Create a blank ColdFusion document, save it as ipn.cfm in the PayPal directly of your website, and paste the code below at the very top of the page:
<cfset
authToken="QaX5z1OIH9L56J2iALil8sZZBnTTjZivYz2ASJOqETdS8OBy9xiE0KippOm">
<cfset txToken = url.tx>
<cfset query="cmd=_notify-synch&tx=" & txToken & "&at=" & authToken>
<CFHTTP url="https://www.paypal.com/cgi-bin/webscr?#query#"
method="GET"
resolveurl="false">
</CFHTTP>
<cfif left(cfhttp.FileContent,7) is "SUCCESS">
<cfloop list="#cfhttp.FileContent#"
index="curLine"
delimiters="#chr(10)#">
<cfif listGetAt(curLine,1,"=") is "first_name">
<cfset firstName=listGetAt(curLine,2,"=")>
</cfif>
<cfif listGetAt(curLine,1,"=") is "last_name">
<cfset lastName=listGetAt(curLine,2,"=")>
</cfif>
<cfif listGetAt(curLine,1,"=") is "item_name">
<cfset itemName=listGetAt(curLine,2,"=")>
</cfif>
<cfif listGetAt(curLine,1,"=") is "mc_gross">
<cfset mcGross=listGetAt(curLine,2,"=")>
</cfif>
<cfif listGetAt(curLine,1,"=") is "mc_currency">
<cfset mcCurrency=listGetAt(curLine,2,"=")>
</cfif>
</cfloop>
<cfoutput>
<p><h3>Your order has been successfully received.</h3></p>
<b>Details</b><br>
<li>Name: #firstName# #lastName#</li>
<li>Description: #itemName#</li>
<li>Amount: #mcCurrency# #mcGross#</li>
<hr>
</cfoutput>
<cfelse>
ERROR: Check to make sure the authToken value you set is EXACTLY what PayPal gave you in Step 2
</cfif>
Ok, now go back to the top of the code and replace the authToken variable value with the one you got in Step 2 from turning on the Payment Data Transfer. This will verify the Payment from PayPal. If the page comes back successful, you will see something that gives you payment details. If you get an error, check to make sure the authToken value you set is EXACTLY what PayPal gave you in Step 2.
Step Five:
Now that you have a successful payment, you can do whatever you need to do with that transaction like send them an email or modify a database record to show that a person has paid.
For example, to send them an email, use the code below. You will need to fill in your own info, but if you got this far, you probably already know that.
<cfquery datasource="YourDataSource" name="UserInfo">
Select Email
From tbl_Members
Where UserID = #session.userID#
</cfquery>
<cfmail to="#UserInfo.Email#" from="Your Email" subject="Payment Received">
This is your receipt for the item you purchased from My Site:
Receipt:
Date Here
Name: #firstName# #lastName#
Description: #itemName#
Amount: #mcCurrency# #mcGross#
</cfmail>
Or to add the amount paid to a database:
<cfquery datasource="YourDataSource" name="UpdateInfo">
Update tbl_Payments
Set PaidAmount = #mcGross#
where UserID = #session.userID#
</cfquery>