From 52268deb4fbc0d94dfa1d7e6b7f2ffa95b21a4b0 Mon Sep 17 00:00:00 2001 From: Hamish Date: Fri, 5 Sep 2014 16:44:22 +1000 Subject: [PATCH 1/4] Add appengine http.Client support using build tags --- appengine.go | 14 ++++++++++++++ appengine_not.go | 15 +++++++++++++++ stripe.go | 3 ++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 appengine.go create mode 100644 appengine_not.go diff --git a/appengine.go b/appengine.go new file mode 100644 index 0000000..4753252 --- /dev/null +++ b/appengine.go @@ -0,0 +1,14 @@ +// +build appengine + +package stripe + +import ( + "appengine" + "appengine/urlfetch" + "net/http" +) + +func getHttpClient(r *http.Request) *http.Client { + c := appengine.NewContext(r) + return urlfetch.Client(c) +} \ No newline at end of file diff --git a/appengine_not.go b/appengine_not.go new file mode 100644 index 0000000..0c5c516 --- /dev/null +++ b/appengine_not.go @@ -0,0 +1,15 @@ +// +build !appengine + +package stripe + +import ( + "net/http" +) + +func getHttpClient(r *http.Request) *http.Client { + client := new(http.Client) + if client == nil { + client = &http.Client{} + } + return client +} \ No newline at end of file diff --git a/stripe.go b/stripe.go index ff9d93f..3e20cf3 100644 --- a/stripe.go +++ b/stripe.go @@ -96,7 +96,8 @@ func query(method, path string, values url.Values, v interface{}) error { 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 } From c16c0c6aeddd384883160facc52cf40f4ad3e6b9 Mon Sep 17 00:00:00 2001 From: Hamish Date: Fri, 5 Sep 2014 18:44:16 +1000 Subject: [PATCH 2/4] Fixes for appengine using build tags URL requests are handled slightly differently in appengine, this builds to handle those changes if on app engine --- appengine.go | 11 +++++++++++ appengine_not.go | 11 +++++++++++ stripe.go | 15 ++++++++++----- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/appengine.go b/appengine.go index 4753252..ab71ba0 100644 --- a/appengine.go +++ b/appengine.go @@ -6,9 +6,20 @@ 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 index 0c5c516..660fc5b 100644 --- a/appengine_not.go +++ b/appengine_not.go @@ -4,6 +4,8 @@ package stripe import ( "net/http" + "net/url" + "io" ) func getHttpClient(r *http.Request) *http.Client { @@ -12,4 +14,13 @@ func getHttpClient(r *http.Request) *http.Client { 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/stripe.go b/stripe.go index 3e20cf3..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,13 @@ 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 - client := getHttpClient(req) + client := getHttpClient(_req) r, err := client.Do(req) if err != nil { return err From e1de1521f894ab6259d1b7e53270db73fb3ad134 Mon Sep 17 00:00:00 2001 From: Hamish Date: Fri, 5 Sep 2014 18:45:52 +1000 Subject: [PATCH 3/4] Fix typo in subscription struct --- subscription.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 9c6098bbf264be6b1d386884c1883f4fe9128047 Mon Sep 17 00:00:00 2001 From: Hamish Date: Wed, 14 Dec 2016 12:59:14 +1100 Subject: [PATCH 4/4] Add metadata to plans --- plan.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) 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