creditcard

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2019 License: MIT Imports: 3 Imported by: 3

README

creditcard

Go Report Card GitHub tag (latest by date) GitHub

A Go module to perform basic credit card validation.

Installation

go get github.com/retgits/creditcard

Usage

Create a creditcard.Card struct and use the Validate() method to perform validation

package main

import (
    "fmt"
    "github.com/retgits/creditcard"
)

func main() {
    card := creditcard.Card{
        Type:        "Something",
        Number:      "5019717010103742",
        ExpiryMonth: 11,
        ExpiryYear:  2019,
        CVV:         "1234",
    }
    validation := card.Validate()
    fmt.Printf("%+v\n", validation)
    fmt.Printf("%+v\n", validation.Card)
    // This prints
    // &{Card:0xc000092040 ValidCardNumber:false ValidExpiryMonth:true ValidExpiryYear:true ValidCVV:false IsExpired:false Errors:[given card type doesn't match determined card type]}
    // &{Type:Something Number:5019717010103742 ExpiryMonth:11 ExpiryYear:2019 CVV:1234}
}

The sample here shows that the card's supplied type "Something" doesn't match what the type actually should be.

Supported Credit Card Types

This module supports a variety of credit cards:

  • American Express
  • Aura
  • Bankcard
  • Cabal
  • China UnionPay
  • Dankort
  • Diners Club Carte Blanche
  • Diners Club Enroute
  • Diners Club International
  • Discover
  • Elo
  • Hipercard
  • InstaPayment
  • InterPayment
  • JCB
  • Maestro
  • Mastercard
  • Visa
  • Visa Electron

Test Card Numbers

A list of test credit cards is available from PayPal.

LICENSE

This module is provided under the MIT license

Acknowledgements

This lib is built as a Go equivalent to the Node.js credit-card library and the amazing work by HubCash

Documentation

Overview

Package creditcard is a module that performs credit card validation.

Index

Constants

View Source
const (
	// Unknown card type
	Unknown cardType = iota
	// AmericanExpress  card type
	AmericanExpress
	// Aura card type
	Aura
	// Bankcard card type
	Bankcard
	// Cabal card type
	Cabal
	// ChinaUnionPay card type
	ChinaUnionPay
	// Dankort card type
	Dankort
	// DinersClubCarteBlanche card type
	DinersClubCarteBlanche
	// DinersClubEnroute card type
	DinersClubEnroute
	// DinersClubInternational card type
	DinersClubInternational
	// Discover card type
	Discover
	// Elo card type
	Elo
	// Hipercard card type
	Hipercard
	// InstaPayment card type
	InstaPayment
	// InterPayment card type
	InterPayment
	// JCB card type
	JCB
	// Maestro card type
	Maestro
	// Mastercard card type
	Mastercard
	// Visa card type
	Visa
	// VisaElectron card type
	VisaElectron
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Card

type Card struct {
	// Type is an optional string with one of the supported card types. If the type is supplied, it will be validated that the type matches the number
	Type string
	// Number is the credit card number
	Number string
	// ExpiryMonth is the credit card expiration month
	ExpiryMonth int
	// ExpiryYear is the credit card expiration year
	ExpiryYear int
	// CVV is the credit card CVV code
	CVV string
}

Card is a struct that contains a credit card type. This holds generic information about the credit card

func (*Card) Validate

func (c *Card) Validate() *Validation

Validate performs validation on the card. Apart from a copy of the card, it also returns - ValidCardNumber is a boolean that indicates if a credit card number is valid for a given credit card type if given and verifies that the credit card number passes the Luhn algorithm - ValidExpiryMonth is a boolean that indicates if a value is a valid credit card expiry month (the range is 1 to 12) - ValidExpiryYear is a boolean that indicates if a value is a valid credit card expiry year (the range is 1900 to 2200) - ValidCVV is a boolean that indicates if a CVV is valid for a given credit card type. For example, American Express requires a four digit CVV, while Visa and Mastercard require a three digit CVV - IsExpired is a boolean that indicates if a credit card's expiration date has been reached - Errors is an array of validation errors that might occur during validation

type Validation

type Validation struct {
	// A pointer to the struct that was passed in for validation
	Card *Card
	// ValidCardNumber is a boolean that indicates if a credit card number is valid for a given credit card type if given and verifies that the credit card number passes the Luhn algorithm
	ValidCardNumber bool
	// ValidExpiryMonth is a boolean that indicates if a value is a valid credit card expiry month (the range is 1 to 12)
	ValidExpiryMonth bool
	// ValidExpiryYear is a boolean that indicates if a value is a valid credit card expiry year (the range is 1900 to 2200)
	ValidExpiryYear bool
	// ValidCVV is a boolean that indicates if a CVV is valid for a given credit card type. For example, American Express requires a four digit CVV, while Visa and Mastercard require a three digit CVV
	ValidCVV bool
	// IsExpired is a boolean that indicates if a credit card's expiration date has been reached
	IsExpired bool
	// Errors is an array of validation errors that might occur during validation
	Errors []string
}

Validation is the object returned by the validate method, which contains the validation result of the card

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL