github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/caixa/validations.go (about) 1 package caixa 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/mundipagg/boleto-api/models" 8 "github.com/mundipagg/boleto-api/validations" 9 ) 10 11 func caixaAccountDigitCalculator(agency, account string) string { 12 multiplier := []int{8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2} 13 toCheck := fmt.Sprintf("%04s%011s", agency, account) 14 return caixaModElevenCalculator(toCheck, multiplier) 15 } 16 17 func caixaModElevenCalculator(a string, m []int) string { 18 sum := validations.SumAccountDigits(a, m) 19 digit := (sum * 10) % 11 20 if digit == 10 { 21 return "0" 22 } 23 return strconv.Itoa(digit) 24 } 25 26 func validadeOurNumber(b interface{}) error { 27 switch t := b.(type) { 28 case *models.BoletoRequest: 29 if t.Title.OurNumber > 999999999999999 { 30 return models.NewErrorResponse("MP400", "O nosso número deve conter apenas 15 digitos.") 31 } 32 return nil 33 default: 34 return validations.InvalidType(t) 35 } 36 37 } 38 39 func caixaValidateAccountAndDigit(b interface{}) error { 40 switch t := b.(type) { 41 case *models.BoletoRequest: 42 err := t.Agreement.IsAccountValid(11) 43 if err != nil { 44 return err 45 } 46 errAg := t.Agreement.IsAgencyValid() 47 if errAg != nil { 48 return errAg 49 } 50 t.Agreement.CalculateAccountDigit(caixaAccountDigitCalculator) 51 return nil 52 default: 53 return validations.InvalidType(t) 54 } 55 } 56 57 func caixaAgencyDigitCalculator(agency string) string { 58 multiplier := []int{5, 4, 3, 2} 59 return validations.ModElevenCalculator(agency, multiplier) 60 } 61 62 func caixaValidateAgency(b interface{}) error { 63 switch t := b.(type) { 64 case *models.BoletoRequest: 65 err := t.Agreement.IsAgencyValid() 66 if err != nil { 67 return err 68 } 69 return nil 70 default: 71 return validations.InvalidType(t) 72 } 73 } 74 75 func caixaValidateBoletoType(b interface{}) error { 76 bt := caixaBoletoTypes() 77 78 switch t := b.(type) { 79 80 case *models.BoletoRequest: 81 if len(t.Title.BoletoType) > 0 && bt[t.Title.BoletoType] == "" { 82 return models.NewErrorResponse("MP400", "espécie de boleto informada não existente") 83 } 84 return nil 85 default: 86 return validations.InvalidType(t) 87 } 88 }