Deerwalk Blog

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.

Intended Features

Implementation

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.

This brings us to the next problem of creating wizards. There are mostly three ways of creating wizards in Drupal as it lacks in-built wizard creation functionality. Two of them can be implemented in modules. One is to use JavaScript to save and hide each step of the wizard. In essence each of the input fields are still in the page and only some of them are visible at a time. This is facilitated by the drupal_add_js method also found in previous versions. The method signature has changed the bit however. Another thing to keep in mind is the #ajax field that can be added to drupal forms which allows AJAX capabilities to be placed in the form easily.

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.

Finally we needed to tackle the integration with PayPal. This would be the final part of the registration wizard and obviously the most important one. For testing PayPal provides sandbox accounts on the developer.paypal.com. Before generating the pay button we would require to extract those values from the previous steps of the wizard. If we have used JavaScript then we have no choice but to use JavaScript to change the input parameters on the PayPal form like below

Item Details:

<input name="item_name" type="text" id="item_name" size="45">

Amount:

<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.

Conclusion

This was a general summary of how to create a simple wizard which allows user registration and payment using Drupal 7