WC_POS_Gateways_Card

Provides a Card Payment Gateway.

Defined (1)

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

/includes/gateways/class-wc-pos-card.php  
  1. class WC_POS_Gateways_Card extends WC_Payment_Gateway { 
  2.  
  3. /** 
  4. * Constructor for the gateway. 
  5. */ 
  6. public function __construct() { 
  7. $this->id = 'pos_card'; 
  8. $this->title = __( 'Card', 'woocommerce-pos' ); 
  9. $this->description = ''; 
  10. $this->icon = apply_filters( 'woocommerce_pos_card_icon', '' ); 
  11. $this->has_fields = true; 
  12.  
  13. // Actions 
  14. add_action( 'woocommerce_pos_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); 
  15. add_action( 'woocommerce_thankyou_pos_card', array( $this, 'calculate_cashback' ) ); 
  16.  
  17.  
  18. /** 
  19. * Display the payment fields in the checkout 
  20. */ 
  21. public function payment_fields() { 
  22.  
  23. if ( $this->description ) { 
  24. echo '<p>' . wp_kses_post( $this->description ) . '</p>'; 
  25.  
  26. $currency_pos = get_option( 'woocommerce_currency_pos' ); 
  27.  
  28. if( $currency_pos == 'left' || 'left_space') { 
  29. $left_addon = '<span class="input-group-addon">'. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .'</span>'; 
  30. $right_addon = ''; 
  31. } else { 
  32. $left_addon = ''; 
  33. $right_addon = '<span class="input-group-addon">'. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .'</span>'; 
  34.  
  35. echo ' 
  36. <div class="form-row " id="pos-cashback_field"> 
  37. <label for="pos-cashback" class="">'. __('Cashback', 'woocommerce-pos') .'</label> 
  38. <div class="input-group"> 
  39. '. $left_addon .' 
  40. <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') .'"> 
  41. '. $right_addon .' 
  42. </div> 
  43. </div> 
  44. '; 
  45.  
  46.  
  47. /** 
  48. * @param int $order_id 
  49. * @return array 
  50. */ 
  51. public function process_payment( $order_id ) { 
  52.  
  53. // get order object 
  54. $order = new WC_Order( $order_id ); 
  55.  
  56. // update pos_cash data 
  57. $data = WC_POS_Server::get_raw_data(); 
  58. $cashback = isset( $data['payment_details']['pos-cashback'] ) ? wc_format_decimal( $data['payment_details']['pos-cashback'] ) : 0 ; 
  59.  
  60. if( $cashback !== 0 ) { 
  61.  
  62. // add order meta 
  63. update_post_meta( $order_id, '_pos_card_cashback', $cashback ); 
  64.  
  65. // add cashback as fee line item 
  66. // TODO: this should be handled by $order->add_fee after WC 2.2 
  67. $item_id = wc_add_order_item( $order_id, array( 
  68. 'order_item_name' => __('Cashback', 'woocommerce-pos'),  
  69. 'order_item_type' => 'fee' 
  70. ) ); 
  71.  
  72. if ( $item_id ) { 
  73. wc_add_order_item_meta( $item_id, '_line_total', $cashback ); 
  74. wc_add_order_item_meta( $item_id, '_line_tax', 0 ); 
  75. wc_add_order_item_meta( $item_id, '_line_subtotal', $cashback ); 
  76. wc_add_order_item_meta( $item_id, '_line_subtotal_tax', 0 ); 
  77. wc_add_order_item_meta( $item_id, '_tax_class', 'zero-rate' ); 
  78.  
  79. // update the order total to include fee 
  80. $order_total = get_post_meta( $order_id, '_order_total', true ); 
  81. $order_total += $cashback; 
  82. update_post_meta( $order_id, '_order_total', $order_total ); 
  83.  
  84.  
  85. // payment complete 
  86. $order->payment_complete(); 
  87.  
  88. // success 
  89. return array( 
  90. 'result' => 'success' 
  91. ); 
  92.  
  93. public function calculate_cashback( $order_id ) { 
  94. $message = ''; 
  95. $cashback = get_post_meta( $order_id, '_pos_card_cashback', true ); 
  96.  
  97. // construct message 
  98. if( $cashback ) { 
  99. $message = __('Cashback', 'woocommerce-pos') . ': '; 
  100. $message .= wc_price($cashback); 
  101.  
  102. echo $message; 
  103.