![]()
|
|||||||||||||||||
Introduction In my experience, working with PayPal is like pulling teeth. I find their interface deeply unintuitive and convoluted, and their documentation - although extensive - virtually impossible to navigate. If I had any other choice for online payments, I'd leap at it in a second. But I don't: PayPal is the worldwide standard. So until Google Checkout takes over the market, we have to suck it up and learn to do things their way! This tutorial aims to explain how to integrating your Form Tools form with PayPal and make the process as simple as possible. I've included a zipfile (see below) which does a lot of the dirty work (such as the IPN correspondence) automatically. Although this tutorial is about setting up a donation form, you can apply anything you learn here to add any PayPal-enabled form, such as a shopping cart for ordering merchandise. Also, although this is about PayPal, the same general process applies for most external payment gateways - so if any any point you want to jump ship and use Google Checkout, WorldPay or something else, you can still learn a lot just by understanding the process described herein. The bulk of this tutorial is less to do with Form Tools than it is PayPal. Most of the work is figuring out how to send and receive information from PayPal in a format they require. Hooking it up to Form Tools is actually fairly simple, although conceptually may prove a little confusing at first if you're not familiar with Form Tools' "code" submission type. The tutorial begins with an top-down discussion of the overall process. I chose to start with the abstract process rather than the details because it really is vital that you understand precisely what you're trying to do. It's not the sort of task you can just implement then understand in retrospect - you'd be unlikely to succeed setting up your form without knowing what's going on first! As always, I've tried to keep this tutorial as simple as possible for the not-so technically literate. Here are the requirements:
All of the above points will be discussed below. You can see an example of a PayPal enabled form with Form Tools' own donation form. If this tutorial comes in handy, how about seeing it in action and making a donation? ;-) Overview Let's start with a pretty, unintimidating diagram which outlines the whole process. I've simplified a few things (such as the fact that the user can cancel at virtually any step) to keep it clear.
Here's a few important points:
Instant Payment Notification (IPN) IPN is a service provided by PayPal that automatically notifies your server the instant a transaction occurs. This lets us ensure that all successful payments get marked as "paid" in our Form Tools database. And here's the other piece of good news: you don't really need to know how it works. The zipfile contains a file with all the functions needed to interact with PayPal to confirm a purchase and mark the submission as complete. So... let's get started setting up your form! Step 1: Setting up your Form A look at our donation form For the sake of this tutorial, we're going to work with a concrete example: the donation form on this site. If you haven't already done so, download the zipfile containing the form and PayPal files - you'll need them for the next section. You might want to open them up and look at them while reading this section. Here's what the form looks like: You can add as many fields as you would like to this form, but that's outside the scope of this tutorial. One tip, though: if you plan on adding other fields and expect them to get to PayPal, be sure to read the Adding PayPal Checkout to Your 3rd-party Shopping Cart document, listed below in the References section. Files and Folders Here's a list of all the files and folders in the zipfile.
Adding the donation form to Form Tools
And that's it! All form submissions will now be stored by Form Tools, but they will NOT appear in the user interface by default. In order to make them appear, we need to "finalize" them. This step is coming up...! Step 2: Configuring your Donation Form with PayPal
Get a PayPal account
If you haven't already done so, sign up for a PayPal Account here. Choose the account type (Personal, Premier or Business) that is right for your particular case. This account (email address) will be where all donations will be sent.
Due to the Personal account's limitation of not being able to receive payments from credit card, I'd recommend you opt for a Premier or Business account - despite the fees. Configure your PayPal account
Next, if you have a Premier or Business account, you'll need to make a small configuration change to your PayPal settings.
Get a PayPal Developer account
In order to test your form to check that everything works the way you want, you will need a PayPal developers account. You can sign up for one here. Configure your form to send the correct values to PayPal
Next, you will need to configure the PHP script to send the appropriate values to PayPal for processing. Open up the includes/config.inc.php file and edit the following lines, found at the very top. // PayPal settings $paypal["business"] = "joebloggs@something.com"; // your PayPal account $paypal["site_url"] = "http://www.formtools.org"; // your website root URL $paypal["image_url"] = "/images/logo.jpg"; // (optional: will be included in PayPal pages if defined) $paypal["success_url"] = "/donate/success.php"; // where the user is directed to after successful payment $paypal["cancel_url"] = "/donate/cancelled.php"; // where the user is directed to after cancelled payment $paypal["notify_url"] = "/donate/ipn.php"; // the location of the ipn.php file (in zip) $paypal["currency_code"] = "CAD"; // [USD,CAD,GBP,JPY,EUR] $paypal["post_method"] = "fso"; // "fso": fsockopen, "curl": cmd line, "libCurl": PHP compiles with libCurl $paypal["curl_location"] = "/usr/local/bin/curl"; // only needed if you use the curl or libcurl post_methods // When you are finished testing, change this to: https://www.paypal.com/cgi-bin/webscr $paypal["url"] = "https://www.sandbox.paypal.com/cgi-bin/webscr"; After changing those settings, you're ready to put through a test submission. Customize the files for your server
Next, you need to customize the paths to the Form Tools files in two of the files: process.php and ipn.php. At the top of both of those two files, you'll see these two lines: require("/path/to/FormTools/global/library.php"); require("/path/to/FormTools/process_code.php"); Update them to the appropriate paths for your server. In ipn.php, after those two lines, make sure the $form_tools_form_id value in ipn.php is correct as well. Making some test payments Now the fun / horror starts! Log into your PayPal Developer's account. Everything done through a developer account is "pretend" only - no money is actually changing hands. It provides a useful service to debug your form before pushing it live. After logging in, you will need to create two accounts: one for the "seller" (i.e. to simulate YOUR account), and one for the buyer. Be sure to enter the seller email as the $paypal["business"] option in the config.inc.php file listed above. Now open up your form in your web browser. If you configured everything properly, after submitting your form you should be redirected to the PayPal site where it will prompt you to log in. Log in using one of your test accounts and make the payment. Again, everything you do in the PayPal sandbox (i.e. while the paypal URL is: https://www.sandbox.paypal.com/cgi-bin/webscr) is fake. No payments are actually made, it's simply for test purposes only. Make sure you try putting payments through, cancelling payments and clicking on "Return to Merchant" on the PayPal site to verify all links work properly. Lastly, if you chose to do the optional section above with setting up a custom field in the database for the PayPal return information (see the end of the Adding the donation form to Form Tools section), you will need to uncomment the section at the top of success.php and set the appropriate form ID. Going live! And finally, it's time to go live! All you need to do to push it live is to change that one value in your includes/config.inc.php file. Change this line: $paypal[url] = "https://www.sandbox.paypal.com/cgi-bin/webscr"; to this: $paypal[url] = "https://www.paypal.com/cgi-bin/webscr"; And, if necessary, change the $paypal["business"] line in config.inc.php to your real paypal address. And you're done! A Few Final Tips
References
Has this tutorial helped? See it in action and donate now! :-) |
|||||||||||||||||