Creditfield is a small proof of concept module that provides 3 FormAPI elements to be used in the Drupal Form API for custom forms:

  • Credit Card Number
  • Credit Card Expiration Date
  • Credit Card CVV

These fields provide basic validation and form errors on their own (including Luhn check against card number). You could easily add more within your own form validation callbacks as well.

To use them in your custom Drupal form, simply define a form item like so:

Drupal 7

$form['credit_card_number'] = array(
  '#type' => 'creditfield_cardnumber',
  '#title' => 'Credit Card Number',
  '#maxlength' => 16,
);
	
$form['expiration_date'] = array(
  '#type' => 'creditfield_date',
  '#title' => 'Expiration Date',
);

$form['credit_card_cvv'] = array(
  '#type' => 'creditfield_cvv',
  '#title' => 'CVV Code',
  '#maxlength' => 4,
  '#description' => 'Your 3 or 4 digit security code on the back of your card.',
);

Drupal 8

$form['credit_card_number'] = array(
  '#type' => 'creditfield_cardnumber',
  '#title' => 'Credit Card Number',
  '#maxlength' => 16,
);
	
$form['expiration_date'] = array(
  '#type' => 'creditfield_expiration',
  '#title' => 'Expiration Date',
);

$form['credit_card_cvv'] = array(
  '#type' => 'creditfield_cardcode',
  '#title' => 'CVV Code',
  '#maxlength' => 4,
  '#description' => 'Your 3 or 4 digit security code on the back of your card.',
);

The Drupal 8 version has some changes to accommodate the new API and some refactoring of the validation so it can be unit tested. Basic data validation unit tests are in the Drupal 8 version.

Where to use Creditfield

Creditfield aims to provide a simple way of collecting credit card fields and validating the input, such as a 'online payment' or 'billpay' form. This does not submit the payment information to any gateway for you- that is up to you in your form processing. If you build a lot of custom payment forms, you might find this useful. If you are looking for a more robust solution, you might want to look into the Pay module.

Live Examples

Here are a few live running examples of Creditfield used in custom payment forms with simple modules implementing the FormAPI elements:

Supporting organizations: 
Developed and sponsored by

Project Information

Downloads