This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstripe.go
171 lines (145 loc) · 3.75 KB
/
stripe.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
163
164
165
166
167
168
169
170
171
package stripe
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
// enable logging to print the request and reponses to stdout
var _log bool
// the API Key used to authenticate all Stripe API requests
var _key string
// the default URL for all Stripe API requests
var _url string = "https://api.stripe.com"
const apiVersion = "2014-03-28"
// SetUrl will override the default Stripe API URL. This is primarily used
// for unit testing.
func SetUrl(url string) {
_url = url
}
// SetKey will set the default Stripe API key used to authenticate all Stripe
// API requests.
func SetKey(key string) {
_key = key
}
// Available APIs
var (
Charges = new(ChargeClient)
Coupons = new(CouponClient)
Customers = new(CustomerClient)
Invoices = new(InvoiceClient)
InvoiceItems = new(InvoiceItemClient)
Plans = new(PlanClient)
Subscriptions = new(SubscriptionClient)
Tokens = new(TokenClient)
Cards = new(CardClient)
)
// SetKeyEnv retrieves the Stripe API key using the STRIPE_API_KEY environment
// variable.
func SetKeyEnv() (err error) {
_key = os.Getenv("STRIPE_API_KEY")
if _key == "" {
err = errors.New("STRIPE_API_KEY not found in environment")
}
return
}
// query submits an http.Request and parses the JSON-encoded http.Response,
// storing the result in the value pointed to by v.
func query(method, path string, values url.Values, v interface{}) error {
// parse the stripe URL
endpoint, err := url.Parse(_url)
if err != nil {
return err
}
// set the endpoint for the specific API
endpoint.Path = "/v1" + path
endpoint.User = url.User(_key)
// if this is an http GET, add the url.Values to the endpoint
if method == "GET" {
endpoint.RawQuery = values.Encode()
}
// else if this is not a GET, encode the url.Values in the body.
var reqBody io.Reader
if method != "GET" && values != nil {
reqBody = strings.NewReader(values.Encode())
}
// Log request if logging enabled
if _log {
fmt.Println("REQUEST: ", method, endpoint.String())
fmt.Println(values.Encode())
}
// create the request
req, err := http.NewRequest(method, endpoint.String(), reqBody)
if err != nil {
return err
}
req.Header.Set("Stripe-Version", apiVersion)
// submit the http request
r, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
// read the body of the http message into a byte array
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
return err
}
// Log response if logging enabled
if _log {
fmt.Println("RESPONSE: ", r.StatusCode)
fmt.Println(string(body))
}
// is this an error?
if r.StatusCode != 200 {
error := Error{}
json.Unmarshal(body, &error)
return &error
}
//parse the JSON response into the response object
return json.Unmarshal(body, v)
}
// Error encapsulates an error returned by the Stripe REST API.
type Error struct {
Code int
Detail struct {
Code string `json:"code"`
Message string `json:"message"`
Param string `json:"param"`
Type string `json:"type"`
} `json:"error"`
}
func (e *Error) Error() string {
return e.Detail.Message
}
// Response to a Deletion request.
type DeleteResp struct {
// ID of the Object that was deleted
ID string `json:"id"`
// Boolean value indicating object was successfully deleted.
Deleted bool `json:"deleted"`
}
func appendMetadata(values url.Values, meta map[string]string) {
for k, v := range meta {
values.Add(fmt.Sprintf("metadata[%s]", k), v)
}
}
func listParams(limit int, before, after string) url.Values {
params := make(url.Values)
if limit > 0 {
params.Add("limit", strconv.Itoa(limit))
}
if before != "" {
params.Add("ending_before", before)
}
if after != "" {
params.Add("starting_after", after)
}
return params
}