$vars ) { if ( in_array( $card_type, $vars, true ) ) { $card_type = $valid_type; break; } } // otherwise, let it through unaltered return $card_type; } /** * Determine the credit card type from a given account number (only first 4 * required) * * @since 3.0.0 * @param string $account_number the credit card account number * @return string the credit card type */ public static function card_type_from_account_number( $account_number ) { // card type regex patterns from https://github.com/stripe/jquery.payment/blob/master/src/jquery.payment.coffee $types = array( self::CARD_TYPE_VISA => '/^4/', self::CARD_TYPE_MASTERCARD => '/^(5[1-5]|2[2-7])/', self::CARD_TYPE_AMEX => '/^3[47]/', self::CARD_TYPE_DINERSCLUB => '/^(36|38|30[0-5])/', self::CARD_TYPE_DISCOVER => '/^(6011|65|64[4-9]|622)/', self::CARD_TYPE_JCB => '/^35/', self::CARD_TYPE_MAESTRO => '/^(5018|5020|5038|6304|6759|676[1-3])/', self::CARD_TYPE_LASER => '/^(6706|6771|6709)/', ); foreach ( $types as $type => $pattern ) { if ( 1 === preg_match( $pattern, $account_number ) ) { return $type; } } return null; } /** * Translates a credit card type or bank account name to a full name, * e.g. 'mastercard' => 'MasterCard' or 'savings' => 'eCheck' * * @since 3.0.0 * @param string $payment_type the credit card or bank type, ie 'mastercard', 'amex', 'checking' * @return string the credit card or bank account name, ie 'MasterCard', 'American Express', 'Checking Account' */ public static function payment_type_to_name( $payment_type ) { $name = ''; // normalize for backwards compatibility with gateways that pass the card type directly from Payment_Gateway::get_card_types() $type = self::normalize_card_type( $payment_type ); // known payment type names, excluding credit cards $payment_types = array( 'paypal' => esc_html__( 'PayPal', 'woocommerce-square' ), 'checking' => esc_html__( 'Checking Account', 'woocommerce-square' ), 'savings' => esc_html__( 'Savings Account', 'woocommerce-square' ), 'card' => esc_html__( 'Credit / Debit Card', 'woocommerce-square' ), 'bank' => esc_html__( 'Bank Account', 'woocommerce-square' ), ); // add the credit card names $payment_types = array_merge( wp_list_pluck( self::get_card_types(), 'name' ), $payment_types ); if ( isset( $payment_types[ $type ] ) ) { $name = $payment_types[ $type ]; } elseif ( '' === $type ) { $name = esc_html_x( 'Account', 'payment method type', 'woocommerce-square' ); } else { $name = ucwords( str_replace( '-', ' ', $type ) ); } /** * Payment Gateway Type to Name Filter. * * Allow actors to modify the name returned given a payment type. * * @since 3.0.0 * @param string $name nice payment type name, e.g. American Express * @param string $type payment type, e.g. amex */ return apply_filters( 'wc_payment_gateway_payment_type_to_name', $name, $type ); } /** * Gets the known card types and their variations. * * Returns the card types in the format: * * 'mastercard' { * 'name' => 'MasterCard', * 'variations' => array( 'mc' ), * } * * @since 3.0.0 * * @return array */ public static function get_card_types() { return array( self::CARD_TYPE_VISA => array( 'name' => esc_html_x( 'Visa', 'credit card type', 'woocommerce-square' ), 'variations' => array(), ), self::CARD_TYPE_MASTERCARD => array( 'name' => esc_html_x( 'MasterCard', 'credit card type', 'woocommerce-square' ), 'variations' => array( 'mc' ), ), self::CARD_TYPE_AMEX => array( 'name' => esc_html_x( 'American Express', 'credit card type', 'woocommerce-square' ), 'variations' => array( 'americanexpress' ), ), self::CARD_TYPE_DINERSCLUB => array( 'name' => esc_html_x( 'Diners Club', 'credit card type', 'woocommerce-square' ), 'variations' => array( 'diners' ), ), self::CARD_TYPE_DISCOVER => array( 'name' => esc_html_x( 'Discover', 'credit card type', 'woocommerce-square' ), 'variations' => array( 'disc' ), ), self::CARD_TYPE_JCB => array( 'name' => esc_html_x( 'JCB', 'credit card type', 'woocommerce-square' ), 'variations' => array(), ), self::CARD_TYPE_CARTEBLEUE => array( 'name' => esc_html_x( 'CarteBleue', 'credit card type', 'woocommerce-square' ), 'variations' => array(), ), self::CARD_TYPE_MAESTRO => array( 'name' => esc_html_x( 'Maestro', 'credit card type', 'woocommerce-square' ), 'variations' => array(), ), self::CARD_TYPE_LASER => array( 'name' => esc_html_x( 'Laser', 'credit card type', 'woocommerce-square' ), 'variations' => array(), ), ); } }