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 Card extends WC_Payment_Gateway {
20
21 22 23
24 public function __construct() {
25 $this->id = 'pos_card';
26 $this->title = __( 'Card', 'woocommerce-pos' );
27 $this->description = '';
28 $this->icon = apply_filters( 'woocommerce_pos_card_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_card', array( $this, 'calculate_cashback' ) );
34
35 }
36
37 38 39
40 public function payment_fields() {
41
42 if ( $this->description ) {
43 echo '<p>' . wp_kses_post( $this->description ) . '</p>';
44 }
45
46 $currency_pos = get_option( 'woocommerce_currency_pos' );
47
48 if( $currency_pos == 'left' || 'left_space') {
49 $left_addon = '<span class="input-group-addon">'. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .'</span>';
50 $right_addon = '';
51 } else {
52 $left_addon = '';
53 $right_addon = '<span class="input-group-addon">'. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .'</span>';
54 }
55
56 echo '
57 <div class="form-row " id="pos-cashback_field">
58 <label for="pos-cashback" class="">'. __('Cashback', 'woocommerce-pos') .'</label>
59 <div class="input-group">
60 '. $left_addon .'
61 <input type="text" class="form-control" name="pos-cashback" id="pos-cashback" placeholder="" maxlength="20" data-value="0" data-numpad="cash" data-label="'. __('Cashback', 'woocommerce-pos') .'">
62 '. $right_addon .'
63 </div>
64 </div>
65 ';
66
67 }
68
69 70 71 72
73 public function process_payment( $order_id ) {
74
75
76 $order = new WC_Order( $order_id );
77
78
79 $data = API::get_raw_data();
80 $cashback = isset( $data['payment_details']['pos-cashback'] ) ? wc_format_decimal( $data['payment_details']['pos-cashback'] ) : 0 ;
81
82 if( $cashback !== 0 ) {
83
84
85 update_post_meta( $order_id, '_pos_card_cashback', $cashback );
86
87
88
89 $item_id = wc_add_order_item( $order_id, array(
90 'order_item_name' => __('Cashback', 'woocommerce-pos'),
91 'order_item_type' => 'fee'
92 ) );
93
94 if ( $item_id ) {
95 wc_add_order_item_meta( $item_id, '_line_total', $cashback );
96 wc_add_order_item_meta( $item_id, '_line_tax', 0 );
97 wc_add_order_item_meta( $item_id, '_line_subtotal', $cashback );
98 wc_add_order_item_meta( $item_id, '_line_subtotal_tax', 0 );
99 wc_add_order_item_meta( $item_id, '_tax_class', 'zero-rate' );
100 }
101
102
103 $order_total = get_post_meta( $order_id, '_order_total', true );
104 $order_total += $cashback;
105 update_post_meta( $order_id, '_order_total', $order_total );
106
107 }
108
109
110 $order->payment_complete();
111
112
113 return array(
114 'result' => 'success'
115 );
116 }
117
118 public function calculate_cashback( $order_id ) {
119 $message = '';
120 $cashback = get_post_meta( $order_id, '_pos_card_cashback', true );
121
122
123 if( $cashback ) {
124 $message = __('Cashback', 'woocommerce-pos') . ': ';
125 $message .= wc_price($cashback);
126 }
127
128 echo $message;
129 }
130
131 }