62 lines
853 B
Go
62 lines
853 B
Go
package validate
|
|
|
|
import (
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Card struct {
|
|
Pan string
|
|
Month string
|
|
Year string
|
|
}
|
|
|
|
type ValidationResult struct {
|
|
Code int
|
|
Error error
|
|
}
|
|
|
|
func Luhn(pan string) bool {
|
|
if pan == "" {
|
|
return false
|
|
}
|
|
|
|
lastIndex := len(pan) - 1
|
|
|
|
asDigits := []int{}
|
|
|
|
for _, char := range strings.Split(pan, "") {
|
|
digit, err := strconv.Atoi(char)
|
|
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
asDigits = append(asDigits, digit)
|
|
}
|
|
|
|
sum := 0
|
|
|
|
// do we really need reversed array?
|
|
for i, digit := range slices.Backward(asDigits[:lastIndex]) {
|
|
|
|
if i%2 == len(pan)%2 {
|
|
if digit > 4 {
|
|
sum += 2*digit - 9
|
|
} else {
|
|
sum += 2 * digit
|
|
}
|
|
} else {
|
|
sum += digit
|
|
}
|
|
|
|
}
|
|
|
|
return asDigits[lastIndex] == (10-(sum%10))%10
|
|
}
|
|
|
|
func Validate(card Card) (bool, ValidationResult) {
|
|
|
|
return true, ValidationResult{}
|
|
}
|