Passed
Push — master ( c1548d...55a972 )
by Brian
04:30
created

getpaid_get_non_query_string_ipn_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
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 ( ! apply_filters( "wpinv_{$key}_supports_sandbox", false ) ) {
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
function wpinv_get_gateway_supports( $gateway ) {
240
    $gateways = wpinv_get_enabled_payment_gateways();
241
    $supports = isset( $gateways[ $gateway ]['supports'] ) ? $gateways[ $gateway ]['supports'] : array();
242
    return apply_filters( 'wpinv_gateway_supports', $supports, $gateway );
243
}
244
245
function wpinv_get_chosen_gateway( $invoice_id = 0 ) {
246
	$gateways = array_keys( wpinv_get_enabled_payment_gateways() );
247
248
    $chosen = false;
249
    if ( $invoice_id > 0 && $invoice = wpinv_get_invoice( $invoice_id ) ) {
250
        $chosen = $invoice->get_gateway();
251
    }
252
253
	$chosen   = isset( $_REQUEST['payment-mode'] ) ? sanitize_text_field( $_REQUEST['payment-mode'] ) : $chosen;
254
255
	if ( false !== $chosen ) {
256
		$chosen = preg_replace('/[^a-zA-Z0-9-_]+/', '', $chosen );
257
	}
258
259
	if ( ! empty ( $chosen ) ) {
260
		$enabled_gateway = urldecode( $chosen );
261
	} else if (  !empty( $invoice ) && (float)$invoice->get_subtotal() <= 0 ) {
262
		$enabled_gateway = 'manual';
263
	} else {
264
		$enabled_gateway = wpinv_get_default_gateway();
265
	}
266
    
267
    if ( !wpinv_is_gateway_active( $enabled_gateway ) && !empty( $gateways ) ) {
268
        if(wpinv_is_gateway_active( wpinv_get_default_gateway()) ){
269
            $enabled_gateway = wpinv_get_default_gateway();
270
        }else{
271
            $enabled_gateway = $gateways[0];
272
        }
273
274
    }
275
276
	return apply_filters( 'wpinv_chosen_gateway', $enabled_gateway );
277
}
278
279
function wpinv_record_gateway_error( $title = '', $message = '' ) {
280
    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...
281
}
282
283
function wpinv_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) {
284
	$ret  = 0;
285
	$args = array(
286
		'meta_key'    => '_wpinv_gateway',
287
		'meta_value'  => $gateway_id,
288
		'nopaging'    => true,
289
		'post_type'   => 'wpi_invoice',
290
		'post_status' => $status,
291
		'fields'      => 'ids'
292
	);
293
294
	$payments = new WP_Query( $args );
295
296
	if( $payments )
0 ignored issues
show
introduced by
$payments is of type WP_Query, thus it always evaluated to true.
Loading history...
297
		$ret = $payments->post_count;
298
	return $ret;
299
}
300
301
function wpinv_settings_update_gateways( $input ) {
302
    global $wpinv_options;
303
    
304
    if ( !empty( $input['save_gateway'] ) ) {
305
        $gateways = wpinv_get_option( 'gateways', array( 'manual' => 1 ) );
306
        $gateways = !empty($gateways) ? $gateways : array();
307
        $gateway = $input['save_gateway'];
308
        
309
        if ( !empty( $input[$gateway . '_active'] ) ) {
310
            $gateways[$gateway] = 1;
311
        } else {
312
            if ( isset( $gateways[$gateway] ) ) {
313
                unset( $gateways[$gateway] );
314
            }
315
        }
316
        
317
        $input['gateways'] = $gateways;
318
    }
319
    
320
    if ( !empty( $input['default_gateway'] ) ) {
321
        $gateways = wpinv_get_payment_gateways();
322
        
323
        foreach ( $gateways as $key => $gateway ) {
324
            $active   = 0;
325
            if ( !empty( $input['gateways'] ) && !empty( $input['gateways'][$key] ) ) {
326
                $active = 1;
327
            }
328
            
329
            $input[$key . '_active'] = $active;
330
            
331
            if ( empty( $wpinv_options[$key . '_title'] ) ) {
332
                $input[$key . '_title'] = $gateway['checkout_label'];
333
            }
334
            
335
            if ( !isset( $wpinv_options[$key . '_ordering'] ) && isset( $gateway['ordering'] ) ) {
336
                $input[$key . '_ordering'] = $gateway['ordering'];
337
            }
338
        }
339
    }
340
    
341
    return $input;
342
}
343
add_filter( 'wpinv_settings_tab_gateways_sanitize', 'wpinv_settings_update_gateways', 10, 1 );
344
345
// PayPal Standard settings
346
function wpinv_gateway_settings_paypal( $setting ) {    
347
    $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' );
348
    $setting['paypal_desc']['std'] = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
349
    
350
    $setting['paypal_sandbox'] = array(
351
            'type' => 'checkbox',
352
            'id'   => 'paypal_sandbox',
353
            'name' => __( 'PayPal Sandbox', 'invoicing' ),
354
            'desc' => __( 'PayPal sandbox can be used to test payments.', 'invoicing' ),
355
            'std'  => 1
356
        );
357
        
358
    $setting['paypal_email'] = array(
359
            'type' => 'text',
360
            'id'   => 'paypal_email',
361
            'name' => __( 'PayPal Email', 'invoicing' ),
362
            'desc' => __( 'Please enter your PayPal account\'s email address. Ex: [email protected]', 'invoicing' ),
363
            'std' => __( '[email protected]', 'invoicing' ),
364
        );
365
    /*
366
    $setting['paypal_ipn_url'] = array(
367
            'type' => 'text',
368
            'id'   => 'paypal_ipn_url',
369
            'name' => __( 'PayPal IPN Url', 'invoicing' ),
370
            'desc' => __( 'Configure Instant Payment Notifications(IPN) url at PayPal. Ex: http://yoursite.com/?wpi-ipn=paypal', 'invoicing' ),
371
            'size' => 'large'
372
        );
373
    */
374
        
375
    return $setting;
376
}
377
add_filter( 'wpinv_gateway_settings_paypal', 'wpinv_gateway_settings_paypal', 10, 1 );
378
379
/**
380
 * Displays the ipn url field.
381
 */
382
function wpinv_ipn_url_callback( $args ) {
383
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
384
    
385
    $attrs = $args['readonly'] ? ' readonly' : '';
386
387
    $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()">';
388
    $html .= '<label for="wpinv_settings[' . $sanitize_id . ']">'  . $args['desc'] . '</label>';
389
390
    echo $html;
391
}
392
393
/**
394
 * Checks if a gateway is in test mode.
395
 *
396
 * @param string $gateway The gateway to check for.
397
 *
398
 * @return bool
399
 */
400
function wpinv_is_test_mode( $gateway = '' ) {
401
    $sandbox  = empty( $gateway ) ? false : wpinv_get_option( "{$gateway}_sandbox", true );
402
    $supports = apply_filters( "wpinv_{$gateway}_supports_sandbox", false );
403
    return apply_filters( 'wpinv_is_test_mode', $sandbox && $supports, $gateway );
404
}
405
406
/**
407
 * Retrieves the ipn url.
408
 * 
409
 * @param string $gateway The gateway whose IPN url we should retrieve.
410
 * @param array $args extra args to add to the url.
411
 * 
412
 * @return string
413
 */
414
function wpinv_get_ipn_url( $gateway = false, $args = array() ) {
415
    $args = wp_parse_args(
416
        array(
417
            'wpi-listener' => 'IPN',
418
            'wpi-gateway'  => $gateway
419
        ),
420
        $args
421
    );
422
423
    return apply_filters( 'wpinv_ipn_url', add_query_arg( $args,  home_url( 'index.php' ) ), $gateway, $args );
424
425
}
426
427
/**
428
 * Retrieves the non-query string ipn url.
429
 * 
430
 * @param string $gateway The gateway whose IPN url we should retrieve.
431
 * 
432
 * @return string
433
 */
434
function getpaid_get_non_query_string_ipn_url( $gateway ) {
435
    $gateway = wpinv_sanitize_key( $gateway );
436
    return home_url( "getpaid-ipn/$gateway" );
437
}
438
439
440
/**
441
 * Retrieves request data with slashes removed slashes.
442
 */
443
function wpinv_get_post_data( $method = 'request' ) {
444
445
    if ( $method == 'post' ) {
446
        return wp_unslash( $_POST );
447
    }
448
449
    if ( $method == 'get' ) {
450
        return wp_unslash( $_GET );
451
    }
452
453
    return wp_unslash( $_REQUEST );
454
  
455
}
456
457
/**
458
 * Checks if a given gateway supports subscription payments.
459
 */
460
function wpinv_gateway_support_subscription( $gateway ) {
461
    $supports = false;
462
463
    if ( wpinv_is_gateway_active( $gateway ) ) {
464
        $supports = apply_filters( 'wpinv_' . $gateway . '_support_subscription', $supports );
465
466
        $supports = apply_filters( 'getapid_gateway_supports_subscription', $supports, $gateway );
467
    }
468
469
    return $supports;
470
}
471
472
/**
473
 * Filters payment form gateways.
474
 * 
475
 * @param array $gateways an array of gateways.
476
 * @param GetPaid_Payment_Form $form payment form.
477
 */
478
function wpinv_payment_gateways_on_cart( $gateways, $form ) {
479
480
    if ( $form->is_recurring() ) {
481
482
        foreach ( array_keys( $gateways ) as $gateway ) {
483
484
            if ( ! wpinv_gateway_support_subscription( $gateway ) ) {
485
                unset( $gateways[$gateway] );
486
            }
487
488
        }
489
490
    }
491
492
    return $gateways;
493
}
494
add_filter( 'getpaid_payment_form_gateways', 'wpinv_payment_gateways_on_cart', 10, 2 );
495