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