Skip to content
This repository was archived by the owner on Mar 15, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions appengine.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions appengine_not.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 9 additions & 8 deletions plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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" {
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down