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