2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2011-08-10 17:11:11 +00:00
|
|
|
* WooCommerce Payment Gateways class
|
2012-08-15 18:15:06 +00:00
|
|
|
*
|
2011-08-10 17:11:11 +00:00
|
|
|
* Loads payment gateways via hooks for use in the store.
|
|
|
|
*
|
2012-01-27 16:38:39 +00:00
|
|
|
* @class WC_Payment_Gateways
|
2014-08-15 09:56:19 +00:00
|
|
|
* @version 2.2.0
|
2012-08-15 18:15:06 +00:00
|
|
|
* @package WooCommerce/Classes/Payment
|
2013-02-20 17:14:46 +00:00
|
|
|
* @category Class
|
2012-08-15 18:15:06 +00:00
|
|
|
* @author WooThemes
|
2011-08-10 17:11:11 +00:00
|
|
|
*/
|
2012-01-27 16:38:39 +00:00
|
|
|
class WC_Payment_Gateways {
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/** @var array Array of payment gateway classes. */
|
2014-08-15 09:28:23 +00:00
|
|
|
public $payment_gateways;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2013-09-12 13:41:02 +00:00
|
|
|
/**
|
2014-06-19 19:43:05 +00:00
|
|
|
* @var WC_Payment_Gateways The single instance of the class
|
2013-09-12 13:41:02 +00:00
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
protected static $_instance = null;
|
|
|
|
|
|
|
|
/**
|
2014-06-19 19:43:05 +00:00
|
|
|
* Main WC_Payment_Gateways Instance
|
2013-09-12 13:41:02 +00:00
|
|
|
*
|
2014-06-19 19:43:05 +00:00
|
|
|
* Ensures only one instance of WC_Payment_Gateways is loaded or can be loaded.
|
2013-09-12 13:41:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @static
|
2014-06-19 19:43:05 +00:00
|
|
|
* @return WC_Payment_Gateways Main instance
|
2013-09-12 13:41:02 +00:00
|
|
|
*/
|
|
|
|
public static function instance() {
|
2014-08-15 09:28:23 +00:00
|
|
|
if ( is_null( self::$_instance ) ) {
|
2013-09-12 13:41:02 +00:00
|
|
|
self::$_instance = new self();
|
2014-08-15 09:28:23 +00:00
|
|
|
}
|
2013-09-12 13:41:02 +00:00
|
|
|
return self::$_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cloning is forbidden.
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
public function __clone() {
|
2014-02-17 13:14:41 +00:00
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' );
|
2013-09-12 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unserializing instances of this class is forbidden.
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
public function __wakeup() {
|
2014-02-17 13:14:41 +00:00
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' );
|
2013-09-12 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 17:23:09 +00:00
|
|
|
/**
|
2015-07-16 19:55:48 +00:00
|
|
|
* Initialize payment gateways.
|
2013-01-07 17:23:09 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
/**
|
|
|
|
* Load gateways and hook in functions.
|
|
|
|
*/
|
|
|
|
public function init() {
|
|
|
|
$load_gateways = array(
|
|
|
|
'WC_Gateway_BACS',
|
2013-09-12 13:41:02 +00:00
|
|
|
'WC_Gateway_Cheque',
|
|
|
|
'WC_Gateway_COD',
|
2014-08-15 09:28:23 +00:00
|
|
|
'WC_Gateway_Paypal',
|
2014-09-02 14:11:21 +00:00
|
|
|
);
|
2014-08-15 09:28:23 +00:00
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
if ( 'US' === WC()->countries->get_base_country() ) {
|
|
|
|
if ( class_exists( 'WC_Subscriptions_Order' ) || class_exists( 'WC_Pre_Orders_Order' ) ) {
|
2015-07-21 23:50:24 +00:00
|
|
|
if ( ! function_exists( 'wcs_create_renewal_order' ) ) { // Subscriptions < 2.0
|
|
|
|
$load_gateways[] = 'WC_Addons_Gateway_Simplify_Commerce_Deprecated';
|
|
|
|
} else {
|
|
|
|
$load_gateways[] = 'WC_Addons_Gateway_Simplify_Commerce';
|
|
|
|
}
|
2014-08-15 09:56:19 +00:00
|
|
|
} else {
|
|
|
|
$load_gateways[] = 'WC_Gateway_Simplify_Commerce';
|
|
|
|
}
|
2014-09-02 14:11:21 +00:00
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
// Filter
|
|
|
|
$load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways );
|
2014-08-15 09:28:23 +00:00
|
|
|
|
|
|
|
// Get sort order option
|
2014-09-02 13:21:42 +00:00
|
|
|
$ordering = (array) get_option( 'woocommerce_gateway_order' );
|
|
|
|
$order_end = 999;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2011-12-06 16:45:08 +00:00
|
|
|
// Load gateways in order
|
2014-08-15 09:28:23 +00:00
|
|
|
foreach ( $load_gateways as $gateway ) {
|
2015-01-08 02:11:24 +00:00
|
|
|
$load_gateway = is_string( $gateway ) ? new $gateway() : $gateway;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2014-08-15 09:28:23 +00:00
|
|
|
if ( isset( $ordering[ $load_gateway->id ] ) && is_numeric( $ordering[ $load_gateway->id ] ) ) {
|
2011-12-06 16:45:08 +00:00
|
|
|
// Add in position
|
2014-08-15 09:28:23 +00:00
|
|
|
$this->payment_gateways[ $ordering[ $load_gateway->id ] ] = $load_gateway;
|
|
|
|
} else {
|
2011-12-06 16:45:08 +00:00
|
|
|
// Add to end of the array
|
2014-08-15 09:28:23 +00:00
|
|
|
$this->payment_gateways[ $order_end ] = $load_gateway;
|
2011-12-06 16:45:08 +00:00
|
|
|
$order_end++;
|
2014-08-15 09:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-12-31 18:25:09 +00:00
|
|
|
ksort( $this->payment_gateways );
|
2014-09-02 14:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get gateways.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function payment_gateways() {
|
2011-08-09 15:16:18 +00:00
|
|
|
$_available_gateways = array();
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2014-08-15 09:28:23 +00:00
|
|
|
if ( sizeof( $this->payment_gateways ) > 0 ) {
|
|
|
|
foreach ( $this->payment_gateways as $gateway ) {
|
2012-12-31 18:25:09 +00:00
|
|
|
$_available_gateways[ $gateway->id ] = $gateway;
|
2014-08-15 09:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
return $_available_gateways;
|
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get available gateways.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-08-15 09:28:23 +00:00
|
|
|
public function get_available_payment_gateways() {
|
2011-08-09 15:16:18 +00:00
|
|
|
$_available_gateways = array();
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2014-08-15 09:28:23 +00:00
|
|
|
foreach ( $this->payment_gateways as $gateway ) {
|
2013-11-01 21:14:27 +00:00
|
|
|
if ( $gateway->is_available() ) {
|
2014-08-15 09:28:23 +00:00
|
|
|
if ( ! is_add_payment_method_page() ) {
|
|
|
|
$_available_gateways[ $gateway->id ] = $gateway;
|
|
|
|
} elseif( $gateway->supports( 'add_payment_method' ) ) {
|
|
|
|
$_available_gateways[ $gateway->id ] = $gateway;
|
|
|
|
}
|
2013-11-01 21:14:27 +00:00
|
|
|
}
|
2014-08-15 09:28:23 +00:00
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-05-17 09:40:50 +00:00
|
|
|
return apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways );
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2014-11-26 14:19:53 +00:00
|
|
|
/**
|
|
|
|
* Set the current, active gateway
|
|
|
|
*/
|
|
|
|
public function set_current_gateway( $gateways ) {
|
|
|
|
$default = get_option( 'woocommerce_default_gateway', current( array_keys( $gateways ) ) );
|
|
|
|
$current = WC()->session->get( 'chosen_payment_method', $default );
|
|
|
|
|
|
|
|
if ( isset( $gateways[ $current ] ) ) {
|
|
|
|
$gateways[ $current ]->set_current();
|
|
|
|
} elseif ( isset( $gateways[ $default ] ) ) {
|
|
|
|
$gateways[ $default ]->set_current();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 18:15:06 +00:00
|
|
|
/**
|
|
|
|
* Save options in admin.
|
|
|
|
*/
|
2014-08-15 09:28:23 +00:00
|
|
|
public function process_admin_options() {
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-10-17 12:46:35 +00:00
|
|
|
$default_gateway = ( isset( $_POST['default_gateway'] ) ) ? esc_attr( $_POST['default_gateway'] ) : '';
|
|
|
|
$gateway_order = ( isset( $_POST['gateway_order'] ) ) ? $_POST['gateway_order'] : '';
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2011-12-06 16:45:08 +00:00
|
|
|
$order = array();
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-10-17 12:46:35 +00:00
|
|
|
if ( is_array( $gateway_order ) && sizeof( $gateway_order ) > 0 ) {
|
2011-12-06 16:45:08 +00:00
|
|
|
$loop = 0;
|
2012-10-17 12:46:35 +00:00
|
|
|
foreach ( $gateway_order as $gateway_id ) {
|
|
|
|
$order[ esc_attr( $gateway_id ) ] = $loop;
|
2011-12-06 16:45:08 +00:00
|
|
|
$loop++;
|
2012-10-17 12:46:35 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2011-12-06 16:45:08 +00:00
|
|
|
update_option( 'woocommerce_default_gateway', $default_gateway );
|
|
|
|
update_option( 'woocommerce_gateway_order', $order );
|
|
|
|
}
|
2014-09-02 13:21:42 +00:00
|
|
|
}
|