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
|
44 |
|
'admin_label' => __( 'PayPal Standard', 'give' ), |
28
|
44 |
|
'checkout_label' => __( 'PayPal', 'give' ), |
29
|
44 |
|
), |
30
|
44 |
|
'manual' => array( |
31
|
|
|
'admin_label' => __( 'Test Donation', 'give' ), |
32
|
44 |
|
'checkout_label' => __( 'Test Donation', 'give' ) |
33
|
44 |
|
), |
34
|
44 |
|
); |
35
|
44 |
|
|
36
|
|
|
return apply_filters( 'give_payment_gateways', $gateways ); |
37
|
44 |
|
|
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
|
44 |
|
function give_get_enabled_payment_gateways( $form_id = 0 ) { |
50
|
|
|
|
51
|
44 |
|
$gateways = give_get_payment_gateways(); |
52
|
|
|
|
53
|
44 |
|
$enabled = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' ); |
|
|
|
|
54
|
|
|
|
55
|
44 |
|
$gateway_list = array(); |
56
|
44 |
|
|
57
|
44 |
|
foreach ( $gateways as $key => $gateway ) { |
58
|
44 |
|
if ( isset( $enabled[ $key ] ) && $enabled[ $key ] == 1 ) { |
|
|
|
|
59
|
44 |
|
$gateway_list[ $key ] = $gateway; |
60
|
|
|
} |
61
|
|
|
} |
62
|
44 |
|
|
63
|
|
|
// Set order of payment gateway in list. |
64
|
44 |
|
$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
|
2 |
|
*/ |
78
|
|
|
function give_is_gateway_active( $gateway ) { |
79
|
2 |
|
$gateways = give_get_enabled_payment_gateways(); |
80
|
|
|
|
81
|
2 |
|
$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
|
2 |
|
|
97
|
|
|
$enabled_gateways = array_keys( give_get_enabled_payment_gateways() ); |
98
|
2 |
|
$default_gateway = give_get_option('default_gateway'); |
|
|
|
|
99
|
2 |
|
$default = ! empty( $default_gateway ) && give_is_gateway_active( $default_gateway ) ? $default_gateway : $enabled_gateways[0]; |
100
|
|
|
$form_default = give_get_meta( $form_id, '_give_default_gateway', true ); |
101
|
|
|
|
102
|
2 |
|
// Single Form settings varies compared to the Global default settings. |
103
|
2 |
|
if ( |
104
|
2 |
|
! empty( $form_default ) && |
105
|
2 |
|
$form_id !== null && |
106
|
|
|
$default !== $form_default && |
107
|
2 |
|
'global' !== $form_default && |
108
|
|
|
give_is_gateway_active( $form_default ) |
109
|
|
|
) { |
110
|
|
|
$default = $form_default; |
111
|
2 |
|
} |
112
|
|
|
|
113
|
|
|
return apply_filters( 'give_default_gateway', $default ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the admin label for the specified gateway |
118
|
|
|
* |
119
|
|
|
* @since 1.0 |
120
|
|
|
* |
121
|
|
|
* @param string $gateway Name of the gateway to retrieve a label for |
122
|
|
|
* |
123
|
|
|
* @return string Gateway admin label |
124
|
42 |
|
*/ |
125
|
42 |
View Code Duplication |
function give_get_gateway_admin_label( $gateway ) { |
|
|
|
|
126
|
42 |
|
$gateways = give_get_payment_gateways(); |
127
|
|
|
$label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway; |
128
|
42 |
|
|
129
|
|
|
if ( $gateway == 'manual' ) { |
|
|
|
|
130
|
|
|
$label = __( 'Test Donation', 'give' ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return apply_filters( 'give_gateway_admin_label', $label, $gateway ); |
134
|
42 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns the checkout label for the specified gateway |
138
|
|
|
* |
139
|
|
|
* @since 1.0 |
140
|
|
|
* |
141
|
|
|
* @param string $gateway Name of the gateway to retrieve a label for |
142
|
|
|
* |
143
|
|
|
* @return string Checkout label for the gateway |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
function give_get_gateway_checkout_label( $gateway ) { |
|
|
|
|
146
|
|
|
$gateways = give_get_payment_gateways(); |
147
|
42 |
|
$label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway; |
148
|
42 |
|
|
149
|
|
|
if ( $gateway == 'manual' ) { |
|
|
|
|
150
|
42 |
|
$label = __( 'Test Donation', 'give' ); |
151
|
32 |
|
} |
152
|
32 |
|
|
153
|
|
|
return apply_filters( 'give_gateway_checkout_label', $label, $gateway ); |
154
|
42 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns the options a gateway supports |
158
|
|
|
* |
159
|
|
|
* @since 1.8 |
160
|
|
|
* |
161
|
|
|
* @param string $gateway ID of the gateway to retrieve a label for |
162
|
|
|
* |
163
|
|
|
* @return array Options the gateway supports |
164
|
|
|
*/ |
165
|
|
|
function give_get_gateway_supports( $gateway ) { |
166
|
|
|
$gateways = give_get_enabled_payment_gateways(); |
167
|
|
|
$supports = isset( $gateways[ $gateway ]['supports'] ) ? $gateways[ $gateway ]['supports'] : array(); |
168
|
|
|
|
169
|
|
|
return apply_filters( 'give_gateway_supports', $supports, $gateway ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Sends all the payment data to the specified gateway |
174
|
|
|
* |
175
|
|
|
* @since 1.0 |
176
|
|
|
* |
177
|
|
|
* @param string $gateway Name of the gateway |
178
|
|
|
* @param array $payment_data All the payment data to be sent to the gateway |
179
|
|
|
* |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
function give_send_to_gateway( $gateway, $payment_data ) { |
183
|
|
|
|
184
|
|
|
$payment_data['gateway_nonce'] = wp_create_nonce( 'give-gateway' ); |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Fires while loading payment gateway via AJAX. |
188
|
|
|
* |
189
|
|
|
* The dynamic portion of the hook name '$gateway' must match the ID used when registering the gateway. |
190
|
|
|
* |
191
|
|
|
* @since 1.0 |
192
|
|
|
* |
193
|
|
|
* @param array $payment_data All the payment data to be sent to the gateway. |
194
|
|
|
*/ |
195
|
|
|
do_action( "give_gateway_{$gateway}", $payment_data ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Determines the currently selected donation payment gateway. |
201
|
|
|
* |
202
|
|
|
* @access public |
203
|
|
|
* @since 1.0 |
204
|
|
|
* |
205
|
|
|
* @param int $form_id The ID of the Form |
206
|
|
|
* |
207
|
|
|
* @return string $enabled_gateway The slug of the gateway |
208
|
|
|
*/ |
209
|
|
|
function give_get_chosen_gateway( $form_id ) { |
210
|
|
|
|
211
|
|
|
$request_form_id = isset( $_REQUEST['give_form_id'] ) ? $_REQUEST['give_form_id'] : 0; |
|
|
|
|
212
|
|
|
|
213
|
|
|
// Back to check if 'form-id' is present. |
214
|
|
|
if ( empty( $request_form_id ) ) { |
215
|
|
|
$request_form_id = isset( $_REQUEST['form-id'] ) ? $_REQUEST['form-id'] : 0; |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$request_payment_mode = isset( $_REQUEST['payment-mode'] ) ? $_REQUEST['payment-mode'] : ''; |
|
|
|
|
219
|
|
|
$chosen = false; |
220
|
|
|
|
221
|
|
|
// If both 'payment-mode' and 'form-id' then set for only this form. |
222
|
|
|
if ( ! empty( $request_form_id ) && $form_id == $request_form_id ) { |
223
|
|
|
$chosen = $request_payment_mode; |
224
|
|
|
} elseif ( empty( $request_form_id ) && $request_payment_mode ) { |
225
|
|
|
// If no 'form-id' but there is 'payment-mode'. |
226
|
|
|
$chosen = $request_payment_mode; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// Get the enable gateway based of chosen var. |
230
|
|
|
if ( $chosen && give_is_gateway_active( $chosen ) ) { |
231
|
|
|
$enabled_gateway = urldecode( $chosen ); |
232
|
|
|
} else { |
233
|
|
|
$enabled_gateway = give_get_default_gateway( $form_id ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return apply_filters( 'give_chosen_gateway', $enabled_gateway ); |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Record a log entry |
242
|
|
|
* |
243
|
|
|
* A wrapper function for the Give_Logging class add() method. |
244
|
1 |
|
* |
245
|
1 |
|
* @since 1.0 |
246
|
1 |
|
* @since 2.0 Use global logs object |
247
|
1 |
|
* |
248
|
1 |
|
* @param string $title Log title. Default is empty. |
249
|
1 |
|
* @param string $message Log message. Default is empty. |
250
|
1 |
|
* @param int $parent Parent log. Default is 0. |
251
|
|
|
* @param string $type Log type. Default is null. |
252
|
|
|
* |
253
|
1 |
|
* @return int ID of the new log entry. |
254
|
|
|
*/ |
255
|
|
|
function give_record_log( $title = '', $message = '', $parent = 0, $type = null ) { |
256
|
|
|
return Give()->logs->add( $title, $message, $parent, $type ); |
257
|
1 |
|
} |
258
|
1 |
|
|
259
|
1 |
|
/** |
260
|
|
|
* Record a gateway error. |
261
|
|
|
* |
262
|
|
|
* A simple wrapper function for give_record_log(). |
263
|
|
|
* |
264
|
|
|
* @access public |
265
|
|
|
* @since 1.0 |
266
|
|
|
* |
267
|
|
|
* @param string $title Title of the log entry (default: empty) |
268
|
1 |
|
* @param string $message Message to store in the log entry (default: empty) |
269
|
|
|
* @param int $parent Parent log entry (default: 0) |
270
|
|
|
* |
271
|
|
|
* @return int ID of the new log entry |
272
|
|
|
*/ |
273
|
|
|
function give_record_gateway_error( $title = '', $message = '', $parent = 0 ) { |
274
|
|
|
$title = empty( $title ) ? esc_html__( 'Payment Error', 'give' ) : $title; |
275
|
|
|
|
276
|
|
|
return give_record_log( $title, $message, $parent, 'gateway_error' ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Counts the number of donations made with a gateway. |
281
|
|
|
* |
282
|
|
|
* @since 1.0 |
283
|
|
|
* |
284
|
|
|
* @param string $gateway_id |
285
|
|
|
* @param array|string $status |
286
|
|
|
* |
287
|
|
|
* @return int |
288
|
|
|
*/ |
289
|
|
|
function give_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) { |
290
|
|
|
|
291
|
|
|
$ret = 0; |
292
|
|
|
$args = array( |
293
|
|
|
'meta_key' => '_give_payment_gateway', |
|
|
|
|
294
|
|
|
'meta_value' => $gateway_id, |
|
|
|
|
295
|
|
|
'nopaging' => true, |
|
|
|
|
296
|
|
|
'post_type' => 'give_payment', |
297
|
|
|
'post_status' => $status, |
298
|
|
|
'fields' => 'ids', |
299
|
|
|
); |
300
|
|
|
|
301
|
|
|
$payments = new WP_Query( $args ); |
302
|
|
|
|
303
|
|
|
if ( $payments ) { |
304
|
|
|
$ret = $payments->post_count; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $ret; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Returns a ordered list of all available gateways. |
313
|
|
|
* |
314
|
|
|
* @since 1.4.5 |
315
|
|
|
* |
316
|
|
|
* @param array $gateways List of payment gateways |
317
|
|
|
* |
318
|
|
|
* @return array $gateways All the available gateways |
319
|
|
|
*/ |
320
|
|
|
function give_get_ordered_payment_gateways( $gateways ) { |
321
|
|
|
|
322
|
|
|
// Get gateways setting. |
323
|
|
|
$gateways_setting = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' ); |
|
|
|
|
324
|
|
|
|
325
|
|
|
// Return from here if we do not have gateways setting. |
326
|
|
|
if ( empty( $gateways_setting ) ) { |
327
|
|
|
return $gateways; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
// Reverse array to order payment gateways. |
331
|
|
|
$gateways_setting = array_reverse( $gateways_setting ); |
332
|
|
|
|
333
|
44 |
|
// Reorder gateways array |
334
|
|
|
foreach ( $gateways_setting as $gateway_key => $value ) { |
335
|
|
|
|
336
|
44 |
|
$new_gateway_value = isset( $gateways[ $gateway_key ] ) ? $gateways[ $gateway_key ] : ''; |
337
|
|
|
unset( $gateways[ $gateway_key ] ); |
338
|
|
|
|
339
|
|
|
if ( ! empty( $new_gateway_value ) ) { |
340
|
|
|
$gateways = array_merge( array( $gateway_key => $new_gateway_value ), $gateways ); |
341
|
44 |
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
44 |
|
/** |
345
|
|
|
* Filter payment gateways order. |
346
|
44 |
|
* |
347
|
44 |
|
* @since 1.7 |
348
|
|
|
* |
349
|
44 |
|
* @param array $gateways All the available gateways |
350
|
44 |
|
*/ |
351
|
|
|
return apply_filters( 'give_payment_gateways_order', $gateways ); |
352
|
|
|
} |