function commerce_payment_credit_card_form in Commerce Core 7
Returns a set of credit card form elements that payment method modules can incorporate into their submission form callbacks.
Parameters
$fields: An associative array specifying the fields that should be included on the credit card form. The card number and expiration fields are always present, and fields whose array keys listed below aren't set will be left out of the credit card form:
- type: an array identifying supported card types using the keys of the return array from commerce_payment_credit_card_types().
- owner: TRUE to include an owner name textfield.
- start_date: TRUE to include start date select lists.
- issue: boolean that when present enables an issue number field; if TRUE, makes the field required; if FALSE, makes the field optional.
- code: text label to use for a security code / CVV textfield.
- bank: TRUE to include a bank name textfield.
$default: An array of default values for the available CC fields.
Return value
A credit card form array for use in another form.
1 call to commerce_payment_credit_card_form()
- commerce_payment_example_submit_form in modules/
payment/ modules/ commerce_payment_example.module - Payment method callback: submit form.
File
- modules/
payment/ includes/ commerce_payment.credit_card.inc, line 31 - Credit-card helper functions for Drupal commerce.
Code
function commerce_payment_credit_card_form($fields = array(), $default = array()) {
// Merge default values into the default array.
$default += array(
'type' => '',
'owner' => '',
'number' => '',
'start_month' => '',
'start_year' => date('Y') - 5,
'exp_month' => date('m'),
'exp_year' => date('Y'),
'issue' => '',
'code' => '',
'bank' => '',
);
$current_year_2 = date('y');
$current_year_4 = date('Y');
$form['credit_card'] = array(
'#tree' => TRUE,
'#attached' => array(
'css' => array(
drupal_get_path('module', 'commerce_payment') . '/theme/commerce_payment.theme.css',
),
),
);
// Add a card type selector if specified.
if (isset($fields['type'])) {
$form['credit_card']['type'] = array(
'#type' => 'select',
'#title' => t('Card type'),
'#options' => array_intersect_key(commerce_payment_credit_card_types(), drupal_map_assoc((array) $fields['type'])),
'#default_value' => $default['type'],
);
$form['credit_card']['valid_types'] = array(
'#type' => 'value',
'#value' => $fields['type'],
);
}
else {
$form['credit_card']['valid_types'] = array(
'#type' => 'value',
'#value' => array(),
);
}
// Add a field for the credit card owner if specified.
if (isset($fields['owner'])) {
$form['credit_card']['owner'] = array(
'#type' => 'textfield',
'#title' => t('Card owner'),
'#default_value' => $default['owner'],
'#attributes' => array(
'autocomplete' => 'off',
),
'#required' => TRUE,
'#maxlength' => 64,
'#size' => 32,
);
}
// Always add a field for the credit card number.
$form['credit_card']['number'] = array(
'#type' => 'textfield',
'#title' => t('Card number'),
'#default_value' => $default['number'],
'#attributes' => array(
'autocomplete' => 'off',
),
'#required' => TRUE,
'#maxlength' => 19,
'#size' => 20,
);
// Add fields for the credit card start date if specified.
if (isset($fields['start_date'])) {
$form['credit_card']['start_month'] = array(
'#type' => 'select',
'#title' => t('Start date'),
'#options' => drupal_map_assoc(array_keys(commerce_months())),
'#default_value' => strlen($default['start_month']) == 1 ? '0' . $default['start_month'] : $default['start_month'],
'#required' => TRUE,
'#prefix' => '<div class="commerce-credit-card-start">',
'#suffix' => '<span class="commerce-month-year-divider">/</span>',
);
// Build a year select list that uses a 4 digit key with a 2 digit value.
$options = array();
for ($i = -10; $i < 1; $i++) {
$options[$current_year_4 + $i] = str_pad($current_year_2 + $i, 2, '0', STR_PAD_LEFT);
}
$form['credit_card']['start_year'] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => $default['start_year'],
'#suffix' => '</div>',
);
}
// Always add fields for the credit card expiration date.
$form['credit_card']['exp_month'] = array(
'#type' => 'select',
'#title' => t('Expiration'),
'#options' => drupal_map_assoc(array_keys(commerce_months())),
'#default_value' => strlen($default['exp_month']) == 1 ? '0' . $default['exp_month'] : $default['exp_month'],
'#required' => TRUE,
'#prefix' => '<div class="commerce-credit-card-expiration">',
'#suffix' => '<span class="commerce-month-year-divider">/</span>',
);
// Build a year select list that uses a 4 digit key with a 2 digit value.
$options = array();
for ($i = 0; $i < 20; $i++) {
$options[$current_year_4 + $i] = str_pad($current_year_2 + $i, 2, '0', STR_PAD_LEFT);
}
$form['credit_card']['exp_year'] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => $default['exp_year'],
'#suffix' => '</div>',
);
// Add a field for the card issue number if specified.
if (isset($fields['issue'])) {
$form['credit_card']['issue'] = array(
'#type' => 'textfield',
'#title' => t('Issue number', array(), array(
'context' => 'credit card issue number for card types that require it',
)),
'#default_value' => $default['issue'],
'#attributes' => array(
'autocomplete' => 'off',
),
'#required' => empty($fields['issue']) ? FALSE : TRUE,
'#maxlength' => 2,
'#size' => 2,
);
}
// Add a field for the security code if specified.
if (isset($fields['code'])) {
$form['credit_card']['code'] = array(
'#type' => 'textfield',
'#title' => !empty($fields['code']) ? $fields['code'] : t('Security code'),
'#default_value' => $default['code'],
'#attributes' => array(
'autocomplete' => 'off',
),
'#required' => TRUE,
'#maxlength' => 4,
'#size' => 4,
);
}
// Add a field for the issuing bank if specified.
if (isset($fields['bank'])) {
$form['credit_card']['bank'] = array(
'#type' => 'textfield',
'#title' => t('Issuing bank'),
'#default_value' => $default['bank'],
'#attributes' => array(
'autocomplete' => 'off',
),
'#required' => TRUE,
'#maxlength' => 64,
'#size' => 32,
);
}
return $form;
}