Passed
Push — master ( 0fd3da...d47925 )
by Brian
05:32
created

getpaid_payment_gateway_supports()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 17
rs 10
c 0
b 0
f 0
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', array( 'manual' => 1 ) );
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
/**
93
 * Returns a gateway's name.
94
 *
95
 * @param string $gateway The gateway to key.
96
 * @return string
97
 */
98
function wpinv_get_gateway_admin_label( $gateway ) {
99
100
    if ( empty( $gateway ) || 'none' == $gateway ) {
101
        return esc_html__( 'No Gateway', 'invoicing' );
102
    }
103
104
    $gateways = wpinv_get_payment_gateways();
105
    $label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway;
106
    $gateway  = apply_filters( 'wpinv_gateway_admin_label', $label, $gateway );
107
108
    return wpinv_clean( $gateway );
0 ignored issues
show
Bug Best Practice introduced by
The expression return wpinv_clean($gateway) also could return the type array which is incompatible with the documented return type string.
Loading history...
109
}
110
111
/**
112
 * Retrieves the gateway description.
113
 *
114
 * @param string $gateway
115
 */
116
function wpinv_get_gateway_description( $gateway ) {
117
    global $wpinv_options;
118
119
    $description = ! empty( $wpinv_options[$gateway . '_desc'] ) ? $wpinv_options[$gateway . '_desc'] : '';
120
121
    return apply_filters( 'wpinv_gateway_description', $description, $gateway );
122
}
123
124
function wpinv_get_gateway_button_label( $gateway ) {
125
    return apply_filters( 'wpinv_gateway_' . $gateway . '_button_label', '' );
126
}
127
128
function wpinv_get_gateway_checkout_label( $gateway ) {
129
    $gateways = wpinv_get_payment_gateways();
130
    $label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway;
131
132
    if ( $gateway == 'none' ) {
133
        $label = __( 'None', 'invoicing' );
134
    }
135
136
    return apply_filters( 'wpinv_gateway_checkout_label', ucfirst( $label ), $gateway );
137
}
138
139
function wpinv_settings_sections_gateways( $settings ) {
140
    $gateways = wpinv_get_payment_gateways();
141
142
    if (!empty($gateways)) {
143
        foreach  ($gateways as $key => $gateway) {
144
            $settings[$key] = $gateway['admin_label'];
145
        }
146
    }
147
148
    return $settings;
149
}
150
add_filter( 'wpinv_settings_sections_gateways', 'wpinv_settings_sections_gateways', 10, 1 );
151
152
/**
153
 * Adds GateWay settings.
154
 */
155
function wpinv_settings_gateways( $settings ) {
156
157
    // Loop through each gateway.
158
    foreach  ( wpinv_get_payment_gateways() as $key => $gateway ) {
159
160
        $gateway_settings = array(
161
162
            // Header.
163
            "{$key}_header" => array(
164
165
                'id'     => "{$key}_gateway_header",
166
                'name'   => '<h3>' . wp_sprintf( __( '%s Settings', 'invoicing' ), $gateway['admin_label'] ) . '</h3>',
167
                'custom' => $key,
168
                'type'   => 'gateway_header',
169
170
            ),
171
172
            // Activate/Deactivate a gateway.
173
            "{$key}_active" => array(
174
                'id'   => $key . '_active',
175
                'name' => __( 'Activate', 'invoicing' ),
176
                'desc' => wp_sprintf( __( 'Enable %s', 'invoicing' ), $gateway['admin_label'] ),
177
                'type' => 'checkbox',
178
            ),
179
180
            // Activate/Deactivate sandbox.
181
            "{$key}_sandbox" => array(
182
                'id'   => $key . '_sandbox',
183
                'name' => __( 'Sandbox', 'invoicing' ),
184
                'desc' => __( 'Enable sandbox to test payments', 'invoicing' ),
185
                'type' => 'checkbox',
186
                'std'  => '1',
187
            ),
188
189
            // Checkout title.
190
            "{$key}_title" => array(
191
                'id'   => $key . '_title',
192
                'name' => __( 'Checkout Title', 'invoicing' ),
193
                'std'  => isset( $gateway['checkout_label'] ) ? $gateway['checkout_label'] : '',
194
                'type' => 'text',
195
            ),
196
197
            // Checkout description.
198
            "{$key}_desc" => array(
199
                'id'   => $key . '_desc',
200
                'name' => __( 'Checkout Description', 'invoicing' ),
201
                'std'  => apply_filters( "getpaid_default_{$key}_checkout_description", '' ),
202
                'type' => 'text',
203
            ),
204
205
            // Checkout order.
206
            "{$key}_ordering" => array(
207
                'id'   => $key . '_ordering',
208
                'name' => __( 'Priority', 'invoicing' ),
209
                'std'  => apply_filters( "getpaid_default_{$key}_checkout_description", '' ),
210
                'type' => 'number',
211
                'step' => '1',
212
                'min'  => '0',
213
                'max'  => '100000',
214
                'std'  => isset( $gateway['ordering'] ) ? $gateway['ordering'] : '10',
215
            ),
216
217
        );
218
219
        // Maybe remove the sandbox.
220
        if ( ! getpaid_payment_gateway_supports( $key, 'sandbox' ) ) {
221
            unset( $gateway_settings["{$key}_sandbox"] );
222
        }
223
224
        $gateway_settings = apply_filters( 'wpinv_gateway_settings', $gateway_settings, $key, $gateway );
225
        $gateway_settings = apply_filters( 'wpinv_gateway_settings_' . $key, $gateway_settings, $gateway );
226
227
        $settings[$key] = $gateway_settings;
228
    }
229
230
    return $settings;
231
232
}
233
add_filter( 'wpinv_settings_gateways', 'wpinv_settings_gateways', 10, 1 );
234
235
function wpinv_gateway_header_callback( $args ) {
236
    echo '<input type="hidden" id="wpinv_settings[save_gateway]" name="wpinv_settings[save_gateway]" value="' . esc_attr( $args['custom'] ) . '" />';
237
}
238
239
/**
240
 * Checks if a given gateway supports a given feature.
241
 *
242
 * @param string $gateway
243
 * @param string $feature
244
 * @return bool
245
 * @since 2.3.0
246
 */
247
function getpaid_payment_gateway_supports( $gateway, $feature ) {
248
249
    $supports = false;
250
251
    if ( wpinv_is_gateway_active( $gateway ) ) {
252
253
        $supports = apply_filters( "getpaid_{$gateway}_supports_{$feature}", false );
254
255
        // Backwards compatibility.
256
        $supports = apply_filters( "wpinv_{$gateway}_supports_{$feature}", $supports );
257
        $supports = apply_filters( "wpinv_{$gateway}_support_{$feature}", $supports );
258
259
        $supports = apply_filters( "getpaid_gateway_supports_{$feature}", $supports, $gateway );
260
        $supports = apply_filters( 'getpaid_payment_gateway_supports', $supports, $feature, $gateway );
261
    }
262
263
    return $supports;
264
}
265
266
function wpinv_get_chosen_gateway( $invoice_id = 0 ) {
267
	$gateways = array_keys( wpinv_get_enabled_payment_gateways() );
268
269
    $chosen = false;
270
    if ( $invoice_id > 0 && $invoice = wpinv_get_invoice( $invoice_id ) ) {
271
        $chosen = $invoice->get_gateway();
272
    }
273
274
	$chosen   = isset( $_REQUEST['payment-mode'] ) ? sanitize_text_field( $_REQUEST['payment-mode'] ) : $chosen;
275
276
	if ( false !== $chosen ) {
277
		$chosen = preg_replace('/[^a-zA-Z0-9-_]+/', '', $chosen );
278
	}
279
280
	if ( ! empty ( $chosen ) ) {
281
		$enabled_gateway = urldecode( $chosen );
282
	} else if (  !empty( $invoice ) && (float)$invoice->get_subtotal() <= 0 ) {
283
		$enabled_gateway = 'manual';
284
	} else {
285
		$enabled_gateway = wpinv_get_default_gateway();
286
	}
287
288
    if ( !wpinv_is_gateway_active( $enabled_gateway ) && !empty( $gateways ) ) {
289
        if(wpinv_is_gateway_active( wpinv_get_default_gateway()) ){
290
            $enabled_gateway = wpinv_get_default_gateway();
291
        }else{
292
            $enabled_gateway = $gateways[0];
293
        }
294
295
    }
296
297
	return apply_filters( 'wpinv_chosen_gateway', $enabled_gateway );
298
}
299
300
function wpinv_record_gateway_error( $title = '', $message = '' ) {
301
    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...
302
}
303
304
function wpinv_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) {
305
	$ret  = 0;
306
	$args = array(
307
		'meta_key'    => '_wpinv_gateway',
308
		'meta_value'  => $gateway_id,
309
		'nopaging'    => true,
310
		'post_type'   => 'wpi_invoice',
311
		'post_status' => $status,
312
		'fields'      => 'ids'
313
	);
314
315
	$payments = new WP_Query( $args );
316
317
	if( $payments )
0 ignored issues
show
introduced by
$payments is of type WP_Query, thus it always evaluated to true.
Loading history...
318
		$ret = $payments->post_count;
319
	return $ret;
320
}
321
322
function wpinv_settings_update_gateways( $input ) {
323
    global $wpinv_options;
324
325
    if ( !empty( $input['save_gateway'] ) ) {
326
        $gateways = wpinv_get_option( 'gateways', array( 'manual' => 1 ) );
327
        $gateways = !empty($gateways) ? $gateways : array();
328
        $gateway = $input['save_gateway'];
329
330
        if ( !empty( $input[$gateway . '_active'] ) ) {
331
            $gateways[$gateway] = 1;
332
        } else {
333
            if ( isset( $gateways[$gateway] ) ) {
334
                unset( $gateways[$gateway] );
335
            }
336
        }
337
338
        $input['gateways'] = $gateways;
339
    }
340
341
    if ( !empty( $input['default_gateway'] ) ) {
342
        $gateways = wpinv_get_payment_gateways();
343
344
        foreach ( $gateways as $key => $gateway ) {
345
            $active   = 0;
346
            if ( !empty( $input['gateways'] ) && !empty( $input['gateways'][$key] ) ) {
347
                $active = 1;
348
            }
349
350
            $input[$key . '_active'] = $active;
351
352
            if ( empty( $wpinv_options[$key . '_title'] ) ) {
353
                $input[$key . '_title'] = $gateway['checkout_label'];
354
            }
355
356
            if ( !isset( $wpinv_options[$key . '_ordering'] ) && isset( $gateway['ordering'] ) ) {
357
                $input[$key . '_ordering'] = $gateway['ordering'];
358
            }
359
        }
360
    }
361
362
    return $input;
363
}
364
add_filter( 'wpinv_settings_tab_gateways_sanitize', 'wpinv_settings_update_gateways', 10, 1 );
365
366
// PayPal Standard settings
367
function wpinv_gateway_settings_paypal( $setting ) {
368
    $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' );
369
    $setting['paypal_desc']['std'] = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
370
371
    $setting['paypal_sandbox'] = array(
372
            'type' => 'checkbox',
373
            'id'   => 'paypal_sandbox',
374
            'name' => __( 'PayPal Sandbox', 'invoicing' ),
375
            'desc' => __( 'PayPal sandbox can be used to test payments.', 'invoicing' ),
376
            'std'  => 1
377
        );
378
379
    $setting['paypal_email'] = array(
380
            'type' => 'text',
381
            'id'   => 'paypal_email',
382
            'name' => __( 'PayPal Email', 'invoicing' ),
383
            'desc' => __( 'Please enter your PayPal account\'s email address. Ex: [email protected]', 'invoicing' ),
384
            'std' => __( '[email protected]', 'invoicing' ),
385
        );
386
    /*
387
    $setting['paypal_ipn_url'] = array(
388
            'type' => 'text',
389
            'id'   => 'paypal_ipn_url',
390
            'name' => __( 'PayPal IPN Url', 'invoicing' ),
391
            'desc' => __( 'Configure Instant Payment Notifications(IPN) url at PayPal. Ex: http://yoursite.com/?wpi-ipn=paypal', 'invoicing' ),
392
            'size' => 'large'
393
        );
394
    */
395
396
    return $setting;
397
}
398
add_filter( 'wpinv_gateway_settings_paypal', 'wpinv_gateway_settings_paypal', 10, 1 );
399
400
/**
401
 * Displays the ipn url field.
402
 */
403
function wpinv_ipn_url_callback( $args ) {
404
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
405
406
    $attrs = $args['readonly'] ? ' readonly' : '';
407
408
    $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()">';
409
    $html .= '<label for="wpinv_settings[' . $sanitize_id . ']">'  . $args['desc'] . '</label>';
410
411
    echo $html;
412
}
413
414
/**
415
 * Checks if a gateway is in test mode.
416
 *
417
 * @param string $gateway The gateway to check for.
418
 *
419
 * @return bool
420
 */
421
function wpinv_is_test_mode( $gateway = '' ) {
422
    $sandbox  = empty( $gateway ) ? false : wpinv_get_option( "{$gateway}_sandbox", true );
423
    $supports = getpaid_payment_gateway_supports( $gateway, 'sandbox' );
424
    return apply_filters( 'wpinv_is_test_mode', $sandbox && $supports, $gateway );
425
}
426
427
/**
428
 * Retrieves the ipn url.
429
 *
430
 * @param string $gateway The gateway whose IPN url we should retrieve.
431
 * @param array $args extra args to add to the url.
432
 *
433
 * @return string
434
 */
435
function wpinv_get_ipn_url( $gateway = false, $args = array() ) {
436
    $args = wp_parse_args(
437
        array(
438
            'wpi-listener' => 'IPN',
439
            'wpi-gateway'  => $gateway
440
        ),
441
        $args
442
    );
443
444
    return apply_filters( 'wpinv_ipn_url', add_query_arg( $args,  home_url( 'index.php' ) ), $gateway, $args );
445
446
}
447
448
/**
449
 * Retrieves the non-query string ipn url.
450
 *
451
 * @param string $gateway The gateway whose IPN url we should retrieve.
452
 *
453
 * @return string
454
 */
455
function getpaid_get_non_query_string_ipn_url( $gateway ) {
456
    $gateway = wpinv_sanitize_key( $gateway );
457
    return home_url( "getpaid-ipn/$gateway" );
458
}
459
460
461
/**
462
 * Retrieves request data with slashes removed slashes.
463
 */
464
function wpinv_get_post_data( $method = 'request' ) {
465
466
    if ( $method == 'post' ) {
467
        return wp_unslash( $_POST );
468
    }
469
470
    if ( $method == 'get' ) {
471
        return wp_unslash( $_GET );
472
    }
473
474
    return wp_unslash( $_REQUEST );
475
476
}
477
478
/**
479
 * Checks if a given gateway supports subscription payments.
480
 */
481
function wpinv_gateway_support_subscription( $gateway ) {
482
    return getpaid_payment_gateway_supports( $gateway, 'subscription' );
483
}
484
485
/**
486
 * Filters payment form gateways.
487
 *
488
 * @param array $gateways an array of gateways.
489
 * @param GetPaid_Payment_Form $form payment form.
490
 */
491
function wpinv_payment_gateways_on_cart( $gateways, $form ) {
492
493
    if ( $form->is_recurring() ) {
494
495
        foreach ( array_keys( $gateways ) as $gateway ) {
496
497
            if ( ! wpinv_gateway_support_subscription( $gateway ) ) {
498
                unset( $gateways[$gateway] );
499
            }
500
501
        }
502
503
    }
504
505
    return $gateways;
506
}
507
add_filter( 'getpaid_payment_form_gateways', 'wpinv_payment_gateways_on_cart', 10, 2 );
508