activity_model.php
1 year ago
agent_meta_model.php
1 year ago
agent_model.php
1 year ago
booking_meta_model.php
1 year ago
booking_model.php
1 year ago
bundle_model.php
1 year ago
cart_item_model.php
1 year ago
cart_meta_model.php
1 year ago
cart_model.php
1 year ago
connector_model.php
1 year ago
customer_meta_model.php
1 year ago
customer_model.php
1 year ago
invoice_model.php
1 year ago
join_bundles_services_model.php
1 year ago
location_category_model.php
1 year ago
location_model.php
1 year ago
meta_model.php
1 year ago
model.php
1 year ago
off_period_model.php
1 year ago
order_intent_meta_model.php
1 year ago
order_intent_model.php
1 year ago
order_item_model.php
1 year ago
order_meta_model.php
1 year ago
order_model.php
1 year ago
payment_request_model.php
1 year ago
process_job_model.php
1 year ago
process_model.php
1 year ago
recurrence_model.php
1 year ago
service_category_model.php
1 year ago
service_meta_model.php
1 year ago
service_model.php
1 year ago
session_model.php
1 year ago
settings_model.php
1 year ago
step_settings_model.php
1 year ago
transaction_intent_model.php
1 year ago
transaction_model.php
1 year ago
transaction_refund_model.php
1 year ago
work_period_model.php
1 year ago
transaction_intent_model.php
336 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2024 LatePoint LLC. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | class OsTransactionIntentModel extends OsModel { |
| 7 | var $id, |
| 8 | $intent_key, |
| 9 | $order_id, |
| 10 | $customer_id, |
| 11 | $invoice_id, |
| 12 | $transaction_id, |
| 13 | $payment_data = '', |
| 14 | $payment_data_arr, |
| 15 | $other_data, |
| 16 | $charge_amount, |
| 17 | $specs_charge_amount, |
| 18 | $status, |
| 19 | $order_form_page_url, |
| 20 | $updated_at, |
| 21 | $created_at; |
| 22 | |
| 23 | function __construct( $id = false ) { |
| 24 | parent::__construct(); |
| 25 | $this->table_name = LATEPOINT_TABLE_TRANSACTION_INTENTS; |
| 26 | |
| 27 | if ( $id ) { |
| 28 | $this->load_by_id( $id ); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public function calculate_specs_charge_amount() { |
| 33 | /** |
| 34 | * Convert transaction intent charge amount to specs |
| 35 | * |
| 36 | * @param {string} $charge_amount original charge amount of a transaction intent |
| 37 | * @param {OsTransactionIntentModel} $transaction_intent transaction intent model |
| 38 | * |
| 39 | * @returns {string} The filtered to specs charge amount |
| 40 | * |
| 41 | * @since 5.1.3 |
| 42 | * @hook latepoint_transaction_intent_specs_charge_amount |
| 43 | * |
| 44 | */ |
| 45 | $this->specs_charge_amount = apply_filters( 'latepoint_transaction_intent_specs_charge_amount', $this->charge_amount, $this ); |
| 46 | } |
| 47 | |
| 48 | protected function params_to_sanitize() { |
| 49 | return [ |
| 50 | 'charge_amount' => 'money', |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | public function get_payment_data_value( string $key ): string { |
| 56 | if ( ! isset( $this->payment_data_arr ) ) { |
| 57 | $this->payment_data_arr = json_decode( $this->payment_data, true ); |
| 58 | } |
| 59 | |
| 60 | return $this->payment_data_arr[ $key ] ?? ''; |
| 61 | } |
| 62 | |
| 63 | public function set_payment_data_value( string $key, string $value, bool $save = true ) { |
| 64 | $this->payment_data_arr = json_decode( $this->payment_data, true ); |
| 65 | $this->payment_data_arr[ $key ] = $value; |
| 66 | $this->payment_data = wp_json_encode( $this->payment_data_arr ); |
| 67 | if ( $save ) { |
| 68 | $this->update_attributes( [ 'payment_data' => $this->payment_data ] ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | public function is_processing(): bool { |
| 74 | return $this->status == LATEPOINT_TRANSACTION_INTENT_STATUS_PROCESSING; |
| 75 | } |
| 76 | |
| 77 | public function convert_to_transaction() { |
| 78 | if($this->is_converted()){ |
| 79 | return $this->transaction_id; |
| 80 | } |
| 81 | |
| 82 | if($this->is_processing()){ |
| 83 | $this->add_error( 'transaction_intent_error', __('Can not convert to transaction, because transaction intent conversion is being processed', 'latepoint') ); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | $this->mark_as_processing(); |
| 88 | |
| 89 | try { |
| 90 | |
| 91 | // process payment if there is amount due |
| 92 | $transaction = OsPaymentsHelper::process_payment_for_transaction_intent( $this ); |
| 93 | if(!$transaction || $transaction->status != LATEPOINT_TRANSACTION_STATUS_SUCCEEDED){ |
| 94 | if(!$transaction){ |
| 95 | $this->add_error('transaction_intent_error', __('No payment processor available to process this transaction intent', 'latepoint')); |
| 96 | }else{ |
| 97 | if ( $transaction->get_error() ) { |
| 98 | $this->add_error('transaction_intent_error', $transaction->get_error_messages()); |
| 99 | } |
| 100 | } |
| 101 | $this->mark_as_new(); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Filters transaction right before it's about to be saved when converting from a transaction intent |
| 107 | * |
| 108 | * @param {OsTransactionModel} $transaction Transaction to be filtered |
| 109 | * @returns {OsTransactionModel} The filtered transaction |
| 110 | * |
| 111 | * @since 5.0.0 |
| 112 | * @hook latepoint_before_transaction_save_from_transaction_intent |
| 113 | * |
| 114 | */ |
| 115 | $transaction = apply_filters( 'latepoint_before_transaction_save_from_transaction_intent', $transaction ); |
| 116 | |
| 117 | |
| 118 | if ( $transaction->save() ) { |
| 119 | $this->mark_as_converted( $transaction ); |
| 120 | |
| 121 | /** |
| 122 | * Transaction was created |
| 123 | * |
| 124 | * @param {OsTransactionModel} $transaction instance of transaction model that was created |
| 125 | * |
| 126 | * @since 5.0.0 |
| 127 | * @hook latepoint_transaction_created |
| 128 | * |
| 129 | */ |
| 130 | do_action( 'latepoint_transaction_created', $transaction ); |
| 131 | |
| 132 | if($transaction->invoice_id){ |
| 133 | $invoice = new OsInvoiceModel($transaction->invoice_id); |
| 134 | if($invoice && !$invoice->is_new_record()){ |
| 135 | $invoice->change_status(LATEPOINT_INVOICE_STATUS_PAID); |
| 136 | OsOrdersHelper::check_if_order_invoices_paid_full_balance($this->order_id); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return $transaction->id; |
| 141 | } else { |
| 142 | $this->add_error( 'transaction_intent_error', $transaction->get_error_messages() ); |
| 143 | |
| 144 | $this->mark_as_new(); |
| 145 | return false; |
| 146 | } |
| 147 | } catch ( Exception $e ) { |
| 148 | $this->mark_as_new(); |
| 149 | // translators: %s is the error description |
| 150 | $this->add_error( 'transaction_intent_error', sprintf(__('Error: %s', 'latepoint'), $e->getMessage() )); |
| 151 | OsDebugHelper::log( 'Error converting transaction intent to a transaction', 'transaction_intent_error', $e->getMessage() ); |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public function get_by_intent_key( $intent_key ) { |
| 157 | return $this->where( [ 'intent_key' => $intent_key ] )->set_limit( 1 )->get_results_as_models(); |
| 158 | } |
| 159 | |
| 160 | public function mark_as_converted( OsTransactionModel $transaction ) : bool { |
| 161 | if ( empty( $transaction->id ) ) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | $this->transaction_id = $transaction->id; |
| 166 | $this->status = LATEPOINT_TRANSACTION_INTENT_STATUS_CONVERTED; |
| 167 | |
| 168 | if($this->save()){ |
| 169 | /** |
| 170 | * Transaction intent is converted to transaction |
| 171 | * |
| 172 | * @param {OsTransactionIntentModel} $transaction_intent Instance of transaction intent model that has been converted to transaction |
| 173 | * @param {OsTransactionModel} $transaction Instance of transaction model that transaction intent was converted to |
| 174 | * |
| 175 | * @since 5.0.0 |
| 176 | * @hook latepoint_transaction_intent_converted |
| 177 | * |
| 178 | */ |
| 179 | do_action( 'latepoint_transaction_intent_converted', $this, $transaction ); |
| 180 | return true; |
| 181 | }else{ |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | public function mark_as_processing() { |
| 187 | $this->update_attributes( [ 'status' => LATEPOINT_TRANSACTION_INTENT_STATUS_PROCESSING ] ); |
| 188 | /** |
| 189 | * Order intent is marked as processing |
| 190 | * |
| 191 | * @param {OsTransactionIntentModel} $order_intent Instance of order intent model that has started processing |
| 192 | * |
| 193 | * @since 5.0.0 |
| 194 | * @hook latepoint_order_intent_processing |
| 195 | * |
| 196 | */ |
| 197 | do_action( 'latepoint_order_intent_processing', $this ); |
| 198 | } |
| 199 | |
| 200 | public function mark_as_new() { |
| 201 | $this->update_attributes( [ 'status' => LATEPOINT_TRANSACTION_INTENT_STATUS_NEW ] ); |
| 202 | /** |
| 203 | * Order intent is marked as new |
| 204 | * |
| 205 | * @param {OsTransactionIntentModel} $order_intent Instance of order intent model that is being marked as new |
| 206 | * |
| 207 | * @since 5.0.0 |
| 208 | * @hook latepoint_order_intent_new |
| 209 | * |
| 210 | */ |
| 211 | do_action( 'latepoint_order_intent_new', $this ); |
| 212 | } |
| 213 | |
| 214 | // Determines if order intent has been converted into a order already |
| 215 | public function is_converted() : bool { |
| 216 | if ( empty( $this->transaction_id ) ) { |
| 217 | return false; |
| 218 | } else { |
| 219 | return true; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | public function generate_data_vars(): array { |
| 224 | $vars = [ |
| 225 | 'id' => $this->id, |
| 226 | 'intent_key' => $this->intent_key, |
| 227 | 'payment_data' => !empty($this->payment_data) ? json_decode( $this->payment_data, true ) : [], |
| 228 | 'order_id' => $this->order_id, |
| 229 | 'transaction_id' => $this->transaction_id, |
| 230 | 'order_form_page_url' => $this->order_form_page_url, |
| 231 | 'updated_at' => $this->updated_at, |
| 232 | 'created_at' => $this->created_at, |
| 233 | ]; |
| 234 | |
| 235 | return $vars; |
| 236 | } |
| 237 | |
| 238 | public function get_page_url_with_intent() { |
| 239 | $order_form_page_url = $this->order_form_page_url; |
| 240 | $existing_var_position = strpos( $order_form_page_url, 'latepoint_transaction_intent_key=' ); |
| 241 | if ( $existing_var_position === false ) { |
| 242 | // no intent variable in url |
| 243 | $question_position = strpos( $order_form_page_url, '?' ); |
| 244 | if ( $question_position === false ) { |
| 245 | // no ?query params |
| 246 | $hash_position = strpos( $order_form_page_url, '#' ); |
| 247 | if ( $hash_position === false ) { |
| 248 | // no hashtag in url |
| 249 | $order_form_page_url = $order_form_page_url . '?latepoint_transaction_intent_key=' . $this->intent_key; |
| 250 | } else { |
| 251 | // hashtag in url and no ?query, prepend the hashtag with query |
| 252 | $order_form_page_url = substr_replace( $order_form_page_url, '?latepoint_transaction_intent_key=' . $this->intent_key . '#', $hash_position, 1 ); |
| 253 | } |
| 254 | } else { |
| 255 | // ?query string exists, add intent key to it |
| 256 | $order_form_page_url = substr_replace( $order_form_page_url, '?latepoint_transaction_intent_key=' . $this->intent_key . '&', $question_position, 1 ); |
| 257 | } |
| 258 | } else { |
| 259 | // intent key variable exist in url |
| 260 | preg_match( '/latepoint_transaction_intent_key=([\d,\w]*)/', $order_form_page_url, $matches ); |
| 261 | if ( isset( $matches[1] ) ) { |
| 262 | $order_form_page_url = str_replace( 'latepoint_transaction_intent_key=' . $matches[1], 'latepoint_transaction_intent_key=' . $this->intent_key, $order_form_page_url ); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return $order_form_page_url; |
| 267 | } |
| 268 | |
| 269 | public function generate_intent_key() { |
| 270 | $this->intent_key = bin2hex( openssl_random_pseudo_bytes( 10 ) ); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | public function get_customer() : OsCustomerModel { |
| 275 | if ( $this->customer_id ) { |
| 276 | if ( ! isset( $this->customer ) || ( isset( $this->customer ) && ( $this->customer->id != $this->customer_id ) ) ) { |
| 277 | $this->customer = new OsCustomerModel( $this->customer_id ); |
| 278 | } |
| 279 | } else { |
| 280 | $this->customer = new OsCustomerModel(); |
| 281 | } |
| 282 | |
| 283 | return $this->customer; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | protected function before_create() { |
| 288 | if ( empty( $this->intent_key ) ) { |
| 289 | $this->intent_key = bin2hex( openssl_random_pseudo_bytes( 10 ) ); |
| 290 | } |
| 291 | if ( empty( $this->status ) ) { |
| 292 | $this->status = LATEPOINT_TRANSACTION_INTENT_STATUS_NEW; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | protected function allowed_params( $role = 'admin' ) { |
| 297 | $allowed_params = array( |
| 298 | 'payment_data', |
| 299 | 'intent_key', |
| 300 | 'order_id', |
| 301 | 'customer_id', |
| 302 | 'invoice_id', |
| 303 | 'transaction_id', |
| 304 | 'order_form_page_url', |
| 305 | 'status', |
| 306 | ); |
| 307 | |
| 308 | return $allowed_params; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | protected function params_to_save( $role = 'admin' ) { |
| 313 | $params_to_save = array( |
| 314 | 'payment_data', |
| 315 | 'intent_key', |
| 316 | 'charge_amount', |
| 317 | 'specs_charge_amount', |
| 318 | 'order_id', |
| 319 | 'customer_id', |
| 320 | 'invoice_id', |
| 321 | 'transaction_id', |
| 322 | 'status', |
| 323 | ); |
| 324 | |
| 325 | return $params_to_save; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | protected function properties_to_validate() { |
| 330 | $validations = array( |
| 331 | 'order_id' => array( 'presence' ), |
| 332 | ); |
| 333 | |
| 334 | return $validations; |
| 335 | } |
| 336 | } |