Overview
  • Namespace
  • Class

Namespaces

  • None
  • WC_POS
    • Admin
      • Settings
        • Receipt
    • API
    • Gateways
    • Integrations
    • Products

Classes

  • WC_POS\Activator
  • WC_POS\Admin
  • WC_POS\Admin\Gateways
  • WC_POS\Admin\Menu
  • WC_POS\Admin\Notices
  • WC_POS\Admin\Orders
  • WC_POS\Admin\Page
  • WC_POS\Admin\Permalink
  • WC_POS\Admin\Plugins
  • WC_POS\Admin\Products
  • WC_POS\Admin\Settings
  • WC_POS\Admin\Settings\Access
  • WC_POS\Admin\Settings\Checkout
  • WC_POS\Admin\Settings\Customers
  • WC_POS\Admin\Settings\Gateways
  • WC_POS\Admin\Settings\General
  • WC_POS\Admin\Settings\HotKeys
  • WC_POS\Admin\Settings\Page
  • WC_POS\Admin\Settings\Receipt\Options
  • WC_POS\Admin\Settings\Receipt\Template
  • WC_POS\Admin\Settings\Receipts
  • WC_POS\Admin\Settings\Status
  • WC_POS\Admin\Settings\Tools
  • WC_POS\Admin\Status
  • WC_POS\Ajax
  • WC_POS\API
  • WC_POS\API\Coupons
  • WC_POS\API\Customers
  • WC_POS\API\Gateways
  • WC_POS\API\i18n
  • WC_POS\API\Orders
  • WC_POS\API\Params
  • WC_POS\API\Payload
  • WC_POS\API\Products
  • WC_POS\API\Settings
  • WC_POS\API\Support
  • WC_POS\API\Templates
  • WC_POS\Customers
  • WC_POS\Deactivator
  • WC_POS\Gateways
  • WC_POS\Gateways\Card
  • WC_POS\Gateways\Cash
  • WC_POS\i18n
  • WC_POS\Integrations\Bookings
  • WC_POS\Products
  • WC_POS\Products\Visibility
  • WC_POS\Setup
  • WC_POS\Status
  • WC_POS\Tax
  • WC_POS\Template

Functions

  • is_pos
  • is_pos_admin
  • wc_pos_get_option
  • wc_pos_json_encode
  • wc_pos_locate_template
  • wc_pos_trim_html_string
  • wc_pos_update_option
  • wc_pos_url
  1 <?php
  2 
  3 /**
  4  * Provides a Card Payment Gateway.
  5  *
  6  * @class       WC_POS_Gateways_Card
  7  * @package     WooCommerce POS
  8  * @author      Paul Kilmurray <paul@kilbot.com.au>
  9  * @link        http://www.woopos.com.au
 10  * @extends     WC_Payment_Gateway
 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    * Constructor for the gateway.
 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     // Actions
 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    * Display the payment fields in the checkout
 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    * @param int $order_id
 71    * @return array
 72    */
 73   public function process_payment( $order_id ) {
 74 
 75     // get order object
 76     $order = new WC_Order( $order_id );
 77 
 78     // update pos_cash data
 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       // add order meta
 85       update_post_meta( $order_id, '_pos_card_cashback', $cashback );
 86 
 87       // add cashback as fee line item
 88       // TODO: this should be handled by $order->add_fee after WC 2.2
 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       // update the order total to include fee
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     // payment complete
110     $order->payment_complete();
111 
112     // success
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     // construct message
123     if( $cashback ) {
124       $message = __('Cashback', 'woocommerce-pos') . ': ';
125       $message .= wc_price($cashback);
126     }
127 
128     echo $message;
129   }
130 
131 }
API documentation generated by ApiGen