-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard_tokens.go
54 lines (46 loc) · 1.52 KB
/
card_tokens.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
package mercadopago
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// CardTokenParams represent the params used to create or update a CardToken
type CardTokenParams struct {
ExpirationMonth int `json:"expiration_month"`
ExpirationYear int `json:"expiration_year"`
Cardholder Cardholder `json:"cardholder"`
SecurityCode string `json:"security_code"`
CardNumber string `json:"card_number"`
}
// CardToken contains the information for a token which represents a card
type CardToken struct {
ID string `json:"id"`
PublicKey string `json:"public_key"`
Cardholder Cardholder `json:"cardholder"`
Status string `json:"status"`
DateCreated string `json:"date_created"`
DateLastUpdated string `json:"date_last_updated"`
DateDue string `json:"date_due"`
LuhnValidation bool `json:"luhn_validation"`
LiveMode bool `json:"live_mode"`
RequireEsc bool `json:"require_esc"`
}
// NewCardToken creates a new CardToken
func (c *client) NewCardToken(params CardTokenParams) (*CardToken, error) {
u := fmt.Sprintf("%s/card_tokens?public_key=%s&js_version=%s", c.baseURL, c.publicKey, c.version)
body, err := json.Marshal(params)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, u, bytes.NewReader(body))
if err != nil {
return nil, err
}
var response CardToken
err = c.requestAndDecode(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}