WC_POS_Gateways_Cash

Provides a Cash Payment Gateway.

Defined (1)

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

/includes/gateways/class-wc-pos-cash.php  
  1. class WC_POS_Gateways_Cash extends WC_Payment_Gateway { 
  2.  
  3. /** 
  4. * Constructor for the gateway. 
  5. */ 
  6. public function __construct() { 
  7. $this->id = 'pos_cash'; 
  8. $this->title = __( 'Cash', 'woocommerce-pos' ); 
  9. $this->description = ''; 
  10. $this->icon = apply_filters( 'woocommerce_pos_cash_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_cash', array( $this, 'calculate_change' ) ); 
  16.  
  17. /** 
  18. * Display the payment fields on the checkout modal 
  19. */ 
  20. public function payment_fields() { 
  21.  
  22. if ( $this->description ) { 
  23. echo '<p>' . wp_kses_post( $this->description ) . '</p>'; 
  24.  
  25. $currency_pos = get_option( 'woocommerce_currency_pos' ); 
  26.  
  27. if( $currency_pos == 'left' || 'left_space') { 
  28. $left_addon = '<span class="input-group-addon">'. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .'</span>'; 
  29. $right_addon = ''; 
  30. } else { 
  31. $left_addon = ''; 
  32. $right_addon = '<span class="input-group-addon">'. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .'</span>'; 
  33.  
  34.  
  35. echo ' 
  36. <div class="form-row" id="pos-cash-tendered_field"> 
  37. <label for="pos-cash-tendered" class="">'. __('Amount Tendered', 'woocommerce-pos') .'</label> 
  38. <div class="input-group"> 
  39. '. $left_addon .' 
  40. <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}}"> 
  41. '. $right_addon .' 
  42. </div> 
  43. </div> 
  44. '; 
  45.  
  46.  
  47. public function process_payment( $order_id ) { 
  48.  
  49. // get order object 
  50. $order = new WC_Order( $order_id ); 
  51.  
  52. // update pos_cash data 
  53. $data = WC_POS_Server::get_raw_data(); 
  54. $tendered = isset( $data['payment_details']['pos-cash-tendered'] ) ? wc_format_decimal( $data['payment_details']['pos-cash-tendered'] ) : 0 ; 
  55. $change = isset( $data['payment_details']['pos-cash-change'] ) ? wc_format_decimal( $data['payment_details']['pos-cash-change'] ) : 0 ; 
  56. update_post_meta( $order_id, '_pos_cash_amount_tendered', $tendered ); 
  57. update_post_meta( $order_id, '_pos_cash_change', $change ); 
  58.  
  59. // payment complete 
  60. $order->payment_complete(); 
  61.  
  62. // Return thankyou redirect 
  63. return array( 
  64. 'result' => 'success' 
  65. ); 
  66.  
  67. public function calculate_change( $order_id ) { 
  68. $message = ''; 
  69. $tendered = get_post_meta( $order_id, '_pos_cash_amount_tendered', true ); 
  70. $change = get_post_meta( $order_id, '_pos_cash_change', true ); 
  71.  
  72. // construct message 
  73. if( $tendered && $change ) { 
  74. $message = __('Amount Tendered', 'woocommerce-pos') .': '; 
  75. $message .= wc_price($tendered) .'<br>'; 
  76. $message .= _x('Change', 'Money returned from cash sale', 'woocommerce-pos') .': '; 
  77. $message .= wc_price($change); 
  78.  
  79. echo $message; 
  80.  
  81. static public function payment_details( $payment_details, $order ) { 
  82. $id = is_integer($order) ? $order : $order->id; 
  83.  
  84. $payment_details['method_pos_cash'] = array( 
  85. 'tendered' => get_post_meta( $id, '_pos_cash_amount_tendered', true ),  
  86. 'change' => get_post_meta( $id, '_pos_cash_change', true ) 
  87. ); 
  88.  
  89. return $payment_details; 
  90.