Passed
Push — develop-3.2.x ( 670eeb...f156e7 )
by Mario
02:24
created

do_transactions_actions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller;
12
13
use phpbb\auth\auth;
14
use phpbb\config\config;
15
use phpbb\language\language;
16
use phpbb\log\log;
17
use phpbb\request\request;
18
use phpbb\template\template;
19
use phpbb\user;
20
use skouat\ppde\actions\core;
21
use skouat\ppde\actions\currency;
22
use skouat\ppde\exception\transaction_exception;
23
use skouat\ppde\operators\transactions;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
26
/**
27
 * @property config             config             Config object
28
 * @property ContainerInterface container          Service container interface
29
 * @property string             id_prefix_name     Prefix name for identifier in the URL
30
 * @property string             lang_key_prefix    Prefix for the messages thrown by exceptions
31
 * @property language           language           Language user object
32
 * @property log                log                The phpBB log system.
33
 * @property string             module_name        Name of the module currently used
34
 * @property request            request            Request object.
35
 * @property bool               submit             State of submit $_POST variable
36
 * @property template           template           Template object
37
 * @property string             u_action           Action URL
38
 * @property user               user               User object.
39
 */
40
class admin_transactions_controller extends admin_main
41
{
42
	public $ppde_operator;
43
	protected $adm_relative_path;
44
	protected $auth;
45
	protected $entry_count;
46
	protected $last_page_offset;
47
	protected $php_ext;
48
	protected $phpbb_admin_path;
49
	protected $phpbb_root_path;
50
	protected $ppde_actions;
51
	protected $ppde_actions_currency;
52
	protected $table_prefix;
53
	protected $table_ppde_transactions;
54
55
	/**
56
	 * Constructor
57
	 *
58
	 * @param auth               $auth                       Authentication object
59
	 * @param config             $config                     Config object
60
	 * @param ContainerInterface $container                  Service container interface
61
	 * @param language           $language                   Language user object
62
	 * @param log                $log                        The phpBB log system
63
	 * @param core               $ppde_actions               PPDE actions object
64
	 * @param currency           $ppde_actions_currency      PPDE currency actions object
65
	 * @param transactions       $ppde_operator_transactions Operator object
66
	 * @param request            $request                    Request object
67
	 * @param template           $template                   Template object
68
	 * @param user               $user                       User object.
69
	 * @param string             $adm_relative_path          phpBB admin relative path
70
	 * @param string             $phpbb_root_path            phpBB root path
71
	 * @param string             $php_ext                    phpEx
72
	 * @param string             $table_prefix               The table prefix
73
	 * @param string             $table_ppde_transactions    Name of the table used to store data
74
	 *
75
	 * @access public
76
	 */
77
	public function __construct(auth $auth, config $config, ContainerInterface $container, language $language, log $log, core $ppde_actions, currency $ppde_actions_currency, transactions $ppde_operator_transactions, request $request, template $template, user $user, $adm_relative_path, $phpbb_root_path, $php_ext, $table_prefix, $table_ppde_transactions)
78
	{
79
		$this->auth = $auth;
80
		$this->config = $config;
81
		$this->container = $container;
82
		$this->language = $language;
83
		$this->log = $log;
84
		$this->ppde_actions = $ppde_actions;
85
		$this->ppde_actions_currency = $ppde_actions_currency;
86
		$this->ppde_operator = $ppde_operator_transactions;
87
		$this->request = $request;
88
		$this->template = $template;
89
		$this->user = $user;
90
		$this->adm_relative_path = $adm_relative_path;
91
		$this->phpbb_admin_path = $phpbb_root_path . $adm_relative_path;
92
		$this->phpbb_root_path = $phpbb_root_path;
93
		$this->php_ext = $php_ext;
94
		$this->table_prefix = $table_prefix;
95
		$this->table_ppde_transactions = $table_ppde_transactions;
96
		parent::__construct(
97
			'transactions',
98
			'PPDE_DT',
99
			'transaction'
100
		);
101
	}
102
103
	/**
104
	 * Display the transactions list
105
	 *
106
	 * @param string $id     Module id
107
	 * @param string $mode   Module categorie
108
	 * @param string $action Action name
109
	 *
110
	 * @return void
111
	 * @access public
112
	 */
113
	public function display_transactions($id, $mode, $action)
114
	{
115
		// Set up general vars
116
		$args = array();
117
		$start = $this->request->variable('start', 0);
118
		$deletemark = $this->request->is_set('delmarked') ? $this->request->variable('delmarked', false) : false;
119
		$deleteall = $this->request->is_set('delall') ? $this->request->variable('delall', false) : false;
120
		$marked = $this->request->variable('mark', array(0));
121
		$txn_approve = $this->request->is_set('approve');
122
		$txn_approved = $this->request->variable('txn_errors_approved', 0);
123
		$txn_add = $this->request->is_set('add');
124
		// Sort keys
125
		$sort_days = $this->request->variable('st', 0);
126
		$sort_key = $this->request->variable('sk', 't');
127
		$sort_dir = $this->request->variable('sd', 'd');
128
129
		// Prepares args for entries deletion
130
		if (($deletemark || $deleteall) && $this->auth->acl_get('a_ppde_manage'))
131
		{
132
			$action = 'delete';
133
			$args = array(
134
				'hidden_fields' => array(
135
					'start'     => $start,
136
					'delall'    => $deleteall,
137
					'delmarked' => $deletemark,
138
					'mark'      => $marked,
139
					'st'        => $sort_days,
140
					'sk'        => $sort_key,
141
					'sd'        => $sort_dir,
142
					'i'         => $id,
143
					'mode'      => $mode,
144
				),
145
			);
146
		}
147
148
		if ($txn_approve)
149
		{
150
			$transaction_id = $this->request->variable('id', 0);
151
			$action = 'approve';
152
			$args = array(
153
				'hidden_fields' => array(
154
					'approve'             => true,
155
					'id'                  => $transaction_id,
156
					'txn_errors_approved' => $txn_approved,
157
				),
158
			);
159
		}
160
161
		if ($txn_add)
162
		{
163
			$action = 'add';
164
		}
165
166
		$action = $this->do_action($action, $args);
167
168
		if (!$action)
169
		{
170
			/** @type \phpbb\pagination $pagination */
171
			$pagination = $this->container->get('pagination');
172
173
			// Sorting
174
			$limit_days = array(0 => $this->language->lang('ALL_ENTRIES'), 1 => $this->language->lang('1_DAY'), 7 => $this->language->lang('7_DAYS'), 14 => $this->language->lang('2_WEEKS'), 30 => $this->language->lang('1_MONTH'), 90 => $this->language->lang('3_MONTHS'), 180 => $this->language->lang('6_MONTHS'), 365 => $this->language->lang('1_YEAR'));
175
			$sort_by_text = array('txn' => $this->language->lang('PPDE_DT_SORT_TXN_ID'), 'u' => $this->language->lang('PPDE_DT_SORT_DONORS'), 'ipn' => $this->language->lang('PPDE_DT_SORT_IPN_STATUS'), 'ipn_test' => $this->language->lang('PPDE_DT_SORT_IPN_TYPE'), 'ps' => $this->language->lang('PPDE_DT_SORT_PAYMENT_STATUS'), 't' => $this->language->lang('SORT_DATE'));
176
			$sort_by_sql = array('txn' => 'txn.txn_id', 'u' => 'u.username_clean', 'ipn' => 'txn.confirmed', 'ipn_test' => 'txn.test_ipn', 'ps' => 'txn.payment_status', 't' => 'txn.payment_date');
177
178
			$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
179
			gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
180
181
			// Define where and sort sql for use in displaying transactions
182
			$sql_where = ($sort_days) ? (time() - ($sort_days * 86400)) : 0;
183
			$sql_sort = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
184
185
			$keywords = $this->request->variable('keywords', '', true);
186
			$keywords_param = !empty($keywords) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($keywords)) : '';
187
188
			// Grab log data
189
			$log_data = array();
190
			$log_count = 0;
191
192
			$this->view_txn_log($log_data, $log_count, (int) $this->config['topics_per_page'], $start, $sql_where, $sql_sort, $keywords);
193
194
			$base_url = $this->u_action . '&amp;' . $u_sort_param . $keywords_param;
195
			$pagination->generate_template_pagination($base_url, 'pagination', 'start', $log_count, (int) $this->config['topics_per_page'], $start);
196
197
			$this->template->assign_vars(array(
198
				'S_CLEARLOGS'  => $this->auth->acl_get('a_ppde_manage'),
199
				'S_KEYWORDS'   => $keywords,
200
				'S_LIMIT_DAYS' => $s_limit_days,
201
				'S_SORT_KEY'   => $s_sort_key,
202
				'S_SORT_DIR'   => $s_sort_dir,
203
				'S_TXN'        => $mode,
204
				'U_ACTION'     => $this->u_action . '&amp;' . $u_sort_param . $keywords_param . '&amp;start=' . $start,
205
			));
206
207
			array_map(array($this, 'display_log_assign_template_vars'), $log_data);
208
		}
209
	}
210
211
	/**
212
	 * Do action regarding the value of $action
213
	 *
214
	 * @param string $action Requested action
215
	 * @param array  $args   Arguments required for the action
216
	 *
217
	 * @return string
218
	 * @access private
219
	 */
220
	private function do_action($action, $args)
221
	{
222
		// Initiate an entity
223
		/** @type \skouat\ppde\entity\transactions $entity */
224
		$entity = $this->get_container_entity();
225
226
		switch ($action)
227
		{
228
			case 'view':
229
				// Request Identifier of the transaction
230
				$transaction_id = $this->request->variable('id', 0);
231
232
				// add field username to the table schema needed by entity->import()
233
				$additional_table_schema = array(
234
					'item_username'     => array('name' => 'username', 'type' => 'string'),
235
					'item_user_colour'  => array('name' => 'user_colour', 'type' => 'string'),
236
				);
237
238
				// Grab transaction data
239
				$data_ary = $entity->get_data($this->ppde_operator->build_sql_data($transaction_id), $additional_table_schema);
240
241
				array_map(array($this, 'action_assign_template_vars'), $data_ary);
242
243
				$this->template->assign_vars(array(
244
					'U_ACTION' => $this->u_action,
245
					'U_BACK'   => $this->u_action,
246
					'S_VIEW'   => true,
247
				));
248
			break;
249
			case 'delete':
250
				if (confirm_box(true))
251
				{
252
					$where_sql = '';
253
254
					if ($args['hidden_fields']['delmarked'] && count($args['hidden_fields']['mark']))
255
					{
256
						$where_sql = $this->ppde_operator->build_marked_where_sql($args['hidden_fields']['mark']);
257
					}
258
259
					if ($where_sql || $args['hidden_fields']['delall'])
260
					{
261
						$entity->delete(0, '', $where_sql, $args['hidden_fields']['delall']);
262
						$this->ppde_actions->set_ipn_test_properties(true);
263
						$this->ppde_actions->update_overview_stats();
264
						$this->ppde_actions->set_ipn_test_properties(false);
265
						$this->ppde_actions->update_overview_stats();
266
						$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_PURGED', time());
267
					}
268
				}
269
				else
270
				{
271
					confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields($args['hidden_fields']));
272
				}
273
				// Clear $action status
274
				$action = '';
275
			break;
276
			case 'approve':
277
				if (confirm_box(true))
278
				{
279
					$transaction_id = (int) $args['hidden_fields']['id'];
280
					$txn_approved = !empty($args['hidden_fields']['txn_errors_approved']) ? false : true;
281
282
					// Update DB record
283
					$entity->load($transaction_id);
284
					$entity->set_txn_errors_approved($txn_approved);
285
					$entity->save(false);
286
287
					// Prepare transaction settings before doing actions
288
					$this->ppde_actions->set_transaction_data($entity->get_data($this->ppde_operator->build_sql_data($transaction_id)));
289
					$this->ppde_actions->set_ipn_test_properties($entity->get_test_ipn());
290
					$this->ppde_actions->is_donor_is_member();
291
292
					$this->do_transactions_actions(!$this->ppde_actions->get_ipn_test() && $this->ppde_actions->get_donor_is_member());
293
294
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED', time());
295
				}
296
				else
297
				{
298
					confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields($args['hidden_fields']));
299
				}
300
				// Clear $action status
301
				$action = '';
302
			break;
303
			case 'add':
304
				$errors = array();
305
306
				$transaction_data = $this->request_transaction_vars();
307
308
				if ($this->request->is_set_post('submit'))
309
				{
310
					try
311
					{
312
						$data_ary = $this->build_data_ary($transaction_data);
313
314
						$this->ppde_actions->log_to_db($data_ary);
315
316
						// Prepare transaction settings before doing actions
317
						$this->ppde_actions->set_transaction_data($transaction_data);
318
						$this->ppde_actions->is_donor_is_member();
319
320
						$this->do_transactions_actions($this->ppde_actions->get_donor_is_member() && !$transaction_data['MT_ANONYMOUS']);
321
322
						$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_MT_ADDED', time(), array($transaction_data['MT_USERNAME']));
323
						trigger_error($this->language->lang('PPDE_MT_ADDED') . adm_back_link($this->u_action));
324
					}
325
					catch (transaction_exception $e)
326
					{
327
						$errors = $e->get_errors();
328
					}
329
				}
330
331
				$this->ppde_actions_currency->build_currency_select_menu((int) $this->config['ppde_default_currency']);
332
333
				$this->s_error_assign_template_vars($errors);
334
335
				$this->template->assign_vars($transaction_data);
336
337
				$this->template->assign_vars(array(
338
					'U_ACTION'             => $this->u_action,
339
					'U_BACK'               => $this->u_action,
340
					'S_ADD'                => true,
341
					'ANONYMOUS_USER_ID'    => ANONYMOUS,
342
					'U_FIND_USERNAME'      => append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=searchuser&amp;form=manual_transaction&amp;field=username&amp;select_single=true'),
343
					'PAYMENT_TIME_FORMATS' => $this->get_payment_time_examples(),
344
				));
345
			break;
346
		}
347
348
		return $action;
349
	}
350
351
	/**
352
	 * Does actions for validated transaction
353
	 *
354
	 * @param bool $is_member
355
	 *
356
	 * @return void
357
	 * @access private
358
	 */
359
	private function do_transactions_actions($is_member)
360
	{
361
		$this->ppde_actions->update_overview_stats();
362
		$this->ppde_actions->update_raised_amount();
363
364
		if ($is_member)
365
		{
366
			$this->ppde_actions->update_donor_stats();
367
			$this->ppde_actions->donors_group_user_add();
368
			$this->ppde_actions->notification->notify_donor_donation_received();
369
		}
370
	}
371
372
	/**
373
	 * Returns requested data from manual transaction form
374
	 *
375
	 * @return array
376
	 * @access private
377
	 */
378
	private function request_transaction_vars()
379
	{
380
		return array(
381
			'MT_ANONYMOUS'          => $this->request->is_set('u'),
382
			'MT_USERNAME'           => $this->request->variable('username', '', true),
383
			'MT_FIRST_NAME'         => $this->request->variable('first_name', '', true),
384
			'MT_LAST_NAME'          => $this->request->variable('last_name', '', true),
385
			'MT_PAYER_EMAIL'        => $this->request->variable('payer_email', '', true),
386
			'MT_RESIDENCE_COUNTRY'  => $this->request->variable('residence_country', ''),
387
			'MT_MC_GROSS'           => $this->request->variable('mc_gross', (float) 0),
388
			'MT_MC_CURRENCY'        => $this->request->variable('mc_currency', ''),
389
			'MT_MC_FEE'             => $this->request->variable('mc_fee', (float) 0),
390
			'MT_PAYMENT_DATE_YEAR'  => $this->request->variable('payment_date_year', (int) $this->user->format_date(time(), 'Y')),
391
			'MT_PAYMENT_DATE_MONTH' => $this->request->variable('payment_date_month', (int) $this->user->format_date(time(), 'n')),
392
			'MT_PAYMENT_DATE_DAY'   => $this->request->variable('payment_date_day', (int) $this->user->format_date(time(), 'j')),
393
			'MT_PAYMENT_TIME'       => $this->request->variable('payment_time', $this->user->format_date(time(), 'H:i:s')),
394
			'MT_MEMO'               => $this->request->variable('memo', '', true),
395
		);
396
	}
397
398
	/**
399
	 * Returns a list of valid times that the user can provide in the manual transaction form
400
	 *
401
	 * @return array Array of strings representing the current time, each in a different format
402
	 * @access private
403
	 */
404
	private function get_payment_time_examples()
405
	{
406
		$formats = array(
407
			'H:i:s',
408
			'G:i',
409
			'h:i:s a',
410
			'g:i A',
411
		);
412
413
		$examples = array();
414
415
		foreach ($formats as $format)
416
		{
417
			$examples[] = $this->user->format_date(time(), $format);
418
		}
419
420
		return $examples;
421
	}
422
423
	/**
424
	 * View log
425
	 *
426
	 * @param array  &$log         The result array with the logs
427
	 * @param mixed  &$log_count   If $log_count is set to false, we will skip counting all entries in the
428
	 *                             database. Otherwise an integer with the number of total matching entries is returned.
429
	 * @param int     $limit       Limit the number of entries that are returned
430
	 * @param int     $offset      Offset when fetching the log entries, f.e. when paginating
431
	 * @param int     $limit_days
432
	 * @param string  $sort_by     SQL order option, e.g. 'l.log_time DESC'
433
	 * @param string  $keywords    Will only return log entries that have the keywords in log_operation or log_data
434
	 *
435
	 * @return int Returns the offset of the last valid page, if the specified offset was invalid (too high)
436
	 * @access private
437
	 */
438
	private function view_txn_log(&$log, &$log_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'txn.payment_date DESC', $keywords = '')
439
	{
440
		$count_logs = ($log_count !== false);
441
442
		$log = $this->get_logs($count_logs, $limit, $offset, $limit_days, $sort_by, $keywords);
443
		$log_count = $this->get_log_count();
444
445
		return $this->get_valid_offset();
446
	}
447
448
	/**
449
	 * @param bool   $count_logs
450
	 * @param int    $limit
451
	 * @param int    $offset
452
	 * @param int    $log_time
453
	 * @param string $sort_by
454
	 * @param string $keywords
455
	 *
456
	 * @return array $log
457
	 * @access private
458
	 */
459
	private function get_logs($count_logs = true, $limit = 0, $offset = 0, $log_time = 0, $sort_by = 'txn.payment_date DESC', $keywords = '')
460
	{
461
		$this->entry_count = 0;
462
		$this->last_page_offset = $offset;
463
		$url_ary = array();
464
465
		if ($this->get_container_entity()->is_in_admin() && $this->phpbb_admin_path)
466
		{
467
			$url_ary['profile_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&amp;mode=overview');
468
			$url_ary['txn_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=-skouat-ppde-acp-ppde_module&amp;mode=transactions');
469
470
		}
471
		else
472
		{
473
			$url_ary['profile_url'] = append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=viewprofile');
474
			$url_ary['txn_url'] = '';
475
		}
476
477
		$get_logs_sql_ary = $this->ppde_operator->get_logs_sql_ary($keywords, $sort_by, $log_time);
478
479
		if ($count_logs)
480
		{
481
			$this->entry_count = $this->ppde_operator->query_sql_count($get_logs_sql_ary, 'txn.transaction_id');
482
483
			if ($this->entry_count == 0)
484
			{
485
				// Save the queries, because there are no logs to display
486
				$this->last_page_offset = 0;
487
488
				return array();
489
			}
490
491
			// Return the user to the last page that is valid
492
			while ($this->last_page_offset >= $this->entry_count)
493
			{
494
				$this->last_page_offset = max(0, $this->last_page_offset - $limit);
495
			}
496
		}
497
498
		return $this->ppde_operator->build_log_ary($get_logs_sql_ary, $url_ary, $limit, $this->last_page_offset);
499
	}
500
501
	/**
502
	 * @return integer
503
	 */
504
	public function get_log_count()
505
	{
506
		return ($this->entry_count) ? (int) $this->entry_count : 0;
507
	}
508
509
	/**
510
	 * @return integer
511
	 */
512
	public function get_valid_offset()
513
	{
514
		return ($this->last_page_offset) ? (int) $this->last_page_offset : 0;
515
	}
516
517
	/**
518
	 * Prepare data array() before send it to $entity
519
	 *
520
	 * @param array $transaction_data
521
	 *
522
	 * @return array
523
	 * @throws transaction_exception
524
	 */
525
	private function build_data_ary($transaction_data)
526
	{
527
		$errors = array();
528
529
		if ($this->request->is_set('u') && $transaction_data['MT_USERNAME'] === '')
530
		{
531
			$user_id = ANONYMOUS;
532
		}
533
		else
534
		{
535
			$user_ary = $this->ppde_operator->query_donor_user_data('username', $transaction_data['MT_USERNAME']);
536
537
			if ($user_ary)
538
			{
539
				$user_id = $user_ary['user_id'];
540
			}
541
			else
542
			{
543
				$errors[] = $this->language->lang('PPDE_MT_DONOR_NOT_FOUND', $transaction_data['MT_USERNAME']);
544
			}
545
		}
546
547
		if ($transaction_data['MT_MC_GROSS'] <= 0)
548
		{
549
			$errors[] = $this->language->lang('PPDE_MT_MC_GROSS_TOO_LOW');
550
		}
551
552
		if ($transaction_data['MT_MC_FEE'] < 0)
553
		{
554
			$errors[] = $this->language->lang('PPDE_MT_MC_FEE_NEGATIVE');
555
		}
556
557
		if ($transaction_data['MT_MC_FEE'] >= $transaction_data['MT_MC_GROSS'])
558
		{
559
			$errors[] = $this->language->lang('PPDE_MT_MC_FEE_TOO_HIGH');
560
		}
561
562
		$payment_date = implode('-', [
563
			$transaction_data['MT_PAYMENT_DATE_YEAR'],
564
			$transaction_data['MT_PAYMENT_DATE_MONTH'],
565
			$transaction_data['MT_PAYMENT_DATE_DAY'],
566
		]);
567
568
		$payment_date_timestamp_at_midnight = $this->user->get_timestamp_from_format('Y-m-d H:i:s', $payment_date . ' 00:00:00');
569
570
		if ($payment_date_timestamp_at_midnight === false)
0 ignored issues
show
introduced by
The condition $payment_date_timestamp_at_midnight === false is always false.
Loading history...
571
		{
572
			$errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_ERROR', $payment_date);
573
		}
574
575
		$payment_time = $transaction_data['MT_PAYMENT_TIME'];
576
		$payment_time_timestamp = strtotime($payment_time);
577
578
		if ($payment_time_timestamp === false)
579
		{
580
			$errors[] = $this->language->lang('PPDE_MT_PAYMENT_TIME_ERROR', $payment_time);
581
		}
582
583
		// Normalize payment time to start from today at midnight
584
		$payment_time_timestamp_from_midnight = $payment_time_timestamp - strtotime('00:00:00');
585
586
		$payment_date_time = $payment_date_timestamp_at_midnight + $payment_time_timestamp_from_midnight;
587
588
		if ($payment_date_time > time())
589
		{
590
			$errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_FUTURE', $this->user->format_date($payment_date_time));
591
		}
592
593
		if ($errors)
594
		{
595
			throw (new transaction_exception())->set_errors($errors);
596
		}
597
598
		return array(
599
			'business'          => $this->config['ppde_account_id'],
600
			'confirmed'         => true,
601
			'exchange_rate'     => '',
602
			'first_name'        => $transaction_data['MT_FIRST_NAME'],
603
			'item_name'         => '',
604
			'item_number'       => implode('_', ['uid', $user_id, time()]),
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $user_id does not seem to be defined for all execution paths leading up to this point.
Loading history...
605
			'last_name'         => $transaction_data['MT_LAST_NAME'],
606
			'mc_currency'       => $transaction_data['MT_MC_CURRENCY'],
607
			'mc_gross'          => $transaction_data['MT_MC_GROSS'],
608
			'mc_fee'            => $transaction_data['MT_MC_FEE'],
609
			'net_amount'        => (float) 0, // This value is calculated in core_actions:log_to_db()
610
			'parent_txn_id'     => '',
611
			'payer_email'       => $transaction_data['MT_PAYER_EMAIL'],
612
			'payer_id'          => '',
613
			'payer_status'      => '',
614
			'payment_date'      => $payment_date_time,
615
			'payment_status'    => 'Completed',
616
			'payment_type'      => '',
617
			'memo'              => $transaction_data['MT_MEMO'],
618
			'receiver_id'       => '',
619
			'receiver_email'    => '',
620
			'residence_country' => strtoupper($transaction_data['MT_RESIDENCE_COUNTRY']),
621
			'settle_amount'     => (float) 0,
622
			'settle_currency'   => '',
623
			'test_ipn'          => false,
624
			'txn_errors'        => '',
625
			'txn_id'            => 'PPDE' . gen_rand_string(13),
626
			'txn_type'          => 'ppde_manual_donation',
627
			'user_id'           => $user_id,
628
		);
629
	}
630
631
	/**
632
	 * Set log output vars for display in the template
633
	 *
634
	 * @param array $row
635
	 *
636
	 * @return void
637
	 * @access protected
638
	 */
639
	protected function display_log_assign_template_vars($row)
640
	{
641
		$this->template->assign_block_vars('log', array(
642
			'CONFIRMED'        => ($row['confirmed']) ? $this->language->lang('PPDE_DT_VERIFIED') : $this->language->lang('PPDE_DT_UNVERIFIED'),
643
			'DATE'             => $this->user->format_date($row['payment_date']),
644
			'ID'               => $row['transaction_id'],
645
			'PAYMENT_STATUS'   => $this->language->lang(array('PPDE_DT_PAYMENT_STATUS_VALUES', strtolower($row['payment_status']))),
646
			'TNX_ID'           => $row['txn_id'],
647
			'USERNAME'         => $row['username_full'],
648
			'S_CONFIRMED'      => (bool) $row['confirmed'],
649
			'S_PAYMENT_STATUS' => (strtolower($row['payment_status']) === 'completed') ? true : false,
650
			'S_TXN_ERRORS'     => !empty($row['txn_errors']),
651
			'S_TEST_IPN'       => (bool) $row['test_ipn'],
652
		));
653
	}
654
655
	/**
656
	 * Set output vars for display in the template
657
	 *
658
	 * @param array $data
659
	 *
660
	 * @return void
661
	 * @access protected
662
	 */
663
	protected function action_assign_template_vars($data)
664
	{
665
		$s_hidden_fields = build_hidden_fields(array(
666
			'id'                  => $data['transaction_id'],
667
			'txn_errors_approved' => $data['txn_errors_approved'],
668
		));
669
670
		$this->template->assign_vars(array(
671
			'BOARD_USERNAME' => get_username_string('full', $data['user_id'], $data['username'], $data['user_colour'], $this->language->lang('GUEST'), append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&amp;mode=overview')),
672
			'EXCHANGE_RATE'  => '1 ' . $data['mc_currency'] . ' = ' . $data['exchange_rate'] . ' ' . $data['settle_currency'],
673
			'ITEM_NAME'      => $data['item_name'],
674
			'ITEM_NUMBER'    => $data['item_number'],
675
			'MC_CURRENCY'    => $data['net_amount'] . ' ' . $data['mc_currency'],
676
			'MC_GROSS'       => $data['mc_gross'] . ' ' . $data['mc_currency'],
677
			'MC_FEE'         => $data['mc_fee'] . ' ' . $data['mc_currency'],
678
			'MC_NET'         => $data['net_amount'] . ' ' . $data['mc_currency'],
679
			'MEMO'           => $data['memo'],
680
			'NAME'           => $data['first_name'] . ' ' . $data['last_name'],
681
			'PAYER_EMAIL'    => $data['payer_email'],
682
			'PAYER_ID'       => $data['payer_id'],
683
			'PAYER_STATUS'   => $data['payer_status'] ? $this->language->lang('PPDE_DT_VERIFIED') : $this->language->lang('PPDE_DT_UNVERIFIED'),
684
			'PAYMENT_DATE'   => $this->user->format_date($data['payment_date']),
685
			'PAYMENT_STATUS' => $this->language->lang(array('PPDE_DT_PAYMENT_STATUS_VALUES', strtolower($data['payment_status']))),
686
			'RECEIVER_EMAIL' => $data['receiver_email'],
687
			'RECEIVER_ID'    => $data['receiver_id'],
688
			'SETTLE_AMOUNT'  => $data['settle_amount'] . ' ' . $data['settle_currency'],
689
			'TXN_ID'         => $data['txn_id'],
690
691
			'L_PPDE_DT_SETTLE_AMOUNT'         => $this->language->lang('PPDE_DT_SETTLE_AMOUNT', $data['settle_currency']),
692
			'L_PPDE_DT_EXCHANGE_RATE_EXPLAIN' => $this->language->lang('PPDE_DT_EXCHANGE_RATE_EXPLAIN', $this->user->format_date($data['payment_date'])),
693
			'S_CONVERT'                       => ($data['settle_amount'] == 0 && empty($data['exchange_rate'])) ? false : true,
694
			'S_ERROR'                         => !empty($data['txn_errors']),
695
			'S_ERROR_APPROVED'                => !empty($data['txn_errors_approved']),
696
			'S_HIDDEN_FIELDS'                 => $s_hidden_fields,
697
			'ERROR_MSG'                       => (!empty($data['txn_errors'])) ? $data['txn_errors'] : '',
698
		));
699
	}
700
}
701