paypal
4 years ago
stripe
1 year ago
actions.php
9 months ago
functions.php
2 years ago
offline-donations.php
2 years ago
functions.php
416 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 3.0.0 add $version param |
| 21 | * @since 2.30.0 add filter give_payment_gateways_admin_label |
| 22 | * @since 1.0 |
| 23 | * @param int|null $version |
| 24 | * @return array $gateways All the available gateways |
| 25 | */ |
| 26 | function give_get_payment_gateways($version = null) |
| 27 | { |
| 28 | // Default, built-in gateways |
| 29 | $gateways = apply_filters( |
| 30 | 'give_payment_gateways', |
| 31 | Give_Cache_Setting::get_option('gateways', []) |
| 32 | ); |
| 33 | |
| 34 | array_walk($gateways, static function (&$gateway, $gatewayId) { |
| 35 | if (isset($gateway['admin_label'])) { |
| 36 | $gateway['admin_label'] = apply_filters( |
| 37 | 'give_payment_gateways_admin_label', |
| 38 | $gateway['admin_label'], |
| 39 | $gatewayId |
| 40 | ); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | $gatewayLabels = []; |
| 45 | if (!$version || $version === 2) { |
| 46 | $gatewayLabels = array_merge($gatewayLabels, (array)give_get_option('gateways_label', [])); |
| 47 | } |
| 48 | |
| 49 | if (!$version || $version === 3) { |
| 50 | $gatewayLabels = array_merge($gatewayLabels, (array)give_get_option('gateways_label_v3', [])); |
| 51 | } |
| 52 | |
| 53 | // Replace payment gateway checkout label with admin defined checkout label. |
| 54 | if ($gatewayLabels) { |
| 55 | foreach ($gatewayLabels as $gatewayId => $checkoutLabel) { |
| 56 | if ($checkoutLabel && array_key_exists($gatewayId, $gateways)) { |
| 57 | $gateways[$gatewayId]['checkout_label'] = $checkoutLabel; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return $gateways; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns a list of all enabled gateways. |
| 67 | * |
| 68 | * @since 3.0.0 add $version param |
| 69 | * @since 1.0 |
| 70 | * |
| 71 | * @param int|null $form_id Form ID |
| 72 | * @param int|null $version |
| 73 | * @return array $gateway_list All the available gateways |
| 74 | */ |
| 75 | function give_get_enabled_payment_gateways($form_id = 0, $version = null) |
| 76 | { |
| 77 | $gateways = give_get_payment_gateways($version); |
| 78 | |
| 79 | $enabled = []; |
| 80 | if (!$version || $version === 2) { |
| 81 | $gatewaysFromPostRequest = isset($_POST['gateways']) ? (array)$_POST['gateways'] : null; |
| 82 | $enabled = array_merge($enabled, $gatewaysFromPostRequest ?? (array)give_get_option('gateways', [])); |
| 83 | } |
| 84 | |
| 85 | if (!$version || $version === 3) { |
| 86 | $gatewaysFromPostRequest = isset($_POST['gateways_v3']) ? (array)$_POST['gateways_v3'] : null; |
| 87 | $enabled = array_merge($enabled, $gatewaysFromPostRequest ?? (array)give_get_option('gateways_v3', [])); |
| 88 | } |
| 89 | |
| 90 | $gateway_list = []; |
| 91 | |
| 92 | foreach ( $gateways as $key => $gateway ) { |
| 93 | if ( isset( $enabled[ $key ] ) && $enabled[ $key ] == 1 ) { |
| 94 | $gateway_list[ $key ] = $gateway; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Set order of payment gateway in list. |
| 99 | $gateway_list = give_get_ordered_payment_gateways($gateway_list, $version); |
| 100 | |
| 101 | return apply_filters('give_enabled_payment_gateways', $gateway_list, $form_id, $version); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Checks whether a specified gateway is activated. |
| 106 | * |
| 107 | * @since 3.0.2 add $version param |
| 108 | * @since 1.0 |
| 109 | * |
| 110 | * @param string $gateway Name of the gateway to check for |
| 111 | * @param int|null $version |
| 112 | * @return bool true if enabled, false otherwise |
| 113 | */ |
| 114 | function give_is_gateway_active($gateway, $version = 2) |
| 115 | { |
| 116 | $gateways = give_get_enabled_payment_gateways(null, $version); |
| 117 | |
| 118 | $ret = array_key_exists( $gateway, $gateways ); |
| 119 | |
| 120 | return apply_filters('give_is_gateway_active', $ret, $gateway, $gateways, $version); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Gets the default payment gateway selected from the Give Settings |
| 125 | * |
| 126 | * @since 3.0.2 add $version param |
| 127 | * @since 1.0 |
| 128 | * |
| 129 | * @param int|null $form_id int ID of the Give Form |
| 130 | * @param int|null $version |
| 131 | * @return string Gateway ID |
| 132 | */ |
| 133 | function give_get_default_gateway($form_id, $version = 2) |
| 134 | { |
| 135 | $suffix = $version === 3 ? '_v3' : ''; |
| 136 | $enabled_gateways = array_keys(give_get_enabled_payment_gateways($form_id, $version)); |
| 137 | $default_gateway = give_get_option('default_gateway' . $suffix); |
| 138 | $default = !empty($default_gateway) && give_is_gateway_active( |
| 139 | $default_gateway, |
| 140 | $version |
| 141 | ) ? $default_gateway : $enabled_gateways[0]; |
| 142 | $form_default = give_get_meta( $form_id, '_give_default_gateway', true ); |
| 143 | |
| 144 | // Single Form settings varies compared to the Global default settings. |
| 145 | if ( |
| 146 | ! empty( $form_default ) && |
| 147 | $form_id !== null && |
| 148 | $default !== $form_default && |
| 149 | 'global' !== $form_default && |
| 150 | give_is_gateway_active($form_default, $version) |
| 151 | ) { |
| 152 | $default = $form_default; |
| 153 | } |
| 154 | |
| 155 | return apply_filters('give_default_gateway', $default, $version); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the admin label for the specified gateway |
| 160 | * |
| 161 | * @since 2.21.0 remove hard coded admin_labels |
| 162 | * @since 1.0 |
| 163 | * |
| 164 | * @param string $gateway Name of the gateway to retrieve a label for |
| 165 | * |
| 166 | * @return string Gateway admin label |
| 167 | */ |
| 168 | function give_get_gateway_admin_label($gateway) |
| 169 | { |
| 170 | $gateways = give_get_payment_gateways(); |
| 171 | $label = isset($gateways[$gateway]) ? $gateways[$gateway]['admin_label'] : $gateway; |
| 172 | |
| 173 | return apply_filters('give_gateway_admin_label', $label, $gateway); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns the checkout label for the specified gateway |
| 178 | * |
| 179 | * @since 3.0.0 add $version param |
| 180 | * @since 1.0 |
| 181 | * @since 2.15.0 Code removed. Here no need to forcefully change manual payment gateway checkout label to "Test Donation". |
| 182 | * |
| 183 | * @param string $gateway Name of the gateway to retrieve a label for |
| 184 | * @param int|null $version |
| 185 | * @return string Checkout label for the gateway |
| 186 | */ |
| 187 | function give_get_gateway_checkout_label($gateway, $version = 2) |
| 188 | { |
| 189 | $gateways = give_get_payment_gateways($version); |
| 190 | $label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway; |
| 191 | |
| 192 | return apply_filters('give_gateway_checkout_label', $label, $gateway, $version); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns the options a gateway supports |
| 197 | * |
| 198 | * @since 1.8 |
| 199 | * |
| 200 | * @param string $gateway ID of the gateway to retrieve a label for |
| 201 | * |
| 202 | * @return array Options the gateway supports |
| 203 | */ |
| 204 | function give_get_gateway_supports( $gateway ) { |
| 205 | $gateways = give_get_enabled_payment_gateways(); |
| 206 | $supports = isset( $gateways[ $gateway ]['supports'] ) ? $gateways[ $gateway ]['supports'] : []; |
| 207 | |
| 208 | return apply_filters( 'give_gateway_supports', $supports, $gateway ); |
| 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 | /** |
| 226 | * Fires while loading payment gateway via AJAX. |
| 227 | * |
| 228 | * The dynamic portion of the hook name '$gateway' must match the ID used when registering the gateway. |
| 229 | * |
| 230 | * @since 1.0 |
| 231 | * |
| 232 | * @param array $payment_data All the payment data to be sent to the gateway. |
| 233 | */ |
| 234 | do_action( "give_gateway_{$gateway}", $payment_data ); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /** |
| 239 | * Determines the currently selected donation payment gateway. |
| 240 | * |
| 241 | * @access public |
| 242 | * @since 1.0 |
| 243 | * |
| 244 | * @param int $form_id The ID of the Form |
| 245 | * |
| 246 | * @return string $enabled_gateway The slug of the gateway |
| 247 | */ |
| 248 | function give_get_chosen_gateway( $form_id ) { |
| 249 | |
| 250 | $request_form_id = isset( $_REQUEST['give_form_id'] ) ? $_REQUEST['give_form_id'] : 0; |
| 251 | |
| 252 | // Back to check if 'form-id' is present. |
| 253 | if ( empty( $request_form_id ) ) { |
| 254 | $request_form_id = isset( $_REQUEST['form-id'] ) ? $_REQUEST['form-id'] : 0; |
| 255 | } |
| 256 | |
| 257 | $request_payment_mode = isset( $_REQUEST['payment-mode'] ) ? $_REQUEST['payment-mode'] : ''; |
| 258 | $chosen = false; |
| 259 | |
| 260 | // If both 'payment-mode' and 'form-id' then set for only this form. |
| 261 | if ( ! empty( $request_form_id ) && $form_id == $request_form_id ) { |
| 262 | $chosen = $request_payment_mode; |
| 263 | } elseif ( empty( $request_form_id ) && $request_payment_mode ) { |
| 264 | // If no 'form-id' but there is 'payment-mode'. |
| 265 | $chosen = $request_payment_mode; |
| 266 | } |
| 267 | |
| 268 | // Get the enable gateway based of chosen var. |
| 269 | if ( $chosen && give_is_gateway_active( $chosen ) ) { |
| 270 | $enabled_gateway = urldecode( $chosen ); |
| 271 | } else { |
| 272 | $enabled_gateway = give_get_default_gateway( $form_id ); |
| 273 | } |
| 274 | |
| 275 | return apply_filters( 'give_chosen_gateway', $enabled_gateway ); |
| 276 | |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Record a log entry |
| 281 | * |
| 282 | * A wrapper function for the Give_Logging class add() method. |
| 283 | * |
| 284 | * @deprecated 2.10.0 |
| 285 | * @use Log::LOG_TYPE( $message ); |
| 286 | * @see Log |
| 287 | * |
| 288 | * @since 1.0 |
| 289 | * @since 2.0 Use global logs object |
| 290 | * |
| 291 | * @param string $title Log title. Default is empty. |
| 292 | * @param string $message Log message. Default is empty. |
| 293 | * @param int $parent Parent log. Default is 0. |
| 294 | * @param string $type Log type. Default is null. |
| 295 | * |
| 296 | * @return int ID of the new log entry. |
| 297 | */ |
| 298 | function give_record_log( $title = '', $message = '', $parent = 0, $type = null ) { |
| 299 | return Give()->logs->add( $title, $message, $parent, $type ); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Record a gateway error. |
| 304 | * |
| 305 | * A simple wrapper function for give_record_log(). |
| 306 | * |
| 307 | * @deprecated 2.10.0 |
| 308 | * @use Log::LOG_TYPE( $message ); |
| 309 | * @see Log |
| 310 | * |
| 311 | * @access public |
| 312 | * @since 1.0 |
| 313 | * |
| 314 | * @param string $title Title of the log entry (default: empty) |
| 315 | * @param string $message Message to store in the log entry (default: empty) |
| 316 | * @param int $parent Parent log entry (default: 0) |
| 317 | * |
| 318 | * @return int ID of the new log entry |
| 319 | */ |
| 320 | function give_record_gateway_error( $title = '', $message = '', $parent = 0 ) { |
| 321 | $title = empty( $title ) ? esc_html__( 'Payment Error', 'give' ) : $title; |
| 322 | |
| 323 | return give_record_log( $title, $message, $parent, 'gateway_error' ); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Counts the number of donations made with a gateway. |
| 328 | * |
| 329 | * @since 1.0 |
| 330 | * |
| 331 | * @param string $gateway_id |
| 332 | * @param array|string $status |
| 333 | * |
| 334 | * @return int |
| 335 | */ |
| 336 | function give_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) { |
| 337 | |
| 338 | $ret = 0; |
| 339 | $args = [ |
| 340 | 'meta_key' => '_give_payment_gateway', |
| 341 | 'meta_value' => $gateway_id, |
| 342 | 'nopaging' => true, |
| 343 | 'post_type' => 'give_payment', |
| 344 | 'post_status' => $status, |
| 345 | 'fields' => 'ids', |
| 346 | ]; |
| 347 | |
| 348 | $payments = new WP_Query( $args ); |
| 349 | |
| 350 | if ( $payments ) { |
| 351 | $ret = $payments->post_count; |
| 352 | } |
| 353 | |
| 354 | return $ret; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /** |
| 359 | * Returns a ordered list of all available gateways. |
| 360 | * |
| 361 | * @since 3.0.2 add $version param |
| 362 | * @since 1.4.5 |
| 363 | * |
| 364 | * @param array $gateways List of payment gateways |
| 365 | * @param int|null $version |
| 366 | * @return array $gateways All the available gateways |
| 367 | */ |
| 368 | function give_get_ordered_payment_gateways($gateways, $version = null) |
| 369 | { |
| 370 | // Get gateways setting. |
| 371 | $gateways_setting = []; |
| 372 | if (!$version || $version === 2) { |
| 373 | $gatewaysFromPostRequest = isset($_POST['gateways']) ? (array)$_POST['gateways'] : null; |
| 374 | $gateways_setting = array_merge( |
| 375 | $gateways_setting, |
| 376 | $gatewaysFromPostRequest ?? (array)give_get_option('gateways', []) |
| 377 | ); |
| 378 | } |
| 379 | |
| 380 | if (!$version || $version === 3) { |
| 381 | $gatewaysFromPostRequest = isset($_POST['gateways_v3']) ? (array)$_POST['gateways_v3'] : null; |
| 382 | $gateways_setting = array_merge( |
| 383 | $gateways_setting, |
| 384 | $gatewaysFromPostRequest ?? (array)give_get_option('gateways_v3', []) |
| 385 | ); |
| 386 | } |
| 387 | |
| 388 | // Return from here if we do not have gateways setting. |
| 389 | if ( empty( $gateways_setting ) ) { |
| 390 | return $gateways; |
| 391 | } |
| 392 | |
| 393 | // Reverse array to order payment gateways. |
| 394 | $gateways_setting = array_reverse( $gateways_setting ); |
| 395 | |
| 396 | // Reorder gateways array |
| 397 | foreach ( $gateways_setting as $gateway_key => $value ) { |
| 398 | $new_gateway_value = $gateways[$gateway_key] ?? ''; |
| 399 | unset( $gateways[ $gateway_key ] ); |
| 400 | |
| 401 | if ( ! empty( $new_gateway_value ) ) { |
| 402 | $gateways = array_merge( [ $gateway_key => $new_gateway_value ], $gateways ); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Filter payment gateways order. |
| 408 | * |
| 409 | * @since 3.0.2 add $version param |
| 410 | * @since 1.7 |
| 411 | * |
| 412 | * @param array $gateways All the available gateways |
| 413 | */ |
| 414 | return apply_filters('give_payment_gateways_order', $gateways, $version); |
| 415 | } |
| 416 |