PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.4.1
LatePoint – Calendar Booking Plugin for Appointments and Events v5.4.1
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 months ago auth_helper.php 3 months ago blocks_helper.php 3 months ago booking_helper.php 3 months ago bricks_helper.php 3 months ago bundles_helper.php 3 months ago calendar_helper.php 3 months ago carts_helper.php 3 months ago connector_helper.php 3 months ago csv_helper.php 3 months ago customer_helper.php 3 months ago customer_import_helper.php 3 months ago database_helper.php 3 months 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 months 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 2 months 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 3 months ago orders_helper.php 3 months ago otp_helper.php 3 months ago pages_helper.php 3 months ago params_helper.php 3 months ago payments_helper.php 3 months ago price_breakdown_helper.php 3 months ago process_jobs_helper.php 3 months ago processes_helper.php 3 months ago replacer_helper.php 3 months ago resource_helper.php 3 months ago roles_helper.php 3 months ago router_helper.php 3 months ago service_helper.php 3 months ago sessions_helper.php 3 months ago settings_helper.php 3 months ago short_links_systems_helper.php 3 months ago shortcodes_helper.php 2 months ago sms_helper.php 3 months ago steps_helper.php 3 months ago stripe_connect_helper.php 2 months ago styles_helper.php 3 months ago support_topics_helper.php 3 months ago time_helper.php 2 months ago timeline_helper.php 3 months ago transaction_helper.php 3 months ago transaction_intent_helper.php 3 months ago update_helper.php 2 months ago util_helper.php 3 months ago version_specific_updates_helper.php 3 months ago whatsapp_helper.php 3 months ago work_periods_helper.php 3 months ago wp_datetime.php 3 months ago wp_user_helper.php 3 months ago
payments_helper.php
403 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 get_all_payment_methods_for_select(): array {
111 $payment_methods_for_select = [];
112 $payment_times = self::get_all_payment_times();
113 foreach ( $payment_times as $payment_time_code => $payment_time_methods ) {
114 foreach ( $payment_time_methods as $payment_time_method_code => $payment_time_processors ) {
115 foreach ( $payment_time_processors as $payment_time_processor_code => $payment_time_method ) {
116 $payment_methods_for_select[ $payment_time_method_code ] = $payment_time_method['label'];
117 }
118 }
119 }
120
121 $payment_methods_for_select['other'] = __( 'Other', 'latepoint' );
122
123 /**
124 * List of all payment methods for select field
125 *
126 * @param {array} $payment_methods_for_select Array of payment methods for select field
127 *
128 * @returns {array} Filtered array of payment methods for select field
129 * @since 5.0.0
130 * @hook latepoint_all_payment_methods_for_select
131 *
132 */
133 return apply_filters( 'latepoint_all_payment_methods_for_select', $payment_methods_for_select );
134 }
135
136 public static function get_local_payment_method_info(): array {
137 return [
138 'code' => LATEPOINT_PAYMENT_METHOD_LOCAL,
139 'label' => __( 'Pay Locally', 'latepoint' ),
140 'image_url' => LATEPOINT_IMAGES_URL . 'payment_later.png',
141 ];
142 }
143
144
145 public static function get_enabled_payment_methods_for_payment_time( string $payment_time ): array {
146 $enabled_payment_methods_for_time = [];
147
148 $enabled_payment_times = self::get_enabled_payment_times();
149 if ( ! empty( $enabled_payment_times[ $payment_time ] ) ) {
150 $enabled_payment_methods_for_time = $enabled_payment_times[ $payment_time ];
151 }
152
153 /**
154 * List of enabled payment methods for a specific payment time
155 *
156 * @param {array} $enabled_payment_methods_for_time Array of enabled payment methods for requested time
157 * @param {string} $payment_time Payment time that should be supported by payment methods
158 *
159 * @returns {array} Filtered array of enabled payment methods for requested time type
160 * @since 5.0.0
161 * @hook latepoint_enabled_payment_methods_for_payment_time
162 *
163 */
164 return apply_filters( 'latepoint_enabled_payment_methods_for_payment_time', $enabled_payment_methods_for_time, $payment_time );
165 }
166
167 public static function get_enabled_payment_processors_for_payment_time_and_method( string $payment_time, string $payment_method ): array {
168 $enabled_payment_processors_for_payment_time_and_method = [];
169
170 $enabled_payment_methods_for_time = self::get_enabled_payment_methods_for_payment_time( $payment_time );
171 if ( ! empty( $enabled_payment_methods_for_time[ $payment_method ] ) ) {
172 $enabled_payment_processors_for_payment_time_and_method = $enabled_payment_methods_for_time[ $payment_method ];
173 }
174
175 /**
176 * List of enabled payment processors for a specific payment time and method
177 *
178 * @param {array} $enabled_payment_processors_for_payment_time_and_method Array of enabled payment processors for requested time and method
179 * @param {string} $payment_time Payment time that should be supported by payment processors
180 * @param {string} $payment_method Payment method that should be supported by payment processors
181 *
182 * @returns {array} Filtered array of enabled payment processors for requested time and method
183 * @since 5.0.0
184 * @hook latepoint_enabled_payment_processors_for_payment_time_and_method
185 *
186 */
187 return apply_filters( 'latepoint_enabled_payment_processors_for_payment_time_and_method', $enabled_payment_processors_for_payment_time_and_method, $payment_time, $payment_method );
188 }
189
190
191 public static function get_all_payment_times(): array {
192 $payment_times = [
193 LATEPOINT_PAYMENT_TIME_NOW => [],
194 LATEPOINT_PAYMENT_TIME_LATER => [],
195 ];
196 $payment_times[ LATEPOINT_PAYMENT_TIME_LATER ][ LATEPOINT_PAYMENT_METHOD_LOCAL ]['latepoint'] = self::get_local_payment_method_info();
197
198
199 /**
200 * List of all payment times
201 *
202 * @param {array} $payment_times Array of payment times
203 *
204 * @returns {array} Filtered array of enabled payment times
205 * @since 5.0.0
206 * @hook latepoint_get_all_payment_times
207 *
208 */
209 return apply_filters( 'latepoint_get_all_payment_times', $payment_times );
210 }
211
212 /**
213 *
214 * Array of payment times that have at least one payment method enabled
215 *
216 * @return array
217 */
218 public static function get_enabled_payment_times(): array {
219 $enabled_payment_times = [];
220 if ( self::is_local_payments_enabled() ) {
221 $enabled_payment_times[ LATEPOINT_PAYMENT_TIME_LATER ][ LATEPOINT_PAYMENT_METHOD_LOCAL ]['latepoint'] = self::get_local_payment_method_info();
222 }
223
224 /**
225 * List of only enabled payment times
226 *
227 * @param {array} $enabled_payment_times Array of payment times
228 *
229 * @returns {array} Filtered array of enabled payment times
230 * @since 5.0.0
231 * @hook latepoint_get_enabled_payment_times
232 *
233 */
234 return apply_filters( 'latepoint_get_enabled_payment_times', $enabled_payment_times );
235 }
236
237
238 public static function get_transactions_for_select() {
239 $transactions = new OsTransactionModel();
240 $transactions = $transactions->set_limit( 100 )->get_results_as_models();
241 $transactions_options = [];
242 foreach ( $transactions as $transaction ) {
243 $name = $transaction->token . ', ' . OsMoneyHelper::format_price( $transaction->amount, true, false ) . ' [' . $transaction->processor . '/' . $transaction->payment_method . ' ' . $transaction->status . ']';
244 $transactions_options[] = [
245 'value' => $transaction->id,
246 'label' => $name,
247 ];
248 }
249
250 return $transactions_options;
251 }
252
253
254 public static function get_payment_portions_list() {
255 $payment_portions = [
256 LATEPOINT_PAYMENT_PORTION_FULL => __( 'Full Balance', 'latepoint' ),
257 LATEPOINT_PAYMENT_PORTION_REMAINING => __( 'Remaining Balance', 'latepoint' ),
258 LATEPOINT_PAYMENT_PORTION_DEPOSIT => __( 'Deposit', 'latepoint' ),
259 LATEPOINT_PAYMENT_PORTION_CUSTOM => __( 'Custom', 'latepoint' ),
260 ];
261
262 return apply_filters( 'latepoint_payment_portions', $payment_portions );
263 }
264
265 public static function get_list_of_transaction_kinds() {
266 $statuses = [
267 LATEPOINT_TRANSACTION_KIND_CAPTURE => __( 'Capture', 'latepoint' ),
268 LATEPOINT_TRANSACTION_KIND_AUTHORIZATION => __( 'Authorization', 'latepoint' ),
269 LATEPOINT_TRANSACTION_KIND_VOID => __( 'Void', 'latepoint' ),
270 ];
271
272 return apply_filters( 'latepoint_transaction_kinds', $statuses );
273 }
274
275 public static function get_nice_transaction_kind_name( $kind ) {
276 $kids_list = OsPaymentsHelper::get_list_of_transaction_kinds();
277
278 return $kids_list[ $kind ] ?? $kind;
279 }
280
281 public static function get_transaction_statuses_list() {
282 $statuses = [
283 LATEPOINT_TRANSACTION_STATUS_SUCCEEDED => __( 'Succeeded', 'latepoint' ),
284 LATEPOINT_TRANSACTION_STATUS_PROCESSING => __( 'Processing', 'latepoint' ),
285 LATEPOINT_TRANSACTION_STATUS_FAILED => __( 'Failed', 'latepoint' ),
286 ];
287
288 return apply_filters( 'latepoint_transaction_statuses', $statuses );
289 }
290
291
292 public static function get_nice_transaction_status_name( $status ) {
293 $statuses_list = OsPaymentsHelper::get_transaction_statuses_list();
294
295 return $statuses_list[ $status ] ?? $status;
296 }
297
298 public static function display_transaction_payment_method_info( $payment_method ) {
299 switch ( $payment_method ) {
300 case LATEPOINT_PAYMENT_METHOD_CARD:
301 echo '<div class="lp-method-logo"><i class="latepoint-icon latepoint-icon-credit-card"></i></div>';
302 break;
303 case LATEPOINT_PAYMENT_METHOD_PAYPAL:
304 echo '<div class="lp-method-logo"><i class="latepoint-icon latepoint-icon-paypal"></i></div>';
305 break;
306 default:
307 echo '<div class="lp-method-name">' . esc_html( $payment_method ) . '</div>';
308 break;
309 }
310 }
311
312 public static function process_payment_for_order_intent( OsOrderIntentModel $order_intent ) {
313 if ( ( $order_intent->charge_amount <= 0 ) || OsSettingsHelper::is_env_demo() ) {
314 return false;
315 }
316 $payment_processing_result = [];
317
318
319 /**
320 * Hook to change a result of payment processing when order intent is being converted to order and payment is required
321 *
322 * @param {array} $result Array that holds result of payment processing
323 * @param {OsOrderIntentModel} $order_intent Order intent which is being converted to Order which payment is being processed
324 *
325 * @returns {array} Filtered array that holds result of payment processing
326 * @since 5.0.0
327 * @hook latepoint_process_payment_for_order_intent
328 *
329 */
330 $payment_processing_result = apply_filters( 'latepoint_process_payment_for_order_intent', $payment_processing_result, $order_intent );
331 if ( $payment_processing_result && $payment_processing_result['status'] == LATEPOINT_STATUS_SUCCESS ) {
332 $transaction = new OsTransactionModel();
333 $transaction->token = $payment_processing_result['charge_id'];
334 $transaction->payment_method = $order_intent->get_payment_data_value( 'method' );
335 $transaction->payment_portion = $order_intent->get_payment_data_value( 'portion' );
336 $transaction->amount = $order_intent->charge_amount;
337 $transaction->customer_id = $order_intent->customer_id;
338 $transaction->processor = $payment_processing_result['processor'];
339 $transaction->kind = $payment_processing_result['kind'] ?? LATEPOINT_TRANSACTION_KIND_CAPTURE;
340 $transaction->status = LATEPOINT_TRANSACTION_STATUS_SUCCEEDED;
341 } else {
342 $transaction = false;
343 }
344
345 return $transaction;
346 }
347
348 public static function process_payment_for_transaction_intent( OsTransactionIntentModel $transaction_intent ) {
349 if ( ( $transaction_intent->charge_amount <= 0 ) || OsSettingsHelper::is_env_demo() ) {
350 return false;
351 }
352 $payment_processing_result = [];
353
354
355 /**
356 * Hook to change a result of payment processing when transaction intent is being converted to transaction and payment is required
357 *
358 * @param {array} $result Array that holds result of payment processing
359 * @param {OsTransactionIntentModel} $transaction_intent Transaction intent which is being converted to Transaction which payment is being processed
360 *
361 * @returns {array} Filtered array that holds result of payment processing
362 * @since 5.0.0
363 * @hook latepoint_process_payment_for_transaction_intent
364 *
365 */
366 $payment_processing_result = apply_filters( 'latepoint_process_payment_for_transaction_intent', $payment_processing_result, $transaction_intent );
367 if ( $payment_processing_result && $payment_processing_result['status'] == LATEPOINT_STATUS_SUCCESS ) {
368 $transaction = new OsTransactionModel();
369 $transaction->token = $payment_processing_result['charge_id'];
370 $transaction->payment_method = $transaction_intent->get_payment_data_value( 'method' );
371 $transaction->payment_portion = $transaction_intent->get_payment_data_value( 'portion' );
372 $transaction->amount = $transaction_intent->charge_amount;
373 $transaction->order_id = $transaction_intent->order_id;
374 $transaction->customer_id = $transaction_intent->customer_id;
375 $transaction->invoice_id = $transaction_intent->invoice_id;
376 $transaction->processor = $payment_processing_result['processor'];
377 $transaction->kind = $payment_processing_result['kind'] ?? LATEPOINT_TRANSACTION_KIND_CAPTURE;
378 $transaction->status = LATEPOINT_TRANSACTION_STATUS_SUCCEEDED;
379 } else {
380 $transaction = false;
381 }
382
383 return $transaction;
384 }
385
386
387 public static function convert_charge_amount_to_requirements( $charge_amount, OsCartModel $cart ) {
388
389 /**
390 * Hook to convert amount to requirements based on a payment processor and method selected in a cart
391 *
392 * @param {float} $chart_amount Amount to charge
393 * @param {OsCartModel} $cart Cart object
394 *
395 * @returns {float} Filtered amount to spec
396 * @since 5.0.0
397 * @hook latepoint_convert_charge_amount_to_requirements
398 *
399 */
400 return apply_filters( 'latepoint_convert_charge_amount_to_requirements', $charge_amount, $cart );
401 }
402 }
403