Go library for the Stripe API.
Go Other
Latest commit 96681be Dec 14, 2017 @brandur-stripe brandur-stripe Merge pull request #501 from stripe/ob-coveralls
Add support for coveralls.io
Permalink
Failed to load latest commit information.
.github Added a template for GitHub issues Nov 3, 2016
account Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
applepaydomain Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
balance Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
bankaccount Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
bitcoinreceiver Remove Bitcoin receiver tests and mark package deprecated Sep 22, 2017
bitcointransaction Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
card Now that we have `nil` check, remove the other guard Oct 24, 2017
charge Create FraudDetailsParams Sep 15, 2017
client Add support for /v1/exchange_rates API requests Oct 31, 2017
countryspec Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
coupon Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
currency Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
customer Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
discount Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
dispute Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
ephemeralkey Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
event Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
exchangerate Add support for /v1/exchange_rates API requests Oct 31, 2017
fee Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
feerefund Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
fileupload Skip file upload test for now Oct 19, 2017
form A few minor wording fixes in comment Oct 3, 2017
invoice Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
invoiceitem Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
loginlink Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
order Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
orderitem Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
orderreturn Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
paymentsource Adds verify source endpoint Oct 10, 2017
payout Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
plan Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
product Properly pass the Params parameters for sku and product Sep 26, 2017
recipient Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
recipienttransfer Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
refund Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
reversal Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
scripts Add `go vet` and `gofmt` check to CI Mar 28, 2016
sku Properly pass the Params parameters for sku and product Sep 26, 2017
source Support passing original_source to clone a source to a connected account Dec 12, 2017
sourcetransaction Add support for listing source transactions Oct 26, 2017
sub Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
subitem Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
testing Add support for /v1/exchange_rates API requests Oct 31, 2017
threedsecure Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
token Properly support passing card and bank_account on Token creation Dec 12, 2017
transfer Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
webhook Compile all tests in CI (even if they're not run) Jul 27, 2017
.gitignore Add support for coveralls.io Dec 14, 2017
.travis.yml Add support for coveralls.io Dec 14, 2017
CHANGELOG Reformat CHANGELOG to CHANGELOG.md Oct 20, 2017
CHANGELOG.md Bump version to 28.8.0 Dec 13, 2017
LICENSE update license to Stripe Aug 30, 2014
Makefile Add support for coveralls.io Dec 14, 2017
README.md Add support for coveralls.io Dec 14, 2017
VERSION Bump version to 28.8.0 Dec 13, 2017
account.go Ensure we can pass a token instead if needed Oct 25, 2017
account_test.go Add a test for the Token branch of AppendTo Oct 25, 2017
address.go Alphabetize the world Sep 15, 2017
applepaydomain.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
application.go Custom deserializer for Application Nov 4, 2016
application_test.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
balance.go Alphabetize the world Sep 15, 2017
bankaccount.go Properly support passing card and bank_account on Token creation Dec 12, 2017
bankaccount_test.go Make `account_holder_name`/`account_holder_type` truly optional Oct 25, 2017
bitcoinreceiver.go Properly sort RejectTransactions/RefundAddress Sep 15, 2017
bitcointransaction.go Alphabetize the world Sep 15, 2017
card.go Properly support passing card and bank_account on Token creation Dec 12, 2017
charge.go Add support for /v1/exchange_rates API requests Oct 31, 2017
charge_test.go Drop ChargeParams.Dest Sep 15, 2017
countryspec.go Alphabetize the world Sep 15, 2017
coupon.go Alphabetize the world Sep 15, 2017
customer.go Alphabetize the world Sep 15, 2017
discount.go Alphabetize the world Sep 15, 2017
dispute.go Alphabetize the world Sep 15, 2017
ephemeralkey.go Hoist AssociatedObjects to be alphabetized Sep 15, 2017
ephemeralkey_test.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
error.go Alphabetize the world Sep 15, 2017
error_test.go Added a mutex around backend varible for concurrent reads Oct 17, 2017
event.go Alphabetize the world Sep 15, 2017
example_test.go Fix Del methods to always take an extra parameter and be named Del Aug 17, 2017
exchangerate.go Add support for /v1/exchange_rates API requests Oct 31, 2017
exchangerate_test.go Add support for /v1/exchange_rates API requests Oct 31, 2017
fee.go Alphabetize the world Sep 15, 2017
feerefund.go Create FraudDetailsParams Sep 15, 2017
fileupload.go Fix a few fields that were misalphabetized Sep 15, 2017
invoice.go Fix a few fields that were misalphabetized Sep 15, 2017
invoiceitem.go Alphabetize the world Sep 15, 2017
iter.go Alphabetize the world Sep 15, 2017
iter_test.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
loginlink.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
order.go Alphabetize the world Sep 15, 2017
orderitem.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
orderreturn.go Alphabetize the world Sep 15, 2017
params.go Expand on Context comment for additional clarity Oct 19, 2017
params_test.go Support Context on API requests Oct 19, 2017
paymentsource.go Adds verify source endpoint Oct 10, 2017
paymentsource_test.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
payout.go Add support for automatic on Payout Nov 16, 2017
plan.go Alphabetize the world Sep 15, 2017
plan_test.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
product.go Alphabetize the world Sep 15, 2017
recipient.go Alphabetize the world Sep 15, 2017
recipient_test.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
recipienttransfer.go Fix a few fields that were misalphabetized Sep 15, 2017
recipienttransfer_test.go Tweak parameters names in resources and tests Apr 6, 2017
refund.go Alphabetize the world Sep 15, 2017
refund_test.go Fix issue #324: Refund.Charge should be a *Charge Oct 3, 2016
reversal.go Alphabetize the world Sep 15, 2017
review.go Alphabetize the world Sep 15, 2017
sku.go Alphabetize the world Sep 15, 2017
source.go Support passing original_source to clone a source to a connected account Dec 12, 2017
source_test.go Fix source test to properly verify TypeData encoding Sep 20, 2017
sourcetransaction.go Add support for listing source transactions Oct 26, 2017
stripe.go Bump version to 28.8.0 Dec 13, 2017
stripe_test.go Rename language fields in Stripe's user agent to start with `lang` Nov 2, 2017
sub.go Fix a few fields that were misalphabetized Sep 15, 2017
sub_test.go Power test suite with stripe-mock + rework parameters with form Sep 15, 2017
subitem.go Alphabetize the world Sep 15, 2017
threedsecure.go Alphabetize the world Sep 15, 2017
token.go Alphabetize the world Sep 15, 2017
transfer.go Alphabetize the world Sep 15, 2017
transfer_test.go Fix issue #322: Transfer.SourceTx has wrong type Oct 3, 2016

README.md

Go Stripe

GoDoc Build Status Coverage Status

Summary

The official Stripe Go client library.

Versioning

Each revision of the binding is tagged and the version is updated accordingly.

Given Go's lack of built-in versioning, it is highly recommended you use a package management tool in order to ensure a newer version of the binding does not affect backwards compatibility.

To see the list of past versions, run git tag. To manually get an older version of the client, clone this repo, checkout the specific tag and build the library:

git clone https://github.com/stripe/stripe-go.git
cd stripe-go
git checkout api_version_tag
make build

For more details on changes between versions, see the binding changelog and API changelog.

Installation

go get github.com/stripe/stripe-go

Documentation

For a comprehensive list of examples, check out the API documentation.

For details on all the functionality in this library, see the GoDoc documentation.

Below are a few simple examples:

Customers

params := &stripe.CustomerParams{
	Balance: -123,
	Desc:  "Stripe Developer",
	Email: "gostripe@stripe.com",
}
params.SetSource("tok_1234")

customer, err := customer.New(params)

Charges

params := &stripe.ChargeListParams{Customer: customer.ID}
params.Filters.AddFilter("include[]", "", "total_count")

// set this so you can easily retry your request in case of a timeout
params.Params.IdempotencyKey = stripe.NewIdempotencyKey()

i := charge.List(params)
for i.Next() {
	charge := i.Charge()
}

if err := i.Err(); err != nil {
	// handle
}

Events

i := event.List(nil)
for i.Next() {
	e := i.Event()

	// 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"]
}

Alternatively, you can use the even.Data.Raw property to unmarshal to the appropriate struct.

Authentication with Connect

There are two ways of authenticating requests when performing actions on behalf of a connected account, one that uses the Stripe-Account header containing an account's ID, and one that uses the account's keys. Usually the former is the recommended approach. See the documentation for more information.

To use the Stripe-Account approach, pass the StripeAccount field to a ListParams or Params class. For example:

// For a list request
listParams := &stripe.ChargeListParams{StripeAccount: merchantID}
// For any other kind of request
params := &stripe.CustomerParams{StripeAccount: merchantID}

To use a key, pass it to API's Init function:

import (
	"github.com/stripe/stripe-go"
	"github.com/stripe/stripe-go/client"
)

stripe := &client.API{}
stripe.Init("access_token", nil)

Google AppEngine

If you're running the client in a Google AppEngine environment, you'll need to create a per-request Stripe client since the http.DefaultClient is not available. Here's a sample handler:

import (
	"fmt"
	"net/http"

	"google.golang.org/appengine"
	"google.golang.org/appengine/urlfetch"

	"github.com/stripe/stripe-go"
	"github.com/stripe/stripe-go/client"
)

func handler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	httpClient := urlfetch.Client(c)

	sc := client.New("sk_live_key", stripe.NewBackends(httpClient))

	fmt.Fprintf(w, "Ready to make calls to the Stripe API")
}

Usage

While some resources may contain more/less APIs, the following pattern is applied throughout the library for a given $resource$:

Without a Client

If you're only dealing with a single key, you can simply import the packages required for the resources you're interacting with without the need to create a client.

import (
	"github.com/stripe/stripe-go"
	"github.com/stripe/stripe-go/$resource$"
)

// Setup
stripe.Key = "sk_key"

stripe.SetBackend("api", backend) // optional, useful for mocking

// Create
$resource$, err := $resource$.New(stripe.$Resource$Params)

// Get
$resource$, err := $resource$.Get(id, stripe.$Resource$Params)

// Update
$resource$, err := $resource$.Update(stripe.$Resource$Params)

// Delete
resourceDeleted, err := $resource$.Del(id, stripe.$Resource$Params)

// List
i := $resource$.List(stripe.$Resource$ListParams)
for i.Next() {
	$resource$ := i.$Resource$()
}

if err := i.Err(); err != nil {
	// handle
}

With a Client

If you're dealing with multiple keys, it is recommended you use client.API. This allows you to create as many clients as needed, each with their own individual key.

import (
	"github.com/stripe/stripe-go"
	"github.com/stripe/stripe-go/client"
)

// Setup
sc := &client.API{}
sc.Init("sk_key", nil) // the second parameter overrides the backends used if needed for mocking

// Create
$resource$, err := sc.$Resource$s.New(stripe.$Resource$Params)

// Get
$resource$, err := sc.$Resource$s.Get(id, stripe.$Resource$Params)

// Update
$resource$, err := sc.$Resource$s.Update(stripe.$Resource$Params)

// Delete
resourceDeleted, err := sc.$Resource$s.Del(id, stripe.$Resource$Params)

// List
i := sc.$Resource$s.List(stripe.$Resource$ListParams)
for i.Next() {
	resource := i.$Resource$()
}

if err := i.Err(); err != nil {
	// handle
}

Writing a Plugin

If you're writing a plugin that uses the library, we'd appreciate it if you identified using stripe.SetAppInfo:

stripe.SetAppInfo(&stripe.AppInfo{
    Name:    "MyAwesomePlugin",
    URL:     "https://myawesomeplugin.info",
    Version: "1.2.34",
})

This information is passed along when the library makes calls to the Stripe API. Note that while Name is always required, URL and Version are optional.

Development

Pull requests from the community are welcome. If you submit one, please keep the following guidelines in mind:

  1. Code must be go fmt compliant.
  2. All types, structs and funcs should be documented.
  3. Ensure that make test succeeds.

Test

The test suite needs testify's require package to run:

github.com/stretchr/testify/require

It also depends on [stripe-mock], so make sure to fetch and run it from a background terminal ([stripe-mock's README][stripe-mock] also contains instructions for installing via Homebrew and other methods):

go get -u github.com/stripe/stripe-mock
stripe-mock

Run all tests:

go test ./...

Run tests for one package:

go test ./invoice

Run a single test:

go test ./invoice -run TestInvoiceGet

For any requests, bug or comments, please open an issue or submit a pull request.