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