PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 0.0.3
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v0.0.3
2.4.1 2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 All 68 releases
better-payment / includes / Admin / Better_Payment_DB.php

Better_Payment_DB.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 0.0.3, at includes/Admin/Better_Payment_DB.php

453 lines 16.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Admin;
4
5 use Better_Payment\Lite\Classes\Better_Payment_Helper;
6
7 use function Better_Payment\Lite\Classes\better_payment_dd;
8
9 /**
10 * Exit if accessed directly
11 */
12 if (!defined('ABSPATH')) {
13 exit;
14 }
15
16 /**
17 * This class responsible for database work
18 * using wordpress functionality
19 * get_option and update_option.
20 *
21 * @since 0.0.1
22 */
23 class Better_Payment_DB {
24
25 public static function get_table_name() {
26 global $wpdb;
27
28 $table = "{$wpdb->prefix}better_payment";
29 return $table;
30 }
31 /**
32 * Get all default settings value.
33 *
34 * @param string $name
35 * @return array
36 * @since 0.0.1
37 */
38 public static function default_settings() {
39 return apply_filters('better_payment_option_default_settings', array(
40 'better_payment_settings_general_general_paypal' => esc_html__('yes', 'better-payment'),
41 'better_payment_settings_general_general_stripe' => esc_html__('yes', 'better-payment'),
42 'better_payment_settings_general_general_email' => esc_html__('yes', 'better-payment'),
43 'better_payment_settings_general_general_currency' => esc_html__('USD', 'better-payment'),
44
45 'better_payment_settings_general_email_to' => esc_html__('', 'better-payment'),
46 'better_payment_settings_general_email_subject' => esc_html__('', 'better-payment'),
47 'better_payment_settings_general_email_message_admin' => esc_html__('', 'better-payment'),
48 'better_payment_settings_general_email_from_email' => esc_html__('', 'better-payment'),
49 'better_payment_settings_general_email_from_name' => esc_html__('', 'better-payment'),
50 'better_payment_settings_general_email_reply_to' => esc_html__('', 'better-payment'),
51 'better_payment_settings_general_email_cc' => esc_html__('', 'better-payment'),
52 'better_payment_settings_general_email_bcc' => esc_html__('', 'better-payment'),
53 'better_payment_settings_general_email_send_as' => esc_html__('html', 'better-payment'),
54
55 'better_payment_settings_general_email_subject_customer' => esc_html__('', 'better-payment'),
56 'better_payment_settings_general_email_message_customer' => esc_html__('', 'better-payment'),
57 'better_payment_settings_general_email_from_email_customer' => esc_html__('', 'better-payment'),
58 'better_payment_settings_general_email_from_name_customer' => esc_html__('', 'better-payment'),
59 'better_payment_settings_general_email_reply_to_customer' => esc_html__('', 'better-payment'),
60 'better_payment_settings_general_email_cc_customer' => esc_html__('', 'better-payment'),
61 'better_payment_settings_general_email_bcc_customer' => esc_html__('', 'better-payment'),
62 'better_payment_settings_general_email_send_as_customer' => esc_html__('html', 'better-payment'),
63
64 'better_payment_settings_payment_paypal_live_mode' => esc_html__('no', 'better-payment'),
65 'better_payment_settings_payment_paypal_email' => esc_html__('', 'better-payment'),
66 'better_payment_settings_opt_in' => esc_html__('', 'better-payment'),
67 'better_payment_settings_payment_paypal_live_client_id' => esc_html__('', 'better-payment'),
68 'better_payment_settings_payment_paypal_test_client_id' => esc_html__('', 'better-payment'),
69 'better_payment_settings_payment_paypal_live_secret' => esc_html__('', 'better-payment'),
70 'better_payment_settings_payment_paypal_test_secret' => esc_html__('', 'better-payment'),
71 'better_payment_settings_payment_stripe_live_mode' => esc_html__('no', 'better-payment'),
72 'better_payment_settings_payment_stripe_live_public' => esc_html__('', 'better-payment'),
73 'better_payment_settings_payment_stripe_live_secret' => esc_html__('', 'better-payment'),
74 'better_payment_settings_payment_stripe_test_public' => esc_html__('', 'better-payment'),
75 'better_payment_settings_payment_stripe_test_secret' => esc_html__('', 'better-payment'),
76 ));
77 }
78 /**
79 * Get all settings value from options table.
80 *
81 * @param string $name
82 * @return array
83 * @since 0.0.1
84 */
85 public static function get_settings($name = '')
86 {
87 $settings = get_option('better_payment_settings', true);
88 $default = self::default_settings();
89 if (!empty($name) && isset($settings[$name])) {
90 return $settings[$name];
91 }
92
93 if (!empty($name) && !isset($settings[$name]) && isset($default[$name])) {
94 return $default[$name];
95 }
96
97 if (!empty($name) && !isset($settings[$name]) && !isset($default[$name])) {
98 return '';
99 }
100
101 return is_array($settings) ? $settings : [];
102 }
103 /**
104 * Update settings
105 * @param array $value
106 * @return boolean
107 * @since 0.0.1
108 */
109 public static function update_settings($value, $key = '')
110 {
111 if (!empty($key)) {
112 return update_option($key, $value);
113 }
114 return update_option('better_payment_settings', $value);
115 }
116
117 /**
118 * Get all transactions from better payment table.
119 *
120 * @param string $args
121 * @return array
122 * @since 0.0.1
123 */
124 public static function get_transactions($args = [], $count_only=0, $version = 'v1', $fetchNull = 0)
125 {
126 global $wpdb;
127 $items = '';
128
129 $defaults = array(
130 'search_text' => '',
131 'payment_date_from' => '',
132 'payment_date_to' => '',
133 'order_by' => 'id',
134 'order' => 'DESC',
135 'source' => '',
136 'paged' => 1,
137 'per_page' => 20,
138 'offset' => 0,
139 'status' => 'all',
140 );
141
142 $allowed_order_by = array(
143 'id',
144 'payment_date',
145 'email',
146 'amount'
147 );
148
149 $allowed_order = array(
150 'DESC',
151 'ASC'
152 );
153
154 $better_payment_db_obj = new Better_Payment_DB();
155
156 $allowed_statuses = $better_payment_db_obj->allowed_statuses('all', 'v2');
157 $allowed_statuses_types = $better_payment_db_obj->allowed_statuses_types('v2');
158
159 $args = wp_parse_args($args, $defaults);
160 $table = self::get_table_name();
161
162 $search_text = sanitize_text_field($args['search_text']);
163 $search_text = empty($search_text) ? '' : "%$search_text%";
164
165 $order_by = in_array($args['order_by'], $allowed_order_by) ? sanitize_text_field($args['order_by']) : $defaults['order_by'];
166 $order = in_array($args['order'], $allowed_order) ? sanitize_text_field($args['order']) : $defaults['order'];
167 $offset = $args['offset'] > 0 ? intval($args['offset']) : $defaults['offset'];
168 $per_page = $args['per_page'] > 0 ? intval($args['per_page']) : $defaults['per_page'];
169
170 if($args['status'] == 'null') {
171 $status = sanitize_text_field($args['status']);
172 }else {
173 $allowed_statuses_by_version = ($version == 'v1') ? $allowed_statuses : $allowed_statuses_types;
174 $status = in_array($args['status'], $allowed_statuses_by_version) ? sanitize_text_field($args['status']) : $defaults['status'];
175 }
176
177 $whereQuery = '';
178
179 if($search_text != '' && strlen($search_text) >= 2){
180 $whereQuery = $wpdb->prepare(' AND email LIKE %s OR transaction_id LIKE %s OR amount LIKE %s OR source LIKE %s ', $search_text, $search_text, $search_text, $search_text );
181 }
182
183 if( $args['source'] != '' ){
184 $whereQuery .= $wpdb->prepare(" AND source = %s", $args['source']);
185 }
186
187 if( $status != 'all' ){
188 if($status == 'null'){
189 $whereQuery .= esc_sql(" AND status is NULL");
190 }else {
191 //version 2
192 if('v1' === $version){
193 $whereQuery .= $wpdb->prepare( " AND status = %s OR status = %s", $status, strtolower($status) );
194 } else {
195 $statuses_by_type = $better_payment_db_obj->allowed_statuses($status, 'v2');
196
197 $statuses = array_map(function($status) {
198 return "'" . esc_sql($status) . "'";
199 }, $statuses_by_type);
200
201 $statuses = implode(',', $statuses);
202
203 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
204 $whereQuery .= " AND ( status IN ( $statuses ) " . esc_sql($nullDataQuery) . " ) ";
205 }
206 }
207 }
208
209 if( $args['payment_date_from'] != '' && $args['payment_date_to'] != ''){
210 $whereQuery .= $wpdb->prepare(" AND payment_date BETWEEN %s AND %s", $args['payment_date_from'], $args['payment_date_to']);
211 }
212
213 if($count_only === 1){
214 $items = $wpdb->get_var(
215 $wpdb->prepare(
216 "SELECT count(id) FROM $table WHERE 1
217 $whereQuery
218 ORDER BY $order_by $order
219 LIMIT %d, %d
220 ",
221 $offset,
222 $per_page
223 )
224 );
225 }else {
226 $items = $wpdb->get_results(
227 $wpdb->prepare(
228 "SELECT * FROM $table WHERE 1
229 $whereQuery
230 ORDER BY $order_by $order
231 LIMIT %d, %d
232 ",
233 $offset,
234 $per_page
235 )
236 );
237 }
238
239 // better_payment_dd($items, 1);
240
241 return $items;
242 }
243
244 /**
245 * Get the count of total transactions
246 *
247 * @return int
248 * @since 0.0.1
249 */
250 public static function get_transaction_count($args = '', $version = 'v1', $fetchNull = 0)
251 {
252 global $wpdb;
253 $table = self::get_table_name();
254
255 $count = 0;
256
257 if($args !== ''){
258 $count = self::get_transactions($args, 1, $version, $fetchNull);
259 }else {
260 $count = $wpdb->get_var(
261 "SELECT count(id) FROM $table"
262 );
263 }
264
265 return $count;
266 }
267
268 /**
269 * Get transactions analytics
270 *
271 * @return int
272 * @since 0.0.1
273 */
274 public static function get_transactions_analytics()
275 {
276 global $wpdb;
277 $transaction_analytics = [];
278
279 $table = self::get_table_name();
280 $better_payment_db_obj = new Better_Payment_DB();
281
282 $total_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses(array(), '', '', 'count', 1);
283
284 $completed_statuses = $better_payment_db_obj->allowed_statuses('completed', 'v2');
285 $completed_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses( $completed_statuses, '', '', 'count' );
286
287 $incomplete_statuses = $better_payment_db_obj->allowed_statuses('incomplete', 'v2');
288 $incomplete_transactions = $better_payment_db_obj->get_transactions_amount_by_statuses( $incomplete_statuses, '', '', 'count', 1 );
289
290 $transaction_analytics['total_transactions'] = $total_transactions;
291 $transaction_analytics['completed_transactions'] = $completed_transactions;
292 $transaction_analytics['incomplete_transactions'] = $incomplete_transactions;
293
294 return $transaction_analytics;
295 }
296
297 /**
298 * Get transaction details from better payment table.
299 *
300 * @param string $args
301 * @return object
302 * @since 0.0.1
303 */
304 public static function get_transaction($id)
305 {
306 global $wpdb;
307
308 $table = self::get_table_name();
309 $transaction_id = (int) sanitize_text_field($id);
310
311 $item = $wpdb->get_row(
312 $wpdb->prepare(
313 "SELECT * FROM $table WHERE id = %d",
314 $transaction_id
315 )
316 );
317
318 return $item;
319 }
320
321 /**
322 * Delete a transaction
323 *
324 * @param int $id
325 *
326 * @return int|boolean
327 * @since 0.0.1
328 */
329 public static function delete_transaction($id)
330 {
331 global $wpdb;
332 $table = self::get_table_name();
333
334 return $wpdb->delete(
335 $table,
336 ['id' => $id],
337 ['%d']
338 );
339 }
340
341 /**
342 * Allowed transaction statuses
343 *
344 * @since 0.0.1
345 */
346 public function allowed_statuses($type = 'all', $version = 'v1'){
347 $helperObj = new Better_Payment_Helper();
348 $statuses = $helperObj->get_statuses_by_transaction_type($type, $version);
349
350 return $statuses;
351 }
352
353 /**
354 * Allowed transaction statuses
355 *
356 * @since 0.0.1
357 */
358 public function allowed_statuses_types($version = 'v1'){
359 $helperObj = new Better_Payment_Helper();
360 $statuses = $helperObj->get_transaction_types($version);
361
362 return $statuses;
363 }
364
365 /**
366 * Get transactions amount
367 *
368 * @return array
369 * @since 0.0.1
370 */
371 public function get_transactions_by_statuses($statuses = array(), $payment_date_from = '', $payment_date_to = '', $fetchNull=0) {
372 global $wpdb;
373
374 $table = Better_Payment_DB::get_table_name();
375 $allowed_statuses = $this->allowed_statuses('all', 'v2');
376
377 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
378
379 $statuses = is_array($statuses) && count($statuses) ? $statuses : $allowed_statuses;
380
381 #ToDo: Consider currency while calculating total amount
382 $statuses = array_map(function($status) use ($allowed_statuses) {
383 if(!in_array($status, $allowed_statuses)) {
384 return false;
385 }
386
387 return "'" . esc_sql($status) . "'";
388 }, $statuses);
389
390 $statuses = implode(',', $statuses);
391
392 if(!empty($payment_date_from) && !empty($payment_date_to)){
393 $amount = $wpdb->get_results(
394 "SELECT amount, payment_date FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ") AND payment_date BETWEEN '" . esc_sql($payment_date_from) . "' AND '" . esc_sql($payment_date_to) . "'"
395 );
396 } else {
397 $amount = $wpdb->get_results(
398 "SELECT amount, payment_date FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . " )"
399 );
400 }
401
402 return $amount;
403 }
404
405 /**
406 * Get transactions amount
407 *
408 * @return array
409 * @since 0.0.1
410 */
411 public function get_transactions_amount_by_statuses($statuses = array(), $payment_date_from = '', $payment_date_to = '', $count_or_amount = 'amount', $fetchNull=0) {
412 global $wpdb;
413
414 $table = Better_Payment_DB::get_table_name();
415 $allowed_statuses = $this->allowed_statuses('all', 'v2');
416
417 $nullDataQuery = $fetchNull ? " OR status IS NULL " : '';
418
419 $statuses = is_array($statuses) && count($statuses) ? $statuses : $allowed_statuses;
420
421 #ToDo: Consider currency while calculating total amount
422 $statuses = array_map(function($status) use ($allowed_statuses) {
423 if(!in_array($status, $allowed_statuses)) {
424 return false;
425 }
426
427 return "'" . esc_sql($status) . "'";
428 }, $statuses);
429
430 $statuses = implode(',', $statuses);
431
432 if($count_or_amount == 'count') {
433 $count = (int) $wpdb->get_var(
434 "SELECT count(id) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . " )"
435 );
436
437 return $count;
438 }
439
440 if(!empty($payment_date_from) && !empty($payment_date_to)){
441 $amount = $wpdb->get_var(
442 "SELECT sum(amount) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ") AND payment_date BETWEEN '" . esc_sql($payment_date_from) . "' AND '" . esc_sql($payment_date_to) . "'"
443 );
444 }else {
445 $amount = $wpdb->get_var(
446 "SELECT sum(amount) FROM $table WHERE ( status IN (" . $statuses . ") " . esc_sql($nullDataQuery) . ")"
447 );
448 }
449
450 return $amount;
451 }
452 }
453