abstracts
2 years ago
admin
2 years ago
blocks
5 years ago
cli
3 years ago
customizer
3 years ago
data-stores
2 years ago
emails
2 years ago
export
2 years ago
gateways
2 years ago
import
2 years ago
integrations
4 years ago
interfaces
3 years ago
legacy
2 years ago
libraries
3 years ago
log-handlers
4 years ago
payment-tokens
5 years ago
queue
4 years ago
react-admin
2 years ago
rest-api
2 years ago
shipping
2 years ago
shortcodes
2 years ago
theme-support
3 years ago
tracks
2 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
2 years ago
widgets
3 years ago
class-wc-ajax.php
2 years ago
class-wc-api.php
4 years ago
class-wc-auth.php
4 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
3 years ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
2 years ago
class-wc-cart-totals.php
2 years ago
class-wc-cart.php
2 years ago
class-wc-checkout.php
2 years ago
class-wc-cli.php
3 years ago
class-wc-comments.php
3 years ago
class-wc-countries.php
2 years ago
class-wc-coupon.php
3 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
4 years ago
class-wc-customer.php
2 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
3 years ago
class-wc-discounts.php
3 years ago
class-wc-download-handler.php
3 years ago
class-wc-emails.php
3 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
2 years ago
class-wc-frontend-scripts.php
3 years ago
class-wc-geo-ip.php
4 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
3 years ago
class-wc-https.php
2 years ago
class-wc-install.php
2 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
4 years ago
class-wc-meta-data.php
4 years ago
class-wc-order-factory.php
3 years ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
4 years ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
4 years ago
class-wc-order-item-shipping.php
4 years ago
class-wc-order-item-tax.php
4 years ago
class-wc-order-item.php
4 years ago
class-wc-order-query.php
4 years ago
class-wc-order-refund.php
2 years ago
class-wc-order.php
2 years ago
class-wc-payment-gateways.php
4 years ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
3 years ago
class-wc-post-types.php
3 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
4 years ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
2 years ago
class-wc-product-attribute.php
4 years ago
class-wc-product-download.php
4 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
3 years ago
class-wc-product-variable.php
3 years ago
class-wc-product-variation.php
4 years ago
class-wc-query.php
3 years ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
3 years ago
class-wc-register-wp-admin-settings.php
4 years ago
class-wc-rest-authentication.php
3 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
2 years ago
class-wc-shipping-rate.php
3 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
4 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
3 years ago
class-wc-tax.php
2 years ago
class-wc-template-loader.php
2 years ago
class-wc-tracker.php
2 years ago
class-wc-validation.php
3 years ago
class-wc-webhook.php
2 years ago
class-woocommerce.php
2 years ago
wc-account-functions.php
2 years ago
wc-attribute-functions.php
3 years ago
wc-cart-functions.php
2 years ago
wc-conditional-functions.php
2 years ago
wc-core-functions.php
2 years ago
wc-coupon-functions.php
3 years ago
wc-deprecated-functions.php
3 years ago
wc-formatting-functions.php
2 years ago
wc-notice-functions.php
3 years ago
wc-order-functions.php
2 years ago
wc-order-item-functions.php
3 years ago
wc-page-functions.php
2 years ago
wc-product-functions.php
3 years ago
wc-rest-functions.php
3 years ago
wc-stock-functions.php
3 years ago
wc-template-functions.php
2 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
3 years ago
wc-update-functions.php
2 years ago
wc-user-functions.php
2 years ago
wc-webhook-functions.php
4 years ago
wc-widget-functions.php
5 years ago
class-wc-payment-gateways.php
237 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 | ); |
| 82 | |
| 83 | if ( $this->should_load_paypal_standard() ) { |
| 84 | $load_gateways[] = 'WC_Gateway_Paypal'; |
| 85 | } |
| 86 | |
| 87 | // Filter. |
| 88 | $load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways ); |
| 89 | |
| 90 | // Get sort order option. |
| 91 | $ordering = (array) get_option( 'woocommerce_gateway_order' ); |
| 92 | $order_end = 999; |
| 93 | |
| 94 | // Load gateways in order. |
| 95 | foreach ( $load_gateways as $gateway ) { |
| 96 | if ( is_string( $gateway ) && class_exists( $gateway ) ) { |
| 97 | $gateway = new $gateway(); |
| 98 | } |
| 99 | |
| 100 | // Gateways need to be valid and extend WC_Payment_Gateway. |
| 101 | if ( ! is_a( $gateway, 'WC_Payment_Gateway' ) ) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | if ( isset( $ordering[ $gateway->id ] ) && is_numeric( $ordering[ $gateway->id ] ) ) { |
| 106 | // Add in position. |
| 107 | $this->payment_gateways[ $ordering[ $gateway->id ] ] = $gateway; |
| 108 | } else { |
| 109 | // Add to end of the array. |
| 110 | $this->payment_gateways[ $order_end ] = $gateway; |
| 111 | $order_end++; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | ksort( $this->payment_gateways ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get gateways. |
| 120 | * |
| 121 | * @return array |
| 122 | */ |
| 123 | public function payment_gateways() { |
| 124 | $_available_gateways = array(); |
| 125 | |
| 126 | if ( count( $this->payment_gateways ) > 0 ) { |
| 127 | foreach ( $this->payment_gateways as $gateway ) { |
| 128 | $_available_gateways[ $gateway->id ] = $gateway; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return $_available_gateways; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get array of registered gateway ids |
| 137 | * |
| 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 array_filter( (array) apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways ), array( $this, 'filter_valid_gateway_class' ) ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Callback for array filter. Returns true if gateway is of correct type. |
| 168 | * |
| 169 | * @since 3.6.0 |
| 170 | * @param object $gateway Gateway to check. |
| 171 | * @return bool |
| 172 | */ |
| 173 | protected function filter_valid_gateway_class( $gateway ) { |
| 174 | return $gateway && is_a( $gateway, 'WC_Payment_Gateway' ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Set the current, active gateway. |
| 179 | * |
| 180 | * @param array $gateways Available payment gateways. |
| 181 | */ |
| 182 | public function set_current_gateway( $gateways ) { |
| 183 | // Be on the defensive. |
| 184 | if ( ! is_array( $gateways ) || empty( $gateways ) ) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | $current_gateway = false; |
| 189 | |
| 190 | if ( WC()->session ) { |
| 191 | $current = WC()->session->get( 'chosen_payment_method' ); |
| 192 | |
| 193 | if ( $current && isset( $gateways[ $current ] ) ) { |
| 194 | $current_gateway = $gateways[ $current ]; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if ( ! $current_gateway ) { |
| 199 | $current_gateway = current( $gateways ); |
| 200 | } |
| 201 | |
| 202 | // Ensure we can make a call to set_current() without triggering an error. |
| 203 | if ( $current_gateway && is_callable( array( $current_gateway, 'set_current' ) ) ) { |
| 204 | $current_gateway->set_current(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Save options in admin. |
| 210 | */ |
| 211 | public function process_admin_options() { |
| 212 | $gateway_order = isset( $_POST['gateway_order'] ) ? wc_clean( wp_unslash( $_POST['gateway_order'] ) ) : ''; // WPCS: input var ok, CSRF ok. |
| 213 | $order = array(); |
| 214 | |
| 215 | if ( is_array( $gateway_order ) && count( $gateway_order ) > 0 ) { |
| 216 | $loop = 0; |
| 217 | foreach ( $gateway_order as $gateway_id ) { |
| 218 | $order[ esc_attr( $gateway_id ) ] = $loop; |
| 219 | $loop++; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | update_option( 'woocommerce_gateway_order', $order ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Determines if PayPal Standard should be loaded. |
| 228 | * |
| 229 | * @since 5.5.0 |
| 230 | * @return bool Whether PayPal Standard should be loaded or not. |
| 231 | */ |
| 232 | protected function should_load_paypal_standard() { |
| 233 | $paypal = new WC_Gateway_Paypal(); |
| 234 | return $paypal->should_load(); |
| 235 | } |
| 236 | } |
| 237 |