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
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
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
296 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 | $updated_at, |
| 20 | $created_at; |
| 21 | |
| 22 | function __construct( $id = false ) { |
| 23 | parent::__construct(); |
| 24 | $this->table_name = LATEPOINT_TABLE_TRANSACTION_INTENTS; |
| 25 | |
| 26 | if ( $id ) { |
| 27 | $this->load_by_id( $id ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | protected function params_to_sanitize() { |
| 32 | return [ |
| 33 | 'charge_amount' => 'money', |
| 34 | ]; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | public function get_payment_data_value( string $key ): string { |
| 39 | if ( ! isset( $this->payment_data_arr ) ) { |
| 40 | $this->payment_data_arr = json_decode( $this->payment_data, true ); |
| 41 | } |
| 42 | |
| 43 | return $this->payment_data_arr[ $key ] ?? ''; |
| 44 | } |
| 45 | |
| 46 | public function set_payment_data_value( string $key, string $value, bool $save = true ) { |
| 47 | $this->payment_data_arr = json_decode( $this->payment_data, true ); |
| 48 | $this->payment_data_arr[ $key ] = $value; |
| 49 | $this->payment_data = wp_json_encode( $this->payment_data_arr ); |
| 50 | if ( $save ) { |
| 51 | $this->update_attributes( [ 'payment_data' => $this->payment_data ] ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | public function is_processing(): bool { |
| 57 | return $this->status == LATEPOINT_TRANSACTION_INTENT_STATUS_PROCESSING; |
| 58 | } |
| 59 | |
| 60 | public function convert_to_transaction() { |
| 61 | if($this->is_converted()){ |
| 62 | return $this->transaction_id; |
| 63 | } |
| 64 | |
| 65 | if($this->is_processing()){ |
| 66 | $this->add_error( 'transaction_intent_error', __('Can not convert to transaction, because transaction intent conversion is being processed', 'latepoint') ); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | $this->mark_as_processing(); |
| 71 | |
| 72 | try { |
| 73 | |
| 74 | // process payment if there is amount due |
| 75 | $transaction = OsPaymentsHelper::process_payment_for_transaction_intent( $this ); |
| 76 | if(!$transaction || $transaction->status != LATEPOINT_TRANSACTION_STATUS_SUCCEEDED){ |
| 77 | if(!$transaction){ |
| 78 | $this->add_error('transaction_intent_error', __('No payment processor available to process this transaction intent', 'latepoint')); |
| 79 | }else{ |
| 80 | if ( $transaction->get_error() ) { |
| 81 | $this->add_error('transaction_intent_error', $transaction->get_error_messages()); |
| 82 | } |
| 83 | } |
| 84 | $this->mark_as_new(); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Filters transaction right before it's about to be saved when converting from a transaction intent |
| 90 | * |
| 91 | * @param {OsTransactionModel} $transaction Transaction to be filtered |
| 92 | * @returns {OsTransactionModel} The filtered transaction |
| 93 | * |
| 94 | * @since 5.0.0 |
| 95 | * @hook latepoint_before_transaction_save_from_transaction_intent |
| 96 | * |
| 97 | */ |
| 98 | $transaction = apply_filters( 'latepoint_before_transaction_save_from_transaction_intent', $transaction ); |
| 99 | |
| 100 | |
| 101 | if ( $transaction->save() ) { |
| 102 | $this->mark_as_converted( $transaction ); |
| 103 | |
| 104 | /** |
| 105 | * Transaction was created |
| 106 | * |
| 107 | * @param {OsTransactionModel} $transaction instance of transaction model that was created |
| 108 | * |
| 109 | * @since 5.0.0 |
| 110 | * @hook latepoint_transaction_created |
| 111 | * |
| 112 | */ |
| 113 | do_action( 'latepoint_transaction_created', $transaction ); |
| 114 | |
| 115 | if($transaction->invoice_id){ |
| 116 | $invoice = new OsInvoiceModel($transaction->invoice_id); |
| 117 | if($invoice && !$invoice->is_new_record()){ |
| 118 | $invoice->change_status(LATEPOINT_INVOICE_STATUS_PAID); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $transaction->id; |
| 123 | } else { |
| 124 | $this->add_error( 'transaction_intent_error', $transaction->get_error_messages() ); |
| 125 | |
| 126 | $this->mark_as_new(); |
| 127 | return false; |
| 128 | } |
| 129 | } catch ( Exception $e ) { |
| 130 | $this->mark_as_new(); |
| 131 | // translators: %s is the error description |
| 132 | $this->add_error( 'transaction_intent_error', sprintf(__('Error: %s', 'latepoint'), $e->getMessage() )); |
| 133 | OsDebugHelper::log( 'Error converting transaction intent to a transaction', 'transaction_intent_error', $e->getMessage() ); |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public function get_by_intent_key( $intent_key ) { |
| 139 | return $this->where( [ 'intent_key' => $intent_key ] )->set_limit( 1 )->get_results_as_models(); |
| 140 | } |
| 141 | |
| 142 | public function mark_as_converted( OsTransactionModel $transaction ) { |
| 143 | if ( empty( $transaction->id ) ) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | $this->update_attributes( [ 'transaction_id' => $transaction->id, 'status' => LATEPOINT_TRANSACTION_INTENT_STATUS_CONVERTED ] ); |
| 148 | /** |
| 149 | * Transaction intent is converted to transaction |
| 150 | * |
| 151 | * @param {OsTransactionIntentModel} $transaction_intent Instance of transaction intent model that has been converted to transaction |
| 152 | * @param {OsTransactionModel} $transaction Instance of transaction model that transaction intent was converted to |
| 153 | * |
| 154 | * @since 5.0.0 |
| 155 | * @hook latepoint_transaction_intent_converted |
| 156 | * |
| 157 | */ |
| 158 | do_action( 'latepoint_transaction_intent_converted', $this, $transaction ); |
| 159 | } |
| 160 | |
| 161 | public function mark_as_processing() { |
| 162 | $this->update_attributes( [ 'status' => LATEPOINT_TRANSACTION_INTENT_STATUS_PROCESSING ] ); |
| 163 | /** |
| 164 | * Order intent is marked as processing |
| 165 | * |
| 166 | * @param {OsTransactionIntentModel} $order_intent Instance of order intent model that has started processing |
| 167 | * |
| 168 | * @since 5.0.0 |
| 169 | * @hook latepoint_order_intent_processing |
| 170 | * |
| 171 | */ |
| 172 | do_action( 'latepoint_order_intent_processing', $this ); |
| 173 | } |
| 174 | |
| 175 | public function mark_as_new() { |
| 176 | $this->update_attributes( [ 'status' => LATEPOINT_TRANSACTION_INTENT_STATUS_NEW ] ); |
| 177 | /** |
| 178 | * Order intent is marked as new |
| 179 | * |
| 180 | * @param {OsTransactionIntentModel} $order_intent Instance of order intent model that is being marked as new |
| 181 | * |
| 182 | * @since 5.0.0 |
| 183 | * @hook latepoint_order_intent_new |
| 184 | * |
| 185 | */ |
| 186 | do_action( 'latepoint_order_intent_new', $this ); |
| 187 | } |
| 188 | |
| 189 | // Determines if order intent has been converted into a order already |
| 190 | public function is_converted() : bool { |
| 191 | if ( empty( $this->transaction_id ) ) { |
| 192 | return false; |
| 193 | } else { |
| 194 | return true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | public function generate_data_vars(): array { |
| 199 | $vars = [ |
| 200 | 'id' => $this->id, |
| 201 | 'intent_key' => $this->intent_key, |
| 202 | 'payment_data' => !empty($this->payment_data) ? json_decode( $this->payment_data, true ) : [], |
| 203 | 'order_id' => $this->order_id, |
| 204 | 'transaction_id' => $this->transaction_id, |
| 205 | 'updated_at' => $this->updated_at, |
| 206 | 'created_at' => $this->created_at, |
| 207 | ]; |
| 208 | |
| 209 | return $vars; |
| 210 | } |
| 211 | |
| 212 | public function get_page_url_with_intent() { |
| 213 | $booking_page_url = $this->booking_form_page_url; |
| 214 | $existing_var_position = strpos( $booking_page_url, 'latepoint_transaction_intent_key=' ); |
| 215 | if ( $existing_var_position === false ) { |
| 216 | // no intent variable in url |
| 217 | $question_position = strpos( $booking_page_url, '?' ); |
| 218 | if ( $question_position === false ) { |
| 219 | // no ?query params |
| 220 | $hash_position = strpos( $booking_page_url, '#' ); |
| 221 | if ( $hash_position === false ) { |
| 222 | // no hashtag in url |
| 223 | $booking_page_url = $booking_page_url . '?latepoint_transaction_intent_key=' . $this->intent_key; |
| 224 | } else { |
| 225 | // hashtag in url and no ?query, prepend the hashtag with query |
| 226 | $booking_page_url = substr_replace( $booking_page_url, '?latepoint_transaction_intent_key=' . $this->intent_key . '#', $hash_position, 1 ); |
| 227 | } |
| 228 | } else { |
| 229 | // ?query string exists, add intent key to it |
| 230 | $booking_page_url = substr_replace( $booking_page_url, '?latepoint_transaction_intent_key=' . $this->intent_key . '&', $question_position, 1 ); |
| 231 | } |
| 232 | } else { |
| 233 | // intent key variable exist in url |
| 234 | preg_match( '/latepoint_transaction_intent_key=([\d,\w]*)/', $booking_page_url, $matches ); |
| 235 | if ( isset( $matches[1] ) ) { |
| 236 | $booking_page_url = str_replace( 'latepoint_transaction_intent_key=' . $matches[1], 'latepoint_transaction_intent_key=' . $this->intent_key, $booking_page_url ); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return $booking_page_url; |
| 241 | } |
| 242 | |
| 243 | public function generate_intent_key() { |
| 244 | $this->intent_key = bin2hex( openssl_random_pseudo_bytes( 10 ) ); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | protected function before_create() { |
| 249 | if ( empty( $this->intent_key ) ) { |
| 250 | $this->intent_key = bin2hex( openssl_random_pseudo_bytes( 10 ) ); |
| 251 | } |
| 252 | if ( empty( $this->status ) ) { |
| 253 | $this->status = LATEPOINT_TRANSACTION_INTENT_STATUS_NEW; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | protected function allowed_params( $role = 'admin' ) { |
| 258 | $allowed_params = array( |
| 259 | 'payment_data', |
| 260 | 'intent_key', |
| 261 | 'order_id', |
| 262 | 'customer_id', |
| 263 | 'invoice_id', |
| 264 | 'transaction_id', |
| 265 | 'status', |
| 266 | ); |
| 267 | |
| 268 | return $allowed_params; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | protected function params_to_save( $role = 'admin' ) { |
| 273 | $params_to_save = array( |
| 274 | 'payment_data', |
| 275 | 'intent_key', |
| 276 | 'charge_amount', |
| 277 | 'specs_charge_amount', |
| 278 | 'order_id', |
| 279 | 'customer_id', |
| 280 | 'invoice_id', |
| 281 | 'transaction_id', |
| 282 | 'status', |
| 283 | ); |
| 284 | |
| 285 | return $params_to_save; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | protected function properties_to_validate() { |
| 290 | $validations = array( |
| 291 | 'order_id' => array( 'presence' ), |
| 292 | ); |
| 293 | |
| 294 | return $validations; |
| 295 | } |
| 296 | } |