forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbraintree.go
162 lines (132 loc) · 3.21 KB
/
braintree.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package braintree
import (
"bytes"
"encoding/xml"
"fmt"
"log"
"net/http"
)
type Environment string
const (
Development Environment = "development"
Sandbox Environment = "sandbox"
Production Environment = "production"
LibraryVersion = "0.9.0"
)
func (e Environment) BaseURL() string {
switch e {
case Development:
return "http://localhost:3000"
case Sandbox:
return "https://sandbox.braintreegateway.com"
case Production:
return "https://www.braintreegateway.com"
}
panic(`invalid environment "` + e + `"`)
}
func New(env Environment, merchId, pubKey, privKey string) *Braintree {
return &Braintree{
Environment: env,
MerchantId: merchId,
PublicKey: pubKey,
PrivateKey: privKey,
}
}
type Braintree struct {
Environment Environment
MerchantId string
PublicKey string
PrivateKey string
Logger *log.Logger
HttpClient *http.Client
}
func (g *Braintree) MerchantURL() string {
return g.Environment.BaseURL() + "/merchants/" + g.MerchantId
}
func (g *Braintree) execute(method, path string, xmlObj interface{}) (*Response, error) {
var buf bytes.Buffer
if xmlObj != nil {
xmlBody, err := xml.Marshal(xmlObj)
if err != nil {
return nil, err
}
_, err = buf.Write(xmlBody)
if err != nil {
return nil, err
}
}
url := g.MerchantURL() + "/" + path
if g.Logger != nil {
g.Logger.Printf("> %s %s\n%s", method, url, buf.String())
}
req, err := http.NewRequest(method, url, &buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/xml")
req.Header.Set("Accept", "application/xml")
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set("User-Agent", fmt.Sprintf("Braintree Go %s", LibraryVersion))
req.Header.Set("X-ApiVersion", "3")
req.SetBasicAuth(g.PublicKey, g.PrivateKey)
httpClient := g.HttpClient
if httpClient == nil {
httpClient = http.DefaultClient
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
btr := &Response{
Response: resp,
}
err = btr.unpackBody()
if err != nil {
return nil, err
}
if g.Logger != nil {
g.Logger.Printf("<\n%s", string(btr.Body))
}
err = btr.apiError()
if err != nil {
return nil, err
}
return btr, nil
}
func (g *Braintree) ClientToken() *ClientTokenGateway {
return &ClientTokenGateway{g}
}
func (g *Braintree) MerchantAccount() *MerchantAccountGateway {
return &MerchantAccountGateway{g}
}
func (g *Braintree) Transaction() *TransactionGateway {
return &TransactionGateway{g}
}
func (g *Braintree) CreditCard() *CreditCardGateway {
return &CreditCardGateway{g}
}
func (g *Braintree) PayPalAccount() *PayPalAccountGateway {
return &PayPalAccountGateway{g}
}
func (g *Braintree) Customer() *CustomerGateway {
return &CustomerGateway{g}
}
func (g *Braintree) Subscription() *SubscriptionGateway {
return &SubscriptionGateway{g}
}
func (g *Braintree) Plan() *PlanGateway {
return &PlanGateway{g}
}
func (g *Braintree) Address() *AddressGateway {
return &AddressGateway{g}
}
func (g *Braintree) AddOn() *AddOnGateway {
return &AddOnGateway{g}
}
func (g *Braintree) Discount() *DiscountGateway {
return &DiscountGateway{g}
}
func (g *Braintree) WebhookNotification() *WebhookNotificationGateway {
return &WebhookNotificationGateway{g}
}