1 <?php
2
3 4 5 6 7 8 9 10 11
12
13 namespace WC_POS\Gateways;
14
15 use WC_Order;
16 use WC_Payment_Gateway;
17 use WC_POS\API;
18
19 class Cash extends WC_Payment_Gateway {
20
21 22 23
24 public function __construct() {
25 $this->id = 'pos_cash';
26 $this->title = __( 'Cash', 'woocommerce-pos' );
27 $this->description = '';
28 $this->icon = apply_filters( 'woocommerce_pos_cash_icon', '' );
29 $this->has_fields = true;
30
31
32 add_action( 'woocommerce_pos_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
33 add_action( 'woocommerce_thankyou_pos_cash', array( $this, 'calculate_change' ) );
34 }
35
36 37 38
39 public function payment_fields() {
40
41 if ( $this->description ) {
42 echo '<p>' . wp_kses_post( $this->description ) . '</p>';
43 }
44
45 $currency_pos = get_option( 'woocommerce_currency_pos' );
46
47 if( $currency_pos == 'left' || 'left_space') {
48 $left_addon = '<span class="input-group-addon">'. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .'</span>';
49 $right_addon = '';
50 } else {
51 $left_addon = '';
52 $right_addon = '<span class="input-group-addon">'. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .'</span>';
53 }
54
55
56 echo '
57 <div class="form-row" id="pos-cash-tendered_field">
58 <label for="pos-cash-tendered" class="">'. __('Amount Tendered', 'woocommerce-pos') .'</label>
59 <div class="input-group">
60 '. $left_addon .'
61 <input type="text" class="form-control" name="pos-cash-tendered" id="pos-cash-tendered" maxlength="20" data-numpad="cash" data-label="'. __('Amount Tendered', 'woocommerce-pos') .'" data-placement="bottom" data-value="{{total}}">
62 '. $right_addon .'
63 </div>
64 </div>
65 ';
66
67 }
68
69 public function process_payment( $order_id ) {
70
71
72 $order = new WC_Order( $order_id );
73
74
75 $data = API::get_raw_data();
76 $tendered = isset( $data['payment_details']['pos-cash-tendered'] ) ? wc_format_decimal( $data['payment_details']['pos-cash-tendered'] ) : 0 ;
77 $change = isset( $data['payment_details']['pos-cash-change'] ) ? wc_format_decimal( $data['payment_details']['pos-cash-change'] ) : 0 ;
78 update_post_meta( $order_id, '_pos_cash_amount_tendered', $tendered );
79 update_post_meta( $order_id, '_pos_cash_change', $change );
80
81
82 $order->payment_complete();
83
84
85 return array(
86 'result' => 'success'
87 );
88 }
89
90 public function calculate_change( $order_id ) {
91 $message = '';
92 $tendered = get_post_meta( $order_id, '_pos_cash_amount_tendered', true );
93 $change = get_post_meta( $order_id, '_pos_cash_change', true );
94
95
96 if( $tendered && $change ) {
97 $message = __('Amount Tendered', 'woocommerce-pos') .': ';
98 $message .= wc_price($tendered) .'<br>';
99 $message .= _x('Change', 'Money returned from cash sale', 'woocommerce-pos') .': ';
100 $message .= wc_price($change);
101 }
102
103 echo $message;
104 }
105
106 static public function payment_details( $order ) {
107 return array(
108 'tendered' => get_post_meta( $order->id, '_pos_cash_amount_tendered', true ),
109 'change' => get_post_meta( $order->id, '_pos_cash_change', true )
110 );
111 }
112
113 }