activities_controller.php
11 months ago
auth_controller.php
9 months ago
booking_form_settings_controller.php
1 year ago
bookings_controller.php
1 year ago
calendars_controller.php
9 months ago
carts_controller.php
1 year ago
controller.php
9 months ago
customer_cabinet_controller.php
9 months ago
customers_controller.php
9 months ago
dashboard_controller.php
9 months ago
default_agent_controller.php
1 year ago
events_controller.php
1 year ago
form_fields_controller.php
9 months ago
integrations_controller.php
9 months ago
invoices_controller.php
1 year ago
manage_booking_by_key_controller.php
1 year ago
manage_order_by_key_controller.php
1 year ago
notifications_controller.php
1 year ago
orders_controller.php
1 year ago
pro_controller.php
1 year ago
process_jobs_controller.php
9 months ago
processes_controller.php
1 year ago
search_controller.php
1 year ago
services_controller.php
9 months ago
settings_controller.php
1 year ago
steps_controller.php
9 months ago
stripe_connect_controller.php
9 months ago
support_topics_controller.php
1 year ago
todos_controller.php
1 year ago
transactions_controller.php
1 year ago
wizard_controller.php
1 year ago
transactions_controller.php
254 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | |
| 6 | |
| 7 | if (!class_exists('OsTransactionsController')) : |
| 8 | |
| 9 | |
| 10 | class OsTransactionsController extends OsController { |
| 11 | |
| 12 | function __construct() { |
| 13 | parent::__construct(); |
| 14 | |
| 15 | $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'transactions/'; |
| 16 | $this->vars['page_header'] = OsMenuHelper::get_menu_items_by_id('payments'); |
| 17 | $this->vars['breadcrumbs'][] = array('label' => __('Transactions', 'latepoint'), 'link' => OsRouterHelper::build_link(OsRouterHelper::build_route_name('transactions', 'index'))); |
| 18 | |
| 19 | $this->action_access['public'] = array_merge( $this->action_access['public'], [ 'view_receipt_by_key' ] ); |
| 20 | } |
| 21 | |
| 22 | public function edit_form() { |
| 23 | if (filter_var($this->params['id'], FILTER_VALIDATE_INT)) { |
| 24 | // existing |
| 25 | $transaction = new OsTransactionModel($this->params['id']); |
| 26 | }else{ |
| 27 | // new |
| 28 | $transaction = new OsTransactionModel(); |
| 29 | if (filter_var($this->params['order_id'], FILTER_VALIDATE_INT)) { |
| 30 | $transaction->order_id = $this->params['order_id']; |
| 31 | } |
| 32 | } |
| 33 | $this->vars['real_or_rand_id'] = ($transaction->is_new_record()) ? 'new_transaction_' . OsUtilHelper::random_text('alnum', 5) : $transaction->id; |
| 34 | $this->vars['transaction'] = $transaction; |
| 35 | |
| 36 | $this->format_render(__FUNCTION__); |
| 37 | } |
| 38 | |
| 39 | public function view_receipt_by_key(){ |
| 40 | $receipt_access_key = sanitize_text_field($this->params['key']); |
| 41 | |
| 42 | if(empty($receipt_access_key)) { |
| 43 | echo __( 'Invalid Receipt Key', 'latepoint' ); |
| 44 | exit; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | $transaction = new OsTransactionModel(); |
| 49 | $transaction = $transaction->where(['access_key' => $receipt_access_key])->set_limit(1)->get_results_as_models(); |
| 50 | |
| 51 | if(empty($transaction)) { |
| 52 | echo __( 'Receipt not found', 'latepoint' ); |
| 53 | exit; |
| 54 | } |
| 55 | |
| 56 | if(empty($transaction->receipt_number)) $transaction->update_attributes(['receipt_number' => $transaction->generate_receipt_number()]); |
| 57 | |
| 58 | $invoice = new OsInvoiceModel($transaction->invoice_id); |
| 59 | |
| 60 | $this->vars['invoice'] = $invoice; |
| 61 | $this->vars['transaction'] = $transaction; |
| 62 | |
| 63 | $this->set_layout( 'clean' ); |
| 64 | $this->format_render( __FUNCTION__ ); |
| 65 | } |
| 66 | |
| 67 | public function destroy() { |
| 68 | if (filter_var($this->params['id'], FILTER_VALIDATE_INT)) { |
| 69 | $this->check_nonce('destroy_transaction_'.$this->params['id']); |
| 70 | $transaction = new OsTransactionModel($this->params['id']); |
| 71 | if ($transaction->delete()) { |
| 72 | $status = LATEPOINT_STATUS_SUCCESS; |
| 73 | $response_html = __('Transaction Removed', 'latepoint'); |
| 74 | } else { |
| 75 | $status = LATEPOINT_STATUS_ERROR; |
| 76 | $response_html = __('Error Removing Transaction', 'latepoint'); |
| 77 | } |
| 78 | } else { |
| 79 | $status = LATEPOINT_STATUS_ERROR; |
| 80 | $response_html = __('Error Removing Transaction', 'latepoint'); |
| 81 | } |
| 82 | if ($this->get_return_format() == 'json') { |
| 83 | $this->send_json(array('status' => $status, 'message' => $response_html)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | Index of transactions |
| 89 | */ |
| 90 | |
| 91 | public function index() { |
| 92 | |
| 93 | $per_page = OsSettingsHelper::get_number_of_records_per_page(); |
| 94 | $page_number = isset($this->params['page_number']) ? $this->params['page_number'] : 1; |
| 95 | |
| 96 | $this->vars['page_header'] = false; |
| 97 | |
| 98 | $transactions = new OsTransactionModel(); |
| 99 | |
| 100 | |
| 101 | // TABLE SEARCH FILTERS |
| 102 | $filter = $this->params['filter'] ?? false; |
| 103 | $query_args = []; |
| 104 | if ($filter) { |
| 105 | if (!empty($filter['id'])) $query_args['id'] = $filter['id']; |
| 106 | if (!empty($filter['token'])) $query_args['token'] = $filter['token']; |
| 107 | if (!empty($filter['booking_id'])) $query_args['booking_id'] = $filter['booking_id']; |
| 108 | if (!empty($filter['processor'])) $query_args['processor'] = $filter['processor']; |
| 109 | if (!empty($filter['payment_method'])) $query_args['payment_method'] = $filter['payment_method']; |
| 110 | if (!empty($filter['amount'])) $query_args['amount'] = $filter['amount']; |
| 111 | if (!empty($filter['status'])) $query_args['status'] = $filter['status']; |
| 112 | if (!empty($filter['kind'])) $query_args['kind'] = $filter['kind']; |
| 113 | |
| 114 | if (!empty($filter['customer']['full_name'])) { |
| 115 | $transactions->select(LATEPOINT_TABLE_TRANSACTIONS . '.*, ' . LATEPOINT_TABLE_CUSTOMERS . '.first_name, ' . LATEPOINT_TABLE_CUSTOMERS . '.last_name'); |
| 116 | $transactions->join(LATEPOINT_TABLE_CUSTOMERS, ['id' => LATEPOINT_TABLE_TRANSACTIONS . '.customer_id']); |
| 117 | |
| 118 | $query_args['concat_ws(" ", ' . LATEPOINT_TABLE_CUSTOMERS . '.first_name,' . LATEPOINT_TABLE_CUSTOMERS . '.last_name) LIKE'] = '%' . $filter['customer']['full_name'] . '%'; |
| 119 | $this->vars['customer_name_query'] = $filter['customer']['full_name']; |
| 120 | |
| 121 | } |
| 122 | |
| 123 | if (!empty($filter['created_at_from']) && !empty($filter['created_at_to'])) { |
| 124 | $query_args['created_at >='] = $filter['created_at_from'] . ' 00:00:00'; |
| 125 | $query_args['created_at <='] = $filter['created_at_to'] . ' 23:59:59'; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | // OUTPUT CSV IF REQUESTED |
| 131 | if (isset($this->params['download']) && $this->params['download'] == 'csv') { |
| 132 | $csv_filename = 'payments_' . OsUtilHelper::random_text() . '.csv'; |
| 133 | |
| 134 | header("Content-Type: text/csv"); |
| 135 | header("Content-Disposition: attachment; filename={$csv_filename}"); |
| 136 | |
| 137 | $labels_row = [__('ID', 'latepoint'), |
| 138 | __('Token', 'latepoint'), |
| 139 | __('Order ID', 'latepoint'), |
| 140 | __('Customer', 'latepoint'), |
| 141 | __('Processor', 'latepoint'), |
| 142 | __('Method', 'latepoint'), |
| 143 | __('Amount', 'latepoint'), |
| 144 | __('Status', 'latepoint'), |
| 145 | __('Type', 'latepoint'), |
| 146 | __('Date', 'latepoint')]; |
| 147 | |
| 148 | |
| 149 | $transactions_data = []; |
| 150 | $transactions_data[] = $labels_row; |
| 151 | |
| 152 | |
| 153 | $transactions_arr = $transactions->where($query_args)->filter_allowed_records()->get_results_as_models(); |
| 154 | |
| 155 | if ($transactions_arr) { |
| 156 | foreach ($transactions_arr as $transaction) { |
| 157 | $values_row = [ |
| 158 | $transaction->id, |
| 159 | $transaction->token, |
| 160 | $transaction->order_id, |
| 161 | ($transaction->customer_id ? $transaction->customer->full_name : 'n/a'), |
| 162 | $transaction->processor, |
| 163 | $transaction->payment_method, |
| 164 | OsMoneyHelper::format_price($transaction->amount, true, false), |
| 165 | $transaction->status, |
| 166 | $transaction->kind, |
| 167 | $transaction->created_at, |
| 168 | ]; |
| 169 | $values_row = apply_filters('latepoint_transaction_row_for_csv_export', $values_row, $transaction, $this->params); |
| 170 | $transactions_data[] = $values_row; |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | $transactions_data = apply_filters('latepoint_transactions_data_for_csv_export', $transactions_data, $this->params); |
| 176 | OsCSVHelper::array_to_csv($transactions_data); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if ($query_args) $transactions->where($query_args); |
| 181 | $transactions->filter_allowed_records(); |
| 182 | |
| 183 | |
| 184 | $count_transactions = clone $transactions; |
| 185 | $total_transactions = $count_transactions->count(); |
| 186 | |
| 187 | $transactions = $transactions->order_by(LATEPOINT_TABLE_TRANSACTIONS . '.created_at desc')->set_limit($per_page); |
| 188 | if ($page_number > 1) { |
| 189 | $transactions = $transactions->set_offset(($page_number - 1) * $per_page); |
| 190 | } |
| 191 | |
| 192 | $this->vars['transactions'] = $transactions->get_results_as_models(); |
| 193 | |
| 194 | $this->vars['total_transactions'] = $total_transactions; |
| 195 | $this->vars['current_page_number'] = $page_number; |
| 196 | $this->vars['per_page'] = $per_page; |
| 197 | $total_pages = ceil($total_transactions / $per_page); |
| 198 | $this->vars['total_pages'] = $total_pages; |
| 199 | |
| 200 | $this->vars['showing_from'] = (($page_number - 1) * $per_page) ? (($page_number - 1) * $per_page) : 1; |
| 201 | $this->vars['showing_to'] = min($page_number * $per_page, $total_transactions); |
| 202 | |
| 203 | $this->format_render(['json_view_name' => '_table_body', 'html_view_name' => __FUNCTION__], [], ['total_pages' => $total_pages, 'showing_from' => $this->vars['showing_from'], 'showing_to' => $this->vars['showing_to'], 'total_records' => $total_transactions]); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | public function refund_transaction() { |
| 208 | try { |
| 209 | if ( ! filter_var( $this->params['transaction_refund']['transaction_id'], FILTER_VALIDATE_INT ) ) { |
| 210 | throw new Exception(esc_html__( 'Invalid Transaction', 'latepoint' )); |
| 211 | } |
| 212 | |
| 213 | $transaction = new OsTransactionModel( $this->params['transaction_refund']['transaction_id'] ); |
| 214 | |
| 215 | if ( empty( $transaction ) || !$transaction->can_refund() ) { |
| 216 | throw new Exception(esc_html__( 'Invalid Transaction', 'latepoint' )); |
| 217 | } |
| 218 | |
| 219 | $refund_amount = ( $this->params['transaction_refund']['portion'] == 'custom' ) ? $this->params['transaction_refund']['custom_amount'] : ( $transaction->amount - $transaction->get_total_refunded_amount() ); |
| 220 | $refund_amount = OsParamsHelper::sanitize_param( $refund_amount, 'money' ); |
| 221 | |
| 222 | if ( empty( $refund_amount ) || $refund_amount > $transaction->amount - $transaction->get_total_refunded_amount() ) { |
| 223 | throw new Exception( __( 'Invalid Refund Amount', 'latepoint' ) ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Process refund for different payment gateways |
| 228 | * |
| 229 | * @param {bool | OsTransactionRefundModel} $transaction_refund |
| 230 | * @param {OsTransactionModel} $transaction |
| 231 | * @param {array} $refund_amount |
| 232 | * |
| 233 | * @returns {OsTransactionRefundModel} |
| 234 | * @since 5.1.8 |
| 235 | * @hook latepoint_process_refund |
| 236 | * |
| 237 | */ |
| 238 | $transaction_refund = apply_filters('latepoint_process_refund', false, $transaction, $refund_amount); |
| 239 | |
| 240 | if ( $transaction_refund ) { |
| 241 | $this->vars['transaction'] = new OsTransactionModel( $transaction->id ); # reload to get new refund info |
| 242 | $message = $this->render( LATEPOINT_VIEWS_ABSPATH . 'orders/_transaction_box', 'none' ); |
| 243 | $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $message ) ); |
| 244 | } |
| 245 | } catch ( Exception $e ) { |
| 246 | $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => $e->getMessage() ) ); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
| 251 | } |
| 252 | |
| 253 | |
| 254 | endif; |