Passed
Push — master ( c16653...06c25c )
by Brian
04:36
created

wpinv_get_post_data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * Contains gateway functions.
4
 *
5
 */
6
defined( 'ABSPATH' ) || exit;
7
8
/**
9
 * Returns an array of payment gateways.
10
 * 
11
 * @return array
12
 */
13
function wpinv_get_payment_gateways() {
14
    return apply_filters( 'wpinv_payment_gateways', array() );
15
}
16
17
function wpinv_payment_gateway_titles( $all_gateways ) {
18
    global $wpinv_options;
19
20
    $gateways = array();
21
    foreach ( $all_gateways as $key => $gateway ) {
22
        if ( !empty( $wpinv_options[$key . '_title'] ) ) {
23
            $all_gateways[$key]['checkout_label'] = __( $wpinv_options[$key . '_title'], 'invoicing' );
24
        }
25
26
        $gateways[$key] = isset( $wpinv_options[$key . '_ordering'] ) ? $wpinv_options[$key . '_ordering'] : ( isset( $gateway['ordering'] ) ? $gateway['ordering'] : '' );
27
    }
28
29
    asort( $gateways );
30
31
    foreach ( $gateways as $gateway => $key ) {
32
        $gateways[$gateway] = $all_gateways[$gateway];
33
    }
34
35
    return $gateways;
36
}
37
add_filter( 'wpinv_payment_gateways', 'wpinv_payment_gateway_titles', 1000, 1 );
38
39
function wpinv_get_enabled_payment_gateways( $sort = false ) {
40
    $gateways = wpinv_get_payment_gateways();
41
    $enabled  = wpinv_get_option( 'gateways', false );
42
43
    $gateway_list = array();
44
45
    foreach ( $gateways as $key => $gateway ) {
46
        if ( isset( $enabled[ $key ] ) && $enabled[ $key ] == 1 ) {
47
            $gateway_list[ $key ] = $gateway;
48
        }
49
    }
50
51
    if ( true === $sort ) {
52
        uasort( $gateway_list, 'wpinv_sort_gateway_order' );
53
        
54
        // Reorder our gateways so the default is first
55
        $default_gateway_id = wpinv_get_default_gateway();
56
57
        if ( wpinv_is_gateway_active( $default_gateway_id ) ) {
58
            $default_gateway    = array( $default_gateway_id => $gateway_list[ $default_gateway_id ] );
59
            unset( $gateway_list[ $default_gateway_id ] );
60
61
            $gateway_list = array_merge( $default_gateway, $gateway_list );
62
        }
63
    }
64
65
    return apply_filters( 'wpinv_enabled_payment_gateways', $gateway_list );
66
}
67
68
function wpinv_sort_gateway_order( $a, $b ) {
69
    return $a['ordering'] - $b['ordering'];
70
}
71
72
function wpinv_is_gateway_active( $gateway ) {
73
    $gateways = wpinv_get_enabled_payment_gateways();
74
75
    $ret = is_array($gateways) && $gateway ?  array_key_exists( $gateway, $gateways ) : false;
76
77
    return apply_filters( 'wpinv_is_gateway_active', $ret, $gateway, $gateways );
78
}
79
80
function wpinv_get_default_gateway() {
81
    $default = wpinv_get_option( 'default_gateway', 'paypal' );
82
83
    if ( !wpinv_is_gateway_active( $default ) ) {
84
        $gateways = wpinv_get_enabled_payment_gateways();
85
        $gateways = array_keys( $gateways );
86
        $default  = reset( $gateways );
87
    }
88
89
    return apply_filters( 'wpinv_default_gateway', $default );
90
}
91
92
function wpinv_get_gateway_admin_label( $gateway ) {
93
    $gateways = wpinv_get_payment_gateways();
94
    $label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway;
95
    $payment  = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : false;
96
97
    if( $gateway == 'manual' && $payment ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payment of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
98
        if( !( (float)wpinv_payment_total( $payment ) > 0 ) ) {
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_payment_total() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

98
        if( !( (float)/** @scrutinizer ignore-deprecated */ wpinv_payment_total( $payment ) > 0 ) ) {
Loading history...
99
            $label = __( 'Free Purchase', 'invoicing' );
100
        }
101
    }
102
103
    return apply_filters( 'wpinv_gateway_admin_label', $label, $gateway );
104
}
105
106
function wpinv_get_gateway_description( $gateway ) {
107
    global $wpinv_options;
108
109
    $description = ! empty( $wpinv_options[$gateway . '_desc'] ) ? $wpinv_options[$gateway . '_desc'] : '';
110
111
    return apply_filters( 'wpinv_gateway_description', $description, $gateway );
112
}
113
114
function wpinv_get_gateway_button_label( $gateway ) {
115
    return apply_filters( 'wpinv_gateway_' . $gateway . '_button_label', '' );
116
}
117
118
function wpinv_get_gateway_checkout_label( $gateway ) {
119
    $gateways = wpinv_get_payment_gateways();
120
    $label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway;
121
122
    if ( $gateway == 'none' ) {
123
        $label = __( 'None', 'invoicing' );
124
    }
125
126
    return apply_filters( 'wpinv_gateway_checkout_label', ucfirst( $label ), $gateway );
127
}
128
129
function wpinv_settings_sections_gateways( $settings ) {
130
    $gateways = wpinv_get_payment_gateways();
131
    
132
    if (!empty($gateways)) {
133
        foreach  ($gateways as $key => $gateway) {
134
            $settings[$key] = $gateway['admin_label'];
135
        }
136
    }
137
    
138
    return $settings;    
139
}
140
add_filter( 'wpinv_settings_sections_gateways', 'wpinv_settings_sections_gateways', 10, 1 );
141
142
/**
143
 * Adds GateWay settings.
144
 */
145
function wpinv_settings_gateways( $settings ) {
146
147
    // Loop through each gateway.
148
    foreach  ( wpinv_get_payment_gateways() as $key => $gateway ) {
149
150
        $gateway_settings = array(
151
152
            // Header.
153
            "{$key}_header" => array(
154
155
                'id'     => "{$key}_gateway_header",
156
                'name'   => '<h3>' . wp_sprintf( __( '%s Settings', 'invoicing' ), $gateway['admin_label'] ) . '</h3>',
157
                'custom' => $key,
158
                'type'   => 'gateway_header',
159
160
            ),
161
162
            // Activate/Deactivate a gateway.
163
            "{$key}_active" => array(
164
                'id'   => $key . '_active',
165
                'name' => __( 'Activate', 'invoicing' ),
166
                'desc' => wp_sprintf( __( 'Enable %s', 'invoicing' ), $gateway['admin_label'] ),
167
                'type' => 'checkbox',
168
            ),
169
170
            // Activate/Deactivate sandbox.
171
            "{$key}_sandbox" => array(
172
                'id'   => $key . '_sandbox',
173
                'name' => __( 'Sandbox', 'invoicing' ),
174
                'desc' => __( 'Enable sandbox to test payments', 'invoicing' ),
175
                'type' => 'checkbox',
176
            ),
177
178
            // Checkout title.
179
            "{$key}_title" => array(
180
                'id'   => $key . '_title',
181
                'name' => __( 'Checkout Title', 'invoicing' ),
182
                'std'  => isset( $gateway['checkout_label'] ) ? $gateway['checkout_label'] : '',
183
                'type' => 'text',
184
            ),
185
186
            // Checkout description.
187
            "{$key}_desc" => array(
188
                'id'   => $key . '_desc',
189
                'name' => __( 'Checkout Description', 'invoicing' ),
190
                'std'  => apply_filters( "getpaid_default_{$key}_checkout_description", '' ),
191
                'type' => 'text',
192
            ),
193
194
            // Checkout order.
195
            "{$key}_ordering" => array(
196
                'id'   => $key . '_ordering',
197
                'name' => __( 'Priority', 'invoicing' ),
198
                'std'  => apply_filters( "getpaid_default_{$key}_checkout_description", '' ),
199
                'type' => 'number',
200
                'step' => '1',
201
                'min'  => '-100000',
202
                'max'  => '100000',
203
                'std'  => isset( $gateway['ordering'] ) ? $gateway['ordering'] : '10',
204
            ),
205
206
        );
207
208
        // Maybe remove the sandbox.
209
        if ( ! apply_filters( "wpinv_{$key}_supports_sandbox", false ) ) {
210
            unset( $gateway_settings["{$key}_sandbox"] );
211
        }
212
  
213
        $gateway_settings = apply_filters( 'wpinv_gateway_settings', $gateway_settings, $key, $gateway );
214
        $gateway_settings = apply_filters( 'wpinv_gateway_settings_' . $key, $gateway_settings, $gateway );
215
        
216
        $settings[$key] = $gateway_settings;
217
    }
218
219
    return $settings;
220
221
}
222
add_filter( 'wpinv_settings_gateways', 'wpinv_settings_gateways', 10, 1 );
223
224
function wpinv_gateway_header_callback( $args ) {
225
    echo '<input type="hidden" id="wpinv_settings[save_gateway]" name="wpinv_settings[save_gateway]" value="' . esc_attr( $args['custom'] ) . '" />';
226
}
227
228
function wpinv_get_gateway_supports( $gateway ) {
229
    $gateways = wpinv_get_enabled_payment_gateways();
230
    $supports = isset( $gateways[ $gateway ]['supports'] ) ? $gateways[ $gateway ]['supports'] : array();
231
    return apply_filters( 'wpinv_gateway_supports', $supports, $gateway );
232
}
233
234
function wpinv_gateway_supports_buy_now( $gateway ) {
235
    $supports = wpinv_get_gateway_supports( $gateway );
236
    $ret = in_array( 'buy_now', $supports );
237
    return apply_filters( 'wpinv_gateway_supports_buy_now', $ret, $gateway );
238
}
239
240
function wpinv_shop_supports_buy_now() {
241
    $gateways = wpinv_get_enabled_payment_gateways();
242
    $ret      = false;
243
244
    if ( !wpinv_use_taxes()  && $gateways ) {
245
        foreach ( $gateways as $gateway_id => $gateway ) {
246
            if ( wpinv_gateway_supports_buy_now( $gateway_id ) ) {
247
                $ret = true;
248
                break;
249
            }
250
        }
251
    }
252
253
    return apply_filters( 'wpinv_shop_supports_buy_now', $ret );
254
}
255
256
257
function wpinv_show_gateways() {
258
    $gateways = wpinv_get_enabled_payment_gateways();
259
    $show_gateways = false;
260
261
    $chosen_gateway = isset( $_GET['payment-mode'] ) ? preg_replace('/[^a-zA-Z0-9-_]+/', '', $_GET['payment-mode'] ) : false;
262
263
    if ( count( $gateways ) > 1 && empty( $chosen_gateway ) ) {
264
        $show_gateways = true;
265
        if ( wpinv_get_cart_total() <= 0 ) {
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_get_cart_total() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

265
        if ( /** @scrutinizer ignore-deprecated */ wpinv_get_cart_total() <= 0 ) {
Loading history...
266
            $show_gateways = false;
267
        }
268
    }
269
    
270
    if ( !$show_gateways && wpinv_cart_has_recurring_item() ) {
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_cart_has_recurring_item() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

270
    if ( !$show_gateways && /** @scrutinizer ignore-deprecated */ wpinv_cart_has_recurring_item() ) {
Loading history...
271
        $show_gateways = true;
272
    }
273
274
    return apply_filters( 'wpinv_show_gateways', $show_gateways );
275
}
276
277
function wpinv_get_chosen_gateway( $invoice_id = 0 ) {
278
	$gateways = array_keys( wpinv_get_enabled_payment_gateways() );
279
280
    $chosen = false;
281
    if ( $invoice_id > 0 && $invoice = wpinv_get_invoice( $invoice_id ) ) {
282
        $chosen = $invoice->get_gateway();
283
    }
284
285
	$chosen   = isset( $_REQUEST['payment-mode'] ) ? sanitize_text_field( $_REQUEST['payment-mode'] ) : $chosen;
286
287
	if ( false !== $chosen ) {
288
		$chosen = preg_replace('/[^a-zA-Z0-9-_]+/', '', $chosen );
289
	}
290
291
	if ( ! empty ( $chosen ) ) {
292
		$enabled_gateway = urldecode( $chosen );
293
	} else if (  !empty( $invoice ) && (float)$invoice->get_subtotal() <= 0 ) {
294
		$enabled_gateway = 'manual';
295
	} else {
296
		$enabled_gateway = wpinv_get_default_gateway();
297
	}
298
    
299
    if ( !wpinv_is_gateway_active( $enabled_gateway ) && !empty( $gateways ) ) {
300
        if(wpinv_is_gateway_active( wpinv_get_default_gateway()) ){
301
            $enabled_gateway = wpinv_get_default_gateway();
302
        }else{
303
            $enabled_gateway = $gateways[0];
304
        }
305
306
    }
307
308
	return apply_filters( 'wpinv_chosen_gateway', $enabled_gateway );
309
}
310
311
function wpinv_record_gateway_error( $title = '', $message = '', $parent = 0 ) {
312
    return wpinv_error_log( $message, $title );
0 ignored issues
show
Bug introduced by
Are you sure the usage of wpinv_error_log($message, $title) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
313
}
314
315
function wpinv_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) {
316
	$ret  = 0;
317
	$args = array(
318
		'meta_key'    => '_wpinv_gateway',
319
		'meta_value'  => $gateway_id,
320
		'nopaging'    => true,
321
		'post_type'   => 'wpi_invoice',
322
		'post_status' => $status,
323
		'fields'      => 'ids'
324
	);
325
326
	$payments = new WP_Query( $args );
327
328
	if( $payments )
0 ignored issues
show
introduced by
$payments is of type WP_Query, thus it always evaluated to true.
Loading history...
329
		$ret = $payments->post_count;
330
	return $ret;
331
}
332
333
function wpinv_settings_update_gateways( $input ) {
334
    global $wpinv_options;
335
    
336
    if ( !empty( $input['save_gateway'] ) ) {
337
        $gateways = wpinv_get_option( 'gateways', false );
338
        $gateways = !empty($gateways) ? $gateways : array();
339
        $gateway = $input['save_gateway'];
340
        
341
        if ( !empty( $input[$gateway . '_active'] ) ) {
342
            $gateways[$gateway] = 1;
343
        } else {
344
            if ( isset( $gateways[$gateway] ) ) {
345
                unset( $gateways[$gateway] );
346
            }
347
        }
348
        
349
        $input['gateways'] = $gateways;
350
    }
351
    
352
    if ( !empty( $input['default_gateway'] ) ) {
353
        $gateways = wpinv_get_payment_gateways();
354
        
355
        foreach ( $gateways as $key => $gateway ) {
356
            $active   = 0;
357
            if ( !empty( $input['gateways'] ) && !empty( $input['gateways'][$key] ) ) {
358
                $active = 1;
359
            }
360
            
361
            $input[$key . '_active'] = $active;
362
            
363
            if ( empty( $wpinv_options[$key . '_title'] ) ) {
364
                $input[$key . '_title'] = $gateway['checkout_label'];
365
            }
366
            
367
            if ( !isset( $wpinv_options[$key . '_ordering'] ) && isset( $gateway['ordering'] ) ) {
368
                $input[$key . '_ordering'] = $gateway['ordering'];
369
            }
370
        }
371
    }
372
    
373
    return $input;
374
}
375
add_filter( 'wpinv_settings_tab_gateways_sanitize', 'wpinv_settings_update_gateways', 10, 1 );
376
377
// PayPal Standard settings
378
function wpinv_gateway_settings_paypal( $setting ) {    
379
    $setting['paypal_active']['desc'] = $setting['paypal_active']['desc'] . ' ' . __( '( Supported Currencies: AUD, BRL, CAD, CZK, DKK, EUR, HKD, HUF, ILS, JPY, MYR, MXN, NOK, NZD, PHP, PLN, GBP, SGD, SEK, CHF, TWD, THB, USD )', 'invoicing' );
380
    $setting['paypal_desc']['std'] = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
381
    
382
    $setting['paypal_sandbox'] = array(
383
            'type' => 'checkbox',
384
            'id'   => 'paypal_sandbox',
385
            'name' => __( 'PayPal Sandbox', 'invoicing' ),
386
            'desc' => __( 'PayPal sandbox can be used to test payments.', 'invoicing' ),
387
            'std'  => 1
388
        );
389
        
390
    $setting['paypal_email'] = array(
391
            'type' => 'text',
392
            'id'   => 'paypal_email',
393
            'name' => __( 'PayPal Email', 'invoicing' ),
394
            'desc' => __( 'Please enter your PayPal account\'s email address. Ex: [email protected]', 'invoicing' ),
395
            'std' => __( '[email protected]', 'invoicing' ),
396
        );
397
    /*
398
    $setting['paypal_ipn_url'] = array(
399
            'type' => 'text',
400
            'id'   => 'paypal_ipn_url',
401
            'name' => __( 'PayPal IPN Url', 'invoicing' ),
402
            'desc' => __( 'Configure Instant Payment Notifications(IPN) url at PayPal. Ex: http://yoursite.com/?wpi-ipn=paypal', 'invoicing' ),
403
            'size' => 'large'
404
        );
405
    */
406
        
407
    return $setting;
408
}
409
add_filter( 'wpinv_gateway_settings_paypal', 'wpinv_gateway_settings_paypal', 10, 1 );
410
411
/**
412
 * Displays the ipn url field.
413
 */
414
function wpinv_ipn_url_callback( $args ) {
415
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
416
    
417
    $attrs = $args['readonly'] ? ' readonly' : '';
418
419
    $html = '<input class="regular-text" type="text" ' . $attrs . ' value="' . esc_attr( $args['std'] ) . '" name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . ']" onClick="this.select()">';
420
    $html .= '<label for="wpinv_settings[' . $sanitize_id . ']">'  . $args['desc'] . '</label>';
421
422
    echo $html;
423
}
424
425
/**
426
 * Checks if a gateway is in test mode.
427
 * 
428
 * @param string $gateway The gateway to check for.
429
 * 
430
 * @return bool
431
 */
432
function wpinv_is_test_mode( $gateway = '' ) {
433
    $sandbox = empty( $gateway ) ? false : wpinv_get_option( "{$gateway}_sandbox", false );
434
    return apply_filters( 'wpinv_is_test_mode', $sandbox, $gateway );
435
}
436
437
/**
438
 * Retrieves the ipn url.
439
 * 
440
 * @param string $gateway The gateway whose IPN url we should retrieve.
441
 * @param array $args extra args to add to the url.
442
 * 
443
 * @return string
444
 */
445
function wpinv_get_ipn_url( $gateway = false, $args = array() ) {
446
    $args = wp_parse_args(
447
        array(
448
            'wpi-listener' => 'IPN',
449
            'wpi-gateway'  => $gateway
450
        ),
451
        $args
452
    );
453
454
    return apply_filters( 'wpinv_ipn_url', add_query_arg( $args,  home_url( 'index.php' ) ), $gateway, $args );
455
456
}
457
458
/**
459
 * Retrieves request data with slashes removed slashes.
460
 */
461
function wpinv_get_post_data( $method = 'request' ) {
462
463
    if ( $method == 'post' ) {
464
        return wp_unslash( $_POST );
465
    }
466
467
    if ( $method == 'get' ) {
468
        return wp_unslash( $_GET );
469
    }
470
471
    return wp_unslash( $_REQUEST );
472
  
473
}
474
475
/**
476
 * Checks if a given gateway supports subscription payments.
477
 */
478
function wpinv_gateway_support_subscription( $gateway ) {
479
    $supports = false;
480
481
    if ( wpinv_is_gateway_active( $gateway ) ) {
482
        $supports = apply_filters( 'wpinv_' . $gateway . '_support_subscription', $supports );
483
        $supports = apply_filters( 'getapid_gateway_supports_subscription', $supports, $gateway );
484
    }
485
486
    return $supports;
487
}
488
489
/**
490
 * Filters payment form gateways.
491
 * 
492
 * @param array $gateways an array of gateways.
493
 * @param GetPaid_Payment_Form $form payment form.
494
 */
495
function wpinv_payment_gateways_on_cart( $gateways, $form ) {
496
497
    if ( $form->is_recurring() ) {
498
499
        foreach ( array_keys( $gateways ) as $gateway ) {
500
501
            if ( ! wpinv_gateway_support_subscription( $gateway ) ) {
502
                unset( $gateways[$gateway] );
503
            }
504
505
        }
506
507
    }
508
509
    return $gateways;
510
}
511
add_filter( 'getpaid_payment_form_gateways', 'wpinv_payment_gateways_on_cart', 10, 2 );
512
513
/**
514
 * Validates checkout fields.
515
 *
516
 * @param GetPaid_Payment_Form_Submission $submission
517
 */
518
function wpinv_checkout_validate_gateway( $submission ) {
519
520
    $data = $submission->get_data();
521
522
    // Non-recurring gateways should not be allowed to process recurring invoices.
523
    if ( $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) {
524
        wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway does not support subscription payment.', 'invoicing' ) );
525
    }
526
527
    if ( ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) {
528
        wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway is not active', 'invoicing' ) );
529
    }
530
531
}
532
533
/**
534
 * Validates a zip code.
535
 */
536
function wpinv_checkout_validate_cc_zip( $zip = 0, $country_code = '' ) {
537
538
    if ( empty( $zip ) || empty( $country_code ) ){
539
        return false;
540
    }
541
542
    // Prepare the country code.
543
    $country_code = strtoupper( trim( $country_code ) );
544
545
    // Fetch the regexes.
546
    $zip_regex = wpinv_get_data( 'zip-regexes' );
547
548
    // Check if it is valid.
549
    $is_valid = ! isset ( $zip_regex[ $country_code ] ) || preg_match( "/" . $zip_regex[ $country_code ] . "/i", $zip );
550
551
    return apply_filters( 'wpinv_is_zip_valid', $is_valid, $zip, $country_code );
552
}
553
554
function wpinv_checkout_validate_agree_to_terms() {
555
    // Validate agree to terms
556
    if ( ! isset( $_POST['wpi_agree_to_terms'] ) || $_POST['wpi_agree_to_terms'] != 1 ) {
557
        // User did not agree
558
        wpinv_set_error( 'agree_to_terms', apply_filters( 'wpinv_agree_to_terms_text', __( 'You must agree to the terms of use', 'invoicing' ) ) );
559
    }
560
}
561
562
function wpinv_checkout_validate_invoice_user() {
563
    global $wpi_cart, $user_ID;
564
565
    if(empty($wpi_cart)){
566
        $wpi_cart = wpinv_get_invoice_cart();
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_get_invoice_cart() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

566
        $wpi_cart = /** @scrutinizer ignore-deprecated */ wpinv_get_invoice_cart();
Loading history...
567
    }
568
569
    $invoice_user = (int)$wpi_cart->get_user_id();
570
    $valid_user_data = array(
571
        'user_id' => $invoice_user
572
    );
573
574
    // If guest checkout allowed
575
    if ( !wpinv_require_login_to_checkout() ) {
576
        return $valid_user_data;
577
    }
578
    
579
    // Verify there is a user_ID
580
    if ( $user_ID == $invoice_user ) {
581
        // Get the logged in user data
582
        $user_data = get_userdata( $user_ID );
583
        $required_fields  = wpinv_checkout_required_fields();
584
585
        // Loop through required fields and show error messages
586
         if ( !empty( $required_fields ) ) {
587
            foreach ( $required_fields as $field_name => $value ) {
588
                if ( in_array( $value, $required_fields ) && empty( $_POST[ 'wpinv_' . $field_name ] ) ) {
589
                    wpinv_set_error( $value['error_id'], $value['error_message'] );
590
                }
591
            }
592
        }
593
594
        // Verify data
595
        if ( $user_data ) {
596
            // Collected logged in user data
597
            $valid_user_data = array(
598
                'user_id'     => $user_ID,
599
                'email'       => isset( $_POST['wpinv_email'] ) ? sanitize_email( $_POST['wpinv_email'] ) : $user_data->user_email,
600
                'first_name'  => isset( $_POST['wpinv_first_name'] ) && ! empty( $_POST['wpinv_first_name'] ) ? sanitize_text_field( $_POST['wpinv_first_name'] ) : $user_data->first_name,
601
                'last_name'   => isset( $_POST['wpinv_last_name'] ) && ! empty( $_POST['wpinv_last_name']  ) ? sanitize_text_field( $_POST['wpinv_last_name']  ) : $user_data->last_name,
602
            );
603
604
            if ( !empty( $_POST[ 'wpinv_email' ] ) && !is_email( $_POST[ 'wpinv_email' ] ) ) {
605
                wpinv_set_error( 'invalid_email', __( 'Please enter a valid email address', 'invoicing' ) );
606
            }
607
        } else {
608
            // Set invalid user error
609
            wpinv_set_error( 'invalid_user', __( 'The user billing information is invalid', 'invoicing' ) );
610
        }
611
    } else {
612
        // Set invalid user error
613
        wpinv_set_error( 'invalid_user_id', __( 'The invalid invoice user id', 'invoicing' ) );
614
    }
615
616
    // Return user data
617
    return $valid_user_data;
618
}
619
620
function wpinv_checkout_validate_current_user() {
621
    global $wpi_cart;
622
623
    $data = array();
624
    
625
    if ( is_user_logged_in() ) {
626
        if ( !wpinv_require_login_to_checkout() || ( wpinv_require_login_to_checkout() && (int)$wpi_cart->get_user_id() === (int)get_current_user_id() ) ) {
627
            $data['user_id'] = (int)get_current_user_id();
628
        } else {
629
            wpinv_set_error( 'logged_in_only', __( 'You are not allowed to pay for this invoice', 'invoicing' ) );
630
        }
631
    } else {
632
        // If guest checkout allowed
633
        if ( !wpinv_require_login_to_checkout() ) {
634
            $data['user_id'] = 0;
635
        } else {
636
            wpinv_set_error( 'logged_in_only', __( 'You must be logged in to pay for this invoice', 'invoicing' ) );
637
        }
638
    }
639
640
    return $data;
641
}
642
643
644
/**
645
 * Processes checkout payments.
646
 *
647
 * @param WPInv_Invoice $invoice
648
 * @param GetPaid_Payment_Form_Submission $submission
649
 */
650
function wpinv_process_checkout( $invoice, $submission ) {
651
652
    // No need to send free invoices to the gateway.
653
    if ( $invoice->is_free() ) {
654
        $invoice->set_gateway( 'none' );
655
        $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true );
656
        $invoice->mark_paid();
657
        wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
658
    }
659
660
    // Clear an checkout errors.
661
    wpinv_clear_errors();
662
663
    // Fires before sending to the gateway.
664
    do_action( 'getpaid_checkout_before_gateway', $invoice, $submission );
665
666
    // Allow the sumission data to be modified before it is sent to the gateway.
667
    $submission_data    = $submission->get_data();
668
    $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $submission_data['wpi-gateway'], $submission, $invoice );
669
    $submission_data    = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice );
670
671
    // Validate the currency.
672
    if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) {
673
        wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support the invoice currency', 'invoicing' ) );
674
    }
675
676
    // Check to see if we have any errors.
677
    if ( wpinv_get_errors() ) {
678
        wpinv_send_back_to_checkout();
679
    }
680
681
    // Send info to the gateway for payment processing
682
    do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission );
683
684
    // Backwards compatibility.
685
    wpinv_send_to_gateway( $submission_gateway, $invoice->get_payment_meta() );
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_send_to_gateway() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

685
    /** @scrutinizer ignore-deprecated */ wpinv_send_to_gateway( $submission_gateway, $invoice->get_payment_meta() );
Loading history...
686
687
}
688