PluginProbe ʕ •ᴥ•ʔ
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.2.1
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.2.1
5.6.8 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 1 year ago agent_helper.php 1 year ago auth_helper.php 9 months ago blocks_helper.php 1 year ago booking_helper.php 1 year ago bricks_helper.php 1 year ago bundles_helper.php 1 year ago calendar_helper.php 9 months ago carts_helper.php 1 year ago connector_helper.php 1 year ago csv_helper.php 9 months ago customer_helper.php 9 months ago customer_import_helper.php 9 months ago database_helper.php 9 months ago debug_helper.php 1 year ago defaults_helper.php 1 year ago elementor_helper.php 1 year ago email_helper.php 9 months ago encrypt_helper.php 1 year ago events_helper.php 1 year ago form_helper.php 9 months ago icalendar_helper.php 1 year ago image_helper.php 1 year ago invoices_helper.php 9 months ago license_helper.php 1 year ago location_helper.php 1 year ago marketing_systems_helper.php 1 year ago meeting_systems_helper.php 1 year ago menu_helper.php 9 months ago meta_helper.php 1 year ago migrations_helper.php 1 year ago money_helper.php 1 year ago notifications_helper.php 9 months ago order_intent_helper.php 9 months ago orders_helper.php 1 year ago otp_helper.php 9 months ago pages_helper.php 1 year ago params_helper.php 1 year ago payments_helper.php 1 year ago price_breakdown_helper.php 1 year ago process_jobs_helper.php 1 year ago processes_helper.php 1 year ago replacer_helper.php 1 year ago resource_helper.php 1 year ago roles_helper.php 9 months ago router_helper.php 1 year ago service_helper.php 1 year ago sessions_helper.php 1 year ago settings_helper.php 9 months ago short_links_systems_helper.php 9 months ago shortcodes_helper.php 9 months ago sms_helper.php 1 year ago steps_helper.php 9 months ago stripe_connect_helper.php 9 months ago styles_helper.php 9 months ago support_topics_helper.php 1 year ago time_helper.php 9 months ago timeline_helper.php 9 months ago transaction_helper.php 1 year ago transaction_intent_helper.php 1 year ago util_helper.php 9 months ago version_specific_updates_helper.php 9 months ago whatsapp_helper.php 1 year ago work_periods_helper.php 1 year ago wp_datetime.php 1 year ago wp_user_helper.php 1 year ago
payments_helper.php
400 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[] = [ 'value' => $transaction->id, 'label' => $name ];
245 }
246
247 return $transactions_options;
248 }
249
250
251 public static function get_payment_portions_list() {
252 $payment_portions = [
253 LATEPOINT_PAYMENT_PORTION_FULL => __( 'Full Balance', 'latepoint' ),
254 LATEPOINT_PAYMENT_PORTION_REMAINING => __( 'Remaining Balance', 'latepoint' ),
255 LATEPOINT_PAYMENT_PORTION_DEPOSIT => __( 'Deposit', 'latepoint' ),
256 LATEPOINT_PAYMENT_PORTION_CUSTOM => __( 'Custom', 'latepoint' )
257 ];
258
259 return apply_filters( 'latepoint_payment_portions', $payment_portions );
260 }
261
262 public static function get_list_of_transaction_kinds() {
263 $statuses = [
264 LATEPOINT_TRANSACTION_KIND_CAPTURE => __( 'Capture', 'latepoint' ),
265 LATEPOINT_TRANSACTION_KIND_AUTHORIZATION => __( 'Authorization', 'latepoint' ),
266 LATEPOINT_TRANSACTION_KIND_VOID => __( 'Void', 'latepoint' ),
267 ];
268
269 return apply_filters( 'latepoint_transaction_kinds', $statuses );
270 }
271
272 public static function get_nice_transaction_kind_name( $kind ) {
273 $kids_list = OsPaymentsHelper::get_list_of_transaction_kinds();
274
275 return $kids_list[ $kind ] ?? $kind;
276 }
277
278 public static function get_transaction_statuses_list() {
279 $statuses = [
280 LATEPOINT_TRANSACTION_STATUS_SUCCEEDED => __( 'Succeeded', 'latepoint' ),
281 LATEPOINT_TRANSACTION_STATUS_PROCESSING => __( 'Processing', 'latepoint' ),
282 LATEPOINT_TRANSACTION_STATUS_FAILED => __( 'Failed', 'latepoint' )
283 ];
284
285 return apply_filters( 'latepoint_transaction_statuses', $statuses );
286 }
287
288
289 public static function get_nice_transaction_status_name( $status ) {
290 $statuses_list = OsPaymentsHelper::get_transaction_statuses_list();
291
292 return $statuses_list[ $status ] ?? $status;
293 }
294
295 public static function display_transaction_payment_method_info( $payment_method ) {
296 switch ( $payment_method ) {
297 case LATEPOINT_PAYMENT_METHOD_CARD:
298 echo '<div class="lp-method-logo"><i class="latepoint-icon latepoint-icon-credit-card"></i></div>';
299 break;
300 case LATEPOINT_PAYMENT_METHOD_PAYPAL:
301 echo '<div class="lp-method-logo"><i class="latepoint-icon latepoint-icon-paypal"></i></div>';
302 break;
303 default:
304 echo '<div class="lp-method-name">' . esc_html( $payment_method ) . '</div>';
305 break;
306 }
307 }
308
309 public static function process_payment_for_order_intent( OsOrderIntentModel $order_intent ) {
310 if ( ( $order_intent->charge_amount <= 0 ) || OsSettingsHelper::is_env_demo() ) {
311 return false;
312 }
313 $payment_processing_result = [];
314
315
316 /**
317 * Hook to change a result of payment processing when order intent is being converted to order and payment is required
318 *
319 * @param {array} $result Array that holds result of payment processing
320 * @param {OsOrderIntentModel} $order_intent Order intent which is being converted to Order which payment is being processed
321 *
322 * @returns {array} Filtered array that holds result of payment processing
323 * @since 5.0.0
324 * @hook latepoint_process_payment_for_order_intent
325 *
326 */
327 $payment_processing_result = apply_filters( 'latepoint_process_payment_for_order_intent', $payment_processing_result, $order_intent );
328 if ( $payment_processing_result && $payment_processing_result['status'] == LATEPOINT_STATUS_SUCCESS ) {
329 $transaction = new OsTransactionModel();
330 $transaction->token = $payment_processing_result['charge_id'];
331 $transaction->payment_method = $order_intent->get_payment_data_value( 'method' );
332 $transaction->payment_portion = $order_intent->get_payment_data_value( 'portion' );
333 $transaction->amount = $order_intent->charge_amount;
334 $transaction->customer_id = $order_intent->customer_id;
335 $transaction->processor = $payment_processing_result['processor'];
336 $transaction->kind = $payment_processing_result['kind'] ?? LATEPOINT_TRANSACTION_KIND_CAPTURE;
337 $transaction->status = LATEPOINT_TRANSACTION_STATUS_SUCCEEDED;
338 } else {
339 $transaction = false;
340 }
341
342 return $transaction;
343 }
344
345 public static function process_payment_for_transaction_intent( OsTransactionIntentModel $transaction_intent ) {
346 if ( ( $transaction_intent->charge_amount <= 0 ) || OsSettingsHelper::is_env_demo() ) {
347 return false;
348 }
349 $payment_processing_result = [];
350
351
352 /**
353 * Hook to change a result of payment processing when transaction intent is being converted to transaction and payment is required
354 *
355 * @param {array} $result Array that holds result of payment processing
356 * @param {OsTransactionIntentModel} $transaction_intent Transaction intent which is being converted to Transaction which payment is being processed
357 *
358 * @returns {array} Filtered array that holds result of payment processing
359 * @since 5.0.0
360 * @hook latepoint_process_payment_for_transaction_intent
361 *
362 */
363 $payment_processing_result = apply_filters( 'latepoint_process_payment_for_transaction_intent', $payment_processing_result, $transaction_intent );
364 if ( $payment_processing_result && $payment_processing_result['status'] == LATEPOINT_STATUS_SUCCESS ) {
365 $transaction = new OsTransactionModel();
366 $transaction->token = $payment_processing_result['charge_id'];
367 $transaction->payment_method = $transaction_intent->get_payment_data_value( 'method' );
368 $transaction->payment_portion = $transaction_intent->get_payment_data_value( 'portion' );
369 $transaction->amount = $transaction_intent->charge_amount;
370 $transaction->order_id = $transaction_intent->order_id;
371 $transaction->customer_id = $transaction_intent->customer_id;
372 $transaction->invoice_id = $transaction_intent->invoice_id;
373 $transaction->processor = $payment_processing_result['processor'];
374 $transaction->kind = $payment_processing_result['kind'] ?? LATEPOINT_TRANSACTION_KIND_CAPTURE;
375 $transaction->status = LATEPOINT_TRANSACTION_STATUS_SUCCEEDED;
376 } else {
377 $transaction = false;
378 }
379
380 return $transaction;
381 }
382
383
384 public static function convert_charge_amount_to_requirements( $charge_amount, OsCartModel $cart ) {
385
386 /**
387 * Hook to convert amount to requirements based on a payment processor and method selected in a cart
388 *
389 * @param {float} $chart_amount Amount to charge
390 * @param {OsCartModel} $cart Cart object
391 *
392 * @returns {float} Filtered amount to spec
393 * @since 5.0.0
394 * @hook latepoint_convert_charge_amount_to_requirements
395 *
396 */
397 return apply_filters( 'latepoint_convert_charge_amount_to_requirements', $charge_amount, $cart );
398 }
399
400 }