| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmTransLitePayment extends FrmTransLiteDb { |
| 7 |
|
| 8 |
public $table_name = 'frm_payments'; |
| 9 |
public $singular = 'payment'; |
| 10 |
|
| 11 |
/** |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
public function get_defaults() { |
| 15 |
return array( |
| 16 |
'receipt_id' => array( |
| 17 |
'sanitize' => 'sanitize_text_field', |
| 18 |
'default' => '', |
| 19 |
), |
| 20 |
'invoice_id' => array( |
| 21 |
'sanitize' => 'sanitize_text_field', |
| 22 |
'default' => '', |
| 23 |
), |
| 24 |
'sub_id' => array( |
| 25 |
'sanitize' => 'sanitize_text_field', |
| 26 |
'default' => '', |
| 27 |
), |
| 28 |
'item_id' => array( |
| 29 |
'sanitize' => 'absint', |
| 30 |
'default' => '', |
| 31 |
), |
| 32 |
'amount' => array( |
| 33 |
'sanitize' => 'float', |
| 34 |
'default' => '', |
| 35 |
), |
| 36 |
'status' => array( |
| 37 |
'sanitize' => 'sanitize_text_field', |
| 38 |
'default' => 'pending', |
| 39 |
), |
| 40 |
'action_id' => array( |
| 41 |
'sanitize' => 'absint', |
| 42 |
'default' => 0, |
| 43 |
), |
| 44 |
'paysys' => array( |
| 45 |
'sanitize' => 'sanitize_text_field', |
| 46 |
'default' => 'manual', |
| 47 |
), |
| 48 |
'created_at' => array( |
| 49 |
'sanitize' => 'sanitize_text_field', |
| 50 |
'default' => current_time( 'mysql', 1 ), |
| 51 |
), |
| 52 |
'begin_date' => array( |
| 53 |
'sanitize' => 'sanitize_text_field', |
| 54 |
'default' => current_time( 'mysql', 1 ), |
| 55 |
), |
| 56 |
'expire_date' => array( |
| 57 |
'sanitize' => 'sanitize_text_field', |
| 58 |
'default' => '0000-00-00', |
| 59 |
), |
| 60 |
'meta_value' => array( |
| 61 |
'sanitize' => 'maybe_serialize', |
| 62 |
'default' => '', |
| 63 |
), |
| 64 |
'test' => array( |
| 65 |
'sanitize' => 'sanitize_text_field', |
| 66 |
'default' => null, |
| 67 |
), |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Gets payments statistic data. |
| 73 |
* |
| 74 |
* @since 6.7 |
| 75 |
* |
| 76 |
* @param string|null $from_date From date. |
| 77 |
* @param string|null $to_date To date. |
| 78 |
* |
| 79 |
* @return array Contains `count` and `total`. |
| 80 |
*/ |
| 81 |
public function get_payments_stats( $from_date = null, $to_date = null ) { |
| 82 |
$data = array( |
| 83 |
'count' => 0, |
| 84 |
'total' => array(), |
| 85 |
); |
| 86 |
|
| 87 |
if ( ! FrmTransLiteAppHelper::payments_table_exists() ) { |
| 88 |
return $data; |
| 89 |
} |
| 90 |
|
| 91 |
$where = array(); |
| 92 |
|
| 93 |
if ( null !== $from_date ) { |
| 94 |
$where['created_at >'] = $from_date; |
| 95 |
} |
| 96 |
|
| 97 |
if ( null !== $to_date ) { |
| 98 |
$where['created_at <'] = $to_date . ' 23:59:59'; |
| 99 |
} |
| 100 |
|
| 101 |
$db_version = get_option( $this->db_opt_name ); |
| 102 |
|
| 103 |
// Do not collect test payment, this is a new feature of Stripe lite. |
| 104 |
if ( 6 <= $db_version ) { |
| 105 |
$where['test'] = array( null, 0 ); |
| 106 |
} |
| 107 |
|
| 108 |
// If only Paypal is used, and the DB isn't migrated (to version 4), use `completed` column instead of `status`. |
| 109 |
if ( 4 > $db_version && FrmDb::db_column_exists( $this->table_name, 'completed' ) ) { |
| 110 |
$where['completed'] = 1; |
| 111 |
} else { |
| 112 |
$where['status'] = 'complete'; |
| 113 |
} |
| 114 |
|
| 115 |
$payments = FrmDb::get_results( $this->table_name, $where, 'action_id,amount' ); |
| 116 |
|
| 117 |
if ( ! $payments ) { |
| 118 |
return $data; |
| 119 |
} |
| 120 |
|
| 121 |
$data['count'] = count( $payments ); |
| 122 |
$data['total'] = $this->get_payment_total_data( $payments ); |
| 123 |
|
| 124 |
return $data; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Gets payment total data. |
| 129 |
* |
| 130 |
* @since 6.7 |
| 131 |
* |
| 132 |
* @param object[] $payments Array of payment objects. |
| 133 |
* |
| 134 |
* @return array Return array of total amount for each currency. |
| 135 |
*/ |
| 136 |
private function get_payment_total_data( $payments ) { |
| 137 |
$data = array(); |
| 138 |
|
| 139 |
foreach ( $payments as $payment ) { |
| 140 |
list( $amount, $currency ) = FrmTransLiteAppHelper::get_amount_and_currency_from_payment( $payment ); |
| 141 |
|
| 142 |
if ( ! isset( $data[ $currency ] ) ) { |
| 143 |
$data[ $currency ] = 0; |
| 144 |
} |
| 145 |
|
| 146 |
$data[ $currency ] += floatval( $amount ); |
| 147 |
} |
| 148 |
|
| 149 |
return $data; |
| 150 |
} |
| 151 |
} |
| 152 |
|