Tuesday, 24 October 2017 17:54

Simple Drupal Webform (v4) PayPal Integration using custom PHP confirmation message

Written by 
Simple Drupal Webform (v4) PayPal Integration using custom PHP confirmation message

I needed something that would allow a client to provide name, email, invoice number, and amount.  All of the solutions I could find were either for the older version of Webform, or they were too cumbersome... too many modules required... not enough simplicity and control.  Many of the solutions would only allow for "donations" of fixed amounts. 

My solution was to adapt the solution found here (which was written for a specific scenario, also using the older version of Webform), adapt it for my needs, and recode for Webform v4.  

Note that this solution requires that you enable the "PHP Filter" module, which is a potential security issue if configured incorrectly.  

My code is as follows - also available as an attachment below:

include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$nid = arg(1); // need to hard-code nid if this is a custom page
$sid = $_GET['sid']; $submission = webform_get_submission($nid, $sid);
$invoice_numbers = $submission->data[3][0];  // 3 is the ID number of the field for invoice numbers under form components
$amount = $submission->data[4][0]; // 4 is the ID number of the amount field under form components


<li>Click "Buy Now" to finalize your registration.</li>
<li> Important: Print out your PayPal payment as a receipt.</li> </ul>

<h4> Amount: $<?php print $amount; ?></h4>

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input name="cmd" type="hidden" value="_xclick" />
<input name="business" type="hidden" value=“This email address is being protected from spambots. You need JavaScript enabled to view it.“ />
<input name="item_name" type="hidden" value="Payment on Invoice <?php print $invoice_numbers; ?>" />
<input name="item_number" type="hidden" value="SKU0123" />
<input name="amount" type="hidden" value="<?php print $amount; ?>" />
<input name="bn" type="hidden" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest" />
<input style = "width: 120px; height: auto;" alt="PayPal - The safer, easier way to pay online!" border="0" name="submit" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" type="image" />
<img alt="" border="0" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" /></form>


Read 4029 times Last modified on Tuesday, 24 October 2017 18:08
0 #2 Nate Covington 2019-10-29 13:08
@giucas - No, this is a "PayPal" thing. Most likely there IS a way for a user to pay with a guest account, but PayPal hides the button and constantly rearranges the layout. I've also seen Internet Explorer behave totally different than Firefox or Chrome. So actual mileage may vary...
0 #1 giucas 2019-06-02 12:08
Thanks for this code!
The paypal form doesn't allow guest payment (with credit card) but only via paypal account. I can't figure out why.
Maybe because the button is not wncrypted?
Add comment