transactions::get_item_name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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-2024 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\entity;
12
13
use phpbb\db\driver\driver_interface;
14
use phpbb\language\language;
15
16
/**
17
 * @property driver_interface db                 phpBB Database object
18
 * @property language         language           phpBB Language object
19
 * @property string           lang_key_prefix    Prefix for the messages thrown by exceptions
20
 * @property string           lang_key_suffix    Suffix for the messages thrown by exceptions
21
 */
22
class transactions extends main
23
{
24
	/**
25
	 * Data for this entity
26
	 *
27
	 * @var array
28
	 *    business
29
	 *    confirmed
30
	 *    custom
31
	 *    exchange_rate
32
	 *    first_name
33
	 *    item_name
34
	 *    item_number
35
	 *    last_name
36
	 *    mc_currency
37
	 *    mc_fee
38
	 *    mc_gross
39
	 *    memo
40
	 *    net_amount
41
	 *    parent_txn_id
42
	 *    payer_email
43
	 *    payer_id
44
	 *    payer_status
45
	 *    payment_date
46
	 *    payment_status
47
	 *    payment_type
48
	 *    receiver_email
49
	 *    receiver_id
50
	 *    residence_country
51
	 *    settle_amount
52
	 *    settle_currency
53
	 *    transaction_id
54
	 *    test_ipn
55
	 *    txn_errors
56
	 *    txn_id
57
	 *    txn_type
58
	 *    user_id
59
	 */
60
	protected $data;
61
	protected $extra_data = [];
62
	protected $transactions_log_table;
63
64
	/**
65
	 * Constructor
66
	 *
67
	 * @param driver_interface $db         Database object
68
	 * @param language         $language   Language object
69
	 * @param string           $table_name Name of the table used to store data
70
	 */
71
	public function __construct(driver_interface $db, language $language, $table_name)
72
	{
73
		$this->transactions_log_table = $table_name;
74
		parent::__construct(
75
			$db,
76
			$language,
77
			'PPDE_DT',
78
			'TRANSACTION',
79
			$table_name,
80
			[
81
				'item_id'                  => ['name' => 'transaction_id', 'type' => 'integer'],
82
				'item_business'            => ['name' => 'business', 'type' => 'string'],
83
				'item_confirmed'           => ['name' => 'confirmed', 'type' => 'boolean'],
84
				'item_custom'              => ['name' => 'custom', 'type' => 'string'],
85
				'item_exchange_rate'       => ['name' => 'exchange_rate', 'type' => 'string'],
86
				'item_first_name'          => ['name' => 'first_name', 'type' => 'string'],
87
				'item_item_name'           => ['name' => 'item_name', 'type' => 'string'],
88
				'item_item_number'         => ['name' => 'item_number', 'type' => 'string'],
89
				'item_last_name'           => ['name' => 'last_name', 'type' => 'string'],
90
				'item_mc_currency'         => ['name' => 'mc_currency', 'type' => 'string'],
91
				'item_mc_fee'              => ['name' => 'mc_fee', 'type' => 'float'],
92
				'item_mc_gross'            => ['name' => 'mc_gross', 'type' => 'float'],
93
				'item_memo'                => ['name' => 'memo', 'type' => 'string'],
94
				'item_net_amount'          => ['name' => 'net_amount', 'type' => 'float'],
95
				'item_parent_txn_id'       => ['name' => 'parent_txn_id', 'type' => 'string'],
96
				'item_payer_email'         => ['name' => 'payer_email', 'type' => 'string'],
97
				'item_payer_id'            => ['name' => 'payer_id', 'type' => 'string'],
98
				'item_payer_status'        => ['name' => 'payer_status', 'type' => 'string'],
99
				'item_payment_date'        => ['name' => 'payment_date', 'type' => 'integer'],
100
				'item_payment_status'      => ['name' => 'payment_status', 'type' => 'string'],
101
				'item_payment_type'        => ['name' => 'payment_type', 'type' => 'string'],
102
				'item_receiver_email'      => ['name' => 'receiver_email', 'type' => 'string'],
103
				'item_receiver_id'         => ['name' => 'receiver_id', 'type' => 'string'],
104
				'item_residence_country'   => ['name' => 'residence_country', 'type' => 'string'],
105
				'item_settle_amount'       => ['name' => 'settle_amount', 'type' => 'float'],
106
				'item_settle_currency'     => ['name' => 'settle_currency', 'type' => 'string'],
107
				'item_test_ipn'            => ['name' => 'test_ipn', 'type' => 'boolean'],
108
				'item_txn_errors'          => ['name' => 'txn_errors', 'type' => 'string'],
109
				'item_txn_errors_approved' => ['name' => 'txn_errors_approved', 'type' => 'boolean'],
110
				'item_txn_id'              => ['name' => 'txn_id', 'type' => 'string'],
111
				'item_txn_type'            => ['name' => 'txn_type', 'type' => 'string'],
112
				'item_user_id'             => ['name' => 'user_id', 'type' => 'integer'],
113
			]
114
		);
115
	}
116
117
	/**
118
	 * Checks if the txn_id exists for this transaction
119
	 *
120
	 * @return int $this->data['transaction_id'] Transaction identifier; 0 if the transaction doesn't exist
121
	 */
122
	public function transaction_exists(): int
123
	{
124
		$sql_ary = [
125
			'SELECT' => 't.transaction_id',
126
			'FROM'   => [$this->transactions_log_table => 't'],
127
			'WHERE'  => "t.txn_id = '" . $this->db->sql_escape($this->data['txn_id']) . "'",
128
		];
129
130
		$sql = $this->db->sql_build_query('SELECT', $sql_ary);
131
		$result = $this->db->sql_query($sql);
132
		$field = (int) $this->db->sql_fetchfield('transaction_id');
133
		$this->db->sql_freeresult($result);
134
135
		return $field;
136
	}
137
138
	/**
139
	 * Get PayPal transaction id
140
	 *
141
	 * @return string
142
	 */
143
	public function get_txn_id(): string
144
	{
145
		return (string) ($this->data['txn_id'] ?? '');
146
	}
147
148
	/**
149
	 * Get PayPal receiver ID
150
	 *
151
	 * @return string
152
	 */
153
	public function get_receiver_id(): string
154
	{
155
		return (string) ($this->data['receiver_id'] ?? '');
156
	}
157
158
	/**
159
	 * Get PayPal receiver e-mail
160
	 *
161
	 * @return string
162
	 */
163
	public function get_receiver_email(): string
164
	{
165
		return (string) ($this->data['receiver_email'] ?? '');
166
	}
167
168
	/**
169
	 * Get PayPal receiver ID
170
	 *
171
	 * @return string
172
	 */
173
	public function get_residence_country(): string
174
	{
175
		return (string) ($this->data['residence_country'] ?? '');
176
	}
177
178
	/**
179
	 * Get PayPal business (same as receiver ID or receiver_email)
180
	 *
181
	 * @return string
182
	 */
183
	public function get_business(): string
184
	{
185
		return (string) ($this->data['business'] ?? '');
186
	}
187
188
	/**
189
	 * Get PayPal transaction status
190
	 *
191
	 * @return bool
192
	 */
193
	public function get_confirmed(): bool
194
	{
195
		return (bool) ($this->data['confirmed'] ?? false);
196
	}
197
198
	/**
199
	 * Get Test IPN status
200
	 *
201
	 * @return bool
202
	 */
203
	public function get_test_ipn(): bool
204
	{
205
		return (bool) ($this->data['test_ipn'] ?? false);
206
	}
207
208
	/**
209
	 * Get PayPal transaction errors
210
	 *
211
	 * @return string
212
	 */
213
	public function get_txn_errors(): string
214
	{
215
		return (string) ($this->data['txn_errors'] ?? '');
216
	}
217
218
	/**
219
	 * Get PayPal transaction errors approval status
220
	 *
221
	 * @return bool
222
	 */
223
	public function get_txn_errors_approved(): bool
224
	{
225
		return (bool) ($this->data['txn_errors_approved'] ?? '');
226
	}
227
228
	/**
229
	 * Get PayPal transaction type
230
	 *
231
	 * @return string
232
	 */
233
	public function get_txn_type(): string
234
	{
235
		return (string) ($this->data['txn_type'] ?? '');
236
	}
237
238
	/**
239
	 * Get PayPal parent transaction ID (in case of refund)
240
	 *
241
	 * @return string
242
	 */
243
	public function get_parent_txn_id(): string
244
	{
245
		return (string) ($this->data['parent_txn_id'] ?? '');
246
	}
247
248
	/**
249
	 * Get PayPal payer e-mail
250
	 *
251
	 * @return string
252
	 */
253
	public function get_payer_email(): string
254
	{
255
		return (string) ($this->data['payer_email'] ?? '');
256
	}
257
258
	/**
259
	 * Get PayPal payer account ID
260
	 *
261
	 * @return string
262
	 */
263
	public function get_payer_id(): string
264
	{
265
		return (string) ($this->data['payer_id'] ?? '');
266
	}
267
268
	/**
269
	 * Get PayPal payer Status (such as unverified/verified)
270
	 *
271
	 * @return string
272
	 */
273
	public function get_payer_status(): string
274
	{
275
		return (string) ($this->data['payer_status'] ?? '');
276
	}
277
278
	/**
279
	 * Get PayPal payer first name
280
	 *
281
	 * @return string
282
	 */
283
	public function get_first_name(): string
284
	{
285
		return (string) ($this->data['first_name'] ?? '');
286
	}
287
288
	/**
289
	 * Get PayPal payer last name
290
	 *
291
	 * @return string
292
	 */
293
	public function get_last_name(): string
294
	{
295
		return (string) ($this->data['last_name'] ?? '');
296
	}
297
298
	/**
299
	 * Get member user_id
300
	 *
301
	 * @return int
302
	 */
303
	public function get_user_id(): int
304
	{
305
		return (int) ($this->data['user_id'] ?? 0);
306
	}
307
308
	/**
309
	 * Get member username
310
	 *
311
	 * @return string
312
	 */
313
	public function get_username(): string
314
	{
315
		return (string) ($this->extra_data['username'] ?? '');
316
	}
317
318
	/**
319
	 * Get PayPal payer last name
320
	 *
321
	 * @return string
322
	 */
323
	public function get_custom(): string
324
	{
325
		return (string) ($this->data['custom'] ?? '');
326
	}
327
328
	/**
329
	 * Get PayPal item name
330
	 *
331
	 * @return string
332
	 */
333
	public function get_item_name(): string
334
	{
335
		return (string) ($this->data['item_name'] ?? '');
336
	}
337
338
	/**
339
	 * Get PayPal item number (contains user_id)
340
	 *
341
	 * @return string
342
	 */
343
	public function get_item_number(): string
344
	{
345
		return (string) ($this->data['item_number'] ?? '');
346
	}
347
348
	/**
349
	 * Get PayPal currency name (eg: USD, EUR, etc.)
350
	 *
351
	 * @return string
352
	 */
353
	public function get_mc_currency(): string
354
	{
355
		return (string) ($this->data['mc_currency'] ?? '');
356
	}
357
358
	/**
359
	 * Get PayPal fees
360
	 *
361
	 * @return float
362
	 */
363
	public function get_mc_fee(): float
364
	{
365
		return (float) ($this->data['mc_fee'] ?? 0);
366
	}
367
368
	/**
369
	 * Get PayPal amount
370
	 * This is the amount of donation received before fees
371
	 *
372
	 * @return float
373
	 */
374
	public function get_mc_gross(): float
375
	{
376
		return (float) ($this->data['mc_gross'] ?? 0);
377
	}
378
379
	/**
380
	 * Get Net amount
381
	 * This is the amount of donation received after fees
382
	 *
383
	 * @return float
384
	 */
385
	public function get_net_amount(): float
386
	{
387
		return (float) ($this->data['net_amount'] ?? 0);
388
	}
389
390
	/**
391
	 * Get PayPal payment date
392
	 *
393
	 * @return int
394
	 */
395
	public function get_payment_date(): int
396
	{
397
		return (int) ($this->data['payment_date'] ?? 0);
398
	}
399
400
	/**
401
	 * Get PayPal payment status
402
	 *
403
	 * @return string
404
	 */
405
	public function get_payment_status(): string
406
	{
407
		return (string) ($this->data['payment_status'] ?? '');
408
	}
409
410
	/**
411
	 * Get PayPal payment type
412
	 *
413
	 * @return string
414
	 */
415
	public function get_payment_type(): string
416
	{
417
		return (string) ($this->data['payment_type'] ?? '');
418
	}
419
420
	/**
421
	 * Get PayPal memo
422
	 *
423
	 * @return string
424
	 */
425
	public function get_memo(): string
426
	{
427
		return (string) ($this->data['memo'] ?? '');
428
	}
429
430
	/**
431
	 * Get PayPal settle amount
432
	 * This is in case or the currency of the Payer is not in the same currency of the Receiver
433
	 *
434
	 * @return float
435
	 */
436
	public function get_settle_amount(): float
437
	{
438
		return (float) ($this->data['settle_amount'] ?? 0);
439
	}
440
441
	/**
442
	 * Get PayPal settle currency
443
	 *
444
	 * @return string
445
	 */
446
	public function get_settle_currency(): string
447
	{
448
		return (string) ($this->data['settle_currency'] ?? '');
449
	}
450
451
	/**
452
	 * Get PayPal exchange rate
453
	 * This is when the donation don’t use the same currency defined by the receiver
454
	 *
455
	 * @return string
456
	 */
457
	public function get_exchange_rate(): string
458
	{
459
		return (string) ($this->data['exchange_rate'] ?? '');
460
	}
461
462
	/**
463
	 * Set PayPal transaction errors approval status
464
	 *
465
	 * @param bool $txn_errors_approved
466
	 *
467
	 * @return transactions $this object for chaining calls; load()->set()->save()
468
	 */
469
	public function set_txn_errors_approved($txn_errors_approved)
470
	{
471
		$this->data['txn_errors_approved'] = (bool) $txn_errors_approved;
472
473
		return $this;
474
	}
475
476
	/**
477
	 * Set member user_id
478
	 *
479
	 * @param int $user_id
480
	 *
481
	 * @return transactions $this object for chaining calls; load()->set()->save()
482
	 */
483
	public function set_user_id($user_id)
484
	{
485
		$this->data['user_id'] = (int) $user_id;
486
487
		return $this;
488
	}
489
490
	/**
491
	 * Set member username
492
	 *
493
	 * @param string $username
494
	 *
495
	 * @return transactions $this object for chaining calls; load()->set()->save()
496
	 */
497
	public function set_username($username)
498
	{
499
		$this->extra_data['username'] = (string) $username;
500
501
		return $this;
502
	}
503
}
504