2011-08-10 17:11:11 +00:00
< ? php
2013-02-20 17:14:46 +00:00
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
2011-08-10 17:11:11 +00:00
/**
* Bank Transfer Payment Gateway
2012-08-15 18:15:06 +00:00
*
2011-09-22 14:14:55 +00:00
* Provides a Bank Transfer Payment Gateway . Based on code by Mike Pepper .
2011-08-10 17:11:11 +00:00
*
2012-12-31 18:25:09 +00:00
* @ class WC_Gateway_BACS
2012-08-15 18:15:06 +00:00
* @ extends WC_Payment_Gateway
2013-08-05 15:34:48 +00:00
* @ version 2.1 . 0
2012-08-15 18:15:06 +00:00
* @ package WooCommerce / Classes / Payment
* @ author WooThemes
2011-08-10 17:11:11 +00:00
*/
2012-12-31 18:25:09 +00:00
class WC_Gateway_BACS extends WC_Payment_Gateway {
2011-08-10 17:11:11 +00:00
2012-08-15 18:15:06 +00:00
/**
* Constructor for the gateway .
*/
public function __construct () {
2013-08-05 15:34:48 +00:00
$this -> id = 'bacs' ;
$this -> icon = apply_filters ( 'woocommerce_bacs_icon' , '' );
$this -> has_fields = false ;
$this -> method_title = __ ( 'BACS' , 'woocommerce' );
$this -> method_description = __ ( 'Allows payments by BACS, more commonly known as direct bank/wire transfer.' , 'woocommerce' );
2012-08-15 18:15:06 +00:00
2011-09-22 14:14:55 +00:00
// Load the settings.
2013-01-02 13:38:33 +00:00
$this -> init_form_fields ();
2011-09-22 14:14:55 +00:00
$this -> init_settings ();
2012-08-15 18:15:06 +00:00
2013-06-09 07:02:26 +00:00
// Define user set variables
2013-08-05 15:34:48 +00:00
$this -> title = $this -> get_option ( 'title' );
$this -> description = $this -> get_option ( 'description' );
2013-08-02 10:40:40 +00:00
$this -> instructions = $this -> get_option ( 'instructions' , $this -> description );
2013-06-09 07:02:26 +00:00
2013-05-22 11:20:47 +00:00
// BACS account fields shown on the thanks page and in emails
2013-08-05 15:34:48 +00:00
$this -> account_details = get_option ( 'woocommerce_bacs_accounts' ,
array (
array (
'account_name' => $this -> get_option ( 'account_name' ),
'account_number' => $this -> get_option ( 'account_number' ),
'sort_code' => $this -> get_option ( 'sort_code' ),
'bank_name' => $this -> get_option ( 'bank_name' ),
'iban' => $this -> get_option ( 'iban' ),
'bic' => $this -> get_option ( 'bic' )
)
2013-05-31 15:44:04 +00:00
)
2013-05-22 11:20:47 +00:00
);
2011-09-22 14:14:55 +00:00
// Actions
2012-12-31 18:25:09 +00:00
add_action ( 'woocommerce_update_options_payment_gateways_' . $this -> id , array ( $this , 'process_admin_options' ) );
2013-08-05 15:34:48 +00:00
add_action ( 'woocommerce_update_options_payment_gateways_' . $this -> id , array ( $this , 'save_account_details' ) );
2012-12-15 11:53:32 +00:00
add_action ( 'woocommerce_thankyou_bacs' , array ( $this , 'thankyou_page' ) );
2012-08-15 18:15:06 +00:00
2011-11-02 20:32:35 +00:00
// Customer Emails
2014-02-28 22:11:57 +00:00
add_action ( 'woocommerce_email_before_order_table' , array ( $this , 'email_instructions' ), 10 , 3 );
2012-08-15 18:15:06 +00:00
}
2011-08-10 17:11:11 +00:00
2012-08-15 18:15:06 +00:00
/**
2011-09-22 14:14:55 +00:00
* Initialise Gateway Settings Form Fields
*/
2013-08-05 15:34:48 +00:00
public function init_form_fields () {
2013-04-16 09:58:03 +00:00
$this -> form_fields = array (
2011-09-22 14:14:55 +00:00
'enabled' => array (
2013-08-05 15:34:48 +00:00
'title' => __ ( 'Enable/Disable' , 'woocommerce' ),
'type' => 'checkbox' ,
'label' => __ ( 'Enable Bank Transfer' , 'woocommerce' ),
2013-08-02 10:40:40 +00:00
'default' => 'yes'
),
2011-09-22 14:14:55 +00:00
'title' => array (
2013-08-05 15:34:48 +00:00
'title' => __ ( 'Title' , 'woocommerce' ),
'type' => 'text' ,
2013-08-02 10:40:40 +00:00
'description' => __ ( 'This controls the title which the user sees during checkout.' , 'woocommerce' ),
2013-08-05 15:34:48 +00:00
'default' => __ ( 'Direct Bank Transfer' , 'woocommerce' ),
'desc_tip' => true ,
2013-08-02 10:40:40 +00:00
),
2011-09-22 14:14:55 +00:00
'description' => array (
2013-08-02 10:40:40 +00:00
'title' => __ ( 'Description' , 'woocommerce' ),
'type' => 'textarea' ,
'description' => __ ( 'Payment method description that the customer will see on your checkout.' , 'woocommerce' ),
'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' ),
'desc_tip' => true ,
),
'instructions' => array (
'title' => __ ( 'Instructions' , 'woocommerce' ),
'type' => 'textarea' ,
'description' => __ ( 'Instructions that will be added to the thank you page and emails.' , 'woocommerce' ),
'default' => '' ,
'desc_tip' => true ,
),
2012-07-26 14:11:29 +00:00
'account_details' => array (
2013-08-05 15:34:48 +00:00
'type' => 'account_details'
2013-08-02 10:40:40 +00:00
),
);
2012-08-15 18:15:06 +00:00
}
2013-08-05 15:34:48 +00:00
/**
* generate_account_details_html function .
*/
public function generate_account_details_html () {
ob_start ();
?>
< tr valign = " top " >
< th scope = " row " class = " titledesc " >< ? php _e ( 'Account Details' , 'woocommerce' ); ?> :</th>
< td class = " forminp " id = " bacs_accounts " >
< table class = " widefat wc_input_table sortable " cellspacing = " 0 " >
< thead >
< tr >
< th class = " sort " >& nbsp ; </ th >
< th >< ? php _e ( 'Account Name' , 'woocommerce' ); ?> </th>
< th >< ? php _e ( 'Account Number' , 'woocommerce' ); ?> </th>
< th >< ? php _e ( 'Bank Name' , 'woocommerce' ); ?> </th>
< th >< ? php _e ( 'Sort Code' , 'woocommerce' ); ?> </th>
< th >< ? php _e ( 'IBAN' , 'woocommerce' ); ?> </th>
< th >< ? php _e ( 'BIC / Swift' , 'woocommerce' ); ?> </th>
</ tr >
</ thead >
< tfoot >
< tr >
< 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>
</ tr >
</ tfoot >
< tbody class = " accounts " >
< ? php
$i = - 1 ;
if ( $this -> account_details ) {
foreach ( $this -> account_details as $account ) {
$i ++ ;
echo ' < tr class = " account " >
< td class = " sort " ></ td >
< td >< input type = " text " value = " ' . esc_attr( $account['account_name'] ) . ' " name = " bacs_account_name[' . $i . '] " /></ td >
< td >< input type = " text " value = " ' . esc_attr( $account['account_number'] ) . ' " name = " bacs_account_number[' . $i . '] " /></ td >
< td >< input type = " text " value = " ' . esc_attr( $account['bank_name'] ) . ' " name = " bacs_bank_name[' . $i . '] " /></ td >
< td >< input type = " text " value = " ' . esc_attr( $account['sort_code'] ) . ' " name = " bacs_sort_code[' . $i . '] " /></ td >
< td >< input type = " text " value = " ' . esc_attr( $account['iban'] ) . ' " name = " bacs_iban[' . $i . '] " /></ td >
< td >< input type = " text " value = " ' . esc_attr( $account['bic'] ) . ' " name = " bacs_bic[' . $i . '] " /></ td >
</ tr > ' ;
}
}
?>
</ tbody >
</ table >
< script type = " text/javascript " >
jQuery ( function () {
jQuery ( '#bacs_accounts' ) . on ( 'click' , 'a.add' , function (){
var size = jQuery ( '#bacs_accounts tbody .account' ) . size ();
jQuery ( ' < tr class = " account " > \
< td class = " sort " ></ td > \
< td >< input type = " text " name = " bacs_account_name[' + size + '] " /></ td > \
< td >< input type = " text " name = " bacs_account_number[' + size + '] " /></ td > \
< td >< input type = " text " name = " bacs_bank_name[' + size + '] " /></ td > \
< td >< input type = " text " name = " bacs_sort_code[' + size + '] " /></ td > \
< td >< input type = " text " name = " bacs_iban[' + size + '] " /></ td > \
< td >< input type = " text " name = " bacs_bic[' + size + '] " /></ td > \
</ tr > ').appendTo(' #bacs_accounts table tbody');
return false ;
});
});
</ script >
</ td >
</ tr >
< ? php
return ob_get_clean ();
2012-08-15 18:15:06 +00:00
}
2011-08-10 17:11:11 +00:00
2013-08-05 15:34:48 +00:00
/**
* Save account details table
*/
public function save_account_details () {
$accounts = array ();
if ( isset ( $_POST [ 'bacs_account_name' ] ) ) {
2013-11-25 13:34:21 +00:00
$account_names = array_map ( 'wc_clean' , $_POST [ 'bacs_account_name' ] );
$account_numbers = array_map ( 'wc_clean' , $_POST [ 'bacs_account_number' ] );
$bank_names = array_map ( 'wc_clean' , $_POST [ 'bacs_bank_name' ] );
$sort_codes = array_map ( 'wc_clean' , $_POST [ 'bacs_sort_code' ] );
$ibans = array_map ( 'wc_clean' , $_POST [ 'bacs_iban' ] );
$bics = array_map ( 'wc_clean' , $_POST [ 'bacs_bic' ] );
2013-08-05 15:34:48 +00:00
foreach ( $account_names as $i => $name ) {
2014-02-28 22:05:43 +00:00
if ( ! isset ( $account_names [ $i ] ) ) {
2013-08-05 15:34:48 +00:00
continue ;
2014-02-28 22:05:43 +00:00
}
2013-08-05 15:34:48 +00:00
$accounts [] = array (
'account_name' => $account_names [ $i ],
'account_number' => $account_numbers [ $i ],
'bank_name' => $bank_names [ $i ],
'sort_code' => $sort_codes [ $i ],
'iban' => $ibans [ $i ],
'bic' => $bics [ $i ]
);
}
}
update_option ( 'woocommerce_bacs_accounts' , $accounts );
}
2012-08-15 18:15:06 +00:00
/**
* Output for the order received page .
*/
2013-08-05 15:34:48 +00:00
public function thankyou_page ( $order_id ) {
2014-02-28 22:05:43 +00:00
if ( $this -> instructions ) {
2013-08-02 10:40:40 +00:00
echo wpautop ( wptexturize ( wp_kses_post ( $this -> instructions ) ) );
2014-02-28 22:05:43 +00:00
}
2013-08-05 15:34:48 +00:00
$this -> bank_details ( $order_id );
2011-11-02 20:32:35 +00:00
}
2012-08-15 18:15:06 +00:00
2011-11-02 20:32:35 +00:00
/**
2012-08-15 18:15:06 +00:00
* Add content to the WC emails .
*
* @ access public
* @ param WC_Order $order
* @ param bool $sent_to_admin
2014-02-28 22:11:57 +00:00
* @ param bool $plain_text
2012-08-15 18:15:06 +00:00
* @ return void
*/
2014-03-06 11:57:47 +00:00
public function email_instructions ( $order , $sent_to_admin , $plain_text = false ) {
2012-08-15 18:15:06 +00:00
2014-02-28 22:05:43 +00:00
if ( $sent_to_admin || $order -> status !== 'on-hold' || $order -> payment_method !== 'bacs' ) {
2013-08-05 15:34:48 +00:00
return ;
2014-02-28 22:05:43 +00:00
}
2012-08-15 18:15:06 +00:00
2014-02-28 22:05:43 +00:00
if ( $this -> instructions ) {
2014-02-28 23:35:40 +00:00
echo wpautop ( wptexturize ( $this -> instructions ) ) . PHP_EOL ;
2014-02-28 22:05:43 +00:00
}
2012-08-15 18:15:06 +00:00
2013-08-05 15:34:48 +00:00
$this -> bank_details ( $order -> id );
2011-08-10 17:11:11 +00:00
}
2013-08-05 15:34:48 +00:00
/**
* Get bank details and place into a list format
*/
private function bank_details ( $order_id = '' ) {
2014-02-28 22:05:43 +00:00
if ( empty ( $this -> account_details ) ) {
2013-08-05 15:34:48 +00:00
return ;
2014-02-28 22:05:43 +00:00
}
2013-08-05 15:34:48 +00:00
2014-02-28 23:31:53 +00:00
echo '<h2>' . __ ( 'Our Bank Details' , 'woocommerce' ) . '</h2>' . PHP_EOL ;
2013-08-05 15:34:48 +00:00
$bacs_accounts = apply_filters ( 'woocommerce_bacs_accounts' , $this -> account_details );
if ( ! empty ( $bacs_accounts ) ) {
foreach ( $bacs_accounts as $bacs_account ) {
2014-02-28 23:31:53 +00:00
echo '<ul class="order_details bacs_details">' . PHP_EOL ;
2013-08-05 15:34:48 +00:00
$bacs_account = ( object ) $bacs_account ;
// BACS account fields shown on the thanks page and in emails
$account_fields = apply_filters ( 'woocommerce_bacs_account_fields' , array (
'account_number' => array (
'label' => __ ( 'Account Number' , 'woocommerce' ),
'value' => $bacs_account -> account_number
),
'sort_code' => array (
'label' => __ ( 'Sort Code' , 'woocommerce' ),
'value' => $bacs_account -> sort_code
),
'iban' => array (
'label' => __ ( 'IBAN' , 'woocommerce' ),
'value' => $bacs_account -> iban
),
'bic' => array (
'label' => __ ( 'BIC' , 'woocommerce' ),
'value' => $bacs_account -> bic
)
), $order_id );
2014-02-28 22:05:43 +00:00
if ( $bacs_account -> account_name || $bacs_account -> bank_name ) {
2014-02-28 23:31:53 +00:00
echo '<h3>' . implode ( ' - ' , array_filter ( array ( $bacs_account -> account_name , $bacs_account -> bank_name ) ) ) . '</h3>' . PHP_EOL ;
2014-02-28 22:05:43 +00:00
}
2013-08-05 15:34:48 +00:00
foreach ( $account_fields as $field_key => $field ) {
if ( ! empty ( $field [ 'value' ] ) ) {
2014-02-28 23:31:53 +00:00
echo '<li class="' . esc_attr ( $field_key ) . '">' . esc_attr ( $field [ 'label' ] ) . ': <strong>' . wptexturize ( $field [ 'value' ] ) . '</strong></li>' . PHP_EOL ;
2013-08-05 15:34:48 +00:00
}
}
2014-02-28 23:35:40 +00:00
echo '</ul>' ;
2013-08-05 15:34:48 +00:00
}
}
}
2012-08-15 18:15:06 +00:00
2011-08-10 17:11:11 +00:00
/**
2012-08-15 18:15:06 +00:00
* Process the payment and return the result
*
* @ param int $order_id
* @ return array
*/
2013-08-05 15:34:48 +00:00
public function process_payment ( $order_id ) {
2012-08-15 18:15:06 +00:00
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $order_id );
2012-08-15 18:15:06 +00:00
2011-09-06 11:11:22 +00:00
// Mark as on-hold (we're awaiting the payment)
2013-08-05 15:34:48 +00:00
$order -> update_status ( 'on-hold' , __ ( 'Awaiting BACS payment' , 'woocommerce' ) );
2012-08-15 18:15:06 +00:00
2011-11-26 22:22:25 +00:00
// Reduce stock levels
$order -> reduce_order_stock ();
2011-09-06 11:11:22 +00:00
// Remove cart
2013-08-05 15:34:48 +00:00
WC () -> cart -> empty_cart ();
2012-08-15 18:15:06 +00:00
2011-09-06 11:11:22 +00:00
// Return thankyou redirect
return array (
'result' => 'success' ,
2013-05-31 15:13:14 +00:00
'redirect' => $this -> get_return_url ( $order )
2011-09-06 11:11:22 +00:00
);
2011-08-10 17:11:11 +00:00
}
2014-02-28 23:31:53 +00:00
}