PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.7
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.7
5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / models / transaction_intent_model.php
latepoint / lib / models Last commit date
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
333 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 public function calculate_specs_charge_amount() {
32 /**
33 * Convert transaction intent charge amount to specs
34 *
35 * @param {string} $charge_amount original charge amount of a transaction intent
36 * @param {OsTransactionIntentModel} $transaction_intent transaction intent model
37 *
38 * @returns {string} The filtered to specs charge amount
39 *
40 * @since 5.1.3
41 * @hook latepoint_transaction_intent_specs_charge_amount
42 *
43 */
44 $this->specs_charge_amount = apply_filters( 'latepoint_transaction_intent_specs_charge_amount', $this->charge_amount, $this );
45 }
46
47 protected function params_to_sanitize() {
48 return [
49 'charge_amount' => 'money',
50 ];
51 }
52
53
54 public function get_payment_data_value( string $key ): string {
55 if ( ! isset( $this->payment_data_arr ) ) {
56 $this->payment_data_arr = json_decode( $this->payment_data, true );
57 }
58
59 return $this->payment_data_arr[ $key ] ?? '';
60 }
61
62 public function set_payment_data_value( string $key, string $value, bool $save = true ) {
63 $this->payment_data_arr = json_decode( $this->payment_data, true );
64 $this->payment_data_arr[ $key ] = $value;
65 $this->payment_data = wp_json_encode( $this->payment_data_arr );
66 if ( $save ) {
67 $this->update_attributes( [ 'payment_data' => $this->payment_data ] );
68 }
69 }
70
71
72 public function is_processing(): bool {
73 return $this->status == LATEPOINT_TRANSACTION_INTENT_STATUS_PROCESSING;
74 }
75
76 public function convert_to_transaction() {
77 if($this->is_converted()){
78 return $this->transaction_id;
79 }
80
81 if($this->is_processing()){
82 $this->add_error( 'transaction_intent_error', __('Can not convert to transaction, because transaction intent conversion is being processed', 'latepoint') );
83 return false;
84 }
85
86 $this->mark_as_processing();
87
88 try {
89
90 // process payment if there is amount due
91 $transaction = OsPaymentsHelper::process_payment_for_transaction_intent( $this );
92 if(!$transaction || $transaction->status != LATEPOINT_TRANSACTION_STATUS_SUCCEEDED){
93 if(!$transaction){
94 $this->add_error('transaction_intent_error', __('No payment processor available to process this transaction intent', 'latepoint'));
95 }else{
96 if ( $transaction->get_error() ) {
97 $this->add_error('transaction_intent_error', $transaction->get_error_messages());
98 }
99 }
100 $this->mark_as_new();
101 return false;
102 }
103
104 /**
105 * Filters transaction right before it's about to be saved when converting from a transaction intent
106 *
107 * @param {OsTransactionModel} $transaction Transaction to be filtered
108 * @returns {OsTransactionModel} The filtered transaction
109 *
110 * @since 5.0.0
111 * @hook latepoint_before_transaction_save_from_transaction_intent
112 *
113 */
114 $transaction = apply_filters( 'latepoint_before_transaction_save_from_transaction_intent', $transaction );
115
116
117 if ( $transaction->save() ) {
118 $this->mark_as_converted( $transaction );
119
120 /**
121 * Transaction was created
122 *
123 * @param {OsTransactionModel} $transaction instance of transaction model that was created
124 *
125 * @since 5.0.0
126 * @hook latepoint_transaction_created
127 *
128 */
129 do_action( 'latepoint_transaction_created', $transaction );
130
131 if($transaction->invoice_id){
132 $invoice = new OsInvoiceModel($transaction->invoice_id);
133 if($invoice && !$invoice->is_new_record()){
134 $invoice->change_status(LATEPOINT_INVOICE_STATUS_PAID);
135 OsOrdersHelper::check_if_order_invoices_paid_full_balance($this->order_id);
136 }
137 }
138
139 return $transaction->id;
140 } else {
141 $this->add_error( 'transaction_intent_error', $transaction->get_error_messages() );
142
143 $this->mark_as_new();
144 return false;
145 }
146 } catch ( Exception $e ) {
147 $this->mark_as_new();
148 // translators: %s is the error description
149 $this->add_error( 'transaction_intent_error', sprintf(__('Error: %s', 'latepoint'), $e->getMessage() ));
150 OsDebugHelper::log( 'Error converting transaction intent to a transaction', 'transaction_intent_error', $e->getMessage() );
151 return false;
152 }
153 }
154
155 public function get_by_intent_key( $intent_key ) {
156 return $this->where( [ 'intent_key' => $intent_key ] )->set_limit( 1 )->get_results_as_models();
157 }
158
159 public function mark_as_converted( OsTransactionModel $transaction ) : bool {
160 if ( empty( $transaction->id ) ) {
161 return false;
162 }
163
164 $this->transaction_id = $transaction->id;
165 $this->status = LATEPOINT_TRANSACTION_INTENT_STATUS_CONVERTED;
166
167 if($this->save()){
168 /**
169 * Transaction intent is converted to transaction
170 *
171 * @param {OsTransactionIntentModel} $transaction_intent Instance of transaction intent model that has been converted to transaction
172 * @param {OsTransactionModel} $transaction Instance of transaction model that transaction intent was converted to
173 *
174 * @since 5.0.0
175 * @hook latepoint_transaction_intent_converted
176 *
177 */
178 do_action( 'latepoint_transaction_intent_converted', $this, $transaction );
179 return true;
180 }else{
181 return false;
182 }
183 }
184
185 public function mark_as_processing() {
186 $this->update_attributes( [ 'status' => LATEPOINT_TRANSACTION_INTENT_STATUS_PROCESSING ] );
187 /**
188 * Order intent is marked as processing
189 *
190 * @param {OsTransactionIntentModel} $order_intent Instance of order intent model that has started processing
191 *
192 * @since 5.0.0
193 * @hook latepoint_order_intent_processing
194 *
195 */
196 do_action( 'latepoint_order_intent_processing', $this );
197 }
198
199 public function mark_as_new() {
200 $this->update_attributes( [ 'status' => LATEPOINT_TRANSACTION_INTENT_STATUS_NEW ] );
201 /**
202 * Order intent is marked as new
203 *
204 * @param {OsTransactionIntentModel} $order_intent Instance of order intent model that is being marked as new
205 *
206 * @since 5.0.0
207 * @hook latepoint_order_intent_new
208 *
209 */
210 do_action( 'latepoint_order_intent_new', $this );
211 }
212
213 // Determines if order intent has been converted into a order already
214 public function is_converted() : bool {
215 if ( empty( $this->transaction_id ) ) {
216 return false;
217 } else {
218 return true;
219 }
220 }
221
222 public function generate_data_vars(): array {
223 $vars = [
224 'id' => $this->id,
225 'intent_key' => $this->intent_key,
226 'payment_data' => !empty($this->payment_data) ? json_decode( $this->payment_data, true ) : [],
227 'order_id' => $this->order_id,
228 'transaction_id' => $this->transaction_id,
229 'updated_at' => $this->updated_at,
230 'created_at' => $this->created_at,
231 ];
232
233 return $vars;
234 }
235
236 public function get_page_url_with_intent() {
237 $booking_page_url = $this->booking_form_page_url;
238 $existing_var_position = strpos( $booking_page_url, 'latepoint_transaction_intent_key=' );
239 if ( $existing_var_position === false ) {
240 // no intent variable in url
241 $question_position = strpos( $booking_page_url, '?' );
242 if ( $question_position === false ) {
243 // no ?query params
244 $hash_position = strpos( $booking_page_url, '#' );
245 if ( $hash_position === false ) {
246 // no hashtag in url
247 $booking_page_url = $booking_page_url . '?latepoint_transaction_intent_key=' . $this->intent_key;
248 } else {
249 // hashtag in url and no ?query, prepend the hashtag with query
250 $booking_page_url = substr_replace( $booking_page_url, '?latepoint_transaction_intent_key=' . $this->intent_key . '#', $hash_position, 1 );
251 }
252 } else {
253 // ?query string exists, add intent key to it
254 $booking_page_url = substr_replace( $booking_page_url, '?latepoint_transaction_intent_key=' . $this->intent_key . '&', $question_position, 1 );
255 }
256 } else {
257 // intent key variable exist in url
258 preg_match( '/latepoint_transaction_intent_key=([\d,\w]*)/', $booking_page_url, $matches );
259 if ( isset( $matches[1] ) ) {
260 $booking_page_url = str_replace( 'latepoint_transaction_intent_key=' . $matches[1], 'latepoint_transaction_intent_key=' . $this->intent_key, $booking_page_url );
261 }
262 }
263
264 return $booking_page_url;
265 }
266
267 public function generate_intent_key() {
268 $this->intent_key = bin2hex( openssl_random_pseudo_bytes( 10 ) );
269 }
270
271
272 public function get_customer() : OsCustomerModel {
273 if ( $this->customer_id ) {
274 if ( ! isset( $this->customer ) || ( isset( $this->customer ) && ( $this->customer->id != $this->customer_id ) ) ) {
275 $this->customer = new OsCustomerModel( $this->customer_id );
276 }
277 } else {
278 $this->customer = new OsCustomerModel();
279 }
280
281 return $this->customer;
282 }
283
284
285 protected function before_create() {
286 if ( empty( $this->intent_key ) ) {
287 $this->intent_key = bin2hex( openssl_random_pseudo_bytes( 10 ) );
288 }
289 if ( empty( $this->status ) ) {
290 $this->status = LATEPOINT_TRANSACTION_INTENT_STATUS_NEW;
291 }
292 }
293
294 protected function allowed_params( $role = 'admin' ) {
295 $allowed_params = array(
296 'payment_data',
297 'intent_key',
298 'order_id',
299 'customer_id',
300 'invoice_id',
301 'transaction_id',
302 'status',
303 );
304
305 return $allowed_params;
306 }
307
308
309 protected function params_to_save( $role = 'admin' ) {
310 $params_to_save = array(
311 'payment_data',
312 'intent_key',
313 'charge_amount',
314 'specs_charge_amount',
315 'order_id',
316 'customer_id',
317 'invoice_id',
318 'transaction_id',
319 'status',
320 );
321
322 return $params_to_save;
323 }
324
325
326 protected function properties_to_validate() {
327 $validations = array(
328 'order_id' => array( 'presence' ),
329 );
330
331 return $validations;
332 }
333 }