-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaypal.module
106 lines (90 loc) · 2.68 KB
/
paypal.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
function paypal_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#paypal":
$output = '<p>'.t("Converts shorthand into PayPal code").'</p>';
break;
}
return $output;
}
/**
* Possible permissions
* @return array
*/
function paypal_perm() {
return array("use PayPal filter");
}
/**
* Our main controller
*/
function paypal_filter($op, $delta=0, $format=-1, $text='') {
switch ($op) {
case 'description':
return t('Substitutes [paypal name="product" price="$10.00"] with PayPal code');
case 'list':
return array(0=>t('PayPal filter'));
case 'no cache':
// make sure we process on every page load
return true;
case 'prepare':
return paypal_escape($text);
case 'process':
// Here is where we actually do our changes
return paypal_process($text);
}
}
/**
* Escape our paypal tag
*/
function paypal_escape($text) {
return $text;
}
/**
* Do the work here
*/
function paypal_process($text) {
$regex = '/\[paypal name="(.*?)" price="(.*?)"\]/';
if (preg_match_all($regex, $text, $matches)) {
$finds = $matches[0];
$names = $matches[1];
$prices = $matches[2];
for ($i=0; $i<count($finds);$i++) {
$find = $finds[$i];
$name = $names[$i];
$price = $prices[$i];
$replacement = paypal_get_replacement($name, $price);
$text = str_replace($find, $replacement, $text);
}
}
return $text;
}
/**
* Build the new PayPal code for a given name/price pairing
*/
function paypal_get_replacement($name, $price) {
if (!strlen(trim($name))) {
return "<p>Error: PayPal filter requires a name.</p>";
}
if (!strlen(trim($price))) {
return "<p>Error: PayPal filter requires a price.</p>";
}
if (!is_numeric($price)) {
return "<p>Error: Price must be numeric for $name.</p>";
}
return sprintf('<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<EMAIL_ADDRESS_HERE>">
<input type="hidden" name="item_name" value="%s">
<input type="hidden" name="amount" value="%d">
<input type="hidden" name="no_shipping" value="0">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image"
src="https://www.paypal.com/en_US/i/btn/btn_buynow_SM.gif" border="0"
name="submit" alt="PayPal - The safer, easier way to pay online!"><br
/>
</form>', $name, $price);
}