Documentation ¶
Overview ¶
Package creditcard provides structure and validation for Credit Cards.
It implements various checks on a credit card details and reduces human error using the Luhn algorithm.
Does risk evaluation using maxMind's minFraud service.
Index ¶
- Constants
- func Luhn(number string) bool
- type CreditCard
- func (card *CreditCard) Decrypt(data []byte, salt string) bool
- func (card *CreditCard) Encrypt(salt string) []byte
- func (card *CreditCard) First6() string
- func (card *CreditCard) Issuer() string
- func (card *CreditCard) Last4() string
- func (card *CreditCard) ToJSON() string
- func (card *CreditCard) Validate() error
Examples ¶
Constants ¶
const ( Visa = "visa" MasterCard = "mastercard" Amex = "american express" Diners = "diners" Discover = "discover" JCB = "jcb" Other = "other" )
Issuers list.
Variables ¶
This section is empty.
Functions ¶
func Luhn ¶
Luhn implements the Luhn checksum formula that validates identification numbers. It was designed to protect against accidental errors, not malicious attacks.
https://en.wikipedia.org/wiki/Luhn_algorithm
TODO: - refactor - benchmark
Example ¶
fmt.Println(Luhn("7992739871"))
Output: true
Types ¶
type CreditCard ¶
type CreditCard struct { Name string `json:"name,omitempty"` Number string `json:"number,omitempty"` CVV2 string `json:"cvv_2,omitempty"` Expiry string `json:"expiry,omitempty"` }
CreditCard stores credit card information.
func FromJSON ¶
func FromJSON(data []byte) (card *CreditCard, err error)
FromJSON returns a CreditCard from a JSON data bytes.
JSON data example: {"name":"Leonardo Faoro","number":"1234567891234567","cvv_2":"123","expiry":"06/2019"}
func New ¶
func New(name, number, cvv2, expiry string) *CreditCard
New creates a new CreditCard instance.
Example ¶
card := New("Leonardo Faoro", "4444333322221111", "123", "06/2019") fmt.Println(card.Number)
Output: 4444333322221111
func NewValidate ¶
func NewValidate(name, number, cvv2, expiry string) (*CreditCard, error)
NewValidate creates a new CreditCard instance and validates it using all the checks implemented so far.
func (*CreditCard) Decrypt ¶
func (card *CreditCard) Decrypt(data []byte, salt string) bool
Decrypt the provided data and match it against the creditcard.Number. NOT IMPLEMENTED
func (*CreditCard) Encrypt ¶
func (card *CreditCard) Encrypt(salt string) []byte
Encrypt the Credit Card number using the industry standard and returns a salted SHA512 hash.
Example ¶
card := New("Leonardo Faoro", "4444333322221111", "123", "06/2019") fmt.Println(card.Encrypt("myAwesomeSalt"))
Output: [89 210 45 55 151 142 210 59 208 84 108 251 121 255 44 227 92 245 235 112 38 44 112 212 86 246 207 25 19 117 101 160 91 78 132 95 204 6 138 75 122 66 224 206 140 178 17 119 183 153 113 204 119 53 105 224 109 186 40 76 83 198 120 9]
func (*CreditCard) First6 ¶
func (card *CreditCard) First6() string
First6 returns a string with the first 6 digits of the CreditCard.Number.
Example ¶
card := New("Leonardo Faoro", "4444333322221111", "123", "06/2019") fmt.Println(card.First6())
Output: 444433
func (*CreditCard) Issuer ¶
func (card *CreditCard) Issuer() string
Issuer attempts to identify the Credit Card issuer by recognizing their numeric patterns.
Example ¶
card := New("Leonardo Faoro", "4444333322221111", "123", "06/2019") fmt.Println(card.Issuer())
Output: visa
func (*CreditCard) Last4 ¶
func (card *CreditCard) Last4() string
Last4 returns a string with the last 4 digits of the CreditCard.Number.
Example ¶
card := New("Leonardo Faoro", "4444333322221111", "123", "06/2019") fmt.Println(card.Last4())
Output: 1111
func (*CreditCard) ToJSON ¶
func (card *CreditCard) ToJSON() string
ToJSON returns a string with the CreditCard json marshalled data.
Example ¶
card := New("Leonardo Faoro", "4444333322221111", "123", "06/2019") fmt.Println(card.ToJSON())
Output: {"name":"Leonardo Faoro","number":"4444333322221111","cvv_2":"123","expiry":"06/2019"}
func (*CreditCard) Validate ¶
func (card *CreditCard) Validate() error
Validate implements various checks to ensure a credit card is valid.