Skip to content
This repository
tree: 12a21f3f44
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 146 lines (123 sloc) 3.278 kb
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
package stripe

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"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"

// 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)
)

// 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 = 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
}

// 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"`
}
Something went wrong with that request. Please try again.