WC_Payment_Gateways

WooCommerce Payment Gateways class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-payment-gateways.php  
  1. class WC_Payment_Gateways { 
  2.  
  3. /** @var array Array of payment gateway classes. */ 
  4. public $payment_gateways; 
  5.  
  6. /** 
  7. * @var WC_Payment_Gateways The single instance of the class 
  8. * @since 2.1 
  9. */ 
  10. protected static $_instance = null; 
  11.  
  12. /** 
  13. * Main WC_Payment_Gateways Instance. 
  14. * Ensures only one instance of WC_Payment_Gateways is loaded or can be loaded. 
  15. * @since 2.1 
  16. * @static 
  17. * @return WC_Payment_Gateways Main instance 
  18. */ 
  19. public static function instance() { 
  20. if ( is_null( self::$_instance ) ) { 
  21. self::$_instance = new self(); 
  22. return self::$_instance; 
  23.  
  24. /** 
  25. * Cloning is forbidden. 
  26. * @since 2.1 
  27. */ 
  28. public function __clone() { 
  29. wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); 
  30.  
  31. /** 
  32. * Unserializing instances of this class is forbidden. 
  33. * @since 2.1 
  34. */ 
  35. public function __wakeup() { 
  36. wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); 
  37.  
  38. /** 
  39. * Initialize payment gateways. 
  40. */ 
  41. public function __construct() { 
  42. $this->init(); 
  43.  
  44. /** 
  45. * Load gateways and hook in functions. 
  46. */ 
  47. public function init() { 
  48. $load_gateways = array( 
  49. 'WC_Gateway_BACS',  
  50. 'WC_Gateway_Cheque',  
  51. 'WC_Gateway_COD',  
  52. 'WC_Gateway_Paypal',  
  53. ); 
  54.  
  55. /** 
  56. * Simplify Commerce is @deprecated in 2.6.0. Only load when enabled. 
  57. */ 
  58. 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' ) ) ) ) { 
  59. $simplify_options = get_option( 'woocommerce_simplify_commerce_settings', array() ); 
  60.  
  61. if ( ! empty( $simplify_options['enabled'] ) && 'yes' === $simplify_options['enabled'] ) { 
  62. if ( function_exists( 'wcs_create_renewal_order' ) ) { 
  63. $load_gateways[] = 'WC_Addons_Gateway_Simplify_Commerce'; 
  64. } else { 
  65. $load_gateways[] = 'WC_Gateway_Simplify_Commerce'; 
  66.  
  67. // Filter 
  68. $load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways ); 
  69.  
  70. // Get sort order option 
  71. $ordering = (array) get_option( 'woocommerce_gateway_order' ); 
  72. $order_end = 999; 
  73.  
  74. // Load gateways in order 
  75. foreach ( $load_gateways as $gateway ) { 
  76. $load_gateway = is_string( $gateway ) ? new $gateway() : $gateway; 
  77.  
  78. if ( isset( $ordering[ $load_gateway->id ] ) && is_numeric( $ordering[ $load_gateway->id ] ) ) { 
  79. // Add in position 
  80. $this->payment_gateways[ $ordering[ $load_gateway->id ] ] = $load_gateway; 
  81. } else { 
  82. // Add to end of the array 
  83. $this->payment_gateways[ $order_end ] = $load_gateway; 
  84. $order_end++; 
  85.  
  86. ksort( $this->payment_gateways ); 
  87.  
  88. /** 
  89. * Get gateways. 
  90. * @return array 
  91. */ 
  92. public function payment_gateways() { 
  93. $_available_gateways = array(); 
  94.  
  95. if ( sizeof( $this->payment_gateways ) > 0 ) { 
  96. foreach ( $this->payment_gateways as $gateway ) { 
  97. $_available_gateways[ $gateway->id ] = $gateway; 
  98.  
  99. return $_available_gateways; 
  100.  
  101. /** 
  102. * Get array of registered gateway ids 
  103. * @since 2.6.0 
  104. * @return array of strings 
  105. */ 
  106. public function get_payment_gateway_ids() { 
  107. return wp_list_pluck( $this->payment_gateways, 'id' ); 
  108.  
  109. /** 
  110. * Get available gateways. 
  111. * @return array 
  112. */ 
  113. public function get_available_payment_gateways() { 
  114. $_available_gateways = array(); 
  115.  
  116. foreach ( $this->payment_gateways as $gateway ) { 
  117. if ( $gateway->is_available() ) { 
  118. if ( ! is_add_payment_method_page() ) { 
  119. $_available_gateways[ $gateway->id ] = $gateway; 
  120. } elseif ( $gateway->supports( 'add_payment_method' ) ) { 
  121. $_available_gateways[ $gateway->id ] = $gateway; 
  122. } elseif ( $gateway->supports( 'tokenization' ) ) { 
  123. $_available_gateways[ $gateway->id ] = $gateway; 
  124.  
  125. return apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways ); 
  126.  
  127. /** 
  128. * Set the current, active gateway. 
  129. * @param array $gateway Available payment gateways. 
  130. */ 
  131. public function set_current_gateway( $gateways ) { 
  132. // Be on the defensive 
  133. if ( ! is_array( $gateways ) || empty( $gateways ) ) { 
  134. return; 
  135.  
  136. if ( is_user_logged_in() ) { 
  137. $default_token = WC_Payment_Tokens::get_customer_default_token( get_current_user_id() ); 
  138. if ( ! is_null( $default_token ) ) { 
  139. $default_token_gateway = $default_token->get_gateway_id(); 
  140.  
  141. $current = ( isset( $default_token_gateway ) ? $default_token_gateway : WC()->session->get( 'chosen_payment_method' ) ); 
  142.  
  143. if ( $current && isset( $gateways[ $current ] ) ) { 
  144. $current_gateway = $gateways[ $current ]; 
  145.  
  146. } else { 
  147. $current_gateway = current( $gateways ); 
  148.  
  149. // Ensure we can make a call to set_current() without triggering an error 
  150. if ( $current_gateway && is_callable( array( $current_gateway, 'set_current' ) ) ) { 
  151. $current_gateway->set_current(); 
  152.  
  153. /** 
  154. * Save options in admin. 
  155. */ 
  156. public function process_admin_options() { 
  157. $gateway_order = isset( $_POST['gateway_order'] ) ? $_POST['gateway_order'] : ''; 
  158. $order = array(); 
  159.  
  160. if ( is_array( $gateway_order ) && sizeof( $gateway_order ) > 0 ) { 
  161. $loop = 0; 
  162. foreach ( $gateway_order as $gateway_id ) { 
  163. $order[ esc_attr( $gateway_id ) ] = $loop; 
  164. $loop++; 
  165.  
  166. update_option( 'woocommerce_gateway_order', $order );