-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
97 lines (78 loc) · 2.22 KB
/
card.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
package getnet
import (
"encoding/json"
"errors"
)
var errNumberToken = errors.New("Obrigatório informar o número do cartão tokenizado. Gerado previamente por meio do endpoint /v1/tokens/card.")
type Brand string
const (
endpointTokenCard = "/v1/tokens/card"
endpointCardVerification = "/v1/cards/verification"
Mastercard Brand = "Mastercard"
Visa Brand = "Visa"
Amex Brand = "Amex"
Elo Brand = "Elo"
Hipercard Brand = "Hipercard"
Verified = "VERIFIED"
NotVerified = "NOT VERIFIED"
Denied = "DENIED"
Error = "ERROR"
)
type Card struct {
CardNumber string `json:"-"`
NumberToken string `json:"number_token"`
Brand Brand `json:"brand"`
CardHolderName string `json:"cardholder_name"`
ExpirationMonth string `json:"expiration_month"`
ExpirationYear string `json:"expiration_year"`
SecurityCode string `json:"security_code"`
CustomerID string `json:"-"`
}
func (c Card) Token(cc ClientCredentials) (string, error) {
payload := struct {
CardNumber string `json:"card_number"`
CustomerID string `json:"customer_id,omitempty"`
}{
CardNumber: c.CardNumber,
CustomerID: c.CustomerID,
}
res, err := NewRestClient(cc).Post(endpointTokenCard, payload)
if err != nil {
return "", err
}
var token Token
err = json.Unmarshal(res.Body, &token)
return token.NumberToken, err
}
func (c Card) Verify(cc ClientCredentials) (Verification, error) {
if c.NumberToken == "" {
return Verification{}, errNumberToken
}
if c.Brand != Mastercard && c.Brand != Visa {
return Verification{Status: NotVerified}, nil
}
res, err := NewRestClient(cc).Post(endpointCardVerification, c)
if err != nil {
return Verification{}, err
}
var ver Verification
err = json.Unmarshal(res.Body, &ver)
return ver, err
}
type Token struct {
NumberToken string `json:"number_token"`
}
type Verification struct {
Status string `json:"status"`
VerificationID string `json:"verification_id"`
AuthorizationCode string `json:"authorization_code"`
}
func (v Verification) Verified() bool {
return v.Status == Verified
}
func (v Verification) NotVerified() bool {
return v.Status == NotVerified
}
func (v Verification) Denied() bool {
return v.Status == Denied
}