Passed
Push — master ( 9b92ba...883a6e )
by Stiofan
04:13
created

wpinv-gateway-functions.php ➔ wpinv_get_bank_info()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 3
nop 1
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
/**
3
 * Contains functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
function wpinv_get_payment_gateways() {
15
    // Default, built-in gateways
16
    $gateways = array(
17
        'paypal' => array(
18
            'admin_label'    => __( 'PayPal Standard', 'invoicing' ),
19
            'checkout_label' => __( 'PayPal Standard', 'invoicing' ),
20
            'ordering'       => 1,
21
        ),
22
        'authorizenet' => array(
23
            'admin_label'    => __( 'Authorize.Net (AIM)', 'invoicing' ),
24
            'checkout_label' => __( 'Authorize.Net - Credit Card / Debit Card', 'invoicing' ),
25
            'ordering'       => 4,
26
        ),
27
        'worldpay' => array(
28
            'admin_label'    => __( 'Worldpay', 'invoicing' ),
29
            'checkout_label' => __( 'Worldpay - Credit Card / Debit Card', 'invoicing' ),
30
            'ordering'       => 5,
31
        ),
32
        'bank_transfer' => array(
33
            'admin_label'    => __( 'Pre Bank Transfer', 'invoicing' ),
34
            'checkout_label' => __( 'Pre Bank Transfer', 'invoicing' ),
35
            'ordering'       => 11,
36
        ),
37
        'manual' => array(
38
            'admin_label'    => __( 'Test Payment', 'invoicing' ),
39
            'checkout_label' => __( 'Test Payment', 'invoicing' ),
40
            'ordering'       => 12,
41
        ),
42
    );
43
44
    return apply_filters( 'wpinv_payment_gateways', $gateways );
45
}
46
47
function wpinv_payment_gateway_titles( $all_gateways ) {
48
    global $wpinv_options;
49
    
50
    $gateways = array();
51
    foreach ( $all_gateways as $key => $gateway ) {
52
        if ( !empty( $wpinv_options[$key . '_title'] ) ) {
53
            $all_gateways[$key]['checkout_label'] = __( $wpinv_options[$key . '_title'], 'invoicing' );
54
        }
55
        
56
        $gateways[$key] = isset( $wpinv_options[$key . '_ordering'] ) ? $wpinv_options[$key . '_ordering'] : ( isset( $gateway['ordering'] ) ? $gateway['ordering'] : '' );
57
    }
58
59
    asort( $gateways );
60
    
61
    foreach ( $gateways as $gateway => $key ) {
62
        $gateways[$gateway] = $all_gateways[$gateway];
63
    }
64
    
65
    return $gateways;
66
}
67
add_filter( 'wpinv_payment_gateways', 'wpinv_payment_gateway_titles', 1000, 1 );
68
69
function wpinv_get_enabled_payment_gateways( $sort = false ) {
70
    $gateways = wpinv_get_payment_gateways();
71
    $enabled  = wpinv_get_option( 'gateways', false );
72
73
    $gateway_list = array();
74
75
    foreach ( $gateways as $key => $gateway ) {
76
        if ( isset( $enabled[ $key ] ) && $enabled[ $key ] == 1 ) {
77
            $gateway_list[ $key ] = $gateway;
78
        }
79
    }
80
81
    if ( true === $sort ) {
82
        uasort( $gateway_list, 'wpinv_sort_gateway_order' );
83
        
84
        // Reorder our gateways so the default is first
85
        $default_gateway_id = wpinv_get_default_gateway();
86
87
        if ( wpinv_is_gateway_active( $default_gateway_id ) ) {
88
            $default_gateway    = array( $default_gateway_id => $gateway_list[ $default_gateway_id ] );
89
            unset( $gateway_list[ $default_gateway_id ] );
90
91
            $gateway_list = array_merge( $default_gateway, $gateway_list );
92
        }
93
    }
94
95
    return apply_filters( 'wpinv_enabled_payment_gateways', $gateway_list );
96
}
97
98
function wpinv_sort_gateway_order( $a, $b ) {
99
    return $a['ordering'] - $b['ordering'];
100
}
101
102
function wpinv_is_gateway_active( $gateway ) {
103
    $gateways = wpinv_get_enabled_payment_gateways();
104
105
    $ret = is_array($gateways) && $gateway ?  array_key_exists( $gateway, $gateways ) : false;
106
107
    return apply_filters( 'wpinv_is_gateway_active', $ret, $gateway, $gateways );
108
}
109
110
function wpinv_get_default_gateway() {
111
    $default = wpinv_get_option( 'default_gateway', 'paypal' );
0 ignored issues
show
Documentation introduced by
'paypal' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
113
    if ( !wpinv_is_gateway_active( $default ) ) {
114
        $gateways = wpinv_get_enabled_payment_gateways();
115
        $gateways = array_keys( $gateways );
116
        $default  = reset( $gateways );
117
    }
118
119
    return apply_filters( 'wpinv_default_gateway', $default );
120
}
121
122
function wpinv_get_gateway_admin_label( $gateway ) {
123
    $gateways = wpinv_get_payment_gateways();
124
    $label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway;
125
    $payment  = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : false;
126
127
    if( $gateway == 'manual' && $payment ) {
128
        if( !( (float)wpinv_payment_total( $payment ) > 0 ) ) {
129
            $label = __( 'Free Purchase', 'invoicing' );
130
        }
131
    }
132
133
    return apply_filters( 'wpinv_gateway_admin_label', $label, $gateway );
134
}
135
136
function wpinv_get_gateway_description( $gateway ) {
137
    global $wpinv_options;
138
139
    $description = isset( $wpinv_options[$gateway . '_desc'] ) ? $wpinv_options[$gateway . '_desc'] : '';
140
141
    return apply_filters( 'wpinv_gateway_description', $description, $gateway );
142
}
143
144
function wpinv_get_gateway_button_label( $gateway ) {
145
    return apply_filters( 'wpinv_gateway_' . $gateway . '_button_label', '' );
146
}
147
148
function wpinv_get_gateway_checkout_label( $gateway ) {
149
    $gateways = wpinv_get_payment_gateways();
150
    $label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway;
151
152
    if( $gateway == 'manual' ) {
153
        $label = __( 'Manual Payment', 'invoicing' );
154
    }
155
156
    return apply_filters( 'wpinv_gateway_checkout_label', $label, $gateway );
157
}
158
159
function wpinv_settings_sections_gateways( $settings ) {
160
    $gateways = wpinv_get_payment_gateways();
161
    
162
    if (!empty($gateways)) {
163
        foreach  ($gateways as $key => $gateway) {
164
            $settings[$key] = $gateway['admin_label'];
165
        }
166
    }
167
    
168
    return $settings;    
169
}
170
add_filter( 'wpinv_settings_sections_gateways', 'wpinv_settings_sections_gateways', 10, 1 );
171
172
function wpinv_settings_gateways( $settings ) {
173
    $gateways = wpinv_get_payment_gateways();
174
    
175
    if (!empty($gateways)) {
176
        foreach  ($gateways as $key => $gateway) {
177
            $setting = array();
178
            $setting[$key . '_header'] = array(
179
                    'id'   => 'gateway_header',
180
                    'name' => '<h3>' . wp_sprintf( __( '%s Settings', 'invoicing' ), $gateway['admin_label'] ) . '</h3>',
181
                    'custom' => $key,
182
                    'type' => 'gateway_header',
183
                );
184
            $setting[$key . '_active'] = array(
185
                    'id'   => $key . '_active',
186
                    'name' => __( 'Active', 'invoicing' ),
187
                    'desc' => wp_sprintf( __( 'Enable %s', 'invoicing' ), $gateway['admin_label'] ),
188
                    'type' => 'checkbox',
189
                );
190
                
191
            $setting[$key . '_title'] = array(
192
                    'id'   => $key . '_title',
193
                    'name' => __( 'Title', 'invoicing' ),
194
                    'desc' => __( 'This controls the title which the user sees during checkout.', 'invoicing' ),
195
                    'type' => 'text',
196
                    'std' => isset($gateway['checkout_label']) ? $gateway['checkout_label'] : ''
197
                );
198
            
199
            $setting[$key . '_desc'] = array(
200
                    'id'   => $key . '_desc',
201
                    'name' => __( 'Description', 'invoicing' ),
202
                    'desc' => __( 'This controls the description which the user sees during checkout.', 'invoicing' ),
203
                    'type' => 'text',
204
                    'size' => 'large'
205
                );
206
                
207
            $setting[$key . '_ordering'] = array(
208
                    'id'   => $key . '_ordering',
209
                    'name' => __( 'Display Order', 'invoicing' ),
210
                    'type' => 'number',
211
                    'size' => 'small',
212
                    'std'  => isset($gateway['ordering']) ? $gateway['ordering'] : '10',
213
                    'min'  => '-100000',
214
                    'max'  => '100000',
215
                    'step' => '1'
216
                );
217
                
218
            $setting = apply_filters( 'wpinv_gateway_settings', $setting, $key );
219
            $setting = apply_filters( 'wpinv_gateway_settings_' . $key, $setting );
220
            
221
            $settings[$key] = $setting;
222
        }
223
    }
224
    
225
    return $settings;    
226
}
227
add_filter( 'wpinv_settings_gateways', 'wpinv_settings_gateways', 10, 1 );
228
229
function wpinv_gateway_header_callback( $args ) {
230
    echo '<input type="hidden" id="wpinv_settings[save_gateway]" name="wpinv_settings[save_gateway]" value="' . esc_attr( $args['custom'] ) . '" />';
231
}
232
233
function wpinv_get_gateway_supports( $gateway ) {
234
    $gateways = wpinv_get_enabled_payment_gateways();
235
    $supports = isset( $gateways[ $gateway ]['supports'] ) ? $gateways[ $gateway ]['supports'] : array();
236
    return apply_filters( 'wpinv_gateway_supports', $supports, $gateway );
237
}
238
239
function wpinv_gateway_supports_buy_now( $gateway ) {
240
    $supports = wpinv_get_gateway_supports( $gateway );
241
    $ret = in_array( 'buy_now', $supports );
242
    return apply_filters( 'wpinv_gateway_supports_buy_now', $ret, $gateway );
243
}
244
245
function wpinv_shop_supports_buy_now() {
246
    $gateways = wpinv_get_enabled_payment_gateways();
247
    $ret      = false;
248
249
    if ( !wpinv_use_taxes()  && $gateways ) {
250
        foreach ( $gateways as $gateway_id => $gateway ) {
251
            if ( wpinv_gateway_supports_buy_now( $gateway_id ) ) {
252
                $ret = true;
253
                break;
254
            }
255
        }
256
    }
257
258
    return apply_filters( 'wpinv_shop_supports_buy_now', $ret );
259
}
260
261
function wpinv_send_to_gateway( $gateway, $payment_data ) {
262
    $payment_data['gateway_nonce'] = wp_create_nonce( 'wpi-gateway' );
263
264
    // $gateway must match the ID used when registering the gateway
265
    do_action( 'wpinv_gateway_' . $gateway, $payment_data );
266
}
267
268
function wpinv_show_gateways() {
269
    $gateways = wpinv_get_enabled_payment_gateways();
270
    $show_gateways = false;
271
272
    $chosen_gateway = isset( $_GET['payment-mode'] ) ? preg_replace('/[^a-zA-Z0-9-_]+/', '', $_GET['payment-mode'] ) : false;
273
274
    if ( count( $gateways ) > 1 && empty( $chosen_gateway ) ) {
275
        $show_gateways = true;
276
        if ( wpinv_get_cart_total() <= 0 ) {
277
            $show_gateways = false;
278
        }
279
    }
280
    
281
    if ( !$show_gateways && wpinv_cart_has_recurring_item() ) {
282
        $show_gateways = true;
283
    }
284
285
    return apply_filters( 'wpinv_show_gateways', $show_gateways );
286
}
287
288
function wpinv_get_chosen_gateway( $invoice_id = 0 ) {
289
	$gateways = array_keys( wpinv_get_enabled_payment_gateways() );
290
291
    $chosen = false;
292
    if ( $invoice_id > 0 && $invoice = wpinv_get_invoice( $invoice_id ) ) {
293
        $chosen = $invoice->get_gateway();
294
    }
295
296
	$chosen   = isset( $_REQUEST['payment-mode'] ) ? sanitize_text_field( $_REQUEST['payment-mode'] ) : $chosen;
297
298
	if ( false !== $chosen ) {
299
		$chosen = preg_replace('/[^a-zA-Z0-9-_]+/', '', $chosen );
300
	}
301
302
	if ( ! empty ( $chosen ) ) {
303
		$enabled_gateway = urldecode( $chosen );
304
	} else if (  !empty( $invoice ) && (float)$invoice->get_subtotal() <= 0 ) {
305
		$enabled_gateway = 'manual';
306
	} else {
307
		$enabled_gateway = wpinv_get_default_gateway();
308
	}
309
    
310
    if ( !wpinv_is_gateway_active( $enabled_gateway ) && !empty( $gateways ) ) {
311
        if(wpinv_is_gateway_active( wpinv_get_default_gateway()) ){
312
            $enabled_gateway = wpinv_get_default_gateway();
313
        }else{
314
            $enabled_gateway = $gateways[0];
315
        }
316
317
    }
318
319
	return apply_filters( 'wpinv_chosen_gateway', $enabled_gateway );
320
}
321
322
function wpinv_record_gateway_error( $title = '', $message = '', $parent = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $parent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
323
    return wpinv_error_log( $message, $title );
324
}
325
326
function wpinv_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) {
327
	$ret  = 0;
328
	$args = array(
329
		'meta_key'    => '_wpinv_gateway',
330
		'meta_value'  => $gateway_id,
331
		'nopaging'    => true,
332
		'post_type'   => 'wpi_invoice',
333
		'post_status' => $status,
334
		'fields'      => 'ids'
335
	);
336
337
	$payments = new WP_Query( $args );
338
339
	if( $payments )
340
		$ret = $payments->post_count;
341
	return $ret;
342
}
343
344
function wpinv_settings_update_gateways( $input ) {
345
    global $wpinv_options;
346
    
347
    if ( !empty( $input['save_gateway'] ) ) {
348
        $gateways = wpinv_get_option( 'gateways', false );
349
        $gateways = !empty($gateways) ? $gateways : array();
350
        $gateway = $input['save_gateway'];
351
        
352
        if ( !empty( $input[$gateway . '_active'] ) ) {
353
            $gateways[$gateway] = 1;
354
        } else {
355
            if ( isset( $gateways[$gateway] ) ) {
356
                unset( $gateways[$gateway] );
357
            }
358
        }
359
        
360
        $input['gateways'] = $gateways;
361
    }
362
    
363
    if ( !empty( $input['default_gateway'] ) ) {
364
        $gateways = wpinv_get_payment_gateways();
365
        
366
        foreach ( $gateways as $key => $gateway ) {
367
            $active   = 0;
368
            if ( !empty( $input['gateways'] ) && !empty( $input['gateways'][$key] ) ) {
369
                $active = 1;
370
            }
371
            
372
            $input[$key . '_active'] = $active;
373
            
374
            if ( empty( $wpinv_options[$key . '_title'] ) ) {
375
                $input[$key . '_title'] = $gateway['checkout_label'];
376
            }
377
            
378
            if ( !isset( $wpinv_options[$key . '_ordering'] ) && isset( $gateway['ordering'] ) ) {
379
                $input[$key . '_ordering'] = $gateway['ordering'];
380
            }
381
        }
382
    }
383
    
384
    return $input;
385
}
386
add_filter( 'wpinv_settings_tab_gateways_sanitize', 'wpinv_settings_update_gateways', 10, 1 );
387
388
// PayPal Standard settings
389
function wpinv_gateway_settings_paypal( $setting ) {    
390
    $setting['paypal_desc']['std'] = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
391
    
392
    $setting['paypal_sandbox'] = array(
393
            'type' => 'checkbox',
394
            'id'   => 'paypal_sandbox',
395
            'name' => __( 'PayPal Sandbox', 'invoicing' ),
396
            'desc' => __( 'PayPal sandbox can be used to test payments.', 'invoicing' ),
397
            'std'  => 1
398
        );
399
        
400
    $setting['paypal_email'] = array(
401
            'type' => 'text',
402
            'id'   => 'paypal_email',
403
            'name' => __( 'PayPal Email', 'invoicing' ),
404
            'desc' => __( 'Please enter your PayPal account\'s email address. Ex: [email protected]', 'invoicing' ),
405
            'std' => __( '[email protected]', 'invoicing' ),
406
        );
407
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
408
    $setting['paypal_ipn_url'] = array(
409
            'type' => 'text',
410
            'id'   => 'paypal_ipn_url',
411
            'name' => __( 'PayPal IPN Url', 'invoicing' ),
412
            'desc' => __( 'Configure Instant Payment Notifications(IPN) url at PayPal. Ex: http://yoursite.com/?wpi-ipn=paypal', 'invoicing' ),
413
            'size' => 'large'
414
        );
415
    */
416
        
417
    return $setting;
418
}
419
add_filter( 'wpinv_gateway_settings_paypal', 'wpinv_gateway_settings_paypal', 10, 1 );
420
421
// Pre Bank Transfer settings
422
function wpinv_gateway_settings_bank_transfer( $setting ) {
423
    $setting['bank_transfer_desc']['std'] = __( 'Make your payment directly into our bank account. Please use your Invoice ID as the payment reference. Your invoice won\'t be processed until the funds have cleared in our account.', 'invoicing' );
424
    
425
    $setting['bank_transfer_ac_name'] = array(
426
            'type' => 'text',
427
            'id' => 'bank_transfer_ac_name',
428
            'name' => __( 'Account Name', 'invoicing' ),
429
            'desc' => __( 'Enter the bank account name to which you want to transfer payment.', 'invoicing' ),
430
            'std'  =>  __( 'Mr. John Martin', 'invoicing' ),
431
        );
432
    
433
    $setting['bank_transfer_ac_no'] = array(
434
            'type' => 'text',
435
            'id' => 'bank_transfer_ac_no',
436
            'name' => __( 'Account Number', 'invoicing' ),
437
            'desc' => __( 'Enter your bank account number.', 'invoicing' ),
438
            'std'  =>  __( 'TEST1234567890', 'invoicing' ),
439
        );
440
    
441
    $setting['bank_transfer_bank_name'] = array(
442
            'type' => 'text',
443
            'id'   => 'bank_transfer_bank_name',
444
            'name' => __( 'Bank Name', 'invoicing' ),
445
            'desc' => __( 'Enter the bank name to which you want to transfer payment.', 'invoicing' ),
446
            'std' => __( 'ICICI Bank', 'invoicing' ),
447
        );
448
    
449
    $setting['bank_transfer_ifsc'] = array(
450
            'type' => 'text',
451
            'id'   => 'bank_transfer_ifsc',
452
            'name' => __( 'IFSC Code', 'invoicing' ),
453
            'desc' => __( 'Enter your bank IFSC code.', 'invoicing' ),
454
            'std'  =>  __( 'ICIC0001234', 'invoicing' ),
455
        );
456
        
457
    $setting['bank_transfer_iban'] = array(
458
            'type' => 'text',
459
            'id'   => 'bank_transfer_iban',
460
            'name' => __( 'IBAN', 'invoicing' ),
461
            'desc' => __( 'Enter your International Bank Account Number(IBAN).', 'invoicing' ),
462
            'std'  =>  __( 'GB29NWBK60161331926819', 'invoicing' ),
463
        );
464
        
465
    $setting['bank_transfer_bic'] = array(
466
            'type' => 'text',
467
            'id'   => 'bank_transfer_bic',
468
            'name' => __( 'BIC/Swift Code', 'invoicing' ),
469
            'std'  =>  __( 'ICICGB2L129', 'invoicing' ),
470
        );
471
472
    $setting['bank_transfer_sort_code'] = array(
473
        'type' => 'text',
474
        'id'   => 'bank_transfer_sort_code',
475
        'name' => __( 'Sort Code', 'invoicing' ),
476
        'std'  =>  __( '12-34-56', 'invoicing' ),
477
    );
478
        
479
    $setting['bank_transfer_info'] = array(
480
            'id'   => 'bank_transfer_info',
481
            'name' => __( 'Instructions', 'invoicing' ),
482
            'desc' => __( 'Instructions that will be added to the thank you page and emails.', 'invoicing' ),
483
            'type' => 'textarea',
484
            'std' => __( 'Make your payment directly into our bank account. Please use your Invoice ID as the payment reference. Your invoice won\'t be processed until the funds have cleared in our account.', 'invoicing' ),
485
            'cols' => 37,
486
            'rows' => 5
487
        );
488
        
489
    return $setting;
490
}
491
add_filter( 'wpinv_gateway_settings_bank_transfer', 'wpinv_gateway_settings_bank_transfer', 10, 1 );
492
493
// Authorize.Net settings
494
function wpinv_gateway_settings_authorizenet( $setting ) {
495
    $setting['authorizenet_active']['desc'] = $setting['authorizenet_active']['desc'] . ' ' . __( '( currencies supported: AUD, CAD, CHF, DKK, EUR, GBP, JPY, NOK, NZD, PLN, SEK, USD, ZAR )', 'invoicing' );
496
    $setting['authorizenet_desc']['std'] = __( 'Pay using a Authorize.Net to process credit card / debit card transactions.', 'invoicing' );
497
    
498
    $setting['authorizenet_sandbox'] = array(
499
            'type' => 'checkbox',
500
            'id'   => 'authorizenet_sandbox',
501
            'name' => __( 'Authorize.Net Test Mode', 'invoicing' ),
502
            'desc' => __( 'Enable Authorize.Net test mode to test payments.', 'invoicing' ),
503
            'std'  => 1
504
        );
505
        
506
    $setting['authorizenet_login_id'] = array(
507
            'type' => 'text',
508
            'id'   => 'authorizenet_login_id',
509
            'name' => __( 'API Login ID', 'invoicing' ),
510
            'desc' => __( 'API Login ID can be obtained from Authorize.Net Account > Settings > Security Settings > General Security Settings > API Credentials & Keys. Example : 2j4rBekUnD', 'invoicing' ),
511
            'std' => '2j4rBekUnD',
512
        );
513
    
514
    $setting['authorizenet_transaction_key'] = array(
515
            'type' => 'text',
516
            'id'   => 'authorizenet_transaction_key',
517
            'name' => __( 'Transaction Key', 'invoicing' ),
518
            'desc' => __( 'Transaction Key can be obtained from Authorize.Net Account > Settings > Security Settings > General Security Settings > API Credentials & Keys. Example : 4vyBUOJgR74679xa', 'invoicing' ),
519
            'std' => '4vyBUOJgR74679xa',
520
        );
521
        
522
    $setting['authorizenet_md5_hash'] = array(
523
            'type' => 'text',
524
            'id'   => 'authorizenet_md5_hash',
525
            'name' => __( 'MD5-Hash', 'invoicing' ),
526
            'desc' => __( 'The MD5 Hash security feature allows you to authenticate transaction responses from the Authorize.Net. If you are accepting recurring payments then md5 hash will helps to validate response from Authorize.net. It can be obtained from Authorize.Net Account > Settings > Security Settings > General Settings > MD5 Hash.', 'invoicing' ),
527
            'std' => '',
528
        );
529
        
530
    $setting['authorizenet_ipn_url'] = array(
531
            'type' => 'ipn_url',
532
            'id'   => 'authorizenet_ipn_url',
533
            'name' => __( 'Silent Post URL', 'invoicing' ),
534
            'std' => wpinv_get_ipn_url( 'authorizenet' ),
535
            'desc' => __( 'If you are accepting recurring payments then you must set this url at Authorize.Net Account > Settings > Transaction Format Settings > Transaction Response Settings > Silent Post URL.', 'invoicing' ),
536
            'size' => 'large',
537
            'custom' => 'authorizenet',
538
            'readonly' => true
539
        );
540
        
541
    return $setting;
542
}
543
add_filter( 'wpinv_gateway_settings_authorizenet', 'wpinv_gateway_settings_authorizenet', 10, 1 );
544
545
// Worldpay settings
546
function wpinv_gateway_settings_worldpay( $setting ) {
547
    $setting['worldpay_desc']['std'] = __( 'Pay using a Worldpay account to process credit card / debit card transactions.', 'invoicing' );
548
    
549
    $setting['worldpay_sandbox'] = array(
550
            'type' => 'checkbox',
551
            'id'   => 'worldpay_sandbox',
552
            'name' => __( 'Worldpay Test Mode', 'invoicing' ),
553
            'desc' => __( 'This provides a special Test Environment to enable you to test your installation and integration to your website before going live.', 'invoicing' ),
554
            'std'  => 1
555
        );
556
        
557
    $setting['worldpay_instId'] = array(
558
            'type' => 'text',
559
            'id'   => 'worldpay_instId',
560
            'name' => __( 'Installation Id', 'invoicing' ),
561
            'desc' => __( 'Your installation id. Ex: 211616', 'invoicing' ),
562
            'std' => '211616',
563
        );
564
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
565
    $setting['worldpay_accId1'] = array(
566
            'type' => 'text',
567
            'id'   => 'worldpay_accId1',
568
            'name' => __( 'Merchant Code', 'invoicing' ),
569
            'desc' => __( 'Your merchant code. Ex: 12345', 'invoicing' ),
570
            'std' => '12345',
571
        );
572
    */
573
    
574
    $setting['worldpay_ipn_url'] = array(
575
            'type' => 'ipn_url',
576
            'id'   => 'worldpay_ipn_url',
577
            'name' => __( 'Worldpay Callback Url', 'invoicing' ),
578
            'std' => wpinv_get_ipn_url( 'worldpay' ),
579
            'desc' => wp_sprintf( __( 'Login to your Worldpay Merchant Interface then enable Payment Response & Shopper Response. Next, go to the Payment Response URL field and type "%s" or "%s" for a dynamic payment response.', 'invoicing' ), '<font style="color:#000;font-style:normal">' . wpinv_get_ipn_url( 'worldpay' ) . '</font>', '<font style="color:#000;font-style:normal">&lt;wpdisplay item=MC_callback&gt;</font>' ),
580
            'size' => 'large',
581
            'custom' => 'worldpay',
582
            'readonly' => true
583
        );
584
        
585
    return $setting;
586
}
587
add_filter( 'wpinv_gateway_settings_worldpay', 'wpinv_gateway_settings_worldpay', 10, 1 );
588
589
function wpinv_ipn_url_callback( $args ) {    
590
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
591
    
592
    $attrs = $args['readonly'] ? ' readonly' : '';
593
594
    $html = '<input style="background-color:#fefefe" type="text" ' . $attrs . ' value="' . esc_attr( $args['std'] ) . '" name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . ']" class="large-text">';
595
    $html .= '<label for="wpinv_settings[' . $sanitize_id . ']">'  . $args['desc'] . '</label>';
596
597
    echo $html;
598
}
599
600
function wpinv_is_test_mode( $gateway = '' ) {
601
    if ( empty( $gateway ) ) {
602
        return false;
603
    }
604
    
605
    $is_test_mode = wpinv_get_option( $gateway . '_sandbox', false );
606
    
607
    return apply_filters( 'wpinv_is_test_mode', $is_test_mode, $gateway );
608
}
609
610
function wpinv_get_ipn_url( $gateway = '', $args = array() ) {
611
    $data = array( 'wpi-listener' => 'IPN' );
612
    
613
    if ( !empty( $gateway ) ) {
614
        $data['wpi-gateway'] = wpinv_sanitize_key( $gateway );
615
    }
616
    
617
    $args = !empty( $args ) && is_array( $args ) ? array_merge( $data, $args ) : $data;
618
    
619
    $ipn_url = add_query_arg( $args,  home_url( 'index.php' ) );
620
    
621
    return apply_filters( 'wpinv_ipn_url', $ipn_url );
622
}
623
624
function wpinv_listen_for_payment_ipn() {
625
    // Regular PayPal IPN
626
    if ( isset( $_GET['wpi-listener'] ) && $_GET['wpi-listener'] == 'IPN' ) {
627
        do_action( 'wpinv_verify_payment_ipn' );
628
        
629
        if ( !empty( $_GET['wpi-gateway'] ) ) {
630
            wpinv_error_log( sanitize_text_field( $_GET['wpi-gateway'] ), 'wpinv_listen_for_payment_ipn()', __FILE__, __LINE__ );
631
            do_action( 'wpinv_verify_' . sanitize_text_field( $_GET['wpi-gateway'] ) . '_ipn' );
632
        }
633
    }
634
}
635
add_action( 'init', 'wpinv_listen_for_payment_ipn' );
636
637
function wpinv_get_bank_instructions() {
638
    $bank_instructions = wpinv_get_option( 'bank_transfer_info' );
639
    
640
    return apply_filters( 'wpinv_bank_instructions', $bank_instructions );
641
}
642
643
function wpinv_get_bank_info( $filtered = false ) {
644
    $bank_fields = array(
645
        'bank_transfer_ac_name'     => __( 'Account Name', 'invoicing' ),
646
        'bank_transfer_ac_no'       => __( 'Account Number', 'invoicing' ),
647
        'bank_transfer_bank_name'   => __( 'Bank Name', 'invoicing' ),
648
        'bank_transfer_ifsc'        => __( 'IFSC code', 'invoicing' ),
649
        'bank_transfer_iban'        => __( 'IBAN', 'invoicing' ),
650
        'bank_transfer_bic'         => __( 'BIC/Swift code', 'invoicing' ),
651
        'bank_transfer_sort_code'   => __( 'Sort Code', 'invoicing' )
652
    );
653
    
654
    $bank_info = array();
655
    foreach ( $bank_fields as $field => $label ) {
656
        if ( $filtered && !( $value = wpinv_get_option( $field ) ) ) {
657
            continue;
658
        }
659
        
660
        $bank_info[$field] = array( 'label' => $label, 'value' => $value );
0 ignored issues
show
Bug introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
661
    }
662
    
663
    return apply_filters( 'wpinv_bank_info', $bank_info, $filtered );
664
}
665
666
function wpinv_process_before_send_to_gateway( $invoice, $invoice_data = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $invoice_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
667
    if ( !empty( $invoice ) && $invoice->is_recurring() && $subscription_item = $invoice->get_recurring( true ) ) {        
668
        $args                          = array();
669
        $args['item_id']               = $subscription_item->ID;
670
        $args['initial_amount']        = wpinv_round_amount( $invoice->get_total() );
671
        $args['recurring_amount']      = wpinv_round_amount( $invoice->get_recurring_details( 'total' ) );
672
        $args['currency']              = $invoice->get_currency();
673
        $args['period']                = $subscription_item->get_recurring_period();
674
        $args['interval']              = $subscription_item->get_recurring_interval();
675 View Code Duplication
        if ( $subscription_item->has_free_trial() ) {
676
            $args['trial_period']      = $subscription_item->get_trial_period();
677
            $args['trial_interval']    = $subscription_item->get_trial_interval();
678
        } else {
679
            $args['trial_period']      = '';
680
            $args['trial_interval']    = 0;
681
        }
682
        $args['bill_times']            = (int)$subscription_item->get_recurring_limit();
683
        
684
        $invoice->update_subscription( $args );
685
    }
686
}
687
add_action( 'wpinv_checkout_before_send_to_gateway', 'wpinv_process_before_send_to_gateway', 10, 2 );
688
689
function wpinv_get_post_data( $method = 'request' ) {
690
    $data       = array();
691
    $request    = $_REQUEST;
692
    
693 View Code Duplication
    if ( $method == 'post' ) {
694
        if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] != 'POST' ) {
695
            return $data;
696
        }
697
        
698
        $request = $_POST;
699
    }
700
    
701 View Code Duplication
    if ( $method == 'get' ) {
702
        if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] != 'GET' ) {
703
            return $data;
704
        }
705
        
706
        $request = $_GET;
707
    }
708
    
709
    // Set initial post data to empty string
710
    $post_data = '';
711
    
712
    // Fallback just in case post_max_size is lower than needed
713
    if ( ini_get( 'allow_url_fopen' ) ) {
714
        $post_data = file_get_contents( 'php://input' );
715
    } else {
716
        // If allow_url_fopen is not enabled, then make sure that post_max_size is large enough
717
        ini_set( 'post_max_size', '12M' );
718
    }
719
    // Start the encoded data collection with notification command
720
    $encoded_data = 'cmd=_notify-validate';
721
722
    // Get current arg separator
723
    $arg_separator = wpinv_get_php_arg_separator_output();
724
725
    // Verify there is a post_data
726 View Code Duplication
    if ( $post_data || strlen( $post_data ) > 0 ) {
727
        // Append the data
728
        $encoded_data .= $arg_separator . $post_data;
729
    } else {
730
        // Check if POST is empty
731
        if ( empty( $request ) ) {
732
            // Nothing to do
733
            return;
734
        } else {
735
            // Loop through each POST
736
            foreach ( $request as $key => $value ) {
737
                // Encode the value and append the data
738
                $encoded_data .= $arg_separator . "$key=" . urlencode( $value );
739
            }
740
        }
741
    }
742
743
    // Convert collected post data to an array
744
    parse_str( $encoded_data, $data );
745
746 View Code Duplication
    foreach ( $data as $key => $value ) {
0 ignored issues
show
Bug introduced by
The expression $data of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
747
        if ( false !== strpos( $key, 'amp;' ) ) {
748
            $new_key = str_replace( '&amp;', '&', $key );
749
            $new_key = str_replace( 'amp;', '&' , $new_key );
750
751
            unset( $data[ $key ] );
752
            $data[ $new_key ] = sanitize_text_field( $value );
753
        }
754
    }
755
    
756
    return $data;
757
}
758
759
function wpinv_gateway_support_subscription( $gateway ) {
760
    $return = false;
761
    
762
    if ( wpinv_is_gateway_active( $gateway ) ) {
763
        $return = apply_filters( 'wpinv_' . $gateway . '_support_subscription', false );
764
    }
765
    
766
    return $return;
767
}
768
769
function wpinv_payment_gateways_on_cart( $gateways = array() ) {
770
    if ( !empty( $gateways ) && wpinv_cart_has_recurring_item() ) {
771
        foreach ( $gateways as $gateway => $info ) {
772
            if ( !wpinv_gateway_support_subscription( $gateway ) ) {
773
                unset( $gateways[$gateway] );
774
            }
775
        }
776
    }
777
    
778
    return $gateways;
779
}
780
add_filter( 'wpinv_payment_gateways_on_cart', 'wpinv_payment_gateways_on_cart', 10, 1 );