creditcard

package module
v0.0.0-...-d355ef6 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2018 License: MIT Imports: 8 Imported by: 0

README

Credit Card

It implements various checks on a credit card details and reduces human error using the Luhn algorithm.

Offers various helpers for data manipulation.

Does risk evaluation using maxMind's minFraud service.

MIT License Go Doc Go Report Card

Installation

go get github.com/lfaoro/creditcard

Quick start

// Create a new CreditCard instance.
card := creditcard.New("Leonardo Faoro", "4444333322221111", "123", "06/2019")

// Create a new CreditCard instance and validate it
// using all the checks implemented so far.
card, err := creditcard.NewValidate("Leonardo Faoro", "4444333322221111", "123", "06/2019")
if err != nil {
    log.Fatal(err)
}

// Validate a CreditCard using all the checks implemented so far.
if err := card.Validate(); err != nil {
    log.Fatal(err)
}

// Luhn checksum a number. https://en.wikipedia.org/wiki/Luhn_algorithm
if yes := creditcard.Luhn("4444333322221111"); !yes {
    log.Println("Luhn validation failed.")
}

// Encrypt the CreditCard.Number and get the bytes blob.
b := card.Encrypt("mySuperSecureSalt")

// Common helpers.
fmt.Println(card.First6()) // 123456
fmt.Println(card.Last4()) // 4567
fmt.Println(card.Issuer()) // other, visa, amex, ...
fmt.Println(card.ToJSON()) // {"name":"Leonardo Faoro","number":"4444333322221111","cvv_2":"123","expiry":"06/2019"}

Maxmind's minFraud (https://www.maxmind.com/en/minfraud-services)

minFraud uses a combination of data points across over a billion transactions to provide an accurate risk score for every credit or debit card payment you process. Several variables go into the assessment and scoring, including geolocation, IIN information, reputation checks, IP checks, and device tracking.

Benefits
  • Significantly reduce or eliminate the number of fraudulent transactions your business processes.
  • Significantly reduce or eliminate chargebacks from customers (e.g. if they have been the victim of identity theft and their card has been used in a criminal way).
Quick start
export MAXMIND_USER="123456"
export MAXMIND_PASSWORD="maxMindServicePassword"
export MAXMIND_ENDPOINT="https://minfraud.maxmind.com/minfraud/v2.0/insights"
# NB: alternatively provide a .env file in your app with the above variables.
riskScore, err := maxmind.RiskCheck(card.First6(), card.Last4(), "8.8.8.8", "test@test.com")
if err != nil {
    log.Println("riskCheck error: ", err)
}
fmt.Println(riskScore)

Contibuting

Any help and suggestions are very welcome and appreciated.

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

Examples

Constants

View Source
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

func Luhn(number string) bool

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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