diff --git a/appengine.go b/appengine.go new file mode 100644 index 0000000..ab71ba0 --- /dev/null +++ b/appengine.go @@ -0,0 +1,25 @@ +// +build appengine + +package stripe + +import ( + "appengine" + "appengine/urlfetch" + "net/http" + "net/url" + "io" +) + +func getHttpClient(r *http.Request) *http.Client { + c := appengine.NewContext(r) + return urlfetch.Client(c) +} + +func createRequest(method string, endpoint *url.URL, reqBody io.Reader) (*http.Request, error) { + req, err := http.NewRequest(method, endpoint.String(), reqBody) + if err == nil { + req.Header.Set("Stripe-Version", apiVersion) + req.SetBasicAuth(_key, "") + } + return req, err +} \ No newline at end of file diff --git a/appengine_not.go b/appengine_not.go new file mode 100644 index 0000000..660fc5b --- /dev/null +++ b/appengine_not.go @@ -0,0 +1,26 @@ +// +build !appengine + +package stripe + +import ( + "net/http" + "net/url" + "io" +) + +func getHttpClient(r *http.Request) *http.Client { + client := new(http.Client) + if client == nil { + client = &http.Client{} + } + return client +} + +func createRequest(method string, endpoint *url.URL, reqBody io.Reader) (*http.Request, error) { + endpoint.User = url.User(_key) + req, err := http.NewRequest(method, endpoint.String(), reqBody) + if err == nil { + req.Header.Set("Stripe-Version", apiVersion) + } + return req, err +} \ No newline at end of file diff --git a/plan.go b/plan.go index 9430923..9b07670 100644 --- a/plan.go +++ b/plan.go @@ -17,14 +17,15 @@ const ( // // see https://stripe.com/docs/api#plan_object type Plan struct { - Id string `json:"id"` - Name string `json:"name"` - Amount int64 `json:"amount"` - Interval string `json:"interval"` - IntervalCount int `json:"interval_count"` - Currency string `json:"currency"` - TrialPeriodDays Int `json:"trial_period_days"` - Livemode bool `json:"livemode"` + Id string `json:"id"` + Name string `json:"name"` + Amount int64 `json:"amount"` + Interval string `json:"interval"` + IntervalCount int `json:"interval_count"` + Currency string `json:"currency"` + TrialPeriodDays Int `json:"trial_period_days"` + Livemode bool `json:"livemode"` + Meta map[string]string `json:"metadata"` } // PlanClient encapsulates operations for creating, updating, deleting and diff --git a/stripe.go b/stripe.go index ff9d93f..6ad0542 100644 --- a/stripe.go +++ b/stripe.go @@ -21,6 +21,8 @@ var _key string // the default URL for all Stripe API requests var _url string = "https://api.stripe.com" +var _req *http.Request = new(http.Request) + const apiVersion = "2013-08-13" // SetUrl will override the default Stripe API URL. This is primarily used @@ -35,6 +37,12 @@ func SetKey(key string) { _key = key } +// For appengine usage. This allows the current request context to be accessed +// globally. +func SetRequest(r *http.Request) { + _req = r +} + // Available APIs var ( Charges = new(ChargeClient) @@ -68,7 +76,6 @@ func query(method, path string, values url.Values, v interface{}) error { // 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" { @@ -88,15 +95,14 @@ func query(method, path string, values url.Values, v interface{}) error { } // create the request - req, err := http.NewRequest(method, endpoint.String(), reqBody) + req, err := createRequest(method, endpoint, reqBody) if err != nil { return err } - req.Header.Set("Stripe-Version", apiVersion) - // submit the http request - r, err := http.DefaultClient.Do(req) + client := getHttpClient(_req) + r, err := client.Do(req) if err != nil { return err } diff --git a/subscription.go b/subscription.go index 0151e77..386b1ce 100644 --- a/subscription.go +++ b/subscription.go @@ -29,7 +29,7 @@ type Subscription struct { TrialEnd Int64 `json:"trial_end"` CanceledAt Int64 `json:"canceled_at"` CancelAtPeriodEnd bool `json:"cancel_at_period_end"` - Quantity int64 `json"quantity"` + Quantity int64 `json:"quantity"` } // SubscriptionClient encapsulates operations for updating and canceling