abstracts
8 years ago
admin
8 years ago
api
8 years ago
cli
8 years ago
customizer
8 years ago
data-stores
8 years ago
emails
8 years ago
export
8 years ago
gateways
8 years ago
import
8 years ago
interfaces
8 years ago
legacy
8 years ago
libraries
8 years ago
log-handlers
8 years ago
payment-tokens
9 years ago
shipping
8 years ago
shortcodes
8 years ago
theme-support
8 years ago
walkers
8 years ago
widgets
8 years ago
class-wc-ajax.php
8 years ago
class-wc-api.php
8 years ago
class-wc-auth.php
8 years ago
class-wc-autoloader.php
8 years ago
class-wc-background-emailer.php
8 years ago
class-wc-background-updater.php
8 years ago
class-wc-breadcrumb.php
8 years ago
class-wc-cache-helper.php
8 years ago
class-wc-cart-fees.php
8 years ago
class-wc-cart-session.php
8 years ago
class-wc-cart-totals.php
8 years ago
class-wc-cart.php
8 years ago
class-wc-checkout.php
8 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
8 years ago
class-wc-countries.php
8 years ago
class-wc-coupon.php
8 years ago
class-wc-customer-download-log.php
8 years ago
class-wc-customer-download.php
8 years ago
class-wc-customer.php
8 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
8 years ago
class-wc-datetime.php
8 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
8 years ago
class-wc-discounts.php
8 years ago
class-wc-download-handler.php
8 years ago
class-wc-emails.php
8 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
8 years ago
class-wc-frontend-scripts.php
8 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
8 years ago
class-wc-https.php
9 years ago
class-wc-install.php
8 years ago
class-wc-integrations.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
8 years ago
class-wc-meta-data.php
8 years ago
class-wc-order-factory.php
8 years ago
class-wc-order-item-coupon.php
8 years ago
class-wc-order-item-fee.php
8 years ago
class-wc-order-item-meta.php
8 years ago
class-wc-order-item-product.php
8 years ago
class-wc-order-item-shipping.php
8 years ago
class-wc-order-item-tax.php
8 years ago
class-wc-order-item.php
8 years ago
class-wc-order-query.php
8 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
8 years ago
class-wc-payment-gateways.php
8 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
8 years ago
class-wc-post-types.php
8 years ago
class-wc-product-attribute.php
8 years ago
class-wc-product-download.php
8 years ago
class-wc-product-external.php
8 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
8 years ago
class-wc-product-simple.php
8 years ago
class-wc-product-variable.php
8 years ago
class-wc-product-variation.php
8 years ago
class-wc-query.php
8 years ago
class-wc-regenerate-images-request.php
8 years ago
class-wc-regenerate-images.php
8 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
8 years ago
class-wc-shipping-rate.php
8 years ago
class-wc-shipping-zone.php
8 years ago
class-wc-shipping-zones.php
8 years ago
class-wc-shipping.php
8 years ago
class-wc-shortcodes.php
8 years ago
class-wc-structured-data.php
8 years ago
class-wc-tax.php
8 years ago
class-wc-template-loader.php
8 years ago
class-wc-tracker.php
8 years ago
class-wc-validation.php
8 years ago
class-wc-webhook.php
8 years ago
class-woocommerce.php
8 years ago
wc-account-functions.php
8 years ago
wc-attribute-functions.php
8 years ago
wc-cart-functions.php
8 years ago
wc-conditional-functions.php
8 years ago
wc-core-functions.php
8 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
8 years ago
wc-formatting-functions.php
8 years ago
wc-notice-functions.php
8 years ago
wc-order-functions.php
8 years ago
wc-order-item-functions.php
9 years ago
wc-page-functions.php
8 years ago
wc-product-functions.php
8 years ago
wc-rest-functions.php
8 years ago
wc-stock-functions.php
8 years ago
wc-template-functions.php
8 years ago
wc-template-hooks.php
8 years ago
wc-term-functions.php
8 years ago
wc-update-functions.php
8 years ago
wc-user-functions.php
8 years ago
wc-webhook-functions.php
8 years ago
wc-widget-functions.php
8 years ago
class-wc-payment-gateways.php
217 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * WooCommerce Payment Gateways class |
| 9 | * |
| 10 | * Loads payment gateways via hooks for use in the store. |
| 11 | * |
| 12 | * @class WC_Payment_Gateways |
| 13 | * @version 2.2.0 |
| 14 | * @package WooCommerce/Classes/Payment |
| 15 | * @category Class |
| 16 | * @author WooThemes |
| 17 | */ |
| 18 | class WC_Payment_Gateways { |
| 19 | |
| 20 | /** @var array Array of payment gateway classes. */ |
| 21 | public $payment_gateways; |
| 22 | |
| 23 | /** |
| 24 | * @var WC_Payment_Gateways The single instance of the class |
| 25 | * @since 2.1 |
| 26 | */ |
| 27 | protected static $_instance = null; |
| 28 | |
| 29 | /** |
| 30 | * Main WC_Payment_Gateways Instance. |
| 31 | * |
| 32 | * Ensures only one instance of WC_Payment_Gateways is loaded or can be loaded. |
| 33 | * |
| 34 | * @since 2.1 |
| 35 | * @static |
| 36 | * @return WC_Payment_Gateways Main instance |
| 37 | */ |
| 38 | public static function instance() { |
| 39 | if ( is_null( self::$_instance ) ) { |
| 40 | self::$_instance = new self(); |
| 41 | } |
| 42 | return self::$_instance; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Cloning is forbidden. |
| 47 | * |
| 48 | * @since 2.1 |
| 49 | */ |
| 50 | public function __clone() { |
| 51 | wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Unserializing instances of this class is forbidden. |
| 56 | * |
| 57 | * @since 2.1 |
| 58 | */ |
| 59 | public function __wakeup() { |
| 60 | wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Initialize payment gateways. |
| 65 | */ |
| 66 | public function __construct() { |
| 67 | $this->init(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Load gateways and hook in functions. |
| 72 | */ |
| 73 | public function init() { |
| 74 | $load_gateways = array( |
| 75 | 'WC_Gateway_BACS', |
| 76 | 'WC_Gateway_Cheque', |
| 77 | 'WC_Gateway_COD', |
| 78 | 'WC_Gateway_Paypal', |
| 79 | ); |
| 80 | |
| 81 | /** |
| 82 | * Simplify Commerce is @deprecated in 2.6.0. Only load when enabled. |
| 83 | */ |
| 84 | if ( ! class_exists( 'WC_Gateway_Simplify_Commerce_Loader' ) && in_array( WC()->countries->get_base_country(), apply_filters( 'woocommerce_gateway_simplify_commerce_supported_countries', array( 'US', 'IE' ) ) ) ) { |
| 85 | $simplify_options = get_option( 'woocommerce_simplify_commerce_settings', array() ); |
| 86 | |
| 87 | if ( ! empty( $simplify_options['enabled'] ) && 'yes' === $simplify_options['enabled'] ) { |
| 88 | if ( function_exists( 'wcs_create_renewal_order' ) ) { |
| 89 | $load_gateways[] = 'WC_Addons_Gateway_Simplify_Commerce'; |
| 90 | } else { |
| 91 | $load_gateways[] = 'WC_Gateway_Simplify_Commerce'; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Filter |
| 97 | $load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways ); |
| 98 | |
| 99 | // Get sort order option |
| 100 | $ordering = (array) get_option( 'woocommerce_gateway_order' ); |
| 101 | $order_end = 999; |
| 102 | |
| 103 | // Load gateways in order |
| 104 | foreach ( $load_gateways as $gateway ) { |
| 105 | $load_gateway = is_string( $gateway ) ? new $gateway() : $gateway; |
| 106 | |
| 107 | if ( isset( $ordering[ $load_gateway->id ] ) && is_numeric( $ordering[ $load_gateway->id ] ) ) { |
| 108 | // Add in position |
| 109 | $this->payment_gateways[ $ordering[ $load_gateway->id ] ] = $load_gateway; |
| 110 | } else { |
| 111 | // Add to end of the array |
| 112 | $this->payment_gateways[ $order_end ] = $load_gateway; |
| 113 | $order_end++; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | ksort( $this->payment_gateways ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get gateways. |
| 122 | * @return array |
| 123 | */ |
| 124 | public function payment_gateways() { |
| 125 | $_available_gateways = array(); |
| 126 | |
| 127 | if ( sizeof( $this->payment_gateways ) > 0 ) { |
| 128 | foreach ( $this->payment_gateways as $gateway ) { |
| 129 | $_available_gateways[ $gateway->id ] = $gateway; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return $_available_gateways; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get array of registered gateway ids |
| 138 | * @since 2.6.0 |
| 139 | * @return array of strings |
| 140 | */ |
| 141 | public function get_payment_gateway_ids() { |
| 142 | return wp_list_pluck( $this->payment_gateways, 'id' ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get available gateways. |
| 147 | * |
| 148 | * @return array |
| 149 | */ |
| 150 | public function get_available_payment_gateways() { |
| 151 | $_available_gateways = array(); |
| 152 | |
| 153 | foreach ( $this->payment_gateways as $gateway ) { |
| 154 | if ( $gateway->is_available() ) { |
| 155 | if ( ! is_add_payment_method_page() ) { |
| 156 | $_available_gateways[ $gateway->id ] = $gateway; |
| 157 | } elseif ( $gateway->supports( 'add_payment_method' ) || $gateway->supports( 'tokenization' ) ) { |
| 158 | $_available_gateways[ $gateway->id ] = $gateway; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Set the current, active gateway. |
| 168 | * |
| 169 | * @param array $gateways Available payment gateways. |
| 170 | */ |
| 171 | public function set_current_gateway( $gateways ) { |
| 172 | // Be on the defensive |
| 173 | if ( ! is_array( $gateways ) || empty( $gateways ) ) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if ( is_user_logged_in() ) { |
| 178 | $default_token = WC_Payment_Tokens::get_customer_default_token( get_current_user_id() ); |
| 179 | if ( ! is_null( $default_token ) ) { |
| 180 | $default_token_gateway = $default_token->get_gateway_id(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | $current = ( isset( $default_token_gateway ) ? $default_token_gateway : WC()->session->get( 'chosen_payment_method' ) ); |
| 185 | |
| 186 | if ( $current && isset( $gateways[ $current ] ) ) { |
| 187 | $current_gateway = $gateways[ $current ]; |
| 188 | |
| 189 | } else { |
| 190 | $current_gateway = current( $gateways ); |
| 191 | } |
| 192 | |
| 193 | // Ensure we can make a call to set_current() without triggering an error |
| 194 | if ( $current_gateway && is_callable( array( $current_gateway, 'set_current' ) ) ) { |
| 195 | $current_gateway->set_current(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Save options in admin. |
| 201 | */ |
| 202 | public function process_admin_options() { |
| 203 | $gateway_order = isset( $_POST['gateway_order'] ) ? $_POST['gateway_order'] : ''; |
| 204 | $order = array(); |
| 205 | |
| 206 | if ( is_array( $gateway_order ) && sizeof( $gateway_order ) > 0 ) { |
| 207 | $loop = 0; |
| 208 | foreach ( $gateway_order as $gateway_id ) { |
| 209 | $order[ esc_attr( $gateway_id ) ] = $loop; |
| 210 | $loop++; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | update_option( 'woocommerce_gateway_order', $order ); |
| 215 | } |
| 216 | } |
| 217 |