|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Gateway Functions |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Gateways |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
|
9
|
|
|
* @since 1.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Returns a list of all available gateways. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 1.0 |
|
21
|
|
|
* @return array $gateways All the available gateways |
|
22
|
|
|
*/ |
|
23
|
|
|
function give_get_payment_gateways() { |
|
24
|
|
|
// Default, built-in gateways |
|
25
|
|
|
$gateways = array( |
|
26
|
|
|
'paypal' => array( |
|
27
|
44 |
|
'admin_label' => esc_html__( 'PayPal Standard', 'give' ), |
|
28
|
44 |
|
'checkout_label' => esc_html__( 'PayPal', 'give' ), |
|
29
|
44 |
|
'supports' => array( 'buy_now' ) |
|
30
|
44 |
|
), |
|
31
|
|
|
'manual' => array( |
|
32
|
44 |
|
'admin_label' => esc_html__( 'Test Payment', 'give' ), |
|
33
|
44 |
|
'checkout_label' => esc_html__( 'Test Payment', 'give' ) |
|
34
|
44 |
|
), |
|
35
|
44 |
|
); |
|
36
|
|
|
|
|
37
|
44 |
|
return apply_filters( 'give_payment_gateways', $gateways ); |
|
38
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns a list of all enabled gateways. |
|
43
|
|
|
* |
|
44
|
|
|
* @since 1.0 |
|
45
|
|
|
* @return array $gateway_list All the available gateways |
|
46
|
|
|
*/ |
|
47
|
|
|
function give_get_enabled_payment_gateways() { |
|
48
|
|
|
|
|
49
|
44 |
|
$gateways = give_get_payment_gateways(); |
|
50
|
|
|
|
|
51
|
44 |
|
$enabled = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' ); |
|
52
|
|
|
|
|
53
|
44 |
|
$gateway_list = array(); |
|
54
|
|
|
|
|
55
|
44 |
|
foreach ( $gateways as $key => $gateway ) { |
|
56
|
44 |
|
if ( isset( $enabled[ $key ] ) && $enabled[ $key ] == 1 ) { |
|
57
|
44 |
|
$gateway_list[ $key ] = $gateway; |
|
58
|
44 |
|
} |
|
59
|
44 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Set order of payment gateway in list. |
|
62
|
44 |
|
$gateway_list = give_get_ordered_payment_gateways( $gateway_list ); |
|
63
|
|
|
|
|
64
|
44 |
|
return apply_filters( 'give_enabled_payment_gateways', $gateway_list ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Checks whether a specified gateway is activated. |
|
69
|
|
|
* |
|
70
|
|
|
* @since 1.0 |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $gateway Name of the gateway to check for |
|
73
|
|
|
* |
|
74
|
|
|
* @return boolean true if enabled, false otherwise |
|
75
|
|
|
*/ |
|
76
|
|
|
function give_is_gateway_active( $gateway ) { |
|
77
|
2 |
|
$gateways = give_get_enabled_payment_gateways(); |
|
78
|
|
|
|
|
79
|
2 |
|
$ret = array_key_exists( $gateway, $gateways ); |
|
80
|
|
|
|
|
81
|
2 |
|
return apply_filters( 'give_is_gateway_active', $ret, $gateway, $gateways ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Gets the default payment gateway selected from the Give Settings |
|
86
|
|
|
* |
|
87
|
|
|
* @since 1.0 |
|
88
|
|
|
* @global $give_options Array of all the Give Options |
|
89
|
|
|
* |
|
90
|
|
|
* @param $form_id int ID of the Give Form |
|
91
|
|
|
* |
|
92
|
|
|
* @return string Gateway ID |
|
93
|
|
|
*/ |
|
94
|
|
|
function give_get_default_gateway( $form_id ) { |
|
95
|
|
|
|
|
96
|
2 |
|
global $give_options; |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
2 |
|
$default = isset( $give_options['default_gateway'] ) && give_is_gateway_active( $give_options['default_gateway'] ) ? $give_options['default_gateway'] : 'paypal'; |
|
99
|
2 |
|
$form_default = get_post_meta( $form_id, '_give_default_gateway', true ); |
|
100
|
|
|
|
|
101
|
|
|
//Single Form settings varies compared to the Global default settings |
|
102
|
2 |
|
if ( ! empty( $form_default ) && |
|
103
|
2 |
|
$form_id !== null && |
|
104
|
2 |
|
$default !== $form_default && |
|
105
|
2 |
|
$form_default !== 'global' && |
|
106
|
|
|
give_is_gateway_active( $form_default ) |
|
107
|
2 |
|
) { |
|
108
|
|
|
$default = $form_default; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
return apply_filters( 'give_default_gateway', $default ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns the admin label for the specified gateway |
|
116
|
|
|
* |
|
117
|
|
|
* @since 1.0 |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $gateway Name of the gateway to retrieve a label for |
|
120
|
|
|
* |
|
121
|
|
|
* @return string Gateway admin label |
|
122
|
|
|
*/ |
|
123
|
|
|
function give_get_gateway_admin_label( $gateway ) { |
|
124
|
42 |
|
$gateways = give_get_enabled_payment_gateways(); |
|
125
|
42 |
|
$label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway; |
|
126
|
42 |
|
$payment = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : false; |
|
127
|
|
|
|
|
128
|
42 |
|
if ( $gateway == 'manual' && $payment ) { |
|
129
|
|
|
if ( give_get_payment_amount( $payment ) == 0 ) { |
|
130
|
|
|
$label = esc_html__( 'Test Donation', 'give' ); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
42 |
|
return apply_filters( 'give_gateway_admin_label', $label, $gateway ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the checkout label for the specified gateway |
|
139
|
|
|
* |
|
140
|
|
|
* @since 1.0 |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $gateway Name of the gateway to retrieve a label for |
|
143
|
|
|
* |
|
144
|
|
|
* @return string Checkout label for the gateway |
|
145
|
|
|
*/ |
|
146
|
|
|
function give_get_gateway_checkout_label( $gateway ) { |
|
147
|
42 |
|
$gateways = give_get_enabled_payment_gateways(); |
|
148
|
42 |
|
$label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway; |
|
149
|
|
|
|
|
150
|
42 |
|
if ( $gateway == 'manual' ) { |
|
151
|
32 |
|
$label = esc_html__( 'Test Donation', 'give' ); |
|
152
|
32 |
|
} |
|
153
|
|
|
|
|
154
|
42 |
|
return apply_filters( 'give_gateway_checkout_label', $label, $gateway ); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns the options a gateway supports |
|
159
|
|
|
* |
|
160
|
|
|
* @since 1.8 |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $gateway ID of the gateway to retrieve a label for |
|
163
|
|
|
* |
|
164
|
|
|
* @return array Options the gateway supports |
|
165
|
|
|
*/ |
|
166
|
|
|
function give_get_gateway_supports( $gateway ) { |
|
167
|
|
|
$gateways = give_get_enabled_payment_gateways(); |
|
168
|
|
|
$supports = isset( $gateways[ $gateway ]['supports'] ) ? $gateways[ $gateway ]['supports'] : array(); |
|
169
|
|
|
|
|
170
|
|
|
return apply_filters( 'give_gateway_supports', $supports, $gateway ); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Checks if a gateway supports buy now |
|
175
|
|
|
* |
|
176
|
|
|
* @since 1.8 |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $gateway ID of the gateway to retrieve a label for |
|
179
|
|
|
* |
|
180
|
|
|
* @return bool |
|
181
|
|
|
*/ |
|
182
|
|
|
function give_gateway_supports_buy_now( $gateway ) { |
|
183
|
|
|
$supports = give_get_gateway_supports( $gateway ); |
|
184
|
|
|
$ret = in_array( 'buy_now', $supports ); |
|
185
|
|
|
|
|
186
|
|
|
return apply_filters( 'give_gateway_supports_buy_now', $ret, $gateway ); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Checks if an enabled gateway supports buy now |
|
191
|
|
|
* |
|
192
|
|
|
* @since 1.0 |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
|
|
function give_give_supports_buy_now() { |
|
196
|
|
|
$gateways = give_get_enabled_payment_gateways(); |
|
197
|
|
|
$ret = false; |
|
198
|
|
|
|
|
199
|
|
|
if ( $gateways ) { |
|
|
|
|
|
|
200
|
|
|
foreach ( $gateways as $gateway_id => $gateway ) { |
|
201
|
|
|
if ( give_gateway_supports_buy_now( $gateway_id ) ) { |
|
202
|
|
|
$ret = true; |
|
203
|
|
|
break; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return apply_filters( 'give_give_supports_buy_now', $ret ); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Sends all the payment data to the specified gateway |
|
213
|
|
|
* |
|
214
|
|
|
* @since 1.0 |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $gateway Name of the gateway |
|
217
|
|
|
* @param array $payment_data All the payment data to be sent to the gateway |
|
218
|
|
|
* |
|
219
|
|
|
* @return void |
|
220
|
|
|
*/ |
|
221
|
|
|
function give_send_to_gateway( $gateway, $payment_data ) { |
|
222
|
|
|
|
|
223
|
|
|
$payment_data['gateway_nonce'] = wp_create_nonce( 'give-gateway' ); |
|
224
|
|
|
|
|
225
|
|
|
// $gateway must match the ID used when registering the gateway |
|
226
|
|
|
do_action( 'give_gateway_' . $gateway, $payment_data ); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Determines what the currently selected gateway is. |
|
232
|
|
|
* |
|
233
|
|
|
* If the amount is zero, no option is shown and the donation form uses the manual |
|
234
|
|
|
* gateway to emulate a no-gateway-setup for a free donation. |
|
235
|
|
|
* |
|
236
|
|
|
* @access public |
|
237
|
|
|
* @since 1.0 |
|
238
|
|
|
* |
|
239
|
|
|
* @param int $form_id The ID of the Form |
|
240
|
|
|
* |
|
241
|
|
|
* @return string $enabled_gateway The slug of the gateway |
|
242
|
|
|
*/ |
|
243
|
|
|
function give_get_chosen_gateway( $form_id ) { |
|
244
|
1 |
|
$gateways = give_get_enabled_payment_gateways(); |
|
245
|
1 |
|
$request_form_id = isset( $_REQUEST['give_form_id'] ) ? $_REQUEST['give_form_id'] : 0; |
|
246
|
1 |
|
if ( empty( $request_form_id ) ) { |
|
247
|
1 |
|
$request_form_id = isset( $_REQUEST['form-id'] ) ? $_REQUEST['form-id'] : 0; |
|
248
|
1 |
|
} |
|
249
|
1 |
|
$chosen = give_get_default_gateway( $form_id ); |
|
250
|
1 |
|
$enabled_gateway = ''; |
|
251
|
|
|
|
|
252
|
|
|
//Take into account request Form ID args |
|
253
|
1 |
|
if ( ! empty( $request_form_id ) && $form_id == $request_form_id ) { |
|
254
|
|
|
$chosen = isset( $_REQUEST['payment-mode'] ) ? $_REQUEST['payment-mode'] : ''; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
1 |
|
if ( $chosen ) { |
|
258
|
1 |
|
$enabled_gateway = urldecode( $chosen ); |
|
259
|
1 |
|
} else if ( count( $gateways ) >= 1 && ! $chosen ) { |
|
260
|
|
|
foreach ( $gateways as $gateway_id => $gateway ): |
|
261
|
|
|
$enabled_gateway = $gateway_id; |
|
262
|
|
|
endforeach; |
|
263
|
|
|
} else { |
|
264
|
|
|
$enabled_gateway = give_get_default_gateway( $form_id ); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
|
|
268
|
1 |
|
return apply_filters( 'give_chosen_gateway', $enabled_gateway ); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Record a gateway error. |
|
273
|
|
|
* |
|
274
|
|
|
* A simple wrapper function for give_record_log(). |
|
275
|
|
|
* |
|
276
|
|
|
* @access public |
|
277
|
|
|
* @since 1.0 |
|
278
|
|
|
* |
|
279
|
|
|
* @param string $title Title of the log entry (default: empty) |
|
280
|
|
|
* @param string $message Message to store in the log entry (default: empty) |
|
281
|
|
|
* @param int $parent Parent log entry (default: 0) |
|
282
|
|
|
* |
|
283
|
|
|
* @return int ID of the new log entry |
|
284
|
|
|
*/ |
|
285
|
|
|
function give_record_gateway_error( $title = '', $message = '', $parent = 0 ) { |
|
286
|
|
|
return give_record_log( $title, $message, $parent, 'gateway_error' ); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Counts the number of purchases made with a gateway |
|
291
|
|
|
* |
|
292
|
|
|
* @since 1.0 |
|
293
|
|
|
* |
|
294
|
|
|
* @param string $gateway_id |
|
295
|
|
|
* @param string $status |
|
296
|
|
|
* |
|
297
|
|
|
* @return int |
|
298
|
|
|
*/ |
|
299
|
|
|
function give_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) { |
|
300
|
|
|
|
|
301
|
|
|
$ret = 0; |
|
302
|
|
|
$args = array( |
|
303
|
|
|
'meta_key' => '_give_payment_gateway', |
|
304
|
|
|
'meta_value' => $gateway_id, |
|
305
|
|
|
'nopaging' => true, |
|
306
|
|
|
'post_type' => 'give_payment', |
|
307
|
|
|
'post_status' => $status, |
|
308
|
|
|
'fields' => 'ids' |
|
309
|
|
|
); |
|
310
|
|
|
|
|
311
|
|
|
$payments = new WP_Query( $args ); |
|
312
|
|
|
|
|
313
|
|
|
if ( $payments ) { |
|
314
|
|
|
$ret = $payments->post_count; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
return $ret; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Returns a ordered list of all available gateways. |
|
323
|
|
|
* |
|
324
|
|
|
* @since 1.4.5 |
|
325
|
|
|
* |
|
326
|
|
|
* @param array $gateways List of payment gateways |
|
327
|
|
|
* |
|
328
|
|
|
* @return array $gateways All the available gateways |
|
329
|
|
|
*/ |
|
330
|
|
|
function give_get_ordered_payment_gateways( $gateways ) { |
|
331
|
|
|
|
|
332
|
|
|
// Get gateways setting. |
|
333
|
44 |
|
$gateways_setting = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' ); |
|
334
|
|
|
|
|
335
|
|
|
// Return from here if we do not have gateways setting. |
|
336
|
44 |
|
if ( empty( $gateways_setting ) ) { |
|
337
|
|
|
return $gateways; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
// Reverse array to order payment gateways. |
|
341
|
44 |
|
$gateways_setting = array_reverse( $gateways_setting ); |
|
342
|
|
|
|
|
343
|
|
|
// Reorder gateways array |
|
344
|
44 |
|
foreach ( $gateways_setting as $gateway_key => $value ) { |
|
345
|
|
|
|
|
346
|
44 |
|
$new_gateway_value = isset( $gateways[ $gateway_key ] ) ? $gateways[ $gateway_key ] : ''; |
|
347
|
44 |
|
unset( $gateways[ $gateway_key ] ); |
|
348
|
|
|
|
|
349
|
44 |
|
if ( ! empty( $new_gateway_value ) ) { |
|
350
|
44 |
|
$gateways = array_merge( array( $gateway_key => $new_gateway_value ), $gateways ); |
|
351
|
44 |
|
} |
|
352
|
44 |
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* Filter payment gateways order. |
|
356
|
|
|
* |
|
357
|
|
|
* @since 1.4.5 |
|
358
|
|
|
* |
|
359
|
|
|
* @param array $gateways All the available gateways |
|
360
|
|
|
*/ |
|
361
|
44 |
|
return apply_filters( 'give_payment_gateways_order', $gateways ); |
|
362
|
|
|
} |
|
363
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.