forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_token_gateway.go
55 lines (48 loc) · 1.41 KB
/
client_token_gateway.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
package braintree
import (
"context"
"encoding/xml"
)
const clientTokenVersion = 2
// ClientTokenGateway represents the provider client token generation.
type ClientTokenGateway struct {
*Braintree
}
// Generate generates a new client token.
func (g *ClientTokenGateway) Generate(ctx context.Context) (string, error) {
return g.generate(ctx, &ClientTokenRequest{
Version: clientTokenVersion,
})
}
// GenerateWithCustomer generates a new client token for the customer id.
func (g *ClientTokenGateway) GenerateWithCustomer(ctx context.Context, customerID string) (string, error) {
return g.generate(ctx, &ClientTokenRequest{
Version: clientTokenVersion,
CustomerID: customerID,
})
}
// GenerateWithRequest generates a new client token using custom request options.
func (g *ClientTokenGateway) GenerateWithRequest(ctx context.Context, req *ClientTokenRequest) (string, error) {
if req == nil {
req = &ClientTokenRequest{}
}
if req.Version == 0 {
req.Version = clientTokenVersion
}
return g.generate(ctx, req)
}
func (g *ClientTokenGateway) generate(ctx context.Context, req *ClientTokenRequest) (string, error) {
resp, err := g.execute(ctx, "POST", "client_token", req)
if err != nil {
return "", err
}
switch resp.StatusCode {
case 201:
var b clientToken
if err := xml.Unmarshal(resp.Body, &b); err != nil {
return "", err
}
return b.ClientToken, nil
}
return "", &invalidResponseError{resp}
}