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