class-wc-payment-gateways.php 5.5 KB

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