How to Add a PayPal Button to your Drupal Webform

Recently, a client of mine asked me to add a PayPal button to his website. He wanted to add a sign up form for a Trial Membership along with a PayPal button to make a small payment. So I searched on and found some custom PHP code that works very well. You will need to have an active PayPal account and some knowledge of PHP programming to follow this tutorial.

Things to Do:
1. Choose an email address and sign up for an account at PayPal so you can receive credit card payments online.
2. Go into your PayPal Account settings and set the correct currency for the payment.
3. Write down the following:
Item Name: 
Item Number:
For our example, we used Trial Membership for Item Name and SKU012 for Item Number.

On Your Drupal Website:
4. Create your webform in Drupal with a Student name, Item cost, Phone number and other fields you need.
5. Write down the machine names for your  fields, Student Name and Item Cost. These will be the variable names in the custom PHP code shown below. Replace $name_of_student and $trial_class_cost with your variable names.
6. Edit the Student Name form field and look at the URL. You will find the component number listed there.
For example, for the Trial Class Cost field, the URL is node/104/webform/components/9?destination=node%2F104%2Fwebform and the component number is 9. This goes here:
$trial_class_cost = $submission->data[9]['value'][0];
Be sure to put the correct component numbers for Student Name and Trial Class Cost
7. Add the following code to the Confirmation Message textarea. Be sure to disable your WYSIWYG editor for this textarea. CKeditor will turn the PHP code in your HTML form to garbage code.

<?php include_once(drupal_get_path('module', 'webform') .'/includes/');
$nid = arg(1); // need to hard-code nid if this is a custom page
$sid = $_GET['sid']; $submission = webform_get_submission($nid, $sid);
$name_of_student = $submission->data[1]['value'][0];
$thanks = $name_of_student;
$trial_class_cost = $submission->data[9]['value'][0];
$total_to_pay = $trial_class_cost; ?>

<h2> Thank you <?php print $thanks ?> for submitting your registration.</h2>

<ul> <li class="trial-pay-text">Click "Buy Now" to finalize your registration.</li>
<li class="trial-pay-text"> Important: Print out your PayPal payment as a receipt.</li> </ul>

<h4> Trial Membership (including HST): $<?php print $total_to_pay; ?></h4>

<ul> <li> <form action="" method="post">
<input name="cmd" type="hidden" value="_xclick" />
<input name="business" type="hidden" value="" />
<input name="item_name" type="hidden" value="Trial Membership" />
<input name="item_number" type="hidden" value="SKU012" />
<input name="amount" type="hidden" value="<?php print $total_to_pay; ?>" />
<input name="bn" type="hidden" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest" />
<input alt="PayPal - The safer, easier way to pay online!" border="0" name="submit" src="" type="image" />
<img alt="" border="0" height="1" src="" width="1" /></form> </li> </ul>
Let me know if you have any questions.


Thanks - very handy! Saved me

Thanks - very handy! Saved me quite a bit of time.

Thanks first time I knew this

Thanks first time I knew this


People like you are what make working with Drupal a pleasure

Thank you

That worked on my Drupal webform except in Drupal 7, the arrays look like: $event_title = $submission->data[12]['value'][0];


Thank you for letting us know how we can apply this code to our Drupal 7 websites.