PayPal integrated user registration Drupal 7 module an overview
A large benefit of using a Content Management System (CMS) like Drupal is the off-the-shelf modules that can be used to extend the functionality. Drupal has a strong community of developers and has modules for even the most arcane features and if a module doesn’t exist, we either tweak an existing module or build a new one.
Recently we decided to build a project for a client that used Drupal 7 and found the modules to be lacking. While Drupal 7 introduced many good features to facilitate theme development and an improved API the community has been so slow to upgrade the modules and most modules still only work for Drupal 6. With backward compatibility missing, it can be a pain to implement even the simplest of features.
During this recent project we learned the problem the hard way when we went to implement a user registration with PayPal support. The design team was complete done with the visual part but the only module supporting PayPal was the e-commerce module. Even this module required modification as it did not fit our purpose exactly. For the benefit of everybody else this article provides a walkthrough of what we did and how we did it.
We intended to have:
• Extended profile for registrants (like their web-site address, etc.)
• Registration wizard with payment through PayPal
• Possibility of entering discount codes.
The ubiquitous Profile module does not work with Drupal 7, fortunately, Profile2 does. Profile2 uses a new concept called the Fields to do the job. For those who are not familiar with Fields, it is Drupal’s version of attached properties. For existing Entities we can attach these fields through the field API. In this case Profile2 adds these extra fields to the user profile.
Another method would be to return multiple forms through the drupal_get_form function in the module hook. First we would need the hook_form_alter to find the user registration form. Then return the first page of the form. On submit of the first form we would save the values and then return a new form. At the end of the step only we would perform the actual registration like normal. This method is obviously the most complex and difficult to implement.
The easiest method we found was to avoid building modules altogether and just go with writing PHP directly in different pages which would correspond to each step of the wizard. So the first page would have a button to the second and so on. Care must be taken to ensure that people cannot skip steps by typing the URL directly in the browser.
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="payPalForm">
<input type="hidden" name="item_number" value="General Payment">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="business" value="firstname.lastname@example.org">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="return" value="http://mysite.com/payment-complete/">
<input name="item_name" type="text" id="item_name" size="45">
<input name="amount" type="text" id="amount" size="45">
<input type="submit" name="Submit" value="Submit">
The important thing to change here is the value of the amount field. Here we have made the amount field as a text field to allow greatest flexibility. Upon entering the amount the user will be taking to the PayPal site. Thus, to return the user to our site after payment and to record the transaction details on our site we need to setup IPN (instant payment notification) on the PayPal site.
This was a general summary of how to create a simple wizard which allows user registration and payment using Drupal 7