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