forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/brianvoe/gofakeit/v6/payment.go

711 lines
14 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package gofakeit
import (
"math"
"math/rand"
"strconv"
"strings"
"time"
"github.com/brianvoe/gofakeit/v6/data"
)
// CurrencyInfo is a struct of currency information
2024-04-26 19:30:35 +00:00
type CurrencyInfo struct {
Short string `json:"short" xml:"short"`
Long string `json:"long" xml:"long"`
2024-04-26 19:30:35 +00:00
}
// Currency will generate a struct with random currency information
2024-04-26 19:30:35 +00:00
func Currency() *CurrencyInfo { return currency(globalFaker.Rand) }
// Currency will generate a struct with random currency information
2024-04-26 19:30:35 +00:00
func (f *Faker) Currency() *CurrencyInfo { return currency(f.Rand) }
func currency(r *rand.Rand) *CurrencyInfo {
2024-04-26 19:30:35 +00:00
index := r.Intn(len(data.Data["currency"]["short"]))
2024-04-26 19:30:35 +00:00
return &CurrencyInfo{
2024-04-26 19:30:35 +00:00
Short: data.Data["currency"]["short"][index],
Long: data.Data["currency"]["long"][index],
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
// CurrencyShort will generate a random short currency value
2024-04-26 19:30:35 +00:00
func CurrencyShort() string { return currencyShort(globalFaker.Rand) }
// CurrencyShort will generate a random short currency value
2024-04-26 19:30:35 +00:00
func (f *Faker) CurrencyShort() string { return currencyShort(f.Rand) }
func currencyShort(r *rand.Rand) string { return getRandValue(r, []string{"currency", "short"}) }
// CurrencyLong will generate a random long currency name
2024-04-26 19:30:35 +00:00
func CurrencyLong() string { return currencyLong(globalFaker.Rand) }
// CurrencyLong will generate a random long currency name
2024-04-26 19:30:35 +00:00
func (f *Faker) CurrencyLong() string { return currencyLong(f.Rand) }
func currencyLong(r *rand.Rand) string { return getRandValue(r, []string{"currency", "long"}) }
// Price will take in a min and max value and return a formatted price
2024-04-26 19:30:35 +00:00
func Price(min, max float64) float64 { return price(globalFaker.Rand, min, max) }
// Price will take in a min and max value and return a formatted price
2024-04-26 19:30:35 +00:00
func (f *Faker) Price(min, max float64) float64 { return price(f.Rand, min, max) }
func price(r *rand.Rand, min, max float64) float64 {
2024-04-26 19:30:35 +00:00
return math.Floor(float64Range(r, min, max)*100) / 100
2024-04-26 19:30:35 +00:00
}
// CreditCardInfo is a struct containing credit variables
2024-04-26 19:30:35 +00:00
type CreditCardInfo struct {
Type string `json:"type" xml:"type"`
2024-04-26 19:30:35 +00:00
Number string `json:"number" xml:"number"`
Exp string `json:"exp" xml:"exp"`
Cvv string `json:"cvv" xml:"cvv"`
2024-04-26 19:30:35 +00:00
}
// CreditCard will generate a struct full of credit card information
2024-04-26 19:30:35 +00:00
func CreditCard() *CreditCardInfo { return creditCard(globalFaker.Rand) }
// CreditCard will generate a struct full of credit card information
2024-04-26 19:30:35 +00:00
func (f *Faker) CreditCard() *CreditCardInfo { return creditCard(f.Rand) }
func creditCard(r *rand.Rand) *CreditCardInfo {
2024-04-26 19:30:35 +00:00
ccType := randomString(r, data.CreditCardTypes)
2024-04-26 19:30:35 +00:00
return &CreditCardInfo{
Type: data.CreditCards[randomString(r, data.CreditCardTypes)].Display,
2024-04-26 19:30:35 +00:00
Number: creditCardNumber(r, &CreditCardOptions{Types: []string{ccType}}),
Exp: creditCardExp(r),
Cvv: generate(r, strings.Repeat("#", int(data.CreditCards[randomString(r, data.CreditCardTypes)].Code.Size))),
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
// CreditCardType will generate a random credit card type string
2024-04-26 19:30:35 +00:00
func CreditCardType() string { return creditCardType(globalFaker.Rand) }
// CreditCardType will generate a random credit card type string
2024-04-26 19:30:35 +00:00
func (f *Faker) CreditCardType() string { return creditCardType(f.Rand) }
func creditCardType(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return data.CreditCards[randomString(r, data.CreditCardTypes)].Display
2024-04-26 19:30:35 +00:00
}
// CreditCardOptions is the options for credit card number
2024-04-26 19:30:35 +00:00
type CreditCardOptions struct {
Types []string `json:"types"`
Bins []string `json:"bins"` // optional parameter of prepended numbers
Gaps bool `json:"gaps"`
2024-04-26 19:30:35 +00:00
}
// CreditCardNumber will generate a random luhn credit card number
2024-04-26 19:30:35 +00:00
func CreditCardNumber(cco *CreditCardOptions) string { return creditCardNumber(globalFaker.Rand, cco) }
// CreditCardNumber will generate a random luhn credit card number
2024-04-26 19:30:35 +00:00
func (f *Faker) CreditCardNumber(cco *CreditCardOptions) string { return creditCardNumber(f.Rand, cco) }
func creditCardNumber(r *rand.Rand, cco *CreditCardOptions) string {
2024-04-26 19:30:35 +00:00
if cco == nil {
2024-04-26 19:30:35 +00:00
cco = &CreditCardOptions{}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if cco.Types == nil || len(cco.Types) == 0 {
2024-04-26 19:30:35 +00:00
cco.Types = data.CreditCardTypes
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
ccType := randomString(r, cco.Types)
// Get Card info
2024-04-26 19:30:35 +00:00
var cardInfo data.CreditCardInfo
2024-04-26 19:30:35 +00:00
if info, ok := data.CreditCards[ccType]; ok {
2024-04-26 19:30:35 +00:00
cardInfo = info
2024-04-26 19:30:35 +00:00
} else {
2024-04-26 19:30:35 +00:00
ccType = randomString(r, data.CreditCardTypes)
2024-04-26 19:30:35 +00:00
cardInfo = data.CreditCards[ccType]
2024-04-26 19:30:35 +00:00
}
// Get length and pattern
2024-04-26 19:30:35 +00:00
length := randomUint(r, cardInfo.Lengths)
2024-04-26 19:30:35 +00:00
numStr := ""
2024-04-26 19:30:35 +00:00
if len(cco.Bins) >= 1 {
2024-04-26 19:30:35 +00:00
numStr = randomString(r, cco.Bins)
2024-04-26 19:30:35 +00:00
} else {
2024-04-26 19:30:35 +00:00
numStr = strconv.FormatUint(uint64(randomUint(r, cardInfo.Patterns)), 10)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
numStr += strings.Repeat("#", int(length)-len(numStr))
2024-04-26 19:30:35 +00:00
numStr = numerify(r, numStr)
2024-04-26 19:30:35 +00:00
ui, _ := strconv.ParseUint(numStr, 10, 64)
// Loop through until its a valid luhn
2024-04-26 19:30:35 +00:00
for {
2024-04-26 19:30:35 +00:00
valid := isLuhn(strconv.FormatUint(ui, 10))
2024-04-26 19:30:35 +00:00
if valid {
2024-04-26 19:30:35 +00:00
break
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
ui++
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
numStr = strconv.FormatUint(ui, 10)
// Add gaps to number
2024-04-26 19:30:35 +00:00
if cco.Gaps {
2024-04-26 19:30:35 +00:00
for i, spot := range cardInfo.Gaps {
2024-04-26 19:30:35 +00:00
numStr = numStr[:(int(spot)+i)] + " " + numStr[(int(spot)+i):]
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
return numStr
2024-04-26 19:30:35 +00:00
}
// CreditCardExp will generate a random credit card expiration date string
2024-04-26 19:30:35 +00:00
// Exp date will always be a future date
2024-04-26 19:30:35 +00:00
func CreditCardExp() string { return creditCardExp(globalFaker.Rand) }
// CreditCardExp will generate a random credit card expiration date string
2024-04-26 19:30:35 +00:00
// Exp date will always be a future date
2024-04-26 19:30:35 +00:00
func (f *Faker) CreditCardExp() string { return creditCardExp(f.Rand) }
func creditCardExp(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
month := strconv.Itoa(randIntRange(r, 1, 12))
2024-04-26 19:30:35 +00:00
if len(month) == 1 {
2024-04-26 19:30:35 +00:00
month = "0" + month
2024-04-26 19:30:35 +00:00
}
var currentYear = time.Now().Year() - 2000
2024-04-26 19:30:35 +00:00
return month + "/" + strconv.Itoa(randIntRange(r, currentYear+1, currentYear+10))
2024-04-26 19:30:35 +00:00
}
// CreditCardCvv will generate a random CVV number
2024-04-26 19:30:35 +00:00
// Its a string because you could have 017 as an exp date
2024-04-26 19:30:35 +00:00
func CreditCardCvv() string { return creditCardCvv(globalFaker.Rand) }
// CreditCardCvv will generate a random CVV number
2024-04-26 19:30:35 +00:00
// Its a string because you could have 017 as an exp date
2024-04-26 19:30:35 +00:00
func (f *Faker) CreditCardCvv() string { return creditCardCvv(f.Rand) }
func creditCardCvv(r *rand.Rand) string { return numerify(r, "###") }
// isLuhn check is used for checking if credit card is a valid luhn card
2024-04-26 19:30:35 +00:00
func isLuhn(s string) bool {
2024-04-26 19:30:35 +00:00
var t = [...]int{0, 2, 4, 6, 8, 1, 3, 5, 7, 9}
2024-04-26 19:30:35 +00:00
odd := len(s) & 1
2024-04-26 19:30:35 +00:00
var sum int
2024-04-26 19:30:35 +00:00
for i, c := range s {
2024-04-26 19:30:35 +00:00
if c < '0' || c > '9' {
2024-04-26 19:30:35 +00:00
return false
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if i&1 == odd {
2024-04-26 19:30:35 +00:00
sum += t[c-'0']
2024-04-26 19:30:35 +00:00
} else {
2024-04-26 19:30:35 +00:00
sum += int(c - '0')
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return sum%10 == 0
2024-04-26 19:30:35 +00:00
}
// AchRouting will generate a 9 digit routing number
2024-04-26 19:30:35 +00:00
func AchRouting() string { return achRouting(globalFaker.Rand) }
// AchRouting will generate a 9 digit routing number
2024-04-26 19:30:35 +00:00
func (f *Faker) AchRouting() string { return achRouting(f.Rand) }
func achRouting(r *rand.Rand) string { return numerify(r, "#########") }
// AchAccount will generate a 12 digit account number
2024-04-26 19:30:35 +00:00
func AchAccount() string { return achAccount(globalFaker.Rand) }
// AchAccount will generate a 12 digit account number
2024-04-26 19:30:35 +00:00
func (f *Faker) AchAccount() string { return achAccount(f.Rand) }
func achAccount(r *rand.Rand) string { return numerify(r, "############") }
// BitcoinAddress will generate a random bitcoin address consisting of numbers, upper and lower characters
2024-04-26 19:30:35 +00:00
func BitcoinAddress() string { return bitcoinAddress(globalFaker.Rand) }
// BitcoinAddress will generate a random bitcoin address consisting of numbers, upper and lower characters
2024-04-26 19:30:35 +00:00
func (f *Faker) BitcoinAddress() string { return bitcoinAddress(f.Rand) }
func bitcoinAddress(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return randomString(r, []string{"1", "3"}) + password(r, true, true, true, false, false, number(r, 25, 34))
2024-04-26 19:30:35 +00:00
}
// BitcoinPrivateKey will generate a random bitcoin private key base58 consisting of numbers, upper and lower characters
2024-04-26 19:30:35 +00:00
func BitcoinPrivateKey() string { return bitcoinPrivateKey(globalFaker.Rand) }
// BitcoinPrivateKey will generate a random bitcoin private key base58 consisting of numbers, upper and lower characters
2024-04-26 19:30:35 +00:00
func (f *Faker) BitcoinPrivateKey() string { return bitcoinPrivateKey(f.Rand) }
func bitcoinPrivateKey(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
var b strings.Builder
2024-04-26 19:30:35 +00:00
for i := 0; i < 49; i++ {
2024-04-26 19:30:35 +00:00
b.WriteString(randCharacter(r, base58))
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return "5" + randomString(r, []string{"H", "J", "K"}) + b.String()
2024-04-26 19:30:35 +00:00
}
func addPaymentLookup() {
2024-04-26 19:30:35 +00:00
AddFuncLookup("currency", Info{
Display: "Currency",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Medium of exchange, often in the form of paper money or coins, used for trade and transactions",
2024-04-26 19:30:35 +00:00
Example: `{
2024-04-26 19:30:35 +00:00
"short": "IQD",
2024-04-26 19:30:35 +00:00
"long": "Iraq Dinar"
2024-04-26 19:30:35 +00:00
}`,
Output: "map[string]string",
2024-04-26 19:30:35 +00:00
ContentType: "application/json",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return currency(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("currencyshort", Info{
Display: "Currency Short",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Short 3-letter word used to represent a specific currency",
Example: "USD",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return currencyShort(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("currencylong", Info{
Display: "Currency Long",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Complete name of a specific currency used for official identification in financial transactions",
Example: "United States Dollar",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return currencyLong(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("price", Info{
Display: "Price",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "The amount of money or value assigned to a product, service, or asset in a transaction",
Example: "92.26",
Output: "float64",
2024-04-26 19:30:35 +00:00
Params: []Param{
2024-04-26 19:30:35 +00:00
{Field: "min", Display: "Min", Type: "float", Default: "0", Description: "Minimum price value"},
2024-04-26 19:30:35 +00:00
{Field: "max", Display: "Max", Type: "float", Default: "1000", Description: "Maximum price value"},
},
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
min, err := info.GetFloat64(m, "min")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
max, err := info.GetFloat64(m, "max")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
return price(r, min, max), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("creditcard", Info{
Display: "Credit Card",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Plastic card allowing users to make purchases on credit, with payment due at a later date",
2024-04-26 19:30:35 +00:00
Example: `{
2024-04-26 19:30:35 +00:00
"type": "UnionPay",
2024-04-26 19:30:35 +00:00
"number": "4364599489953698",
2024-04-26 19:30:35 +00:00
"exp": "02/24",
2024-04-26 19:30:35 +00:00
"cvv": "300"
2024-04-26 19:30:35 +00:00
}`,
Output: "map[string]any",
2024-04-26 19:30:35 +00:00
ContentType: "application/json",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return creditCard(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("creditcardtype", Info{
Display: "Credit Card Type",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Classification of credit cards based on the issuing company",
Example: "Visa",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return creditCardType(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("creditcardnumber", Info{
Display: "Credit Card Number",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Unique numerical identifier on a credit card used for making electronic payments and transactions",
Example: "4136459948995369",
Output: "string",
2024-04-26 19:30:35 +00:00
Params: []Param{
2024-04-26 19:30:35 +00:00
{
2024-04-26 19:30:35 +00:00
Field: "types", Display: "Types", Type: "[]string", Default: "all",
Options: []string{"visa", "mastercard", "american-express", "diners-club", "discover", "jcb", "unionpay", "maestro", "elo", "hiper", "hipercard"},
2024-04-26 19:30:35 +00:00
Description: "A select number of types you want to use when generating a credit card number",
},
2024-04-26 19:30:35 +00:00
{Field: "bins", Display: "Bins", Type: "[]string", Optional: true, Description: "Optional list of prepended bin numbers to pick from"},
2024-04-26 19:30:35 +00:00
{Field: "gaps", Display: "Gaps", Type: "bool", Default: "false", Description: "Whether or not to have gaps in number"},
},
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
types, err := info.GetStringArray(m, "types")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if len(types) == 1 && types[0] == "all" {
2024-04-26 19:30:35 +00:00
types = []string{}
2024-04-26 19:30:35 +00:00
}
bins, _ := info.GetStringArray(m, "bins")
gaps, err := info.GetBool(m, "gaps")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
options := CreditCardOptions{
2024-04-26 19:30:35 +00:00
Types: types,
Gaps: gaps,
2024-04-26 19:30:35 +00:00
}
if len(bins) >= 1 {
2024-04-26 19:30:35 +00:00
options.Bins = bins
2024-04-26 19:30:35 +00:00
}
return creditCardNumber(r, &options), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("creditcardexp", Info{
Display: "Credit Card Exp",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Date when a credit card becomes invalid and cannot be used for transactions",
Example: "01/21",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return creditCardExp(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("creditcardcvv", Info{
Display: "Credit Card CVV",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Three or four-digit security code on a credit card used for online and remote transactions",
Example: "513",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return creditCardCvv(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("achrouting", Info{
Display: "ACH Routing Number",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Unique nine-digit code used in the U.S. for identifying the bank and processing electronic transactions",
Example: "513715684",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return achRouting(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("achaccount", Info{
Display: "ACH Account Number",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "A bank account number used for Automated Clearing House transactions and electronic transfers",
Example: "491527954328",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return achAccount(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("bitcoinaddress", Info{
Display: "Bitcoin Address",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Cryptographic identifier used to receive, store, and send Bitcoin cryptocurrency in a peer-to-peer network",
Example: "1lWLbxojXq6BqWX7X60VkcDIvYA",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return bitcoinAddress(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("bitcoinprivatekey", Info{
Display: "Bitcoin Private Key",
Category: "payment",
2024-04-26 19:30:35 +00:00
Description: "Secret, secure code that allows the owner to access and control their Bitcoin holdings",
Example: "5vrbXTADWJ6sQBSYd6lLkG97jljNc0X9VPBvbVqsIH9lWOLcoqg",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return bitcoinPrivateKey(r), nil
2024-04-26 19:30:35 +00:00
},
})
2024-04-26 19:30:35 +00:00
}