PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.33.4
GiveWP – Donation Plugin and Fundraising Platform v2.33.4
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / gateways / functions.php
give / includes / gateways Last commit date
paypal 4 years ago stripe 2 years ago actions.php 4 years ago functions.php 2 years ago manual.php 3 years ago offline-donations.php 4 years ago
functions.php
372 lines
1 <?php
2 /**
3 * Gateway Functions
4 *
5 * @package Give
6 * @subpackage Gateways
7 * @copyright Copyright (c) 2016, GiveWP
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 2.30.0 add filter give_payment_gateways_admin_label
21 * @since 1.0
22 * @return array $gateways All the available gateways
23 */
24 function give_get_payment_gateways()
25 {
26 // Default, built-in gateways
27 $gateways = apply_filters(
28 'give_payment_gateways',
29 Give_Cache_Setting::get_option('gateways', [])
30 );
31
32 array_walk($gateways, static function (&$gateway, $gatewayId) {
33 if (isset($gateway['admin_label'])) {
34 $gateway['admin_label'] = apply_filters(
35 'give_payment_gateways_admin_label',
36 $gateway['admin_label'],
37 $gatewayId
38 );
39 }
40 });
41
42 $gatewayLabels = give_get_option('gateways_label', []);
43
44 // Replace payment gateway checkout label with admin defined checkout label.
45 if ($gatewayLabels) {
46 foreach ($gatewayLabels as $gatewayId => $checkoutLabel) {
47 if ($checkoutLabel && array_key_exists($gatewayId, $gateways)) {
48 $gateways[$gatewayId]['checkout_label'] = $checkoutLabel;
49 }
50 }
51 }
52
53 return $gateways;
54 }
55
56 /**
57 * Returns a list of all enabled gateways.
58 *
59 * @since 1.0
60 *
61 * @param int $form_id Form ID
62 *
63 * @return array $gateway_list All the available gateways
64 */
65 function give_get_enabled_payment_gateways( $form_id = 0 ) {
66
67 $gateways = give_get_payment_gateways();
68
69 $enabled = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' );
70
71 $gateway_list = [];
72
73 foreach ( $gateways as $key => $gateway ) {
74 if ( isset( $enabled[ $key ] ) && $enabled[ $key ] == 1 ) {
75 $gateway_list[ $key ] = $gateway;
76 }
77 }
78
79 // Set order of payment gateway in list.
80 $gateway_list = give_get_ordered_payment_gateways( $gateway_list );
81
82 return apply_filters( 'give_enabled_payment_gateways', $gateway_list, $form_id );
83 }
84
85 /**
86 * Checks whether a specified gateway is activated.
87 *
88 * @since 1.0
89 *
90 * @param string $gateway Name of the gateway to check for
91 *
92 * @return boolean true if enabled, false otherwise
93 */
94 function give_is_gateway_active( $gateway ) {
95 $gateways = give_get_enabled_payment_gateways();
96
97 $ret = array_key_exists( $gateway, $gateways );
98
99 return apply_filters( 'give_is_gateway_active', $ret, $gateway, $gateways );
100 }
101
102 /**
103 * Gets the default payment gateway selected from the Give Settings
104 *
105 * @since 1.0
106 *
107 * @param $form_id int ID of the Give Form
108 *
109 * @return string Gateway ID
110 */
111 function give_get_default_gateway( $form_id ) {
112
113 $enabled_gateways = array_keys( give_get_enabled_payment_gateways() );
114 $default_gateway = give_get_option( 'default_gateway' );
115 $default = ! empty( $default_gateway ) && give_is_gateway_active( $default_gateway ) ? $default_gateway : $enabled_gateways[0];
116 $form_default = give_get_meta( $form_id, '_give_default_gateway', true );
117
118 // Single Form settings varies compared to the Global default settings.
119 if (
120 ! empty( $form_default ) &&
121 $form_id !== null &&
122 $default !== $form_default &&
123 'global' !== $form_default &&
124 give_is_gateway_active( $form_default )
125 ) {
126 $default = $form_default;
127 }
128
129 return apply_filters( 'give_default_gateway', $default );
130 }
131
132 /**
133 * Returns the admin label for the specified gateway
134 *
135 * @since 2.21.0 remove hard coded admin_labels
136 * @since 1.0
137 *
138 * @param string $gateway Name of the gateway to retrieve a label for
139 *
140 * @return string Gateway admin label
141 */
142 function give_get_gateway_admin_label($gateway)
143 {
144 $gateways = give_get_payment_gateways();
145 $label = isset($gateways[$gateway]) ? $gateways[$gateway]['admin_label'] : $gateway;
146
147 return apply_filters('give_gateway_admin_label', $label, $gateway);
148 }
149
150 /**
151 * Returns the checkout label for the specified gateway
152 *
153 * @since 1.0
154 * @since 2.15.0 Code removed. Here no need to forcefully change manual payment gateway checkout label to "Test Donation".
155 *
156 * @param string $gateway Name of the gateway to retrieve a label for
157 *
158 * @return string Checkout label for the gateway
159 */
160 function give_get_gateway_checkout_label( $gateway ) {
161 $gateways = give_get_payment_gateways();
162 $label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway;
163
164 return apply_filters( 'give_gateway_checkout_label', $label, $gateway );
165 }
166
167 /**
168 * Returns the options a gateway supports
169 *
170 * @since 1.8
171 *
172 * @param string $gateway ID of the gateway to retrieve a label for
173 *
174 * @return array Options the gateway supports
175 */
176 function give_get_gateway_supports( $gateway ) {
177 $gateways = give_get_enabled_payment_gateways();
178 $supports = isset( $gateways[ $gateway ]['supports'] ) ? $gateways[ $gateway ]['supports'] : [];
179
180 return apply_filters( 'give_gateway_supports', $supports, $gateway );
181 }
182
183 /**
184 * Sends all the payment data to the specified gateway
185 *
186 * @since 1.0
187 *
188 * @param string $gateway Name of the gateway
189 * @param array $payment_data All the payment data to be sent to the gateway
190 *
191 * @return void
192 */
193 function give_send_to_gateway( $gateway, $payment_data ) {
194
195 $payment_data['gateway_nonce'] = wp_create_nonce( 'give-gateway' );
196
197 /**
198 * Fires while loading payment gateway via AJAX.
199 *
200 * The dynamic portion of the hook name '$gateway' must match the ID used when registering the gateway.
201 *
202 * @since 1.0
203 *
204 * @param array $payment_data All the payment data to be sent to the gateway.
205 */
206 do_action( "give_gateway_{$gateway}", $payment_data );
207 }
208
209
210 /**
211 * Determines the currently selected donation payment gateway.
212 *
213 * @access public
214 * @since 1.0
215 *
216 * @param int $form_id The ID of the Form
217 *
218 * @return string $enabled_gateway The slug of the gateway
219 */
220 function give_get_chosen_gateway( $form_id ) {
221
222 $request_form_id = isset( $_REQUEST['give_form_id'] ) ? $_REQUEST['give_form_id'] : 0;
223
224 // Back to check if 'form-id' is present.
225 if ( empty( $request_form_id ) ) {
226 $request_form_id = isset( $_REQUEST['form-id'] ) ? $_REQUEST['form-id'] : 0;
227 }
228
229 $request_payment_mode = isset( $_REQUEST['payment-mode'] ) ? $_REQUEST['payment-mode'] : '';
230 $chosen = false;
231
232 // If both 'payment-mode' and 'form-id' then set for only this form.
233 if ( ! empty( $request_form_id ) && $form_id == $request_form_id ) {
234 $chosen = $request_payment_mode;
235 } elseif ( empty( $request_form_id ) && $request_payment_mode ) {
236 // If no 'form-id' but there is 'payment-mode'.
237 $chosen = $request_payment_mode;
238 }
239
240 // Get the enable gateway based of chosen var.
241 if ( $chosen && give_is_gateway_active( $chosen ) ) {
242 $enabled_gateway = urldecode( $chosen );
243 } else {
244 $enabled_gateway = give_get_default_gateway( $form_id );
245 }
246
247 return apply_filters( 'give_chosen_gateway', $enabled_gateway );
248
249 }
250
251 /**
252 * Record a log entry
253 *
254 * A wrapper function for the Give_Logging class add() method.
255 *
256 * @deprecated 2.10.0
257 * @use Log::LOG_TYPE( $message );
258 * @see Log
259 *
260 * @since 1.0
261 * @since 2.0 Use global logs object
262 *
263 * @param string $title Log title. Default is empty.
264 * @param string $message Log message. Default is empty.
265 * @param int $parent Parent log. Default is 0.
266 * @param string $type Log type. Default is null.
267 *
268 * @return int ID of the new log entry.
269 */
270 function give_record_log( $title = '', $message = '', $parent = 0, $type = null ) {
271 return Give()->logs->add( $title, $message, $parent, $type );
272 }
273
274 /**
275 * Record a gateway error.
276 *
277 * A simple wrapper function for give_record_log().
278 *
279 * @deprecated 2.10.0
280 * @use Log::LOG_TYPE( $message );
281 * @see Log
282 *
283 * @access public
284 * @since 1.0
285 *
286 * @param string $title Title of the log entry (default: empty)
287 * @param string $message Message to store in the log entry (default: empty)
288 * @param int $parent Parent log entry (default: 0)
289 *
290 * @return int ID of the new log entry
291 */
292 function give_record_gateway_error( $title = '', $message = '', $parent = 0 ) {
293 $title = empty( $title ) ? esc_html__( 'Payment Error', 'give' ) : $title;
294
295 return give_record_log( $title, $message, $parent, 'gateway_error' );
296 }
297
298 /**
299 * Counts the number of donations made with a gateway.
300 *
301 * @since 1.0
302 *
303 * @param string $gateway_id
304 * @param array|string $status
305 *
306 * @return int
307 */
308 function give_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) {
309
310 $ret = 0;
311 $args = [
312 'meta_key' => '_give_payment_gateway',
313 'meta_value' => $gateway_id,
314 'nopaging' => true,
315 'post_type' => 'give_payment',
316 'post_status' => $status,
317 'fields' => 'ids',
318 ];
319
320 $payments = new WP_Query( $args );
321
322 if ( $payments ) {
323 $ret = $payments->post_count;
324 }
325
326 return $ret;
327 }
328
329
330 /**
331 * Returns a ordered list of all available gateways.
332 *
333 * @since 1.4.5
334 *
335 * @param array $gateways List of payment gateways
336 *
337 * @return array $gateways All the available gateways
338 */
339 function give_get_ordered_payment_gateways( $gateways ) {
340
341 // Get gateways setting.
342 $gateways_setting = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' );
343
344 // Return from here if we do not have gateways setting.
345 if ( empty( $gateways_setting ) ) {
346 return $gateways;
347 }
348
349 // Reverse array to order payment gateways.
350 $gateways_setting = array_reverse( $gateways_setting );
351
352 // Reorder gateways array
353 foreach ( $gateways_setting as $gateway_key => $value ) {
354
355 $new_gateway_value = isset( $gateways[ $gateway_key ] ) ? $gateways[ $gateway_key ] : '';
356 unset( $gateways[ $gateway_key ] );
357
358 if ( ! empty( $new_gateway_value ) ) {
359 $gateways = array_merge( [ $gateway_key => $new_gateway_value ], $gateways );
360 }
361 }
362
363 /**
364 * Filter payment gateways order.
365 *
366 * @since 1.7
367 *
368 * @param array $gateways All the available gateways
369 */
370 return apply_filters( 'give_payment_gateways_order', $gateways );
371 }
372