This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
payment.go
100 lines (82 loc) · 2.55 KB
/
payment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package faker
import (
"reflect"
"strconv"
"strings"
"github.com/bxcodec/faker/v4/pkg/options"
)
const (
numberBytes = "0123456789"
)
// creditCard struct
type creditCard struct {
ccType string
length int
prefixes []int
}
var creditCards = map[string]creditCard{
"visa": {"VISA", 16, []int{4539, 4556, 4916, 4532, 4929, 40240071, 4485, 4716, 4}},
"mastercard": {"MasterCard", 16, []int{51, 52, 53, 54, 55}},
"american express": {"American Express", 15, []int{34, 37}},
"discover": {"Discover", 16, []int{6011}},
"jcb": {"JCB", 16, []int{3528, 3538, 3548, 3558, 3568, 3578, 3588}},
"diners club": {"Diners Club", 14, []int{36, 38, 39}},
}
var cacheCreditCard string
// GetPayment returns a new Render interface of Payment struct
func GetPayment() Render {
pay := &Payment{}
return pay
}
// Render contains Whole Random Credit Card Generators with their types
type Render interface {
CreditCardType(v reflect.Value) (interface{}, error)
CreditCardNumber(v reflect.Value) (interface{}, error)
}
// Payment struct
type Payment struct{}
func (p Payment) cctype() string {
n := len(creditCards)
if cacheCreditCard != "" {
return cacheCreditCard
}
var ccTypes []string
for _, cc := range creditCards {
ccTypes = append(ccTypes, cc.ccType)
}
cacheCreditCard = ccTypes[rand.Intn(n)]
return cacheCreditCard
}
// CreditCardType returns one of the following credit values:
// VISA, MasterCard, American Express, Discover, JCB and Diners Club
func (p Payment) CreditCardType(v reflect.Value) (interface{}, error) {
return p.cctype(), nil
}
// CCType get a credit card type randomly in string (VISA, MasterCard, etc)
func CCType(opts ...options.OptionFunc) string {
return singleFakeData(CreditCardType, func() interface{} {
p := Payment{}
return p.cctype()
}, opts...).(string)
}
func (p Payment) ccnumber() string {
ccType := p.cctype()
cacheCreditCard = ccType
card := creditCards[strings.ToLower(ccType)]
prefix := strconv.Itoa(card.prefixes[rand.Intn(len(card.prefixes))])
num := prefix
digit := randomStringNumber(card.length - len(prefix))
num += digit
return num
}
// CreditCardNumber generated credit card number according to the card number rules
func (p Payment) CreditCardNumber(v reflect.Value) (interface{}, error) {
return p.ccnumber(), nil
}
// CCNumber get a credit card number randomly in string (VISA, MasterCard, etc)
func CCNumber(opts ...options.OptionFunc) string {
return singleFakeData(CreditCardNumber, func() interface{} {
p := Payment{}
return p.ccnumber()
}, opts...).(string)
}