WC_GZD_Payment_Gateways

WooCommerce Payment Gateways class.

Defined (1)

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

/includes/class-wc-gzd-payment-gateways.php  
  1. class WC_GZD_Payment_Gateways { 
  2.  
  3. protected static $_instance = null; 
  4.  
  5. private $gateway_data = array(); 
  6.  
  7. public static function instance() { 
  8. if ( is_null( self::$_instance ) ) 
  9. self::$_instance = new self(); 
  10. return self::$_instance; 
  11.  
  12. public function __construct() { 
  13. add_action( 'admin_init', array( $this, 'init_fields' ) ); 
  14. add_action( 'woocommerce_calculate_totals', array( $this, 'checkout' ) ); 
  15. add_action( 'woocommerce_cart_calculate_fees', array( $this, 'init_fee' ), 0 ); 
  16.  
  17. /** 
  18. * Set default order button text instead of the button text defined by each payment gateway. 
  19. * Can be overriden by setting force_order_button_text within payment gateway class 
  20. * Manipulate payment gateway description if has a fee and init gateway title filter 
  21. */ 
  22. public function checkout() { 
  23.  
  24. if ( is_admin() ) 
  25. return; 
  26.  
  27. $this->manipulate_gateways(); 
  28.  
  29. public function manipulate_gateways() { 
  30.  
  31. $gateways = WC()->payment_gateways->get_available_payment_gateways(); 
  32.  
  33. foreach( $gateways as $gateway ) { 
  34.  
  35. $this->maybe_set_gateway_data( $gateway ); 
  36.  
  37. if ( ! isset( $gateway->force_order_button_text ) || ! $gateway->force_order_button_text ) 
  38. $gateway->order_button_text = __( get_option( 'woocommerce_gzd_order_submit_btn_text' ), 'woocommerce-germanized' ); 
  39.  
  40. if ( $gateway->get_option( 'fee' ) ) { 
  41.  
  42. $gateway_description = $this->gateway_data[ $gateway->id ][ 'description' ]; 
  43.  
  44. $desc = sprintf( __( '%s payment charge', 'woocommerce-germanized' ), wc_price( $gateway->get_option( 'fee' ) ) ) . '.'; 
  45.  
  46. if ( $gateway->get_option( 'forwarding_fee' ) ) 
  47. $desc .= ' ' . sprintf( __( 'Plus %s forwarding fee (charged by the transport agent)', 'woocommerce-germanized' ), wc_price( $gateway->get_option( 'forwarding_fee' ) ) ) . '.'; 
  48.  
  49. $gateway_description .= apply_filters( 'woocommerce_gzd_payment_gateway_description', ' ' . $desc, $gateway ); 
  50.  
  51. $gateway->description = $gateway_description; 
  52.  
  53. private function maybe_set_gateway_data( $gateway ) { 
  54. if ( ! isset( $this->gateway_data[ $gateway->id ] ) ) { 
  55. $this->gateway_data[ $gateway->id ] = array( 
  56. 'title' => $gateway->title,  
  57. 'description' => $gateway->description,  
  58. ); 
  59.  
  60. /** 
  61. * Manipualte payment gateway title 
  62. *  
  63. * @param string $title  
  64. * @param string $id gateway id 
  65. */ 
  66. public function set_title( $title, $id ) { 
  67. $gateways = WC()->payment_gateways->get_available_payment_gateways(); 
  68. foreach ( $gateways as $gateway ) { 
  69.  
  70. if ( $gateway->id != $id ) 
  71. continue; 
  72.  
  73. $this->maybe_set_gateway_data( $gateway ); 
  74.  
  75. $title = $this->gateway_data[ $gateway->id ][ 'title' ]; 
  76.  
  77. if ( $gateway->get_option( 'fee' ) && ( is_payment_methods() || ( is_checkout() || ( defined( 'DOING_AJAX' ) && isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'woocommerce_update_order_review' ) ) ) ) 
  78. $title = $title . ' <span class="small">(' . sprintf( __( '%s payment charge', 'woocommerce-germanized' ), wc_price( $gateway->get_option( 'fee' ) ) ) . ')</span>'; 
  79.  
  80. return $title; 
  81.  
  82. /** 
  83. * Dynamically set filter to show additional fields 
  84. */ 
  85. public function init_fields() { 
  86. $gateways = WC()->payment_gateways->payment_gateways; 
  87. if ( ! empty( $gateways ) ) { 
  88. foreach ( $gateways as $key => $gateway ) { 
  89. add_filter( 'woocommerce_settings_api_form_fields_' . $gateway->id, array( $this, "set_fields" ) ); 
  90.  
  91. /** 
  92. * Set additional payment gateway admin fields 
  93. *  
  94. * @param array $fields  
  95. */ 
  96. public function set_fields( $fields ) { 
  97.  
  98. $gateway = isset( $_GET[ 'section' ] ) ? wc_clean( $_GET[ 'section' ] ) : ''; 
  99.  
  100. $fields[ 'fee' ] = array( 
  101. 'title' => __( 'Fee', 'woocommerce-germanized' ),  
  102. 'type' => 'decimal',  
  103. 'description' => __( 'This fee is being added if customer selects payment method within checkout.', 'woocommerce-germanized' ),  
  104. 'default' => 0,  
  105. 'desc_tip' => true,  
  106. ); 
  107. $fields[ 'fee_is_taxable' ] = array( 
  108. 'title' => __( 'Fee is taxable?', 'woocommerce-germanized' ),  
  109. 'type' => 'checkbox',  
  110. 'label' => __( 'Check if fee is taxable.', 'woocommerce-germanized' ),  
  111. 'default' => 'no',  
  112. ); 
  113.  
  114. if ( 'wc_gateway_cod' === $gateway ) { 
  115.  
  116. $fields[ 'forwarding_fee' ] = array( 
  117. 'title' => __( 'Forwarding Fee', 'woocommerce-germanized' ),  
  118. 'type' => 'decimal',  
  119. 'desc_tip' => true,  
  120. 'description' => __( 'Forwarding fee will be charged by the transport agent in addition to the cash of delivery fee e.g. DHL - tax free.', 'woocommerce-germanized' ),  
  121. 'default' => 0,  
  122. ); 
  123.  
  124.  
  125. return $fields; 
  126.  
  127. /** 
  128. * Update fee for cart if feeable gateway has been selected as payment method 
  129. */ 
  130. public function init_fee() { 
  131. $gateways = WC()->payment_gateways()->get_available_payment_gateways(); 
  132.  
  133. if ( ! ( $key = WC()->session->get('chosen_payment_method') ) || ! isset( $gateways[ $key ] ) ) 
  134. return; 
  135.  
  136. $gateway = $gateways[ $key ]; 
  137.  
  138. if ( $gateway->get_option( 'fee' ) ) 
  139. $this->set_fee( $gateway ); 
  140.  
  141. /** 
  142. * Sets fee for a specific gateway 
  143. *  
  144. * @param object $gateway  
  145. */ 
  146. public function set_fee( $gateway ) { 
  147.  
  148. $is_taxable = ( ( 'no' === $gateway->get_option( 'fee_is_taxable', 'no' ) || get_option( 'woocommerce_calc_taxes' ) !== 'yes' ) ? false : true ); 
  149. $fee = $gateway->get_option( 'fee' ); 
  150.  
  151. if ( $is_taxable ) { 
  152. $tax_rates = WC_Tax::get_rates(); 
  153. $fee_taxes = WC_Tax::calc_tax( $fee, $tax_rates, true ); 
  154. $fee = $fee - array_sum( $fee_taxes ); 
  155.  
  156. WC()->cart->add_fee( __( 'Payment charge', 'woocommerce-germanized' ), $fee, $is_taxable ); 
  157.