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
|
|
|
/** |
40
|
|
|
* Returns an array of enabled gateways. |
41
|
|
|
* |
42
|
|
|
* @param bool $sort |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
function wpinv_get_enabled_payment_gateways( $sort = false ) { |
46
|
|
|
|
47
|
|
|
$enabled = array(); |
48
|
|
|
|
49
|
|
|
foreach ( wpinv_get_payment_gateways() as $gateway => $data ) { |
50
|
|
|
|
51
|
|
|
if ( (int) wpinv_get_option( "{$gateway}_active", $gateway === 'manual' ) === 1 ) { |
52
|
|
|
$enabled[ $gateway ] = $data; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ( true === $sort ) { |
58
|
|
|
uasort( $enabled, 'wpinv_sort_gateway_order' ); |
59
|
|
|
|
60
|
|
|
// Reorder our gateways so the default is first |
61
|
|
|
$default_gateway_id = wpinv_get_default_gateway(); |
62
|
|
|
if ( isset( $enabled[ $default_gateway_id ] ) ) { |
63
|
|
|
$default_gateway = array( |
64
|
|
|
$default_gateway_id => $enabled[ $default_gateway_id ] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
unset( $enabled[ $default_gateway_id ] ); |
68
|
|
|
$enabled = array_merge( $default_gateway, $enabled ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return apply_filters( 'wpinv_enabled_payment_gateways', $enabled ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
function wpinv_sort_gateway_order( $a, $b ) { |
77
|
|
|
return $a['ordering'] - $b['ordering']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Checks if a given gateway is active. |
82
|
|
|
* |
83
|
|
|
* @param string $gateway |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
function wpinv_is_gateway_active( $gateway ) { |
87
|
|
|
$is_active = (int) wpinv_get_option( "{$gateway}_active", $gateway === 'manual' ) === 1 ; |
88
|
|
|
return apply_filters( 'wpinv_is_gateway_active', $is_active, $gateway ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Retrieves the default gateway. |
93
|
|
|
* |
94
|
|
|
* @return string|false |
95
|
|
|
*/ |
96
|
|
|
function wpinv_get_default_gateway() { |
97
|
|
|
$default = wpinv_get_option( 'default_gateway' ); |
98
|
|
|
$gateways = wpinv_get_enabled_payment_gateways(); |
99
|
|
|
$default = ! empty( $default ) && isset( $gateways[ $default ] ) ? $default : false; |
100
|
|
|
|
101
|
|
|
return apply_filters( 'wpinv_default_gateway', $default ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns a gateway's name. |
106
|
|
|
* |
107
|
|
|
* @param string $gateway The gateway to key. |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
function wpinv_get_gateway_admin_label( $gateway ) { |
111
|
|
|
|
112
|
|
|
if ( empty( $gateway ) || 'none' == $gateway ) { |
113
|
|
|
return esc_html__( 'No Gateway', 'invoicing' ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$gateways = wpinv_get_payment_gateways(); |
117
|
|
|
$label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway; |
118
|
|
|
$gateway = apply_filters( 'wpinv_gateway_admin_label', $label, $gateway ); |
119
|
|
|
|
120
|
|
|
return wpinv_clean( $gateway ); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retrieves the gateway description. |
125
|
|
|
* |
126
|
|
|
* @param string $gateway |
127
|
|
|
*/ |
128
|
|
|
function wpinv_get_gateway_description( $gateway ) { |
129
|
|
|
global $wpinv_options; |
130
|
|
|
|
131
|
|
|
$description = ! empty( $wpinv_options[$gateway . '_desc'] ) ? $wpinv_options[$gateway . '_desc'] : ''; |
132
|
|
|
|
133
|
|
|
return apply_filters( 'wpinv_gateway_description', $description, $gateway ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
function wpinv_get_gateway_button_label( $gateway ) { |
137
|
|
|
return apply_filters( 'wpinv_gateway_' . $gateway . '_button_label', '' ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
function wpinv_get_gateway_checkout_label( $gateway ) { |
141
|
|
|
$gateways = wpinv_get_payment_gateways(); |
142
|
|
|
$label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway; |
143
|
|
|
|
144
|
|
|
if ( $gateway == 'none' ) { |
145
|
|
|
$label = __( 'None', 'invoicing' ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return apply_filters( 'wpinv_gateway_checkout_label', ucfirst( $label ), $gateway ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
function wpinv_settings_sections_gateways( $settings ) { |
152
|
|
|
$gateways = wpinv_get_payment_gateways(); |
153
|
|
|
ksort( $gateways ); |
154
|
|
|
|
155
|
|
|
foreach ( $gateways as $key => $gateway ) { |
156
|
|
|
$settings[ $key ] = $gateway['admin_label']; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $settings; |
160
|
|
|
} |
161
|
|
|
add_filter( 'wpinv_settings_sections_gateways', 'wpinv_settings_sections_gateways', 10, 1 ); |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Adds GateWay settings. |
165
|
|
|
*/ |
166
|
|
|
function wpinv_settings_gateways( $settings ) { |
167
|
|
|
|
168
|
|
|
// Loop through each gateway. |
169
|
|
|
foreach ( wpinv_get_payment_gateways() as $key => $gateway ) { |
170
|
|
|
|
171
|
|
|
$gateway_settings = array( |
172
|
|
|
|
173
|
|
|
// Header. |
174
|
|
|
"{$key}_header" => array( |
175
|
|
|
|
176
|
|
|
'id' => "{$key}_gateway_header", |
177
|
|
|
'name' => '<h3>' . wp_sprintf( __( '%s Settings', 'invoicing' ), $gateway['admin_label'] ) . '</h3>', |
178
|
|
|
'custom' => $key, |
179
|
|
|
'type' => 'gateway_header', |
180
|
|
|
|
181
|
|
|
), |
182
|
|
|
|
183
|
|
|
// Activate/Deactivate a gateway. |
184
|
|
|
"{$key}_active" => array( |
185
|
|
|
'id' => $key . '_active', |
186
|
|
|
'name' => __( 'Activate', 'invoicing' ), |
187
|
|
|
'desc' => wp_sprintf( __( 'Enable %s', 'invoicing' ), $gateway['admin_label'] ), |
188
|
|
|
'type' => 'checkbox', |
189
|
|
|
'std' => $key === 'manual' ? '1' : '0', |
190
|
|
|
), |
191
|
|
|
|
192
|
|
|
// Activate/Deactivate sandbox. |
193
|
|
|
"{$key}_sandbox" => array( |
194
|
|
|
'id' => $key . '_sandbox', |
195
|
|
|
'name' => __( 'Sandbox', 'invoicing' ), |
196
|
|
|
'desc' => __( 'Enable sandbox to test payments', 'invoicing' ), |
197
|
|
|
'type' => 'checkbox', |
198
|
|
|
'std' => '1', |
199
|
|
|
), |
200
|
|
|
|
201
|
|
|
// Checkout title. |
202
|
|
|
"{$key}_title" => array( |
203
|
|
|
'id' => $key . '_title', |
204
|
|
|
'name' => __( 'Checkout Title', 'invoicing' ), |
205
|
|
|
'std' => isset( $gateway['checkout_label'] ) ? $gateway['checkout_label'] : '', |
206
|
|
|
'type' => 'text', |
207
|
|
|
), |
208
|
|
|
|
209
|
|
|
// Checkout description. |
210
|
|
|
"{$key}_desc" => array( |
211
|
|
|
'id' => $key . '_desc', |
212
|
|
|
'name' => __( 'Checkout Description', 'invoicing' ), |
213
|
|
|
'std' => apply_filters( "getpaid_default_{$key}_checkout_description", '' ), |
214
|
|
|
'type' => 'text', |
215
|
|
|
), |
216
|
|
|
|
217
|
|
|
// Checkout order. |
218
|
|
|
"{$key}_ordering" => array( |
219
|
|
|
'id' => $key . '_ordering', |
220
|
|
|
'name' => __( 'Priority', 'invoicing' ), |
221
|
|
|
'std' => apply_filters( "getpaid_default_{$key}_checkout_description", '' ), |
222
|
|
|
'type' => 'number', |
223
|
|
|
'step' => '1', |
224
|
|
|
'min' => '0', |
225
|
|
|
'max' => '100000', |
226
|
|
|
'std' => isset( $gateway['ordering'] ) ? $gateway['ordering'] : '10', |
227
|
|
|
), |
228
|
|
|
|
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
// Maybe remove the sandbox. |
232
|
|
|
if ( ! getpaid_payment_gateway_supports( $key, 'sandbox' ) ) { |
233
|
|
|
unset( $gateway_settings["{$key}_sandbox"] ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$gateway_settings = apply_filters( 'wpinv_gateway_settings', $gateway_settings, $key, $gateway ); |
237
|
|
|
$gateway_settings = apply_filters( 'wpinv_gateway_settings_' . $key, $gateway_settings, $gateway ); |
238
|
|
|
|
239
|
|
|
$settings[$key] = $gateway_settings; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $settings; |
243
|
|
|
|
244
|
|
|
} |
245
|
|
|
add_filter( 'wpinv_settings_gateways', 'wpinv_settings_gateways', 10, 1 ); |
246
|
|
|
|
247
|
|
|
function wpinv_gateway_header_callback( $args ) { |
248
|
|
|
echo '<input type="hidden" id="wpinv_settings[save_gateway]" name="wpinv_settings[save_gateway]" value="' . esc_attr( $args['custom'] ) . '" />'; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Checks if a given gateway supports a given feature. |
253
|
|
|
* |
254
|
|
|
* @param string $gateway |
255
|
|
|
* @param string $feature |
256
|
|
|
* @return bool |
257
|
|
|
* @since 2.3.0 |
258
|
|
|
*/ |
259
|
|
|
function getpaid_payment_gateway_supports( $gateway, $feature ) { |
260
|
|
|
|
261
|
|
|
$supports = false; |
262
|
|
|
|
263
|
|
|
if ( wpinv_is_gateway_active( $gateway ) ) { |
264
|
|
|
|
265
|
|
|
$supports = apply_filters( "getpaid_{$gateway}_supports_{$feature}", false ); |
266
|
|
|
|
267
|
|
|
// Backwards compatibility. |
268
|
|
|
$supports = apply_filters( "wpinv_{$gateway}_supports_{$feature}", $supports ); |
269
|
|
|
$supports = apply_filters( "wpinv_{$gateway}_support_{$feature}", $supports ); |
270
|
|
|
|
271
|
|
|
$supports = apply_filters( "getpaid_gateway_supports_{$feature}", $supports, $gateway ); |
272
|
|
|
$supports = apply_filters( 'getpaid_payment_gateway_supports', $supports, $feature, $gateway ); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $supports; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
function wpinv_get_chosen_gateway( $invoice_id = 0 ) { |
279
|
|
|
$gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
280
|
|
|
|
281
|
|
|
$chosen = false; |
282
|
|
|
if ( $invoice_id > 0 && $invoice = wpinv_get_invoice( $invoice_id ) ) { |
283
|
|
|
$chosen = $invoice->get_gateway(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$chosen = isset( $_REQUEST['payment-mode'] ) ? sanitize_text_field( $_REQUEST['payment-mode'] ) : $chosen; |
287
|
|
|
|
288
|
|
|
if ( false !== $chosen ) { |
289
|
|
|
$chosen = preg_replace('/[^a-zA-Z0-9-_]+/', '', $chosen ); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
if ( ! empty ( $chosen ) ) { |
293
|
|
|
$enabled_gateway = urldecode( $chosen ); |
294
|
|
|
} else if ( !empty( $invoice ) && (float)$invoice->get_subtotal() <= 0 ) { |
295
|
|
|
$enabled_gateway = 'manual'; |
296
|
|
|
} else { |
297
|
|
|
$enabled_gateway = wpinv_get_default_gateway(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if ( !wpinv_is_gateway_active( $enabled_gateway ) && !empty( $gateways ) ) { |
|
|
|
|
301
|
|
|
if(wpinv_is_gateway_active( wpinv_get_default_gateway()) ){ |
302
|
|
|
$enabled_gateway = wpinv_get_default_gateway(); |
303
|
|
|
}else{ |
304
|
|
|
$enabled_gateway = $gateways[0]; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return apply_filters( 'wpinv_chosen_gateway', $enabled_gateway ); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
function wpinv_record_gateway_error( $title = '', $message = '' ) { |
313
|
|
|
return wpinv_error_log( $message, $title ); |
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
function wpinv_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) { |
317
|
|
|
$ret = 0; |
318
|
|
|
$args = array( |
319
|
|
|
'meta_key' => '_wpinv_gateway', |
320
|
|
|
'meta_value' => $gateway_id, |
321
|
|
|
'nopaging' => true, |
322
|
|
|
'post_type' => 'wpi_invoice', |
323
|
|
|
'post_status' => $status, |
324
|
|
|
'fields' => 'ids' |
325
|
|
|
); |
326
|
|
|
|
327
|
|
|
$payments = new WP_Query( $args ); |
328
|
|
|
|
329
|
|
|
if( $payments ) |
|
|
|
|
330
|
|
|
$ret = $payments->post_count; |
331
|
|
|
return $ret; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Displays the ipn url field. |
336
|
|
|
*/ |
337
|
|
|
function wpinv_ipn_url_callback( $args ) { |
338
|
|
|
$sanitize_id = wpinv_sanitize_key( $args['id'] ); |
339
|
|
|
|
340
|
|
|
$attrs = $args['readonly'] ? ' readonly' : ''; |
341
|
|
|
|
342
|
|
|
$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()">'; |
343
|
|
|
$html .= '<label for="wpinv_settings[' . $sanitize_id . ']">' . $args['desc'] . '</label>'; |
344
|
|
|
|
345
|
|
|
echo $html; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Checks if a gateway is in test mode. |
350
|
|
|
* |
351
|
|
|
* @param string $gateway The gateway to check for. |
352
|
|
|
* |
353
|
|
|
* @return bool |
354
|
|
|
*/ |
355
|
|
|
function wpinv_is_test_mode( $gateway = '' ) { |
356
|
|
|
$sandbox = empty( $gateway ) ? false : wpinv_get_option( "{$gateway}_sandbox", true ); |
357
|
|
|
$supports = getpaid_payment_gateway_supports( $gateway, 'sandbox' ); |
358
|
|
|
return apply_filters( 'wpinv_is_test_mode', $sandbox && $supports, $gateway ); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Retrieves the ipn url. |
363
|
|
|
* |
364
|
|
|
* @param string $gateway The gateway whose IPN url we should retrieve. |
365
|
|
|
* @param array $args extra args to add to the url. |
366
|
|
|
* |
367
|
|
|
* @return string |
368
|
|
|
*/ |
369
|
|
|
function wpinv_get_ipn_url( $gateway = false, $args = array() ) { |
370
|
|
|
$args = wp_parse_args( |
371
|
|
|
array( |
372
|
|
|
'wpi-listener' => 'IPN', |
373
|
|
|
'wpi-gateway' => $gateway |
374
|
|
|
), |
375
|
|
|
$args |
376
|
|
|
); |
377
|
|
|
|
378
|
|
|
return apply_filters( 'wpinv_ipn_url', add_query_arg( $args, home_url( 'index.php' ) ), $gateway, $args ); |
379
|
|
|
|
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Retrieves the non-query string ipn url. |
384
|
|
|
* |
385
|
|
|
* @param string $gateway The gateway whose IPN url we should retrieve. |
386
|
|
|
* |
387
|
|
|
* @return string |
388
|
|
|
*/ |
389
|
|
|
function getpaid_get_non_query_string_ipn_url( $gateway ) { |
390
|
|
|
$gateway = wpinv_sanitize_key( $gateway ); |
391
|
|
|
return home_url( "getpaid-ipn/$gateway" ); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Retrieves request data with slashes removed slashes. |
397
|
|
|
*/ |
398
|
|
|
function wpinv_get_post_data( $method = 'request' ) { |
399
|
|
|
|
400
|
|
|
if ( $method == 'post' ) { |
401
|
|
|
return wp_unslash( $_POST ); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
if ( $method == 'get' ) { |
405
|
|
|
return wp_unslash( $_GET ); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
return wp_unslash( $_REQUEST ); |
409
|
|
|
|
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Checks if a given gateway supports subscription payments. |
414
|
|
|
*/ |
415
|
|
|
function wpinv_gateway_support_subscription( $gateway ) { |
416
|
|
|
return getpaid_payment_gateway_supports( $gateway, 'subscription' ); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Filters payment form gateways. |
421
|
|
|
* |
422
|
|
|
* @param array $gateways an array of gateways. |
423
|
|
|
* @param GetPaid_Payment_Form $form payment form. |
424
|
|
|
*/ |
425
|
|
|
function wpinv_payment_gateways_on_cart( $gateways, $form ) { |
426
|
|
|
|
427
|
|
|
if ( $form->is_recurring() ) { |
428
|
|
|
|
429
|
|
|
foreach ( array_keys( $gateways ) as $gateway ) { |
430
|
|
|
|
431
|
|
|
if ( ! wpinv_gateway_support_subscription( $gateway ) ) { |
432
|
|
|
unset( $gateways[$gateway] ); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
return $gateways; |
440
|
|
|
} |
441
|
|
|
add_filter( 'getpaid_payment_form_gateways', 'wpinv_payment_gateways_on_cart', 10, 2 ); |
442
|
|
|
|