WC_Payment_Gateway::validate_fields PHP Method

validate_fields() public method

Validate payment fields on the frontend.
public validate_fields ( ) : boolean
return boolean
    public function validate_fields()
    {
        return true;
    }

Usage Example

 /**
  * Validate payment form fields
  *
  * @see WC_Payment_Gateway::validate_fields()
  */
 public function validate_fields()
 {
     $is_valid = parent::validate_fields();
     $account_number = $this->get_post('elavon_vm_accountNumber');
     $cv_number = $this->get_post('elavon_vm_cvNumber');
     $expiration_month = $this->get_post('elavon_vm_expirationMonth');
     $expiration_year = $this->get_post('elavon_vm_expirationYear');
     // Elavon has stringent length limits for name/address fields
     $billing_field_lengths = array('billing_first_name' => 20, 'billing_last_name' => 30, 'billing_company' => 50, 'billing_address_1' => 30, 'billing_address_2' => 30, 'billing_city' => 30, 'billing_state' => 30, 'billing_postcode' => 9, 'billing_country' => 50, 'billing_email' => 100, 'billing_phone' => 20);
     // for each of our billing fields with maximum lengths
     foreach ($billing_field_lengths as $field_name => $length) {
         // if the supplied length exceeds the maximum
         if (strlen($this->get_post($field_name)) > $length) {
             // is there a checkout field with this name, to grab the label from?  Otherwise, just use the upper-cased version of $field_name
             if (isset(WC()->checkout()->checkout_fields['billing'][$field_name]['label']) && WC()->checkout()->checkout_fields['billing'][$field_name]['label']) {
                 $field_label = WC()->checkout()->checkout_fields['billing'][$field_name]['label'];
             } else {
                 $field_label = ucwords(str_replace('_', ' ', $field_name));
             }
             if (false === stripos($field_label, 'billing')) {
                 wc_add_notice(sprintf(__('The billing %s is too long, %d characters maximum are allowed.  Please fix the %s and try again.', WC_Elavon_VM::TEXT_DOMAIN), $field_label, $length, $field_label), 'error');
                 $is_valid = false;
             } else {
                 wc_add_notice(sprintf(__('The %s is too long, %d characters maximum are allowed.  Please fix the %s and try again.', WC_Elavon_VM::TEXT_DOMAIN), $field_label, $length, $field_label), 'error');
                 $is_valid = false;
             }
         }
     }
     if ($this->cvv_required()) {
         // check security code
         if (empty($cv_number)) {
             wc_add_notice(__('Card security code is missing', WC_Elavon_VM::TEXT_DOMAIN), 'error');
             $is_valid = false;
         }
         if (!ctype_digit($cv_number)) {
             wc_add_notice(__('Card security code is invalid (only digits are allowed)', WC_Elavon_VM::TEXT_DOMAIN), 'error');
             $is_valid = false;
         }
         if (strlen($cv_number) < 3 || strlen($cv_number) > 4) {
             wc_add_notice(__('Card security code is invalid (wrong length)', WC_Elavon_VM::TEXT_DOMAIN), 'error');
             $is_valid = false;
         }
     }
     // check expiration data
     $current_year = date('Y');
     $current_month = date('n');
     if (!ctype_digit($expiration_month) || !ctype_digit($expiration_year) || $expiration_month > 12 || $expiration_month < 1 || $expiration_year < $current_year || $expiration_year == $current_year && $expiration_month < $current_month || $expiration_year > $current_year + 20) {
         wc_add_notice(__('Card expiration date is invalid', WC_Elavon_VM::TEXT_DOMAIN), 'error');
         $is_valid = false;
     }
     // check card number
     $account_number = str_replace(array(' ', '-'), '', $account_number);
     if (empty($account_number) || !ctype_digit($account_number) || !$this->luhn_check($account_number)) {
         wc_add_notice(__('Card number is invalid', WC_Elavon_VM::TEXT_DOMAIN), 'error');
         $is_valid = false;
     }
     return $is_valid;
 }