Several postings have asked for a fast way to take a donation from a user and process it via PayPal. The E-Commerce module has a facility to do this, but it involves a shopping cart and several additional steps for the user, thus driving down the likelihood of completing the donation process.

This example features a custom CCK donation form that dynamically generates the PayPal button once the donation parameters are submitted. The user then simply clicks the button and is off to PayPal to pay for the donation. It is assumed that the reader is familiar with the PayPal developer's requirements to process payments. If not, please go to http://developer.paypal.com. It is also assumed that the reader is familiar with CCK and computed fields. This example does provide some instruction on implementing html via php inside of a computed field. I do not profess to be proficient or professional at coding either html or php, so please accept my code with a grain of salt. It does work for me.

I have not been able to get PayPal's dynamic encryption working yet with this and will return to this post if I ever get it up and running.

Building the CCK Node:

The following fields are defined in this CCK node:

Note: all computed fields are stored.

Donation Date
field_donation_date
Datestamp

Organization
This is a hardcoded code for the organization to which the donation is to be made. This is optional in most installations, but necessary in this instance as we service multiple organizations.
field_organization
Computed field
$node_field[0]['value'] = "WFF";

Organization Name
field_organization_name
Computed field
$node_field[0]['value'] = "Wilson Freedom Federation";

User Name
field_user_name
Computed field

global $user;
$node_field[0]['value'] = $user->name;

Tax Status
In this instance, the user can choose between making a tax deductible contribution to our 501(c)(3) side of the house or a non-deductible donation to our political action wing.
field_tax_status
Text - Check Boxes/Radio Buttons

TD|Tax Deductible - Supports our educational and operational mission
ND|Non Deductible - Supports our political and legislative action organization

Donation Frequency
User can select a one time donation or a recurring donation. Note that PayPal has different calls depending on whether the payment is a one time payment or recurring (subscription) payment. It is all factored into this process.
field_donation_frequency
Text - Check Boxes/Radio Buttons

OT|One Time Donation
AN|Annual Recurring Donation
SA|Semi-Annual Recurring Donation
QT|Quarterly Recurring Donation
MO|Monthly Recurring Donation

Donation Amount
field_donation__amount (note the two underscores between donation and amount)
Text - Select List

10|$10
25|$25
50|$50
75|$75
100|$100
150|$150
250|$250

Item Name
field_item_name
Computed field
$node_field[0]['value'] = $node->field_tax_status_0[0]['value'] . " Contribution to " . $node->field_organization_name_0[0]['value'] . " | " . $node->field_donation_frequency[0]['value'];

Item Number
This is a SKU/product number to identify the donation.
field_item_number
Computed field

$node_field[0]['value'] = $node->field_organization_0[0]['value'] . "|" . $node->field_tax_status_0[0]['value'] . "|" . $node->field_donation_frequency[0]['value'] . "|" .
$node->field_donation__amount[0]['value'];

Submit to PayPal
This generates the PayPal button. Note that the field name is encrypted_paypal (from an earlier version) but it is not encrypted.
field_encrypted_paypal
Computed field

/****************************************
 *     
 * Drupal Computed Field for Paypal Donation (Unencrypted)
 * @created 3/22/07 for Drupal CCK Computed Fields 
 * @author Jim Rea <jarea at teleweb-pub dot com>
 * @version 1.0
 *
 *
 *
 ****************************************/
//
// Note: though the field name is 'field_encrypted_paypal' this field is not encrypted
//
	$item_number = $node->field_item_number[0]["value"];
	$item_name = $node->field_item_name[0]["value"];
	$amount = $node->field_donation__amount[0]["value"];
	$business = "aoetest2@djdjdjdj.com";
	$frequency = $node->field_donation_frequency[0]['value'];

	$button_data = "<form action='https://www.sandbox.paypal.com/cgi-bin/webscr' method='post'>";
	$button_data .= "<input type='image' src='https://www.sandbox.paypal.com/en_US/i/btn/x-click-but02.gif' border='0' name='submit' alt='Make payments with PayPal - it is fast, free and secure!'>";

		switch ($frequency) {
	
	case "OT";	
			$button_data .= "<input type='hidden' name='cmd' value='_xclick'>";
			$button_data .= "<input type='hidden' name='business' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='receiver_email' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='item_number' value='" . $item_number . "'>";
			$button_data .= "<input type='hidden' name='item_name' value='" . $item_name . "'>";
			$button_data .= "<input type='hidden' name='amount' value='" . $amount . "'>";
			$button_data .= "<input type='hidden' name='currency_code' value='USD'>";
			$button_data .= "<input type='hidden' name='no_shipping' value='1'>";
		break;

		case "AN";

			$button_data .= "<input type='hidden' name='cmd' value='_xclick-subscriptions'>";
			$button_data .= "<input type='hidden' name='business' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='receiver_email' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='item_number' value='" . $item_number . "'>";
			$button_data .= "<input type='hidden' name='item_name' value='" . $item_name . "'>";
			$button_data .= "<input type='hidden' name='currency_code' value='USD'>";
			$button_data .= "<input type='hidden' name='no_shipping' value='1'>";
			$button_data .= "<input type='hidden' name='a3' value='" . $amount . "'>";
			$button_data .= "<input type='hidden' name='p3' value='1'>";
			$button_data .= "<input type='hidden' name='t3' value='Y'>";
			$button_data .= "<input type='hidden' name='src' value='1'>";
			$button_data .= "<input type='hidden' name='sra' value='1'>";
			$button_data .= "<input type='hidden' name='no_note' value='1'>";
	break;

case "SA";

			$button_data .= "<input type='hidden' name='cmd' value='_xclick-subscriptions'>";
			$button_data .= "<input type='hidden' name='business' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='receiver_email' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='item_number' value='" . $item_number . "'>";
			$button_data .= "<input type='hidden' name='item_name' value='" . $item_name . "'>";
			$button_data .= "<input type='hidden' name='currency_code' value='USD'>";
			$button_data .= "<input type='hidden' name='no_shipping' value='1'>";
			$button_data .= "<input type='hidden' name='a3' value='" . $amount . "'>";
			$button_data .= "<input type='hidden' name='p3' value='6'>";
			$button_data .= "<input type='hidden' name='t3' value='M'>";
			$button_data .= "<input type='hidden' name='src' value='1'>";
			$button_data .= "<input type='hidden' name='sra' value='1'>";
			$button_data .= "<input type='hidden' name='no_note' value='1'>";
	break;
	
case "QT";

			$button_data .= "<input type='hidden' name='cmd' value='_xclick-subscriptions'>";
			$button_data .= "<input type='hidden' name='business' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='receiver_email' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='item_number' value='" . $item_number . "'>";
			$button_data .= "<input type='hidden' name='item_name' value='" . $item_name . "'>";
			$button_data .= "<input type='hidden' name='currency_code' value='USD'>";
			$button_data .= "<input type='hidden' name='no_shipping' value='1'>";
			$button_data .= "<input type='hidden' name='a3' value='" . $amount . "'>";
			$button_data .= "<input type='hidden' name='p3' value='3'>";
			$button_data .= "<input type='hidden' name='t3' value='M'>";
			$button_data .= "<input type='hidden' name='src' value='1'>";
			$button_data .= "<input type='hidden' name='sra' value='1'>";
			$button_data .= "<input type='hidden' name='no_note' value='1'>";
	break;

case "MO";

			$button_data .= "<input type='hidden' name='cmd' value='_xclick-subscriptions'>";
			$button_data .= "<input type='hidden' name='business' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='receiver_email' value='" . $business . "'>";
			$button_data .= "<input type='hidden' name='item_number' value='" . $item_number . "'>";
			$button_data .= "<input type='hidden' name='item_name' value='" . $item_name . "'>";
			$button_data .= "<input type='hidden' name='currency_code' value='USD'>";
			$button_data .= "<input type='hidden' name='no_shipping' value='1'>";
			$button_data .= "<input type='hidden' name='a3' value='" . $amount . "'>";
			$button_data .= "<input type='hidden' name='p3' value='1'>";
			$button_data .= "<input type='hidden' name='t3' value='M'>";
			$button_data .= "<input type='hidden' name='src' value='1'>";
			$button_data .= "<input type='hidden' name='sra' value='1'>";
			$button_data .= "<input type='hidden' name='no_note' value='1'>";
	break;
	
	}
	
	$button_data .= "</form>";

$node_field[0]['value'] = $button_data;

That is it. Point the user's browser to create content/donation form and (hopefully) begin taking donations.

Good luck.

Comments

Sagar Ramgade’s picture

Hi,
I want to achieve similar kind of functionality where user choose the amount to be donate.
Any update on this.

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

cliffdiver1’s picture

This is a great functionality due to its simplicity (if a lot of the code is paired down). Really, really would love to see a way for the viewer of the node, not the creator, to select the amount to be donated.

Ashford’s picture

Follow the section on the amount, but change the Field type.

Donation Amount
field_donation__amount (note the two underscores between donation and amount)
Text - Select List

When you add the field, select Text - Decimal, instead of Text - Select. After saving the field setting, you may need to Edit the field to fill in the Decimal places (2) and some other settings; such as Default: 1.00. This allows the Viewer to fill in the text box with the amount they want to donate.

jasnpast’s picture

Did you ever get Paypal's dynamic encryption functioning with this configuration? Are you now using some other means to achieve the original goal? Thanks...