/
/
home
/
melaqxso
/
nymetrocualumni.com
/
wp-content
/
plugins
/
give-square
/
includes
Server: server63.web-hosting.com (198.54.116.184)
You: 216.73.216.61
PHP 7.4.33
Dir:
/home/melaqxso/nymetrocualumni.com/wp-content/plugins/give-square/includes
Edit:
/home/melaqxso/nymetrocualumni.com/wp-content/plugins/give-square/includes/helpers.php
<?php /** * Helper Functions for Give Square Gateway * * @package Give-Square * @since 1.0.0 * @license https://opensource.org/licenses/gpl-license GNU Public License * @copyright Copyright (c) 2018, GiveWP */ // Exit if accessed directly. use Give\Framework\Http\ConnectServer\Client\ConnectClient; use Square\Environment; use Square\SquareClient; if ( ! defined('ABSPATH')) { exit; } /** * Square uses it's own credit card form because the card fields are coming from iframe. * * @since 1.0.0 * * @param int $form_id Donation Form ID. * @param array $args Donation Form Arguments. * @param bool $echo Status to display or not. * * @return string $form */ function give_square_credit_card_form($form_id, $args, $echo = true) { ob_start(); do_action('give_before_cc_fields', $form_id); ?> <fieldset id="give_cc_fields" class="give-do-validate"> <legend><?php esc_html_e('Credit Card Info', 'give-square'); ?></legend> <?php $application_id = give_square_get_application_id(); if (empty($application_id)) { // Show frontend notice when Square is not configured properly. Give_Notices::print_frontend_notice( sprintf( /* translators: 1. Text, 2. Text, 3. Test, 4. Link, 5. Link Text */ '%1$s %2$s. %3$s <a href="%4$s">%5$s</a>', __('Square is not set up yet to accept payments in', ''), give_is_test_mode() ? __('Test Mode', 'give-square') : __('Live Mode', 'give-square'), __( 'Please configure the gateway in order to accept donations. If you\'re having trouble please review the', '' ), esc_url('http://docs.givewp.com/addon-square'), __('Square documentation.', 'give-square') ) ); return false; } // Show frontend notice when site not accessed with SSL. if (!is_ssl()) { Give_Notices::print_frontend_notice( __( 'This page requires a valid SSL certificate for secure donations. Please try accessing this page with HTTPS in order to load Credit Card fields.', 'give-square' ) ); return false; } ?> <div id="give_secure_site_wrapper"> <span class="give-icon padlock"></span> <span> <?php esc_attr_e('This is a secure SSL encrypted payment.', 'give-square'); ?> </span> </div> <div id="give-square-credit-card-payment-fields"></div> </fieldset> <?php if (give_is_setting_enabled(give_get_option('give_square_collect_billing_details', false))) { give_default_cc_address_fields($form_id); } do_action('give_square_after_cc_fields', $form_id, $args); $form = ob_get_clean(); if (false !== $echo) { echo $form; } return $form; } /** * Generate Card Nonce. * * @since 1.0.0 * * @return string */ function give_square_generate_card_nonce() { $card_nonce = ''; if (give_is_test_mode()) { $card_nonce = ''; } return $card_nonce; } /** * Get Application ID. * * @since 1.0.0 * * @return mixed */ function give_square_get_application_id() { $application_id = give_get_option('give_square_live_application_id'); if (give_is_test_mode()) { $application_id = give_get_option('give_square_sandbox_application_id'); } return $application_id; } /** * Get Location ID. * * @since 1.0.0 * * @return mixed */ function give_square_get_location_id() { if (give_square_is_manual_api_keys_enabled()) { $location_id = give_get_option('give_square_live_location_id'); // For test mode when Square sandbox connected via manual API Keys. if (give_is_test_mode()) { $location_id = give_get_option('give_square_sandbox_location_id'); } } else { $location_id = give_get_option('give_square_business_location'); // For test mode when Square Sandbox connected via OAuth. if (give_is_test_mode() && give_square_sandbox_is_connected()) { $location_id = give_get_option('give_square_sandbox_business_location'); } } return $location_id; } /** * This function is a wrapper to use simply the function for checkout page redirection. * * @since 1.0.0 * * @param array $args List of arguments. * * @return void */ function give_square_send_back_to_checkout($args = []) { $defaults = [ 'payment-mode' => 'square', ]; $args = wp_parse_args($args, $defaults); give_send_back_to_checkout($args); } /** * This function will check whether Square is connected or not? * * @since 1.0.0 * * @return bool */ function give_square_is_connected() { return give_get_option('give_square_is_connected', false); } /** * This function will check whether Square Sandbox account is connected or not? * * @since 1.0.5 * * @return bool */ function give_square_sandbox_is_connected() { return give_get_option('give_square_sandbox_is_connected', false); } /** * This helper function is to get the host of Square to perform API calls. * * @since 1.0.5 * * @param string $mode Mode. * * @return string */ function give_square_get_host($mode = 'default') { $host = 'squareup.com'; if ( ('default' === $mode && give_is_test_mode()) || 'test' === $mode ) { $host = 'squareupsandbox.com'; } return $host; } /** * @return SquareClient */ function give_square_get_api_client($mode = '') { $environment = ''; $access_token = ''; if (empty($mode)) { $mode = give_square_get_test_mode_text(); } if ('live' === $mode) { $environment = Environment::PRODUCTION; $access_token = give_square_decrypt_string(give_get_option('give_square_live_access_token')); } if ('test' === $mode) { $environment = Environment::SANDBOX; $access_token = give_square_decrypt_string(give_get_option('give_square_sandbox_access_token')); } $api_client = new SquareClient([ 'accessToken' => $access_token, 'environment' => $environment, ]); return $api_client; } /** * This function will help to get list of business locations. * * @since 1.0.0 * * @param string $mode Mode. * * @return array */ function give_square_get_business_locations($mode = 'live') { $locations_list = []; $cache_key = 'live' === $mode ? 'give_cache_square_locations_list' : 'give_cache_square_sandbox_locations_list'; $locations_option = [ '' => __('Select a Location', 'give-square'), ]; // Get locations cache. $locations = Give_Cache::get($cache_key); if (false === $locations) { try { if ('live' === $mode && ! give_square_is_connected()) { return $locations_option; } if ('test' === $mode && ! give_square_sandbox_is_connected()) { return $locations_option; } $api_client = give_square_get_api_client($mode); $location_api = $api_client->getLocationsApi(); $locations_api_result = $location_api->listLocations()->getResult(); $locations_list = $locations_api_result ? $locations_api_result->getLocations() : []; } catch (Exception $e) { $message = sprintf( /* translators: 1. Exception Message. */ __('Unable to fetch locations from Square Payment Gateway. Details: %1$s', 'give-square'), $e->getMessage() ); // Reset oAuth button, if access revoked. if (401 === $e->getCode() && 'live' === $mode) { give_delete_option('give_square_is_connected'); give_delete_option('give_square_business_location'); $message = sprintf( /* translators: 1. Exception Message. */ __( 'Unable to fetch locations from Square Payment Gateway due to revoked access. Details: %1$s', 'give-square' ), $e->getMessage() ); } elseif (401 === $e->getCode() && 'test' === $mode) { give_delete_option('give_square_sandbox_is_connected'); give_delete_option('give_square_sandbox_business_location'); $message = sprintf( /* translators: 1. Exception Message. */ __( 'Unable to fetch locations from Square Payment Gateway due to revoked access. Details: %1$s', 'give-square' ), $e->getMessage() ); } // Record Gateway Error. give_record_gateway_error( __('Square Location Error', 'give-square'), $message ); } foreach ($locations_list as $location) { // Add location to list only if credit card processing is enabled and status is active. if ( is_array($location->getCapabilities()) && 'CREDIT_CARD_PROCESSING' === $location->getCapabilities()[0] && 'ACTIVE' === $location->getStatus() ) { $locations[$location->getId()] = $location->getName(); } } // Set locations cache. Give_Cache::set($cache_key, $locations); } return is_array($locations) ? array_merge($locations_option, $locations) : $locations_option; } /** * This function is used to check whether manual api keys are enabled or not. * * @since 1.0.0 * * @return bool */ function give_square_is_manual_api_keys_enabled() { return give_is_setting_enabled(give_get_option('square_api_keys', 'disabled')); } /** * Check if notice dismissed by admin user or not. * * @since 1.0.0 * * @return bool */ function give_square_is_connect_notice_dismissed() { $current_user = wp_get_current_user(); $is_notice_dismissed = false; if (get_transient("give_hide_square_connect_notice_{$current_user->ID}")) { $is_notice_dismissed = true; } return $is_notice_dismissed; } /** * This function return the text based on the test mode selection. * * @since 1.0.5 * * @return string */ function give_square_get_test_mode_text() { $mode = 'live'; if (give_is_test_mode()) { $mode = 'test'; } return $mode; } /** * @since 2.0.0 */ function give_square_connect_server_url(string $path = ''): string { return give(ConnectClient::class)->getApiUrl($path); } /** * This function prepares the square connect button for reusability. * * @since 1.0.0 * * @param string $mode Mode. * * @return mixed */ function give_square_connect_button($mode = 'live') { // Prepare Square Connect URL. $connect_url = add_query_arg( [ 'action' => 'connect', 'admin_email' => get_bloginfo('admin_email'), 'return_uri' => site_url(), ], give_square_connect_server_url("/square/connect_{$mode}.php") ); $btn_text = ('live' === $mode) ? __('Connect to Square Account', 'give-square') : __('Connect to Square Sandbox Account', 'give-square'); $btn_hover_text = ('live' === $mode) ? __('Connect your site with Square account for easy onboarding.', 'give-square') : __('Connect your site with Square Sandbox account for easy onboarding.', 'give-square'); $image_url = ('live' === $mode) ? esc_url_raw(GIVE_SQUARE_PLUGIN_URL . 'assets/dist/images/square.svg') : esc_url_raw(GIVE_SQUARE_PLUGIN_URL . 'assets/dist/images/square_dark.svg'); $button_class = ('live' === $mode) ? 'give-square-live-btn' : 'give-square-sandbox-btn'; $connect_button = sprintf( '<a href="%1$s" id="give-square-connect-btn" class="give-square-btn %6$s" title="%2$s"><img src="%3$s" alt="%4$s" /><span>%5$s</span></a>', esc_url_raw($connect_url), $btn_hover_text, $image_url, __('Square Payment Gateway.', 'give-square'), $btn_text, $button_class ); return apply_filters('give_square_connect_button', $connect_button); } /** * This function will be used to set the input styles. * * @since 1.0.0 * * @return array */ function give_square_get_input_styles() { // Get input styles from DB. $input_styles = give_get_option( 'square_styles', '{}' ); /** * This filter will be used to modify the input styles array. * * @since 1.0.0 * * @param array $input_styles List of input styles accepted by Square. * */ return apply_filters('give_square_get_input_styles', json_decode($input_styles)); } /** * This function will check whether we need to collect billing details or not. * * @since 1.0.0 * * @return bool */ function give_square_can_collect_billing_details() { return give_is_setting_enabled(give_get_option('give_square_collect_billing_details')); } /** * This function is used to get the unique key to encrypt/decrypt the plain text into AES encrypted text and vice versa. * * @since 1.0.2 * * @return string */ function give_square_get_unique_key() { return '|7vC>PawiP IAUY7Hh@Ts-^srIjpD)Gw29p(?Ni-%YPy=[nFfdFDt(Y-#E&`PEco'; // It should be unique random key. } /** * This function is used to encrypt the plain text to AES encrypted string. * * @since 1.0.2 * * @param string $text Plain Text. * * @return string */ function give_square_encrypt_string($text) { $key = give_square_get_unique_key(); // Remove the base64 encoding from our key. $encryption_key = base64_decode($key); // Generate an initialization vector. $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector. $encrypted = openssl_encrypt($text, 'aes-256-cbc', $encryption_key, 0, $iv); // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::) return base64_encode($encrypted . '::' . $iv); } /** * This function is used to decrypt the AES encrypted string to plain text. * * @since 1.0.2 * * @param string $text Encrypted Text. * * @return string */ function give_square_decrypt_string($text) { if (!$text) { return ''; } $key = give_square_get_unique_key(); // Remove the base64 encoding from our key. $encryption_key = base64_decode($key); // To decrypt, split the encrypted data from our IV - our unique separator used was "::". @list($encrypted_data, $iv) = explode('::', base64_decode($text), 2); return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv); } /** * This function is used to get the access token provided by Square. * * @since 1.0.2 * * @return string */ function give_square_get_access_token() { $access_token = give_square_decrypt_string(give_get_option('give_square_live_access_token')); // If Test Mode enabled & Square OAuth API not connect, use sandbox access token. if (give_is_test_mode()) { $access_token = give_square_decrypt_string(give_get_option('give_square_sandbox_access_token')); } return $access_token; } /** * This function is used to get merchant id. * * @since 1.0.5 * * @param string $mode Mode. * * @return mixed */ function give_square_get_merchant_id($mode = 'live') { $merchant_id = give_get_option('give_square_live_merchant_id'); if ('test' === $mode && give_square_sandbox_is_connected()) { $merchant_id = give_get_option('give_square_sandbox_merchant_id'); } return $merchant_id; }
Ukuran: 15.7 KB