WC_Gateway_BACS

Bank Transfer Payment Gateway.

Defined (1)

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

/includes/gateways/bacs/class-wc-gateway-bacs.php  
  1. class WC_Gateway_BACS extends WC_Payment_Gateway { 
  2.  
  3. /** @var array Array of locales */ 
  4. public $locale; 
  5.  
  6. /** 
  7. * Constructor for the gateway. 
  8. */ 
  9. public function __construct() { 
  10.  
  11. $this->id = 'bacs'; 
  12. $this->icon = apply_filters( 'woocommerce_bacs_icon', '' ); 
  13. $this->has_fields = false; 
  14. $this->method_title = __( 'BACS', 'woocommerce' ); 
  15. $this->method_description = __( 'Allows payments by BACS, more commonly known as direct bank/wire transfer.', 'woocommerce' ); 
  16.  
  17. // Load the settings. 
  18. $this->init_form_fields(); 
  19. $this->init_settings(); 
  20.  
  21. // Define user set variables 
  22. $this->title = $this->get_option( 'title' ); 
  23. $this->description = $this->get_option( 'description' ); 
  24. $this->instructions = $this->get_option( 'instructions' ); 
  25.  
  26. // BACS account fields shown on the thanks page and in emails 
  27. $this->account_details = get_option( 'woocommerce_bacs_accounts',  
  28. array( 
  29. array( 
  30. 'account_name' => $this->get_option( 'account_name' ),  
  31. 'account_number' => $this->get_option( 'account_number' ),  
  32. 'sort_code' => $this->get_option( 'sort_code' ),  
  33. 'bank_name' => $this->get_option( 'bank_name' ),  
  34. 'iban' => $this->get_option( 'iban' ),  
  35. 'bic' => $this->get_option( 'bic' ),  
  36. ),  
  37. ); 
  38.  
  39. // Actions 
  40. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); 
  41. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'save_account_details' ) ); 
  42. add_action( 'woocommerce_thankyou_bacs', array( $this, 'thankyou_page' ) ); 
  43.  
  44. // Customer Emails 
  45. add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 ); 
  46.  
  47. /** 
  48. * Initialise Gateway Settings Form Fields. 
  49. */ 
  50. public function init_form_fields() { 
  51.  
  52. $this->form_fields = array( 
  53. 'enabled' => array( 
  54. 'title' => __( 'Enable/Disable', 'woocommerce' ),  
  55. 'type' => 'checkbox',  
  56. 'label' => __( 'Enable bank transfer', 'woocommerce' ),  
  57. 'default' => 'no',  
  58. ),  
  59. 'title' => array( 
  60. 'title' => __( 'Title', 'woocommerce' ),  
  61. 'type' => 'text',  
  62. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),  
  63. 'default' => __( 'Direct bank transfer', 'woocommerce' ),  
  64. 'desc_tip' => true,  
  65. ),  
  66. 'description' => array( 
  67. 'title' => __( 'Description', 'woocommerce' ),  
  68. 'type' => 'textarea',  
  69. 'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),  
  70. 'default' => __( 'Make your payment directly into our bank account. Please use your Order ID as the payment reference. Your order won\'t be shipped until the funds have cleared in our account.', 'woocommerce' ),  
  71. 'desc_tip' => true,  
  72. ),  
  73. 'instructions' => array( 
  74. 'title' => __( 'Instructions', 'woocommerce' ),  
  75. 'type' => 'textarea',  
  76. 'description' => __( 'Instructions that will be added to the thank you page and emails.', 'woocommerce' ),  
  77. 'default' => '',  
  78. 'desc_tip' => true,  
  79. ),  
  80. 'account_details' => array( 
  81. 'type' => 'account_details',  
  82. ),  
  83. ); 
  84.  
  85.  
  86. /** 
  87. * Generate account details html. 
  88. * @return string 
  89. */ 
  90. public function generate_account_details_html() { 
  91.  
  92. ob_start(); 
  93.  
  94. $country = WC()->countries->get_base_country(); 
  95. $locale = $this->get_country_locale(); 
  96.  
  97. // Get sortcode label in the $locale array and use appropriate one 
  98. $sortcode = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'woocommerce' ); 
  99.  
  100. ?> 
  101. <tr valign="top"> 
  102. <th scope="row" class="titledesc"><?php _e( 'Account details', 'woocommerce' ); ?>:</th> 
  103. <td class="forminp" id="bacs_accounts"> 
  104. <table class="widefat wc_input_table sortable" cellspacing="0"> 
  105. <thead> 
  106. <tr> 
  107. <th class="sort"> </th> 
  108. <th><?php _e( 'Account name', 'woocommerce' ); ?></th> 
  109. <th><?php _e( 'Account number', 'woocommerce' ); ?></th> 
  110. <th><?php _e( 'Bank name', 'woocommerce' ); ?></th> 
  111. <th><?php echo $sortcode; ?></th> 
  112. <th><?php _e( 'IBAN', 'woocommerce' ); ?></th> 
  113. <th><?php _e( 'BIC / Swift', 'woocommerce' ); ?></th> 
  114. </tr> 
  115. </thead> 
  116. <tbody class="accounts"> 
  117. <?php 
  118. $i = -1; 
  119. if ( $this->account_details ) { 
  120. foreach ( $this->account_details as $account ) { 
  121. $i++; 
  122.  
  123. echo '<tr class="account"> 
  124. <td class="sort"></td> 
  125. <td><input type="text" value="' . esc_attr( wp_unslash( $account['account_name'] ) ) . '" name="bacs_account_name[' . $i . ']" /></td> 
  126. <td><input type="text" value="' . esc_attr( $account['account_number'] ) . '" name="bacs_account_number[' . $i . ']" /></td> 
  127. <td><input type="text" value="' . esc_attr( wp_unslash( $account['bank_name'] ) ) . '" name="bacs_bank_name[' . $i . ']" /></td> 
  128. <td><input type="text" value="' . esc_attr( $account['sort_code'] ) . '" name="bacs_sort_code[' . $i . ']" /></td> 
  129. <td><input type="text" value="' . esc_attr( $account['iban'] ) . '" name="bacs_iban[' . $i . ']" /></td> 
  130. <td><input type="text" value="' . esc_attr( $account['bic'] ) . '" name="bacs_bic[' . $i . ']" /></td> 
  131. </tr>'; 
  132. ?> 
  133. </tbody> 
  134. <tfoot> 
  135. <tr> 
  136. <th colspan="7"><a href="#" class="add button"><?php _e( '+ Add account', 'woocommerce' ); ?></a> <a href="#" class="remove_rows button"><?php _e( 'Remove selected account(s)', 'woocommerce' ); ?></a></th> 
  137. </tr> 
  138. </tfoot> 
  139. </table> 
  140. <script type="text/javascript"> 
  141. jQuery(function() { 
  142. jQuery('#bacs_accounts').on( 'click', 'a.add', function() { 
  143.  
  144. var size = jQuery('#bacs_accounts').find('tbody .account').length; 
  145.  
  146. jQuery('<tr class="account">\ 
  147. <td class="sort"></td>\ 
  148. <td><input type="text" name="bacs_account_name[' + size + ']" /></td>\ 
  149. <td><input type="text" name="bacs_account_number[' + size + ']" /></td>\ 
  150. <td><input type="text" name="bacs_bank_name[' + size + ']" /></td>\ 
  151. <td><input type="text" name="bacs_sort_code[' + size + ']" /></td>\ 
  152. <td><input type="text" name="bacs_iban[' + size + ']" /></td>\ 
  153. <td><input type="text" name="bacs_bic[' + size + ']" /></td>\ 
  154. </tr>').appendTo('#bacs_accounts table tbody'); 
  155.  
  156. return false; 
  157. }); 
  158. }); 
  159. </script> 
  160. </td> 
  161. </tr> 
  162. <?php 
  163. return ob_get_clean(); 
  164.  
  165.  
  166. /** 
  167. * Save account details table. 
  168. */ 
  169. public function save_account_details() { 
  170.  
  171. $accounts = array(); 
  172.  
  173. if ( isset( $_POST['bacs_account_name'] ) ) { 
  174.  
  175. $account_names = array_map( 'wc_clean', $_POST['bacs_account_name'] ); 
  176. $account_numbers = array_map( 'wc_clean', $_POST['bacs_account_number'] ); 
  177. $bank_names = array_map( 'wc_clean', $_POST['bacs_bank_name'] ); 
  178. $sort_codes = array_map( 'wc_clean', $_POST['bacs_sort_code'] ); 
  179. $ibans = array_map( 'wc_clean', $_POST['bacs_iban'] ); 
  180. $bics = array_map( 'wc_clean', $_POST['bacs_bic'] ); 
  181.  
  182. foreach ( $account_names as $i => $name ) { 
  183. if ( ! isset( $account_names[ $i ] ) ) { 
  184. continue; 
  185.  
  186. $accounts[] = array( 
  187. 'account_name' => $account_names[ $i ],  
  188. 'account_number' => $account_numbers[ $i ],  
  189. 'bank_name' => $bank_names[ $i ],  
  190. 'sort_code' => $sort_codes[ $i ],  
  191. 'iban' => $ibans[ $i ],  
  192. 'bic' => $bics[ $i ],  
  193. ); 
  194.  
  195. update_option( 'woocommerce_bacs_accounts', $accounts ); 
  196.  
  197.  
  198. /** 
  199. * Output for the order received page. 
  200. * @param int $order_id 
  201. */ 
  202. public function thankyou_page( $order_id ) { 
  203.  
  204. if ( $this->instructions ) { 
  205. echo wpautop( wptexturize( wp_kses_post( $this->instructions ) ) ); 
  206. $this->bank_details( $order_id ); 
  207.  
  208.  
  209. /** 
  210. * Add content to the WC emails. 
  211. * @param WC_Order $order 
  212. * @param bool $sent_to_admin 
  213. * @param bool $plain_text 
  214. */ 
  215. public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { 
  216.  
  217. if ( ! $sent_to_admin && 'bacs' === $order->get_payment_method() && $order->has_status( 'on-hold' ) ) { 
  218. if ( $this->instructions ) { 
  219. echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL; 
  220. $this->bank_details( $order->get_id() ); 
  221.  
  222.  
  223. /** 
  224. * Get bank details and place into a list format. 
  225. * @param int $order_id 
  226. */ 
  227. private function bank_details( $order_id = '' ) { 
  228.  
  229. if ( empty( $this->account_details ) ) { 
  230. return; 
  231.  
  232. // Get order and store in $order 
  233. $order = wc_get_order( $order_id ); 
  234.  
  235. // Get the order country and country $locale 
  236. $country = $order->get_billing_country(); 
  237. $locale = $this->get_country_locale(); 
  238.  
  239. // Get sortcode label in the $locale array and use appropriate one 
  240. $sortcode = isset( $locale[ $country ]['sortcode']['label'] ) ? $locale[ $country ]['sortcode']['label'] : __( 'Sort code', 'woocommerce' ); 
  241.  
  242. $bacs_accounts = apply_filters( 'woocommerce_bacs_accounts', $this->account_details ); 
  243.  
  244. if ( ! empty( $bacs_accounts ) ) { 
  245. $account_html = ''; 
  246. $has_details = false; 
  247.  
  248. foreach ( $bacs_accounts as $bacs_account ) { 
  249. $bacs_account = (object) $bacs_account; 
  250.  
  251. if ( $bacs_account->account_name ) { 
  252. $account_html .= '<h3 class="wc-bacs-bank-details-account-name">' . wp_kses_post( wp_unslash( $bacs_account->account_name ) ) . ':</h3>' . PHP_EOL; 
  253.  
  254. $account_html .= '<ul class="wc-bacs-bank-details order_details bacs_details">' . PHP_EOL; 
  255.  
  256. // BACS account fields shown on the thanks page and in emails 
  257. $account_fields = apply_filters( 'woocommerce_bacs_account_fields', array( 
  258. 'bank_name' => array( 
  259. 'label' => __( 'Bank', 'woocommerce' ),  
  260. 'value' => $bacs_account->bank_name,  
  261. ),  
  262. 'account_number' => array( 
  263. 'label' => __( 'Account number', 'woocommerce' ),  
  264. 'value' => $bacs_account->account_number,  
  265. ),  
  266. 'sort_code' => array( 
  267. 'label' => $sortcode,  
  268. 'value' => $bacs_account->sort_code,  
  269. ),  
  270. 'iban' => array( 
  271. 'label' => __( 'IBAN', 'woocommerce' ),  
  272. 'value' => $bacs_account->iban,  
  273. ),  
  274. 'bic' => array( 
  275. 'label' => __( 'BIC', 'woocommerce' ),  
  276. 'value' => $bacs_account->bic,  
  277. ),  
  278. ), $order_id ); 
  279.  
  280. foreach ( $account_fields as $field_key => $field ) { 
  281. if ( ! empty( $field['value'] ) ) { 
  282. $account_html .= '<li class="' . esc_attr( $field_key ) . '">' . wp_kses_post( $field['label'] ) . ': <strong>' . wp_kses_post( wptexturize( $field['value'] ) ) . '</strong></li>' . PHP_EOL; 
  283. $has_details = true; 
  284.  
  285. $account_html .= '</ul>'; 
  286.  
  287. if ( $has_details ) { 
  288. echo '<section class="woocommerce-bacs-bank-details"><h2 class="wc-bacs-bank-details-heading">' . __( 'Our bank details', 'woocommerce' ) . '</h2>' . PHP_EOL . $account_html . '</section>'; 
  289.  
  290.  
  291. /** 
  292. * Process the payment and return the result. 
  293. * @param int $order_id 
  294. * @return array 
  295. */ 
  296. public function process_payment( $order_id ) { 
  297.  
  298. $order = wc_get_order( $order_id ); 
  299.  
  300. // Mark as on-hold (we're awaiting the payment) 
  301. $order->update_status( 'on-hold', __( 'Awaiting BACS payment', 'woocommerce' ) ); 
  302.  
  303. // Reduce stock levels 
  304. wc_reduce_stock_levels( $order_id ); 
  305.  
  306. // Remove cart 
  307. WC()->cart->empty_cart(); 
  308.  
  309. // Return thankyou redirect 
  310. return array( 
  311. 'result' => 'success',  
  312. 'redirect' => $this->get_return_url( $order ),  
  313. ); 
  314.  
  315.  
  316. /** 
  317. * Get country locale if localized. 
  318. * @return array 
  319. */ 
  320. public function get_country_locale() { 
  321.  
  322. if ( empty( $this->locale ) ) { 
  323.  
  324. // Locale information to be used - only those that are not 'Sort Code' 
  325. $this->locale = apply_filters( 'woocommerce_get_bacs_locale', array( 
  326. 'AU' => array( 
  327. 'sortcode' => array( 
  328. 'label' => __( 'BSB', 'woocommerce' ),  
  329. ),  
  330. ),  
  331. 'CA' => array( 
  332. 'sortcode' => array( 
  333. 'label' => __( 'Bank transit number', 'woocommerce' ),  
  334. ),  
  335. ),  
  336. 'IN' => array( 
  337. 'sortcode' => array( 
  338. 'label' => __( 'IFSC', 'woocommerce' ),  
  339. ),  
  340. ),  
  341. 'IT' => array( 
  342. 'sortcode' => array( 
  343. 'label' => __( 'Branch sort', 'woocommerce' ),  
  344. ),  
  345. ),  
  346. 'NZ' => array( 
  347. 'sortcode' => array( 
  348. 'label' => __( 'Bank code', 'woocommerce' ),  
  349. ),  
  350. ),  
  351. 'SE' => array( 
  352. 'sortcode' => array( 
  353. 'label' => __( 'Bank code', 'woocommerce' ),  
  354. ),  
  355. ),  
  356. 'US' => array( 
  357. 'sortcode' => array( 
  358. 'label' => __( 'Routing number', 'woocommerce' ),  
  359. ),  
  360. ),  
  361. 'ZA' => array( 
  362. 'sortcode' => array( 
  363. 'label' => __( 'Branch code', 'woocommerce' ),  
  364. ),  
  365. ),  
  366. ) ); 
  367.  
  368.  
  369. return $this->locale; 
  370.