PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / trunk
LatePoint – Calendar Booking Plugin for Appointments and Events vtrunk
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 / helpers / payments_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 3 months ago agent_helper.php 3 months ago analytics_helper.php 2 weeks ago auth_helper.php 1 week ago blocks_helper.php 3 weeks ago booking_helper.php 6 days ago bricks_helper.php 3 months ago bundles_helper.php 6 days ago calendar_helper.php 4 days ago carts_helper.php 3 months ago connector_helper.php 3 months ago csv_helper.php 3 months ago customer_helper.php 1 month ago customer_import_helper.php 1 month ago database_helper.php 2 weeks ago debug_helper.php 3 months ago defaults_helper.php 3 months ago elementor_helper.php 3 months ago email_helper.php 3 months ago encrypt_helper.php 3 months ago events_helper.php 3 months ago form_helper.php 3 months ago icalendar_helper.php 3 months ago image_helper.php 3 months ago invoices_helper.php 3 weeks ago license_helper.php 3 months ago location_helper.php 3 months ago marketing_systems_helper.php 3 months ago meeting_systems_helper.php 3 months ago menu_helper.php 3 weeks ago meta_helper.php 3 months ago migrations_helper.php 3 months ago money_helper.php 3 months ago notifications_helper.php 3 months ago nps_survey_helper.php 3 months ago order_intent_helper.php 2 months ago orders_helper.php 2 days ago otp_helper.php 3 months ago pages_helper.php 3 months ago params_helper.php 3 months ago payments_helper.php 2 days ago plugin_version_update_helper.php 2 months ago price_breakdown_helper.php 3 months ago process_jobs_helper.php 3 months ago processes_helper.php 3 months ago razorpay_connect_helper.php 2 weeks ago replacer_helper.php 3 months ago resource_helper.php 6 days ago roles_helper.php 3 weeks ago router_helper.php 3 months ago service_helper.php 3 months ago sessions_helper.php 3 months ago settings_helper.php 2 weeks ago short_links_systems_helper.php 3 months ago shortcodes_helper.php 3 weeks ago sms_helper.php 3 months ago steps_helper.php 2 days ago stripe_connect_helper.php 2 weeks ago styles_helper.php 3 months ago support_topics_helper.php 3 months ago time_helper.php 2 months ago timeline_helper.php 2 months ago transaction_helper.php 1 month ago transaction_intent_helper.php 3 months ago util_helper.php 1 month ago version_specific_updates_helper.php 3 months ago whatsapp_helper.php 1 month ago work_periods_helper.php 3 months ago wp_datetime.php 3 months ago wp_user_helper.php 3 months ago
payments_helper.php
440 lines
1 <?php
2
3 class OsPaymentsHelper {
4
5 public static function get_payment_processors_for_select( $enabled_only = false, $include_other = false ) {
6 $processors_for_select = [];
7 $processors = self::get_payment_processors( $enabled_only );
8 foreach ( $processors as $processor ) {
9 $processors_for_select[ $processor['code'] ] = $processor['name'];
10 }
11 if ( $include_other ) {
12 $processors_for_select['other'] = __( 'Other', 'latepoint' );
13 }
14
15 return apply_filters( 'latepoint_payment_processors_for_select', $processors_for_select, $enabled_only, $include_other );
16 }
17
18 public static function get_payment_processors( bool $enabled_only = false ) {
19 $payment_processors = [];
20
21 /**
22 * Result an array of registered payment processors, array looks like this:
23 * $payment_processors['stripe'] = ['code' => 'stripe', 'name' => 'Stripe', 'image_url' => 'URL_TO_LOGO'];
24 *
25 * @param {array} $payment_processors The array of payment processors
26 *
27 * @returns {array} The array of payment processors
28 * @since 5.0.0
29 * @hook latepoint_payment_processors
30 *
31 */
32 $payment_processors = apply_filters( 'latepoint_payment_processors', $payment_processors );
33 if ( $enabled_only ) {
34 $enabled_processors = [];
35 foreach ( $payment_processors as $payment_processor ) {
36 if ( OsPaymentsHelper::is_payment_processor_enabled( $payment_processor['code'] ?? '' ) ) {
37 $enabled_processors[] = $payment_processor;
38 }
39 }
40 $payment_processors = $enabled_processors;
41 }
42
43 return $payment_processors;
44 }
45
46 public static function get_nice_payment_method_name( $code ) {
47 $payment_methods = OsPaymentsHelper::get_all_payment_methods_for_select();
48
49 return $payment_methods[ $code ] ?? $code;
50 }
51
52
53 public static function get_nice_payment_processor_name( $code ) {
54 $processors = OsPaymentsHelper::get_payment_processors();
55
56 return $processors[ $code ]['name'] ?? $code;
57 }
58
59 public static function get_enabled_payment_processors() {
60 return self::get_payment_processors( true );
61 }
62
63 public static function is_local_payments_enabled() {
64 return OsSettingsHelper::is_on( 'enable_payments_local' );
65 }
66
67 public static function is_accepting_payments(): bool {
68 $enabled_payment_methods = self::get_enabled_payment_methods_for_payment_time( LATEPOINT_PAYMENT_TIME_NOW );
69 return ! empty( $enabled_payment_methods );
70 }
71
72 public static function is_payment_processor_enabled( $processor_code ) {
73 return OsSettingsHelper::is_on( 'enable_payment_processor_' . $processor_code );
74 }
75
76 public static function should_processor_handle_payment_for_transaction_intent( string $processor_code, OsTransactionIntentModel $transaction_intent ): bool {
77 if ( $transaction_intent->get_payment_data_value( 'processor' ) != $processor_code ) {
78 return false;
79 }
80 $payment_times = self::get_enabled_payment_times();
81
82 return ! empty( $payment_times[ LATEPOINT_PAYMENT_TIME_NOW ][ $transaction_intent->get_payment_data_value( 'method' ) ][ $transaction_intent->get_payment_data_value( 'processor' ) ] );
83 }
84
85 public static function should_processor_handle_payment_for_cart( string $processor_code, OsCartModel $cart ): bool {
86 if ( $cart->payment_processor != $processor_code ) {
87 return false;
88 }
89 $payment_times = self::get_enabled_payment_times();
90
91 return ! empty( $payment_times[ $cart->payment_time ][ $cart->payment_method ][ $cart->payment_processor ] );
92 }
93
94 public static function should_processor_handle_payment_for_order_intent( string $processor_code, OsOrderIntentModel $order_intent ): bool {
95 $cart = new OsCartModel();
96 $cart->payment_processor = $order_intent->get_payment_data_value( 'processor' );
97 $cart->payment_time = $order_intent->get_payment_data_value( 'time' );
98 $cart->payment_method = $order_intent->get_payment_data_value( 'method' );
99 $cart->payment_portion = $order_intent->get_payment_data_value( 'portion' );
100
101 return self::should_processor_handle_payment_for_cart( $processor_code, $cart );
102 }
103
104 public static function is_cart_payment_enabled( OsCartModel $cart ): bool {
105 $payment_times = self::get_enabled_payment_times();
106
107 return ! empty( $payment_times[ $cart->payment_time ][ $cart->payment_method ][ $cart->payment_processor ] );
108 }
109
110 public static function is_order_intent_payment_enabled( OsOrderIntentModel $order_intent ): bool {
111 $cart = new OsCartModel();
112 $cart->payment_processor = $order_intent->get_payment_data_value( 'processor' );
113 $cart->payment_time = $order_intent->get_payment_data_value( 'time' );
114 $cart->payment_method = $order_intent->get_payment_data_value( 'method' );
115 $cart->payment_portion = $order_intent->get_payment_data_value( 'portion' );
116
117 return self::is_cart_payment_enabled( $cart );
118 }
119
120 public static function get_all_payment_methods_for_select(): array {
121 $payment_methods_for_select = [];
122 $payment_times = self::get_all_payment_times();
123 foreach ( $payment_times as $payment_time_code => $payment_time_methods ) {
124 foreach ( $payment_time_methods as $payment_time_method_code => $payment_time_processors ) {
125 foreach ( $payment_time_processors as $payment_time_processor_code => $payment_time_method ) {
126 $payment_methods_for_select[ $payment_time_method_code ] = $payment_time_method['label'];
127 }
128 }
129 }
130
131 $payment_methods_for_select['other'] = __( 'Other', 'latepoint' );
132
133 /**
134 * List of all payment methods for select field
135 *
136 * @param {array} $payment_methods_for_select Array of payment methods for select field
137 *
138 * @returns {array} Filtered array of payment methods for select field
139 * @since 5.0.0
140 * @hook latepoint_all_payment_methods_for_select
141 *
142 */
143 return apply_filters( 'latepoint_all_payment_methods_for_select', $payment_methods_for_select );
144 }
145
146 public static function get_local_payment_method_info(): array {
147 return [
148 'code' => LATEPOINT_PAYMENT_METHOD_LOCAL,
149 'label' => __( 'Pay Locally', 'latepoint' ),
150 'image_url' => LATEPOINT_IMAGES_URL . 'payment_later.png',
151 ];
152 }
153
154
155 public static function get_enabled_payment_methods_for_payment_time( string $payment_time ): array {
156 $enabled_payment_methods_for_time = [];
157
158 $enabled_payment_times = self::get_enabled_payment_times();
159 if ( ! empty( $enabled_payment_times[ $payment_time ] ) ) {
160 $enabled_payment_methods_for_time = $enabled_payment_times[ $payment_time ];
161 }
162
163 /**
164 * List of enabled payment methods for a specific payment time
165 *
166 * @param {array} $enabled_payment_methods_for_time Array of enabled payment methods for requested time
167 * @param {string} $payment_time Payment time that should be supported by payment methods
168 *
169 * @returns {array} Filtered array of enabled payment methods for requested time type
170 * @since 5.0.0
171 * @hook latepoint_enabled_payment_methods_for_payment_time
172 *
173 */
174 return apply_filters( 'latepoint_enabled_payment_methods_for_payment_time', $enabled_payment_methods_for_time, $payment_time );
175 }
176
177 public static function get_enabled_payment_processors_for_payment_time_and_method( string $payment_time, string $payment_method ): array {
178 $enabled_payment_processors_for_payment_time_and_method = [];
179
180 $enabled_payment_methods_for_time = self::get_enabled_payment_methods_for_payment_time( $payment_time );
181 if ( ! empty( $enabled_payment_methods_for_time[ $payment_method ] ) ) {
182 $enabled_payment_processors_for_payment_time_and_method = $enabled_payment_methods_for_time[ $payment_method ];
183 }
184
185 /**
186 * List of enabled payment processors for a specific payment time and method
187 *
188 * @param {array} $enabled_payment_processors_for_payment_time_and_method Array of enabled payment processors for requested time and method
189 * @param {string} $payment_time Payment time that should be supported by payment processors
190 * @param {string} $payment_method Payment method that should be supported by payment processors
191 *
192 * @returns {array} Filtered array of enabled payment processors for requested time and method
193 * @since 5.0.0
194 * @hook latepoint_enabled_payment_processors_for_payment_time_and_method
195 *
196 */
197 return apply_filters( 'latepoint_enabled_payment_processors_for_payment_time_and_method', $enabled_payment_processors_for_payment_time_and_method, $payment_time, $payment_method );
198 }
199
200
201 public static function get_all_payment_times(): array {
202 $payment_times = [
203 LATEPOINT_PAYMENT_TIME_NOW => [],
204 LATEPOINT_PAYMENT_TIME_LATER => [],
205 ];
206 $payment_times[ LATEPOINT_PAYMENT_TIME_LATER ][ LATEPOINT_PAYMENT_METHOD_LOCAL ]['latepoint'] = self::get_local_payment_method_info();
207
208
209 /**
210 * List of all payment times
211 *
212 * @param {array} $payment_times Array of payment times
213 *
214 * @returns {array} Filtered array of enabled payment times
215 * @since 5.0.0
216 * @hook latepoint_get_all_payment_times
217 *
218 */
219 return apply_filters( 'latepoint_get_all_payment_times', $payment_times );
220 }
221
222 /**
223 *
224 * Array of payment times that have at least one payment method enabled
225 *
226 * @return array
227 */
228 public static function get_enabled_payment_times(): array {
229 $enabled_payment_times = [];
230 if ( self::is_local_payments_enabled() ) {
231 $enabled_payment_times[ LATEPOINT_PAYMENT_TIME_LATER ][ LATEPOINT_PAYMENT_METHOD_LOCAL ]['latepoint'] = self::get_local_payment_method_info();
232 }
233
234 /**
235 * List of only enabled payment times
236 *
237 * @param {array} $enabled_payment_times Array of payment times
238 *
239 * @returns {array} Filtered array of enabled payment times
240 * @since 5.0.0
241 * @hook latepoint_get_enabled_payment_times
242 *
243 */
244 return apply_filters( 'latepoint_get_enabled_payment_times', $enabled_payment_times );
245 }
246
247
248 public static function get_transactions_for_select() {
249 $transactions = new OsTransactionModel();
250 $transactions = $transactions->set_limit( 100 )->get_results_as_models();
251 $transactions_options = [];
252 foreach ( $transactions as $transaction ) {
253 $name = $transaction->token . ', ' . OsMoneyHelper::format_price( $transaction->amount, true, false ) . ' [' . $transaction->processor . '/' . $transaction->payment_method . ' ' . $transaction->status . ']';
254 $transactions_options[] = [
255 'value' => $transaction->id,
256 'label' => $name,
257 ];
258 }
259
260 return $transactions_options;
261 }
262
263
264 public static function get_payment_portions_list() {
265 $payment_portions = [
266 LATEPOINT_PAYMENT_PORTION_FULL => __( 'Full Balance', 'latepoint' ),
267 LATEPOINT_PAYMENT_PORTION_REMAINING => __( 'Remaining Balance', 'latepoint' ),
268 LATEPOINT_PAYMENT_PORTION_DEPOSIT => __( 'Deposit', 'latepoint' ),
269 LATEPOINT_PAYMENT_PORTION_CUSTOM => __( 'Custom', 'latepoint' ),
270 ];
271
272 return apply_filters( 'latepoint_payment_portions', $payment_portions );
273 }
274
275 public static function get_list_of_transaction_kinds() {
276 $statuses = [
277 LATEPOINT_TRANSACTION_KIND_CAPTURE => __( 'Capture', 'latepoint' ),
278 LATEPOINT_TRANSACTION_KIND_AUTHORIZATION => __( 'Authorization', 'latepoint' ),
279 LATEPOINT_TRANSACTION_KIND_VOID => __( 'Void', 'latepoint' ),
280 ];
281
282 return apply_filters( 'latepoint_transaction_kinds', $statuses );
283 }
284
285 public static function get_nice_transaction_kind_name( $kind ) {
286 $kids_list = OsPaymentsHelper::get_list_of_transaction_kinds();
287
288 return $kids_list[ $kind ] ?? $kind;
289 }
290
291 public static function get_transaction_statuses_list() {
292 $statuses = [
293 LATEPOINT_TRANSACTION_STATUS_SUCCEEDED => __( 'Succeeded', 'latepoint' ),
294 LATEPOINT_TRANSACTION_STATUS_PROCESSING => __( 'Processing', 'latepoint' ),
295 LATEPOINT_TRANSACTION_STATUS_FAILED => __( 'Failed', 'latepoint' ),
296 ];
297
298 return apply_filters( 'latepoint_transaction_statuses', $statuses );
299 }
300
301
302 public static function get_nice_transaction_status_name( $status ) {
303 $statuses_list = OsPaymentsHelper::get_transaction_statuses_list();
304
305 return $statuses_list[ $status ] ?? $status;
306 }
307
308 public static function display_transaction_payment_method_info( $payment_method ) {
309 switch ( $payment_method ) {
310 case LATEPOINT_PAYMENT_METHOD_CARD:
311 echo '<div class="lp-method-logo"><i class="latepoint-icon latepoint-icon-credit-card"></i></div>';
312 break;
313 case LATEPOINT_PAYMENT_METHOD_PAYPAL:
314 echo '<div class="lp-method-logo"><i class="latepoint-icon latepoint-icon-paypal"></i></div>';
315 break;
316 default:
317 echo '<div class="lp-method-name">' . esc_html( $payment_method ) . '</div>';
318 break;
319 }
320 }
321
322 public static function process_payment_for_order_intent( OsOrderIntentModel $order_intent ) {
323 if ( ( $order_intent->charge_amount <= 0 ) || OsSettingsHelper::is_env_demo() ) {
324 return false;
325 }
326 if ( ! self::is_order_intent_payment_enabled( $order_intent ) ) {
327 $order_intent->add_error(
328 'invalid_payment_method',
329 __( 'The selected payment method is not available. Please choose a valid payment method and try again.', 'latepoint' )
330 );
331 return false;
332 }
333 $payment_processing_result = [];
334
335
336 /**
337 * Hook to change a result of payment processing when order intent is being converted to order and payment is required
338 *
339 * @param {array} $result Array that holds result of payment processing
340 * @param {OsOrderIntentModel} $order_intent Order intent which is being converted to Order which payment is being processed
341 *
342 * @returns {array} Filtered array that holds result of payment processing
343 * @since 5.0.0
344 * @hook latepoint_process_payment_for_order_intent
345 *
346 */
347 $payment_processing_result = apply_filters( 'latepoint_process_payment_for_order_intent', $payment_processing_result, $order_intent );
348 if ( $payment_processing_result && $payment_processing_result['status'] == LATEPOINT_STATUS_SUCCESS ) {
349 $existing_transaction = ( new OsTransactionModel() )->where(
350 [
351 'token' => $payment_processing_result['charge_id'],
352 'status' => LATEPOINT_TRANSACTION_STATUS_SUCCEEDED,
353 ]
354 )->set_limit( 1 )->get_results_as_models();
355 if ( ! empty( $existing_transaction ) ) {
356 OsDebugHelper::log( 'Duplicate payment token: ' . $payment_processing_result['charge_id'], 'payment_security_error' );
357 return false;
358 }
359 $transaction = new OsTransactionModel();
360 $transaction->token = $payment_processing_result['charge_id'];
361 $transaction->payment_method = $order_intent->get_payment_data_value( 'method' );
362 $transaction->payment_portion = $order_intent->get_payment_data_value( 'portion' );
363 $transaction->amount = $order_intent->charge_amount;
364 $transaction->customer_id = $order_intent->customer_id;
365 $transaction->processor = $payment_processing_result['processor'];
366 $transaction->kind = $payment_processing_result['kind'] ?? LATEPOINT_TRANSACTION_KIND_CAPTURE;
367 $transaction->status = LATEPOINT_TRANSACTION_STATUS_SUCCEEDED;
368 } else {
369 $transaction = false;
370 }
371
372 return $transaction;
373 }
374
375 public static function process_payment_for_transaction_intent( OsTransactionIntentModel $transaction_intent ) {
376 if ( ( $transaction_intent->charge_amount <= 0 ) || OsSettingsHelper::is_env_demo() ) {
377 return false;
378 }
379 $payment_processing_result = [];
380
381
382 /**
383 * Hook to change a result of payment processing when transaction intent is being converted to transaction and payment is required
384 *
385 * @param {array} $result Array that holds result of payment processing
386 * @param {OsTransactionIntentModel} $transaction_intent Transaction intent which is being converted to Transaction which payment is being processed
387 *
388 * @returns {array} Filtered array that holds result of payment processing
389 * @since 5.0.0
390 * @hook latepoint_process_payment_for_transaction_intent
391 *
392 */
393 $payment_processing_result = apply_filters( 'latepoint_process_payment_for_transaction_intent', $payment_processing_result, $transaction_intent );
394 if ( $payment_processing_result && $payment_processing_result['status'] == LATEPOINT_STATUS_SUCCESS ) {
395 $existing_transaction = ( new OsTransactionModel() )->where(
396 [
397 'token' => $payment_processing_result['charge_id'],
398 'status' => LATEPOINT_TRANSACTION_STATUS_SUCCEEDED,
399 ]
400 )->set_limit( 1 )->get_results_as_models();
401 if ( ! empty( $existing_transaction ) ) {
402 OsDebugHelper::log( 'Duplicate payment token: ' . $payment_processing_result['charge_id'], 'payment_security_error' );
403 return false;
404 }
405 $transaction = new OsTransactionModel();
406 $transaction->token = $payment_processing_result['charge_id'];
407 $transaction->payment_method = $transaction_intent->get_payment_data_value( 'method' );
408 $transaction->payment_portion = $transaction_intent->get_payment_data_value( 'portion' );
409 $transaction->amount = $transaction_intent->charge_amount;
410 $transaction->order_id = $transaction_intent->order_id;
411 $transaction->customer_id = $transaction_intent->customer_id;
412 $transaction->invoice_id = $transaction_intent->invoice_id;
413 $transaction->processor = $payment_processing_result['processor'];
414 $transaction->kind = $payment_processing_result['kind'] ?? LATEPOINT_TRANSACTION_KIND_CAPTURE;
415 $transaction->status = LATEPOINT_TRANSACTION_STATUS_SUCCEEDED;
416 } else {
417 $transaction = false;
418 }
419
420 return $transaction;
421 }
422
423
424 public static function convert_charge_amount_to_requirements( $charge_amount, OsCartModel $cart ) {
425
426 /**
427 * Hook to convert amount to requirements based on a payment processor and method selected in a cart
428 *
429 * @param {float} $chart_amount Amount to charge
430 * @param {OsCartModel} $cart Cart object
431 *
432 * @returns {float} Filtered amount to spec
433 * @since 5.0.0
434 * @hook latepoint_convert_charge_amount_to_requirements
435 *
436 */
437 return apply_filters( 'latepoint_convert_charge_amount_to_requirements', $charge_amount, $cart );
438 }
439 }
440