function commerce_cardonfile_format_credit_card_number in Commerce Card on File 7.2
Formats a truncated credit card number for display.
While most credit cards have 16 numbers, some like American Express have less. This function respects those rules in order to generate a more believable mask.
Parameters
$card_number: Truncated card number (last 4 digits).
$card_type: The card type.
Return value
The formatted credit card number with unknown numbers represented by an X. Thus a visa ending with "1234" gets formatted as "XXXX-XXXX-XXXX-1234".
2 calls to commerce_cardonfile_format_credit_card_number()
- commerce_cardonfile_get_properties in ./
commerce_cardonfile.module - Entity Metadata getter callback.
- commerce_cardonfile_handler_field_card_number::get_value in includes/
views/ handlers/ commerce_cardonfile_handler_field_card_number.inc - Return the entity object or a certain property of the entity.
File
- ./
commerce_cardonfile.module, line 1011 - Supports card on file functionality for credit card payment methods by associating card data reference IDs from payment gateways with user accounts.
Code
function commerce_cardonfile_format_credit_card_number($card_number, $card_type) {
$special_lengths = array(
'amex' => 15,
'cb' => 14,
'dc' => 14,
);
$length = isset($special_lengths[$card_type]) ? $special_lengths[$card_type] : 16;
$padded_number = str_pad($card_number, $length, 'X', STR_PAD_LEFT);
$new_number = chunk_split($padded_number, 4, '-');
// chunk_split() returns an extra '-' at the end of the number, ignore it.
return substr($new_number, 0, -1);
}