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