Validators

terminusgps.authorizenet.validators.validate_credit_card_expiry_month(value: str) None[source]

Raises ValidationError if the value is an invalid credit card expiration date month.

Parameters:

value (str) – A credit card expiration year string.

Raises:
  • ValidationError – If the value contains non-digit characters.

  • ValidationError – If the value is negative.

  • ValidationError – If the value isn’t between 1-12.

Returns:

Nothing.

Return type:

None

terminusgps.authorizenet.validators.validate_credit_card_expiry_year(value: str) None[source]

Raises ValidationError if the value is an invalid credit card expiration date year.

Parameters:

value (str) – A credit card expiration year string.

Raises:
  • ValidationError – If the value contains non-digit characters.

  • ValidationError – If the value is negative.

  • ValidationError – If the value is a year in the past.

Returns:

Nothing.

Return type:

None

terminusgps.authorizenet.validators.validate_credit_card_number(value: str) None[source]

Raises ValidationError if the value is an invalid credit card number.

Uses the Luhn algorithm to validate the credit card number.

Parameters:

value (str) – A credit card number string.

Raises:
  • ValidationError – If the value contains non-digit characters.

  • ValidationError – If the value fails the Luhn algorithm.

Returns:

Nothing.

Return type:

None

Usage

from django import forms

from terminusgps.authorizenet.validators import (
    validate_credit_card_number,
    validate_credit_card_expiry_month,
    validate_credit_card_expiry_year,
)

class CreditCardForm(forms.Form):
    # Adding the validators to a form field renders error messages properly
    cc_number = forms.CharField(
        max_length=17, validators=[validate_credit_card_number]
    )
    cc_expiry_month = forms.CharField(
        max_length=2, validators=[validate_credit_card_expiry_month]
    )
    cc_expiry_year = forms.CharField(
        max_length=2, validators=[validate_credit_card_expiry_year]
    )