-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathextension.driver.php
461 lines (417 loc) · 15.3 KB
/
extension.driver.php
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
require_once(TOOLKIT . '/class.sectionmanager.php');
require_once(TOOLKIT . '/class.entrymanager.php');
require_once(TOOLKIT . '/class.fieldmanager.php');
Class extension_paypal_payments extends Extension
{
/*-------------------------------------------------------------------------
Extension definition
-------------------------------------------------------------------------*/
public function about()
{
return array('name' => 'PayPal Payments',
'version' => '1.0.4',
'release-date' => '2011-05-01',
'author' => array('name' => 'Max Wheeler',
'website' => 'http://makenosound.com/',
'email' => '[email protected]'),
'description' => 'Allows you to process and track Website Payments Standard transactions from PayPal.'
);
}
public function uninstall()
{
# Remove tables
Symphony::Database()->query("DROP TABLE `tbl_paypalpayments_logs`");
# Remove preferences
Symphony::Configuration()->remove('paypal-payments');
Administration::instance()->saveConfig();
}
public function install()
{
# Create tables
Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `tbl_paypalpayments_logs` (
`id` int(11) unsigned NOT NULL auto_increment,
`payment_type` varchar(255) NOT NULL,
`payment_date` datetime NOT NULL,
`payment_status` varchar(255) NOT NULL,
`address_status` varchar(255) NOT NULL,
`payer_status` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`payer_email` varchar(255) NOT NULL,
`payer_id` varchar(255) NOT NULL,
`address_name` varchar(255) NOT NULL,
`address_country` varchar(255) NOT NULL,
`address_country_code` varchar(255) NOT NULL,
`address_zip` varchar(255) NOT NULL,
`address_state` varchar(255) NOT NULL,
`address_city` varchar(255) NOT NULL,
`address_street` varchar(255) NOT NULL,
`residence_country` varchar(255) NOT NULL,
`tax` decimal(10,2) NOT NULL,
`mc_currency` varchar(3) NOT NULL,
`mc_fee` decimal(10,2) NOT NULL,
`mc_gross` decimal(10,2) NOT NULL,
`txn_type` varchar(255) NOT NULL,
`txn_id` varchar(255) NOT NULL,
`notify_version` varchar(255) NOT NULL,
`invoice` varchar(255) NOT NULL,
`verify_sign` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
return true;
}
public function getSubscribedDelegates()
{
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'save_preferences'
),
array(
'page' => '/system/preferences/success/',
'delegate' => 'Save',
'callback' => 'save_preferences'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilter',
'callback' => 'add_filter_to_event_editor'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilter',
'callback' => 'add_filter_to_event_editor'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'add_filter_documentation_to_event'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'add_filter_documentation_to_event'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPreSaveFilter',
'callback' => 'check_paypal_preferences'
),
array(
'page' => '/frontend/',
'delegate' => 'EventFinalSaveFilter',
'callback' => 'process_event_data'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'append_preferences'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendParamsResolve',
'callback' => 'addPaypalParams'
),
);
}
/*-------------------------------------------------------------------------
Append Paypal Params
-------------------------------------------------------------------------*/
public function addPaypalParams(array $context = null) {
$context['params']['paypal-url'] = $this->_build_paypay_url();
$context['params']['paypal-business'] = $this->_get_paypal_business();
}
/*-------------------------------------------------------------------------
Preferences
-------------------------------------------------------------------------*/
public function append_preferences($context)
{
# Add new fieldset
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', 'PayPal Payments'));
# Add Sandbox Merchant Email field
$label = Widget::Label('Sandbox Email/Account ID');
$label->appendChild(Widget::Input('settings[paypal-payments][sandbox-business]', General::Sanitize($this->_get_paypal_business())));
$group->appendChild($label);
$group->appendChild(new XMLElement('p', 'The merchant email address or account ID of the sandbox payment recipient.', array('class' => 'help')));
# Add Merchant Email field
$label = Widget::Label('Merchant Email/Account ID');
$label->appendChild(Widget::Input('settings[paypal-payments][business]', General::Sanitize($this->_get_paypal_business())));
$group->appendChild($label);
$group->appendChild(new XMLElement('p', 'The merchant email address or account ID of the payment recipient.', array('class' => 'help')));
# Country <select>
$countries = array(
'Australia',
'United Kingdom',
'United States',
);
$selected_country = $this->_get_country();
foreach ($countries as $country)
{
$selected = ($country == $selected_country) ? TRUE : FALSE;
$options[] = array($country, $selected);
}
$label = Widget::Label();
$select = Widget::Select('settings[paypal-payments][country]', $options);
$label->setValue('PayPal Country' . $select->generate());
$group->appendChild($label);
$group->appendChild(new XMLElement('p', 'Country you want to target.', array('class' => 'help')));
# Sandbox
$label = Widget::Label();
$input = Widget::Input('settings[paypal-payments][sandbox]', 'yes', 'checkbox');
if(Symphony::Configuration()->get('sandbox', 'paypal-payments') == 'yes') $input->setAttribute('checked', 'checked');
$label->setValue($input->generate() . ' Enable testing mode');
$group->appendChild($label);
$group->appendChild(new XMLElement('p', 'Directs payments to PayPal’s Sandbox: <code>https://www.sandbox.paypal.com/</code>', array('class' => 'help')));
$context['wrapper']->appendChild($group);
}
public function save_preferences($context)
{
if( ! isset($context['settings']['paypal-payments']['sandbox']))
{
$context['settings']['paypal-payments']['sandbox'] = 'no';
}
}
/*-------------------------------------------------------------------------
Navigation
-------------------------------------------------------------------------*/
public function fetchNavigation()
{
$nav = array();
$nav[] = array(
'location' => 261,
'name' => 'PayPal Payments',
'type' => 'content',
'children' => array(
array(
'name' => 'Transactions',
'link' => '/logs/',
'limit' => 'developer',
)
)
);
return $nav;
}
/*-------------------------------------------------------------------------
Helpers
-------------------------------------------------------------------------*/
private function _get_paypal_business()
{
if ($this->_sandbox_enabled()){
return Symphony::Configuration()->get('sandbox-business', 'paypal-payments');
} else {
return Symphony::Configuration()->get('business', 'paypal-payments');
}
}
private function _sandbox_enabled()
{
return (Symphony::Configuration()->get('sandbox', 'paypal-payments') == 'yes') ? TRUE : FALSE;
}
private function _get_country()
{
$country = Symphony::Configuration()->get('country', 'paypal-payments');
return (isset($country)) ? $country : 'United States';
}
public function _build_paypay_url($default = false)
{
$countries_tld = array(
'Australia' => 'com.au',
'United Kingdom' => 'co.uk',
'United States' => 'com',
);
if ($this->_sandbox_enabled()) {
$url = 'https://www.sandbox.paypal.com';
} else if ($default == true) {
$url = 'https://www.paypal.' . $countries_tld['United States'];
} else {
$url = 'https://www.paypal.' . $countries_tld[$this->_get_country()];
}
$url .= '/cgi-bin/webscr';
return $url;
}
public function _count_logs()
{
return (integer)Symphony::Database()->fetchVar('total', 0, "
SELECT
COUNT(l.id) AS `total`
FROM
`tbl_paypalpayments_logs` AS l
");
}
public function _get_logs_by_page($page, $per_page)
{
$start = ($page - 1) * $per_page;
return Symphony::Database()->fetch("
SELECT
l.*
FROM
`tbl_paypalpayments_logs` AS l
ORDER BY
l.payment_date DESC
LIMIT {$start}, {$per_page}
");
}
public function _get_logs()
{
return Symphony::Database()->fetch("
SELECT
l.*
FROM
`tbl_paypalpayments_logs` AS l
ORDER BY
l.payment_date DESC
");
}
public function _get_log($log_id) {
return Symphony::Database()->fetchRow(0, "
SELECT
l.*
FROM
`tbl_paypalpayments_logs` AS l
WHERE
l.id = '{$log_id}'
LIMIT 1
");
}
/*-------------------------------------------------------------------------
Filters
-------------------------------------------------------------------------*/
public function add_filter_to_event_editor(&$context)
{
$context['options'][] = array('paypal-payments', @in_array('paypal-payments', $context['selected']) ,'PayPal Payments: Reroute to PayPal');
}
public function add_filter_documentation_to_event($context)
{
if ( $context['selected'] && in_array('paypal-payments', $context['selected']))
{
$context['documentation'][] = new XMLElement('h3', 'PayPal Payments: Reroute to PayPal');
$context['documentation'][] = new XMLElement('p', 'You can pass data to PayPal’s server by mapping fields to most of the variables/fields listed in <a href="https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_Appx_websitestandard_htmlvariables">Website Payments Standard documentation</a>. The example below shows how you would map <code>amount</code>, <code>first-name</code>/<code>last-name</code> and <code>description</code> to their PayPal equivalents:');
$code = '<input name="fields[amount]" type="text" />
<input name="fields[first-name]" type="text" />
<input name="fields[last-name]" type="text" />
<textarea name="fields[description]"></textarea>
<input name="paypal-payments[cmd]" value="_xclick" type="hidden" />
<input name="paypal-payments[notify_url]" value="{$root}/paypal/" type="hidden" />
<input name="paypal-payments[amount]" value="amount" type="hidden" />
<input name="paypal-payments[name]" value="first-name,last-name" type="hidden" />
<input name="paypal-payments[item_name]" value="description" type="hidden" />
';
$context['documentation'][] = contentBlueprintsEvents::processDocumentationCode($code);
$context['documentation'][] = new XMLElement('p', 'Note that the <code>id</code> of the newly created entry will be automatically passed to PayPal as the <code>invoice</code>. Multiple fields can be mapped by separating them with commas, they will be joined with a space. All field mappings are optional.');
}
}
public function check_paypal_preferences(&$context)
{
if ( ! in_array('paypal-payments', $context['event']->eParamFILTERS)) return;
$business = $this->_get_paypal_business();
if( ! isset($business))
{
$context['messages'][] = array(
'paypal-payments',
FALSE,
'You need to set the your business ID/email in the preferences.'
);
return;
}
}
public function process_event_data($context)
{
# Check if in included filters
if ( ! in_array('paypal-payments', $context['event']->eParamFILTERS)) return;
# Allowed fields
$allowed_fields = array(
'cmd', 'notify_url',
'bn', 'amount',
'item_name', 'item_number',
'quantity', 'undefined_ quantity',
'weight', 'weight_unit',
'on0', 'on1',
'os0', 'os1',
'address_override', 'currency_code',
'custom', 'handling',
'invoice', 'shipping',
'shipping2', 'tax',
'tax_cart', 'weight_cart',
'weight_unit', 'business',
'return', 'rm',
'cancel_return', 'a1',
'p1', 't1',
'a2', 'p2',
't2', 'a3',
'p3', 't3',
'src', 'srt',
'sra', 'no_note',
'custom', 'usr_manage',
'cs', 'currency_code',
'modify', 'lc',
'page_style', 'cbt',
'cn', 'cpp_header_image',
'cpp_headerback_color', 'cpp_headerborder_color',
'cpp_payflow_color', 'image_url',
'no_shipping', 'address1',
'address2', 'city',
'country', 'first_name',
'last_name', 'lc',
'night_phone_a', 'night_phone_b',
'night_phone_c', 'state',
'zip',
);
# Set the default dataset
$data = array(
'invoice' => $context['entry']->get('id'),
'business' => $this->_get_paypal_business()
);
$mapping = $_POST['paypal-payments'];
if ( isset($mapping))
{
foreach ($mapping as $key => $val)
{
# Check the field is allowed
if( ! in_array($key, $allowed_fields)) continue;
# Join multiple fields
if (strpos($val, ","))
{
$values = explode(",", $val);
$combo = array();
foreach ($values as $val)
{
if (preg_match("/^'[^']+'$/", $val)) $combo[] = trim($val, "'");
else $combo[] = isset($context['fields'][$val]) ? $context['fields'][$val] : $val;
}
$mapping[$key] = implode(" ", $combo);
} else {
# If there's a match, map the value of the match. Else output the value.
if (preg_match("/^'[^']+'$/", $val)) $mapping[$key] = trim($val, "'");
else $mapping[$key] = isset($context['fields'][$val]) ? $context['fields'][$val] : $val;
}
}
$data = array_merge($data, $mapping);
}
# Figure out URL
$url = $this->_build_paypay_url();
# Build up faker HTML output
$output = '<html><head><title>Continue to PayPal</title>
<style type="text/css">button{background:#eee;border:3px #ccc solid;color:#444;display:block;font:normal 200%/1.4 Georgia, Palatino, serif;margin:19% auto 40px;padding:20px 40px;-moz-border-radius:30px;-webkit-border-radius:30px;cursor:pointer;}button:hover{background:#444;color:#fff;border-color:#222;}</style>
<script type="text/javascript">
document.write(\'<style type="text/css">button{display:none}</style>\');
</script></head>
<body onload="document.forms.paypal.submit();">
<form id="paypal" method="post" action="'.$url.'">';
foreach($data as $field => $value)
{
$output .= ' <input type="hidden" name="' . $field .'" value="' . $value . '"/>' . "\n";
}
$output .= '<button type="submit">Continue to PayPal to make payment</button>
</form>
</body>
</html>';
header('Content-Type: text/html');
print($output);
exit();
}
}