/[drupal]/contributions/modules/lm_paypal/lm_paypal_donations.module
ViewVC logotype

Contents of /contributions/modules/lm_paypal/lm_paypal_donations.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.27 - (show annotations) (download) (as text)
Sat Oct 11 09:00:17 2008 UTC (13 months, 2 weeks ago) by lyricnz
Branch: MAIN
CVS Tags: DRUPAL-4-7--1-0, HEAD
Branch point for: DRUPAL-4-7, DRUPAL-6--1
Changes since 1.26: +2 -2 lines
File MIME type: text/x-php
#170041 by anarcat: fix typo in donations access denied message
1 <?php
2
3 // $Id: lm_paypal_donations.module,v 1.26 2007/06/14 00:34:55 leemcl Exp $
4
5 /**
6 * @file
7 *
8 * PayPal donations interface.
9 *
10 * Lee McLoughlin <lee@lmmrtech.com>. July 2006
11 * This is a Drupal 4.7 module to dynamically generate PayPal buttons and
12 * processes incoming PayPal IPN messages.
13 *
14 * This module is licensed under Gnu General Public License Version 2
15 * see the LICENSE.txt file for more details.
16 */
17
18 define(LM_PAYPAL_DONATIONS, 'LM_PayPal_Donat');
19
20 // Don't change these here! Use the admin interface at admin/lm_paypal
21 define(LM_PAYPAL_DONATIONS_THANKS_DEFAULT, '/lm_paypal/donations_thanks');
22
23 /**
24 * Initialize global variables
25 */
26 function _lm_paypal_donations_ini() {
27 _lm_paypal_ini();
28 global $_lm_paypal_donations_thanks; // page user is directed to on donation
29
30 static $inited = 0;
31
32 if ($inited) {
33 return;
34 }
35 $inited = 1;
36
37 $_lm_paypal_donations_thanks = variable_get('lm_paypal_donations_thanks', LM_PAYPAL_DONATIONS_THANKS_DEFAULT);
38
39 lm_paypal_web_accept_register('lm_paypal_process_in_donate', 0, 0);
40 }
41
42 /**
43 * Implementation of hook_help().
44 */
45 function lm_paypal_donations_help($section) {
46 _lm_paypal_donations_ini();
47 global $_lm_paypal_welcome; // Welcome message
48 global $base_url;
49 global $_lm_paypal_drupal_major;
50 global $_lm_paypal_drupal_minor;
51
52 if ($_lm_paypal_drupal_major > 4) {
53 // Drupal 5
54 $c = '!'; // t() has changed and to use a link in it use '!'
55 $admin = l('LM PayPal Admin', 'admin/lm_paypal/settings');
56 $access = l('access control', 'admin/user/access');
57 }
58 else {
59 // Drupal 4
60 $c = '%';
61 $admin = l('LM PayPal Admin', 'admin/settings/lm_paypal');
62 $access = l('access control', 'admin/access');
63 }
64 $help = l('LM PayPal Help', 'admin/help/lm_paypal');
65 $help_dons = l('LM PayPal Donations Help', 'admin/help/lm_paypal_donations');
66 $view_dons = l(t('PayPal Donations'), 'lm_paypal/donations');
67
68 switch ($section) {
69 case 'admin/help#lm_paypal_donations': // admin/help/lm_paypal_donations
70 $output = $_lm_paypal_welcome;
71
72 $output .= '<p>'. t('If you are not already familar with PayPal please go to there <a href="http://www.paypal.com">website</a> and read up.') .'</p>';
73
74 $output .= '<p><b>'. t('Special Note') . ':</b>';
75 $output .= '<ul>';
76 $output .= '<li><b>' . t('This module requires the module lm_paypal to be installed, enabled and configured.') . '</b></li>';
77 $output .= '</ul>';
78
79 $output .= '<p>' . t('If you are new to this module you need to:');
80 $output .= '<ul>';
81 $output .= '<li>' . t("First make sure you have configured the main ${c}admin and read ${c}help.", array("${c}admin" => $admin, "${c}help" => $help)) . '</li>';
82 $output .= '</ul>';
83 $output .= '</p>';
84 $output .= '<p>' . t("Ensure the right roles (usually everyone, so <b>anonymous</b> and <b>authenticated</b>!) can see the donation buttons by enabling this in ${c}access under <b>lm_paypal_module</b> 'access lm_paypal_donate'. You can allow roles to view everyones donations using 'view lm_paypal_all_donations'.", array("${c}access" => $access)) .'</p>';
85
86 $output .= '<h2>'. t('Creating Donation Buttons') .'</h2>';
87 $output .= '<p>'. t('Donations buttons are displayed with php like the following which display the three types of donation:') . '</p>';
88
89 $output .= '<pre>'. t('
90 &lt;?php
91 if (function_exists(\'lm_paypal_donate\')) {
92 // 10 = amount, \'USD\' is the currency followed by a description
93 print \'We would really like a $10 donation \' .
94 lm_paypal_donate(10, \'USD\', \'donation to example.com\') .\'&lt;br&gt;\';
95 // The amount is a one element array so an text input with the one value as
96 // default
97 print \'Write your own amount to give, we suggest $5\' .
98 lm_paypal_donate(array(5), \'USD\', \'donation to example.com\') . \'&lt;br&gt;\';
99 // The amount is a multi element array so a select will be used. Note if one
100 // of the elements is itself an array that will be the default selection
101 // The final two parameters are an alternative button (here we use the default)
102 // and a replacement label before the amount
103 print \'We would really like a $10, or more, donation \' .
104 lm_paypal_donate(array(5,array(10),15), \'USD\', \'donation to example.com\', \'\', \'Donation\') .\'&lt;br&gt;\';
105 }
106 ?&gt;
107 ') . '</pre>';
108 $output .= '<p>'. t('It is best to check that the lm_paypal_donate function exists before using it just in case the module has been disabled.') . '</p>';
109 $output .= '<p>'. t('If the button is clicked by a logged in user when the payment arrives the amount will be associated with them. Otherwise an attempt will be made to match the payers email with user emails.') . '</p>';
110
111 $output .= '<h2>'. t('Viewing Donations') .'</h2>';
112 $output .= '<p>'. t("To view all the donations that have arrived use ${c}view_dons.", array("${c}view_dons" => $view_dons)) . '</p>';
113
114 return $output;
115
116 // This is the brief description of the module displayed on the modules page
117 case 'admin/modules#description':
118 // New to Drupal 5 (because the page has moved)
119 case 'admin/settings/modules#description':
120 return t('Provides PayPal donation buttons (requires lm_paypal).');
121
122 // This is the brief description of the module displayed on the help page
123 case 'admin/help#lm_paypal_donations':
124 $output = '<p>'. t('The lm_paypal_donations module. ') .'</p>';
125 return $output;
126
127 // This appears at the start of the module admin page before the options
128 case 'admin/settings/lm_paypal_donations':
129 // This appears at the start of the admin page before the options
130 case 'admin/lm_paypal/donations':
131 // New to Drupal 5 - settings has moved
132 case 'admin/lm_paypal/donations_settings':
133 $output .= $_lm_paypal_welcome;
134
135 $output .= '<p>'. t("For detailed help please read ${c}help_dons", array("${c}help_dons" => $help_dons)) . '</p>';
136 return $output;
137 }
138 }
139
140 /**
141 * Implementation of hook_perm().
142 * Return a list of the access control permissions that this module defines
143 */
144 function lm_paypal_donations_perm() {
145 return array('access lm_paypal_donate', 'view lm_paypal_all_donations');
146 }
147
148 /**
149 * Implementation of hook_menu().
150 */
151 function lm_paypal_donations_menu($may_cache) {
152 _lm_paypal_donations_ini();
153 global $_lm_paypal_drupal_major;
154 global $_lm_paypal_drupal_minor;
155
156 $items = array();
157
158 if ($may_cache) {
159 if ($_lm_paypal_drupal_major > 4) {
160 // New to Drupal 5 - hook_settings gone so settings is a normal page
161 $items[] = array(
162 'path' => 'admin/lm_paypal/donations_settings',
163 'title' => t('LM PayPal Donations Settings'),
164 'callback' => 'drupal_get_form',
165 'callback arguments' => array('lm_paypal_donations_admin_settings'),
166 'access' => user_access('administer site configuration'),
167 'type' => MENU_NORMAL_ITEM,
168 'weight' => 3,
169 // New to Drupal 5 - every path has a description
170 'description' => t('PayPal donations interface configuration.'),
171 );
172 }
173
174 // admin - print all donations
175 $items[] = array(
176 'path' => 'lm_paypal/donations',
177 'title' => t('PayPal Donations'),
178 'callback' => 'lm_paypal_all_donations',
179 'access' => user_access('view lm_paypal_all_donations'),
180 // New to Drupal 5 - every path has a description
181 'description' => t('PayPal view All Donations'),
182 );
183
184 // By default we tell Paypal to redirect users here after donating
185 $items[] = array(
186 'path' => 'lm_paypal/donations_thanks',
187 'title' => t('LM PayPal Donation Thanks'),
188 'type' => MENU_CALLBACK,
189 'callback' => 'lm_paypal_donations_thanks',
190 'access' => user_access('access lm_paypal_donate'),
191 );
192 }
193
194 return $items;
195 }
196
197 // Ugly magic to hide lm_paypal_settings from Drupal 5.0 as it spots its
198 // existance and refuses to access the real page.
199 if (strncmp(VERSION, '4', 1) == 0) {
200 /**
201 * Implementation of hook_settings()
202 * Note: hook_settings not used in Drupal 5.
203 */
204 function lm_paypal_donations_settings() {
205 return lm_paypal_donations_settings_form();
206 }
207 }
208
209 /**
210 * Provide the admin settings page.
211 * Note: New to Drupal 5
212 */
213 function lm_paypal_donations_admin_settings() {
214 $form = lm_paypal_donations_settings_form();
215 return system_settings_form($form);
216 }
217
218 /**
219 * Implementation of hook_settings()
220 */
221 function lm_paypal_donations_settings_form() {
222 _lm_paypal_donations_ini();
223 global $_lm_paypal_donations_thanks;
224 global $_lm_paypal_drupal_major;
225 global $_lm_paypal_drupal_minor;
226
227 if (!user_access('administer lm_paypal')) {
228 drupal_access_denied();
229 return;
230 }
231
232 $form ['lm_paypal_donations_thanks'] = array(
233 '#type' => 'textfield',
234 '#title' => t('LM PayPal donation thanks page'),
235 '#default_value' => $_lm_paypal_donations_thanks,
236 '#maxlength' => 100,
237 '#required' => TRUE,
238 '#description' => t('The page the user is sent to by paypal after a donation. The default is %link but you might want to point it at a page you have created yourself.', array('%link' => LM_PAYPAL_DONATIONS_THANKS_DEFAULT)),
239 );
240
241 return $form;
242 }
243
244 /**
245 * Process a newly arrived donation ipn message
246 *
247 * @param $ipn
248 * The IPN message
249 * @link
250 * html link to the the IPN to display in watchdog reports
251 * @uid
252 * The uid of the user associated with this IPN, zero if the user is unknown
253 * @other
254 * The extra integer passed with the uid
255 * @item_number
256 * The PayPal item_number
257 */
258 function lm_paypal_process_in_donate($ipn, $link, $uid, $other, $item_number) {
259 _lm_paypal_donations_ini();
260 global $_lm_paypal_debug;
261
262 if ($_lm_paypal_debug) {
263 watchdog(LM_PAYPAL_DONATIONS, 'in donate');
264 }
265
266 $sql = "INSERT INTO {lm_paypal_donations} SET ";
267 $sql .= "uid = %d, ";
268 $sql .= "datepaid = %d,";
269 $sql .= "item_name = '%s',";
270 $sql .= "mc_gross = '%s',";
271 $sql .= "mc_fee = '%s',";
272 $sql .= "mc_currency = '%s',";
273 $sql .= "txn_id = '%s'";
274 $insert = db_query(
275 $sql, $uid, time(), $ipn->item_name, $ipn->mc_gross, $ipn->mc_fee, $ipn->mc_currency, $ipn->txn_id);
276 if (!$insert) {
277 watchdog(LM_PAYPAL_DONATIONS, t('Failed to add to donations (uid %uid)', array('%uid' => $uid)), WATCHDOG_ERROR);
278 }
279 }
280
281 /**
282 * Display a dynamically generated PayPal donate button.
283 *
284 * @param $amount
285 * Required.
286 * If a number it is the amount of the donation. User not asked.
287 * If a single element array then a text input will appear with
288 * this as the default value for the amount.
289 * If a multi element array then a select will appear with the given
290 * values. If one of the values is itself an array that will be the
291 * default.
292 * @param $ccc
293 * A PayPal 3 letter currency code for the donation (e.g: USD).
294 * @param $name
295 * The name of the donation, displayed on PayPal donate form
296 * @param $button_url
297 * The url of button to click on to make the donation
298 * @param $amount_label
299 * Label to put before the amount if asking the user
300 * @param $ret_url
301 * If non empty it is the url the user is returned to after the transaction
302 * @return
303 * The html string representing the button.
304 */
305
306 function lm_paypal_donate($amount = '', $ccc = '', $name = '', $button_url = '', $amount_label = '', $ret_url = '') {
307 _lm_paypal_donations_ini();
308 global $user;
309 global $_lm_paypal_debug;
310 global $_lm_paypal_host;
311 global $_lm_paypal_business;
312 global $_lm_paypal_donations_thanks;
313 global $_lm_paypal_js_hide_email;
314
315 if (!user_access('access lm_paypal_donate')) {
316 return t('Access to PayPal donation buttons denied');
317 }
318
319 if ($button_url == '') {
320 // This is the default paypal donate button
321 $button_url = 'http://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif';
322 }
323
324 $button_url = check_url($button_url);
325
326 $ccc = check_plain($ccc);
327 if (is_array($amount)) {
328 if (count($amount) == 1) {
329 $a = $amount[0];
330 $cs = lm_paypal_ccc2symbol($ccc);
331 $sa = "$cs <input name=\"amount\" size=\"3\" value=\"$a\">\n";
332 }
333 else {
334 $sa .= '<select name="amount">';
335 foreach ($amount as $ad) {
336 $def = 0;
337 if (is_array($ad)) {
338 $def = 1;
339 $a = check_plain($ad[0]);
340 }
341 else {
342 $def = 0;
343 $a = check_plain($ad);
344 }
345 $sa .= " <option value=\"$a\"";
346 if ($def)
347 $sa .= ' selected="selected"';
348 $d = lm_paypal_nccc2str($a, $ccc);
349 $sa .= ">$d</option>";
350 }
351 $sa .= '</select>';
352 }
353 }
354 else {
355 $amount = check_plain($amount);
356 }
357
358 if ($name == '' && !isset($sa)) {
359 $d = lm_paypal_nccc2str($amount, $ccc);
360 $name = t('Donate %amount', array('%amount', $d));
361 }
362
363 $name = check_plain($name);
364
365 $biz = check_plain($_lm_paypal_business);
366 if ($ret_url == '' || is_null($ret_url)) {
367 $ret_url = $_lm_paypal_donations_thanks;
368 }
369
370 if (variable_get('clean_url', 0)) {
371 $return_url = url(check_url($ret_url), null, null, TRUE);
372 }
373 else {
374 $return_url = url(null, 'q=' . check_url($ret_url), null, TRUE);
375 }
376
377 $notify_url = url('lm_paypal/ipn', null, null, TRUE);
378
379
380 // If email hiding is enabled then turn on this javascript
381 // and add an onsubmit action
382 $onsub = '';
383 if ($_lm_paypal_js_hide_email) {
384 lm_paypal_add_js();
385 $at = strpos($biz, '@');
386 $person = substr($biz, 0, $at);
387 $host = substr($biz, $at + 1, strlen($biz));
388 $biz = '';
389
390 $onsub = "onsubmit=\"lm_paypal_setbiz(this,'$person','$host')\"";
391 }
392
393 // Output a form that will redirect the user to PayPal - note all the fields
394 // are hidden so only the submit appears
395 $form = "\n<form action=\"http://$_lm_paypal_host/cgi-bin/webscr\" method=\"post\" $onsub>\n";
396
397 if (isset($sa)) {
398 if ($amount_label != '') {
399 $ta = check_plain($amount_label);
400 }
401 else {
402 $ta = t('Amount');
403 }
404 // tr valign=\"baseline\"
405 $form .= "<table ><tr align=\"center\"><td>$ta $sa</td></tr><tr align=\"center\"><td>";
406 }
407 $form .= "<input type=\"hidden\" name=\"cmd\" value=\"_xclick\">\n";
408 $form .= "<input type=\"hidden\" name=\"business\" value=\"$biz\">\n";
409 $form .= "<input type=\"hidden\" name=\"item_name\" value=\"$name\">\n";
410 $form .= "<input type=\"hidden\" name=\"notify_url\" value=\"$notify_url\">\n";
411 // item_number 0 is a donation
412 // The only downside of setting the item number is that PayPal may display
413 // it on the user form ("My item\nItem_number0")
414 //$form .= "<input type=\"hidden\" name=\"item_number\" value=\"0\">\n";
415 if (!isset($sa)) {
416 $form .= "<input type=\"hidden\" name=\"amount\" value=\"$amount\">\n";
417 }
418 $form .= "<input type=\"hidden\" name=\"no_shipping\" value=\"1\">\n";
419 $form .= "<input type=\"hidden\" name=\"return\" value=\"$return_url\">\n";
420 if ($ccc) {
421 $form .= "<input type=\"hidden\" name=\"currency_code\" value=\"$ccc\">\n";
422 }
423 $form .= "<input type=\"hidden\" name=\"no_note\" value=\"1\">\n";
424 $form .= "<input type=\"hidden\" name=\"custom\" value=\"$user->uid\">\n";
425 $form .= "<input type=\"image\" src=\"$button_url\" border=\"0\" name=\"submit\" alt=\"Make payments with PayPal - it's fast, free and secure!\">\n";
426 if (isset($sa)) {
427 $form .= "</td></tr></table>\n";
428 }
429 $form .= "</form>\n";
430
431 $output .= $form;
432 return $output;
433 }
434
435 /**
436 * Returns the default page that users are sent to by PayPal after donating.
437 *
438 * @return
439 * A string containing the page contents.
440 */
441 function lm_paypal_donations_thanks() {
442 return t('<h2>Thank you!</h2><p>If the system is not too busy then your donation will arrive shortly. At worse it will only take a few hours.</p><p>Once again, thanks!</p>');
443 }
444
445 /**
446 * Implementation of hook_user().
447 */
448 function lm_paypal_donations_user($op, &$edit, &$account, $category = NULL) {
449 _lm_paypal_donations_ini();
450 global $_lm_paypal_debug;
451
452 $uid = $account->uid;
453
454 // In the "my account" view area show all donations
455 if ($op == 'view' && (user_access('administer lm_paypal') || $user->uid == $account->uid)) {
456 $output .= lm_paypal_donations($uid, $per_page = 10);
457
458 $ditems [] = array(
459 'title' => '',
460 'value' => $output);
461 $ret_dons = array(t('Paypal Donations') => $ditems);
462
463 return $ret_dons;
464 }
465 }
466
467 /**
468 * Implementation of hook_form_alter().
469 */
470 function lm_paypal_donations_form_alter($form_id, $form) {
471 if ($form_id == 'system_modules' && !$_POST) {
472 // Check that lm_paypal is enabled
473 $me = 'lm_paypal_donations';
474 $dep = array('lm_paypal');
475 lm_paypal_system_module_validate($form,$me,$dep);
476 }
477 }
478
479 /**
480 * View all donations
481 */
482 function lm_paypal_all_donations() {
483 return lm_paypal_donations(0);
484 }
485
486 /**
487 * View donations from one user
488 */
489 function lm_paypal_my_donations($uid) {
490 return lm_paypal_donations($uid);
491 }
492
493 /**
494 * View donations
495 *
496 * @param
497 * if a uid is passed then just print out details of that donation
498 */
499 function lm_paypal_donations($uid, $per_page = 50) {
500 global $user;
501
502 if ($uid == null || !is_numeric($uid) || intval($uid) != $uid) {
503 $uid = 0;
504 }
505
506 if (!($user->uid == $uid || user_access('view lm_paypal_all_donations'))) {
507 return t('<em>You do not have permission to view donations</em>');
508 /*
509 drupal_access_denied();
510 return '';
511 */
512 }
513
514 $header = array(
515 array('data' => t('User'), 'field' => 'u.name'),
516 array('data' => t('Item Name'), 'field' => 'd.item_name'),
517 array('data' => t('Date Paid'), 'field' => 'd.datepaid', 'sort' => 'desc'),
518 array('data' => t('Amount'), 'field' => 'd.mc_gross'),
519 array('data' => t('Currency'), 'field' => 'd.mc_currency'),
520 );
521
522 $sql = "SELECT u.name, d.item_name, d.uid, d.datepaid, d.mc_gross, d.mc_currency FROM {lm_paypal_donations} d INNER JOIN {users} u ON d.uid = u.uid";
523
524 $tablesort = tablesort_sql($header);
525
526 // If not sorting by datepaid then make that the 2nd field to sort on
527 if (strpos($tablesort,'datepaid') === FALSE) {
528 $tablesort .= ', datepaid DESC';
529 }
530
531 // placeholder until I figure out what would be good to filter on
532 $status = $_SESSION['lm_paypal_dons_filter'];
533 $status = 'all';
534 if ($status != 'all') {
535 if ($uid != 0) {
536 $sql .= " WHERE d.uid = $uid AND s.status = '%s'";
537 }
538 else {
539 $sql .= " WHERE s.status = '%s'";
540 }
541 $result = pager_query($sql . $tablesort, $per_page, 0, NULL, $status);
542 }
543 else {
544 if ($uid != 0) {
545 $sql .= " WHERE d.uid = $uid";
546 }
547 $result = pager_query($sql . $tablesort, $per_page);
548 }
549
550 while ($don = db_fetch_object($result)) {
551 $rows[] = array('data' =>
552 array(
553 l(check_plain($don->name), "user/$sub->uid"),
554 check_plain($don->item_name),
555 format_date($don->datepaid, 'small'),
556 check_plain($don->mc_gross),
557 check_plain($don->mc_currency),
558 ),
559 );
560 }
561
562 if (!$rows) {
563 $rows[] = array(array('data' => t('No donations found.'), 'colspan' => 3));
564 }
565
566 $output .= theme('table', $header, $rows);
567 $output .= theme('pager', NULL, $per_page, 0);
568
569 return $output;
570 }

  ViewVC Help
Powered by ViewVC 1.1.2