|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Gateway Functions |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Gateways |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license https://opensource.org/licenses/gpl-license 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
|
|
|
'admin_label' => __( 'PayPal Standard', 'give' ), |
|
28
|
|
|
'checkout_label' => __( 'PayPal', 'give' ), |
|
29
|
|
|
), |
|
30
|
|
|
'manual' => array( |
|
31
|
|
|
'admin_label' => __( 'Test Donation', 'give' ), |
|
32
|
|
|
'checkout_label' => __( 'Test Donation', 'give' ) |
|
33
|
|
|
), |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
return apply_filters( 'give_payment_gateways', $gateways ); |
|
37
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns a list of all enabled gateways. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 1.0 |
|
44
|
|
|
* |
|
45
|
|
|
* @param int $form_id Form ID |
|
46
|
|
|
* |
|
47
|
|
|
* @return array $gateway_list All the available gateways |
|
48
|
|
|
*/ |
|
49
|
|
|
function give_get_enabled_payment_gateways( $form_id = 0 ) { |
|
50
|
|
|
|
|
51
|
|
|
$gateways = give_get_payment_gateways(); |
|
52
|
|
|
|
|
53
|
|
|
$enabled = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' ); |
|
54
|
|
|
|
|
55
|
|
|
$gateway_list = array(); |
|
56
|
|
|
|
|
57
|
|
|
foreach ( $gateways as $key => $gateway ) { |
|
58
|
|
|
if ( isset( $enabled[ $key ] ) && $enabled[ $key ] == 1 ) { |
|
59
|
|
|
$gateway_list[ $key ] = $gateway; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Set order of payment gateway in list. |
|
64
|
|
|
$gateway_list = give_get_ordered_payment_gateways( $gateway_list ); |
|
65
|
|
|
|
|
66
|
|
|
return apply_filters( 'give_enabled_payment_gateways', $gateway_list, $form_id ); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Checks whether a specified gateway is activated. |
|
71
|
|
|
* |
|
72
|
|
|
* @since 1.0 |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $gateway Name of the gateway to check for |
|
75
|
|
|
* |
|
76
|
|
|
* @return boolean true if enabled, false otherwise |
|
77
|
|
|
*/ |
|
78
|
|
|
function give_is_gateway_active( $gateway ) { |
|
79
|
|
|
$gateways = give_get_enabled_payment_gateways(); |
|
80
|
|
|
|
|
81
|
|
|
$ret = array_key_exists( $gateway, $gateways ); |
|
82
|
|
|
|
|
83
|
|
|
return apply_filters( 'give_is_gateway_active', $ret, $gateway, $gateways ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets the default payment gateway selected from the Give Settings |
|
88
|
|
|
* |
|
89
|
|
|
* @since 1.0 |
|
90
|
|
|
* |
|
91
|
|
|
* @param $form_id int ID of the Give Form |
|
92
|
|
|
* |
|
93
|
|
|
* @return string Gateway ID |
|
94
|
|
|
*/ |
|
95
|
|
|
function give_get_default_gateway( $form_id ) { |
|
96
|
|
|
|
|
97
|
|
|
$give_options = give_get_settings(); |
|
98
|
|
|
$default = isset( $give_options['default_gateway'] ) && give_is_gateway_active( $give_options['default_gateway'] ) ? $give_options['default_gateway'] : 'paypal'; |
|
99
|
|
|
$form_default = give_get_meta( $form_id, '_give_default_gateway', true ); |
|
100
|
|
|
|
|
101
|
|
|
// Single Form settings varies compared to the Global default settings. |
|
102
|
|
|
if ( ! empty( $form_default ) && |
|
103
|
|
|
$form_id !== null && |
|
104
|
|
|
$default !== $form_default && |
|
105
|
|
|
$form_default !== 'global' && |
|
106
|
|
|
give_is_gateway_active( $form_default ) |
|
107
|
|
|
) { |
|
108
|
|
|
$default = $form_default; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
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
|
|
|
$gateways = give_get_payment_gateways(); |
|
125
|
|
|
$label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway; |
|
126
|
|
|
$payment = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : false; |
|
127
|
|
|
|
|
128
|
|
|
if ( $gateway == 'manual' && $payment ) { |
|
129
|
|
|
if ( give_get_payment_amount( $payment ) == 0 ) { |
|
130
|
|
|
$label = __( 'Test Donation', 'give' ); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
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
|
|
|
$gateways = give_get_payment_gateways(); |
|
148
|
|
|
$label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway; |
|
149
|
|
|
|
|
150
|
|
|
if ( $gateway == 'manual' ) { |
|
151
|
|
|
$label = __( 'Test Donation', 'give' ); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
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
|
|
|
* Sends all the payment data to the specified gateway |
|
175
|
|
|
* |
|
176
|
|
|
* @since 1.0 |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $gateway Name of the gateway |
|
179
|
|
|
* @param array $payment_data All the payment data to be sent to the gateway |
|
180
|
|
|
* |
|
181
|
|
|
* @return void |
|
182
|
|
|
*/ |
|
183
|
|
|
function give_send_to_gateway( $gateway, $payment_data ) { |
|
184
|
|
|
|
|
185
|
|
|
$payment_data['gateway_nonce'] = wp_create_nonce( 'give-gateway' ); |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Fires while loading payment gateway via AJAX. |
|
189
|
|
|
* |
|
190
|
|
|
* The dynamic portion of the hook name '$gateway' must match the ID used when registering the gateway. |
|
191
|
|
|
* |
|
192
|
|
|
* @since 1.0 |
|
193
|
|
|
* |
|
194
|
|
|
* @param array $payment_data All the payment data to be sent to the gateway. |
|
195
|
|
|
*/ |
|
196
|
|
|
do_action( "give_gateway_{$gateway}", $payment_data ); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Determines the currently selected donation payment gateway. |
|
202
|
|
|
* |
|
203
|
|
|
* @access public |
|
204
|
|
|
* @since 1.0 |
|
205
|
|
|
* |
|
206
|
|
|
* @param int $form_id The ID of the Form |
|
207
|
|
|
* |
|
208
|
|
|
* @return string $enabled_gateway The slug of the gateway |
|
209
|
|
|
*/ |
|
210
|
|
|
function give_get_chosen_gateway( $form_id ) { |
|
211
|
|
|
|
|
212
|
|
|
$request_form_id = isset( $_REQUEST['give_form_id'] ) ? $_REQUEST['give_form_id'] : 0; |
|
213
|
|
|
|
|
214
|
|
|
// Back to check if 'form-id' is present. |
|
215
|
|
|
if ( empty( $request_form_id ) ) { |
|
216
|
|
|
$request_form_id = isset( $_REQUEST['form-id'] ) ? $_REQUEST['form-id'] : 0; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$request_payment_mode = isset( $_REQUEST['payment-mode'] ) ? $_REQUEST['payment-mode'] : ''; |
|
220
|
|
|
$chosen = false; |
|
221
|
|
|
|
|
222
|
|
|
// If both 'payment-mode' and 'form-id' then set for only this form. |
|
223
|
|
|
if ( ! empty( $request_form_id ) && $form_id == $request_form_id ) { |
|
224
|
|
|
$chosen = $request_payment_mode; |
|
225
|
|
|
} elseif ( empty( $request_form_id ) && $request_payment_mode ) { |
|
226
|
|
|
// If no 'form-id' but there is 'payment-mode'. |
|
227
|
|
|
$chosen = $request_payment_mode; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// Get the enable gateway based of chosen var. |
|
231
|
|
|
if ( $chosen && give_is_gateway_active( $chosen ) ) { |
|
232
|
|
|
$enabled_gateway = urldecode( $chosen ); |
|
233
|
|
|
} else { |
|
234
|
|
|
$enabled_gateway = give_get_default_gateway( $form_id ); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return apply_filters( 'give_chosen_gateway', $enabled_gateway ); |
|
238
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Record a log entry |
|
243
|
|
|
* |
|
244
|
|
|
* A wrapper function for the Give_Logging class add() method. |
|
245
|
|
|
* |
|
246
|
|
|
* @since 1.0 |
|
247
|
|
|
* @since 2.0 Use global logs object |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $title Log title. Default is empty. |
|
250
|
|
|
* @param string $message Log message. Default is empty. |
|
251
|
|
|
* @param int $parent Parent log. Default is 0. |
|
252
|
|
|
* @param string $type Log type. Default is null. |
|
253
|
|
|
* |
|
254
|
|
|
* @return int ID of the new log entry. |
|
|
|
|
|
|
255
|
|
|
*/ |
|
256
|
|
|
function give_record_log( $title = '', $message = '', $parent = 0, $type = null ) { |
|
257
|
|
|
return Give()->logs->add( $title, $message, $parent, $type ); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Record a gateway error. |
|
262
|
|
|
* |
|
263
|
|
|
* A simple wrapper function for give_record_log(). |
|
264
|
|
|
* |
|
265
|
|
|
* @access public |
|
266
|
|
|
* @since 1.0 |
|
267
|
|
|
* |
|
268
|
|
|
* @param string $title Title of the log entry (default: empty) |
|
269
|
|
|
* @param string $message Message to store in the log entry (default: empty) |
|
270
|
|
|
* @param int $parent Parent log entry (default: 0) |
|
271
|
|
|
* |
|
272
|
|
|
* @return int ID of the new log entry |
|
|
|
|
|
|
273
|
|
|
*/ |
|
274
|
|
|
function give_record_gateway_error( $title = '', $message = '', $parent = 0 ) { |
|
275
|
|
|
return give_record_log( $title, $message, $parent, 'gateway_error' ); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Counts the number of donations made with a gateway. |
|
280
|
|
|
* |
|
281
|
|
|
* @since 1.0 |
|
282
|
|
|
* |
|
283
|
|
|
* @param string $gateway_id |
|
284
|
|
|
* @param string $status |
|
285
|
|
|
* |
|
286
|
|
|
* @return int |
|
287
|
|
|
*/ |
|
288
|
|
|
function give_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) { |
|
289
|
|
|
|
|
290
|
|
|
$ret = 0; |
|
291
|
|
|
$args = array( |
|
292
|
|
|
'meta_key' => '_give_payment_gateway', |
|
293
|
|
|
'meta_value' => $gateway_id, |
|
294
|
|
|
'nopaging' => true, |
|
295
|
|
|
'post_type' => 'give_payment', |
|
296
|
|
|
'post_status' => $status, |
|
297
|
|
|
'fields' => 'ids', |
|
298
|
|
|
); |
|
299
|
|
|
|
|
300
|
|
|
$payments = new WP_Query( $args ); |
|
301
|
|
|
|
|
302
|
|
|
if ( $payments ) { |
|
303
|
|
|
$ret = $payments->post_count; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
return $ret; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Returns a ordered list of all available gateways. |
|
312
|
|
|
* |
|
313
|
|
|
* @since 1.4.5 |
|
314
|
|
|
* |
|
315
|
|
|
* @param array $gateways List of payment gateways |
|
316
|
|
|
* |
|
317
|
|
|
* @return array $gateways All the available gateways |
|
318
|
|
|
*/ |
|
319
|
|
|
function give_get_ordered_payment_gateways( $gateways ) { |
|
320
|
|
|
|
|
321
|
|
|
// Get gateways setting. |
|
322
|
|
|
$gateways_setting = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' ); |
|
323
|
|
|
|
|
324
|
|
|
// Return from here if we do not have gateways setting. |
|
325
|
|
|
if ( empty( $gateways_setting ) ) { |
|
326
|
|
|
return $gateways; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
// Reverse array to order payment gateways. |
|
330
|
|
|
$gateways_setting = array_reverse( $gateways_setting ); |
|
331
|
|
|
|
|
332
|
|
|
// Reorder gateways array |
|
333
|
|
|
foreach ( $gateways_setting as $gateway_key => $value ) { |
|
334
|
|
|
|
|
335
|
|
|
$new_gateway_value = isset( $gateways[ $gateway_key ] ) ? $gateways[ $gateway_key ] : ''; |
|
336
|
|
|
unset( $gateways[ $gateway_key ] ); |
|
337
|
|
|
|
|
338
|
|
|
if ( ! empty( $new_gateway_value ) ) { |
|
339
|
|
|
$gateways = array_merge( array( $gateway_key => $new_gateway_value ), $gateways ); |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Filter payment gateways order. |
|
345
|
|
|
* |
|
346
|
|
|
* @since 1.7 |
|
347
|
|
|
* |
|
348
|
|
|
* @param array $gateways All the available gateways |
|
349
|
|
|
*/ |
|
350
|
|
|
return apply_filters( 'give_payment_gateways_order', $gateways ); |
|
351
|
|
|
} |
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.