A Stripe client library written in Go.
Currently, the library adheres to Stripe API version 2014-08-20. For more details on changes between versions, see the API changelog.
With the addition of a new version, the previous version is tagged. This allows consumers on older API versions to be able to use a specific version of the client.
To see the list of past versions, run git tag
.
In order to use an older version of the client, clone this repo, checkout the specific tag and build the library manually:
git clone https://github.com/cosn/stripe.git
cd stripe
git checkout api_version_tag
go build
go get github.com/cosn/stripe
Set the STRIPE_KEY environment variable to match your test private key:
export STRIPE_KEY=YOUR_API_KEY
Then run:
go test github.com/cosn/stripe...
First import the package into your code:
import (
"github.com/cosn/stripe"
)
To use the client, initialize it and before making any requests:
stripe := &stripe.Client{}
stripe.Init(YOUR_API_KEY, nil, nil)
The second parameter can be used to set a different http.Client (by default, http.DefaultClient is used). The third parameter can be used to inject a mock Api implementation so calls aren't actually made to Stripe. This can be useful for writing your own unit tests.
While some resources may contain more/less APIs, the following pattern is applied throughout the library for a given resource
:
// Create
resource, err := stripe.Resources.Create(ResourceParams)
// Get
resource, err := stripe.Resources.Get(id)
// Update
resource, err := stripe.Resources.Get(ResourceParams)
// Delete
err := stripe.Resources.Delete(id)
// List
resourceList, err := stripe.Resources.List(ResourceListParams)
Below are a few simple examples. For details on all the functionality in this library, see the GoDoc documentation.
For more details about the Stripe, see the Stripe official documentation.
params := &CustomerParams{
Balance: -123,
Card: &CardParams{
Name: "Go Stripe",
Number: "378282246310005",
Month: "06",
Year: "15",
},
Desc: "Stripe Dev",
Email: "[email protected]",
}
customer, err := stripe.Customers.Create(params)
params := &ChargeListParams{Customer: customer.Id}
params.Filters.AddFilter("include", "", "total_count")
charges, err := stripe.Charges.List(params)
for _, charge := range(charges.Values) {
// perform an action on each charge
}
events, err := stripe.Events.List(nil)
for _, e := range(events.Values) {
// access event data via e.GetObjValue("resource_name_based_on_type", "resource_property_name")
// alternatively you can access values via e.Data.Obj["resource_name_based_on_type"].(map[string]interface{})["resource_property_name"]
// access previous attributes via e.GetPrevValue("resource_name_based_on_type", "resource_property_name")
// alternatively you can access values via e.Data.Prev["resource_name_based_on_type"].(map[string]interface{})["resource_property_name"]
}
Below are the known imporvements planned in the near future:
- Add support for expanding of properties
For any other requests, bug or comments, please open an issue. Pull requests are welcome.