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