Paypal payment or donation

Last modified: June 20, 2009 - 20:05

This is a simple module to accept variable amounts (e.g. payments for services, or donations) via Paypal. Multiple currencies are displayed to the user to chose from. A thank you page is displayed upon return.

Here is the info file:

<?php
name
= Payment
description
= Enables payment via Paypal
package
=
version = "$Name$"
?>

And here is the module:

<?php
// $Id$

// Copyright 2007 Khalid Baheyeldin

// Variable names
define('PAYMENT_EMAIL',          'payment_email');
define('PAYMENT_URL',            'payment_url');
define('PAYMENT_COMPLETE_URL',   'payment_complete_url');
define('PAYMENT_PAGE_HEADER',    'payment_page_header');
define('PAYMENT_PAGE_FOOTER',    'payment_page_footer');
define('PAYMENT_THANKS_TEXT',    'payment_thanks_text');

define('PAYMENT_URL_LIVE',     0);
define('PAYMENT_URL_TEST',     1);

function
payment_urls() {
  return array(
   
PAYMENT_URL_LIVE => 'https://www.paypal.com/cgi-bin/webscr',
   
PAYMENT_URL_TEST => 'https://www.eliteweaver.co.uk/cgi-bin/webscr',
  );
}

function
payment_currencies() {
  return array(
   
'USD' => t('U.S. Dollar'),
   
'CAD' => t('Canadian Dollar'),
   
'EUR' => t('Euro'),
  );
}

function
payment_menu($may_cache) {
 
$items = array();
  if (
$may_cache) {
   
$items[] = array(
     
'path'               => 'admin/settings/payment',
     
'title'              => 'Payment',
     
'description'        => 'Settings for Paypal payment.',
     
'callback'           => 'drupal_get_form',
     
'callback arguments' => array('payment_admin_settings'),
    );
   
$items[] = array(
     
'path'               => 'payment',
     
'title'              => 'Payment',
     
'callback'           => 'drupal_get_form',
     
'callback arguments' => array('payment_form'),
     
'type'               => MENU_NORMAL_ITEM,
     
'access'             => TRUE,
    );
   
$items[] = array(
     
'path'               => 'payment/thanks',
     
'title'              => 'Thank you',
     
'callback'           => 'drupal_get_form',
     
'callback arguments' => array('payment_thanks'),
    );
  }
  return
$items;
}

function
payment_admin_settings() {
 
$form[PAYMENT_EMAIL] = array(
   
'#type'          => 'textfield',
   
'#title'         => t('Email'),
   
'#description'   => t('Email address to send payments to.'),
   
'#default_value' => variable_get(PAYMENT_EMAIL, 'paypal@2bits.com'),
   
'#size'          => 40,
   
'#required'      => TRUE,
   
'#maxlength'     => 255,
  );
 
$form[PAYMENT_URL] = array(
   
'#type'          => 'select',
   
'#title'         => t('Payment URL'),
   
'#default_value' => variable_get(PAYMENT_URL, PAYMENT_URL_LIVE),
   
'#options'       => payment_urls(),
   
'#description'   => t('The Paypal URL to redirect to for payment.'),
  );
 
$form[PAYMENT_COMPLETE_URL] = array(
   
'#type'          => 'textfield',
   
'#title'         => t('Payment complete URL'),
   
'#description'   => t('The URL on your site that the user will be redirected to after they complete payment.'),
   
'#default_value' => variable_get(PAYMENT_COMPLETE_URL, 'payment/thanks'),
   
'#size'          => 40,
   
'#required'      => TRUE,
   
'#maxlength'     => 255,
  );
 
$form[PAYMENT_PAGE_HEADER] = array(
   
'#type'          => 'textarea',
   
'#title'         => t('Payment page header'),
   
'#description'   => t('HTML text to display before the form.'),
   
'#default_value' => variable_get(PAYMENT_PAGE_HEADER, '<p>Please complete the form below and then you will be directed to Paypal for completing the payment.</p>'),
  );
 
$form[PAYMENT_PAGE_FOOTER] = array(
   
'#type'          => 'textarea',
   
'#title'         => t('Payment page footer'),
   
'#description'   => t('HTML text to display after the form.'),
   
'#default_value' => variable_get(PAYMENT_PAGE_FOOTER, '<p>We thank you for your business.</p>'),
  );
 
$form[PAYMENT_THANKS_TEXT] = array(
   
'#type'          => 'textarea',
   
'#title'         => t('Thanks page'),
   
'#description'   => t('HTML text to display on the thank you page.'),
   
'#default_value' => variable_get(PAYMENT_THANKS_TEXT, '<p>Thank you for your payment.</p>'),
  );
  return
system_settings_form($form);
}

function
payment_form() {
 
$urls = payment_urls();
 
$url = $urls[variable_get(PAYMENT_URL, PAYMENT_URL_LIVE)];

 
$form['#action'] = $url;
 
$form[PAYMENT_PAGE_HEADER] = array(
   
'#value' => variable_get(PAYMENT_PAGE_HEADER, '<p>Please complete the form below and then you will be directed to Paypal for completing the payment.</p>'),
  );
 
$form['currency_code'] = array(
   
'#title'       => t('Currency'),
   
'#type'        => 'select',
   
'#name'        => 'currency_code',
   
'#options'     => payment_currencies(),
   
'#description' => t('We accept payments in these currencies.'),
  );
 
$form['amount'] = array(
   
'#type'        => 'textfield',
   
'#title'       => t('Amount'),
   
'#description' => t('Enter the amount you are paying.'),
   
'#size'        => 12,
   
'#maxlength'   => 12,
   
'#required'    => TRUE,
   
'#name'        => 'amount',
  );
 
$form['business'] = array(
   
'#type'  => 'hidden',
   
'#name'  => 'business',
   
'#value' => variable_get(PAYMENT_EMAIL, 'paypal@2bits.com'),
  );
 
$form['cmd'] = array(
   
'#type'  => 'hidden',
   
'#value' => '_xclick',
   
'#name'  => 'cmd',
  );
 
$form['item_name'] = array(
   
'#type'  => 'hidden',
   
'#value' => 'Payment for services',
   
'#name'  => 'item_name',
  );
 
$form['no_shipping'] = array(
   
'#type'  => 'hidden',
   
'#value' => 1,
   
'#name'  => 'no_shipping',
  );
 
$form['return'] = array(
   
'#type'  => 'hidden',
   
'#value' => url(variable_get(PAYMENT_COMPLETE_URL, 'payment/thanks'), NULL, NULL, TRUE),
   
'#name'  => 'return',
  );
 
$form['submit'] = array(
   
'#type'  => 'submit',
   
'#value' => t('Pay'),
   
'#name'  => 'submit',
  );
 
$form[PAYMENT_PAGE_FOOTER] = array(
   
'#value' => variable_get(PAYMENT_PAGE_FOOTER, '<p>Thank you for your business.</p>'),
  );

  return
$form;
}

function
payment_thanks() {
 
$form[PAYMENT_THANKS] = array(
   
'#value' => variable_get(PAYMENT_THANKS_TEXT, '<p>Thank you for your payment.</p>'),
  );
  return
$form;
}
?>

The PAYMENT_URL_TEST => 'https://www.eliteweaver.co.uk/cgi-bin/webscr', is for checking the payment in the test account.

 
 

Drupal is a registered trademark of Dries Buytaert.