github.com/greenpau/go-identity@v1.1.6/credit_card.go (about) 1 // Copyright 2020 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package identity 16 17 import ( 18 "github.com/greenpau/go-identity/pkg/errors" 19 "strings" 20 "time" 21 ) 22 23 // CreditCard represents a credit card. 24 type CreditCard struct { 25 Number string `json:"number,omitempty" xml:"number,omitempty" yaml:"number,omitempty"` 26 Issuer *CreditCardIssuer `json:"issuer,omitempty" xml:"issuer,omitempty" yaml:"issuer,omitempty"` 27 Association *CreditCardAssociation `json:"association,omitempty" xml:"association,omitempty" yaml:"association,omitempty"` 28 Code string `json:"code,omitempty" xml:"code,omitempty" yaml:"code,omitempty"` 29 ExpiresAt time.Time `json:"expires_at,omitempty" xml:"expires_at,omitempty" yaml:"expires_at,omitempty"` 30 IssuedAt time.Time `json:"issued_at,omitempty" xml:"issued_at,omitempty" yaml:"issued_at,omitempty"` 31 } 32 33 // NewCreditCard returns an instance of CreditCard 34 func NewCreditCard() *CreditCard { 35 return &CreditCard{} 36 } 37 38 // AddIssuer adds the name of the issuer, e.g. CitiGroup, CapitalOne, etc. 39 func (cc *CreditCard) AddIssuer(s string) error { 40 41 for _, issuer := range CreditCardIssuers { 42 if s == issuer.Name || s == strings.ToLower(issuer.Name) { 43 cc.Issuer = issuer 44 return nil 45 } 46 for _, alias := range issuer.Aliases { 47 if s == alias || s == strings.ToLower(alias) { 48 cc.Issuer = issuer 49 return nil 50 } 51 } 52 } 53 return errors.ErrCreditCardUnsupportedIssuer.WithArgs(s) 54 } 55 56 // AddAssociation adds the name of the association, e.g. Visa, American 57 // Express, etc., to a credit card 58 func (cc *CreditCard) AddAssociation(s string) error { 59 for _, association := range CreditCardAssociations { 60 if s == association.Name { 61 cc.Association = association 62 return nil 63 } 64 } 65 return errors.ErrCreditCardUnsupportedAssociation.WithArgs(s) 66 }