braintree-go

A Go client library for Braintree

  • Owner: braintree-go/braintree-go
  • Platform:
  • License::
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

Braintree Go

GoDoc
Build Status
Go Report Card

A Go client library for Braintree, the payments company behind awesome companies like GitHub, Heroku, and 37signals.

This is not an official client library. Braintree maintains server-side libraries for Ruby, Python, PHP, Perl, Node, C# and Java, but not Go. This package implements the core functionality of the other client libraries, but it's missing a few advanced features.

With that said, this package contains more than enough to get you started accepting payments using Braintree. If there's a feature the other client libraries implement that you really need, open an issue (or better yet, a pull request).

Usage

Setting up your credentials is easy.

import "github.com/braintree-go/braintree-go"

bt := braintree.New(
  braintree.Sandbox,
  "YOUR_BRAINTREE_MERCH_ID",
  "YOUR_BRAINTREE_PUB_KEY",
  "YOUR_BRAINTREE_PRIV_KEY",
)

So is creating your first transaction.

ctx := context.Background()
tx, err := bt.Transaction().Create(ctx, &braintree.TransactionRequest{
  Type: "sale",
  Amount: braintree.NewDecimal(100, 2), // 100 cents
  CreditCard: &braintree.CreditCard{
    Number:         "4111111111111111",
    ExpirationDate: "05/14",
  },
})

The error returned by these calls is typed. The package returns a generic error when something mechanical goes wrong, such as receiving malformed XML or being unable to connect to the Braintree gateway. However, if Braintree was able to process the request correctly, but was unable to fulfill it due to a semantic failure (such as the credit card being declined) then a BraintreeError type is returned.

In addition to creating transactions, you can also tokenize credit card information for repeat or subscription billing using the CreditCard, Customer, and Subscription types. This package is completely compatible with Braintree.js, so if you encrypt your customers' credit cards in the browser, you can pass them on to Braintree without ever seeing them yourself. This decreases your PCI regulatory exposure and helps to secure your users' data. See the examples folder for a working implementation.

Installation

The usual. go get github.com/braintree-go/braintree-go

Supported Go Versions

  • 1.7
  • 1.8
  • 1.9
  • 1.10
  • 1.11
  • 1.12

Documentation

Braintree provides a ton of documentation on how to use their API. I recommend you use the Ruby documentation when following along, as the Ruby client library is broadly similar to this one.

For details on this package, see GoDoc.

Testing

The integration tests run against a sandbox account created in the Braintree Sandbox.
See TESTING.md for further instructions on how to set up your sandbox for integration testing.

You can run tests locally using the same credentials that Travis CI uses by using the credentials in .default.env. Simply cp .default.env .env if you use a tool that autoloads .env files, or source .default.env to load the credentials into your shell. Then run tests with go test -tags='unit integration' ./....

source .default.env
go test -tags='unit integration' ./...

Webhook Integration Testing

You can use the WebhookTestingGateway to write your own integration tests to verify your application is processing incoming webhook notifications correctly.

A simple example:

package integration_test

import (
  "testing"
  "net/http/httptest"

  "github.com/braintree-go/braintree-go"
)

func TestMyWebhook(t *testing.T) {
  bt := braintree.New(
    braintree.Sandbox,
    "merchaint_id",
    "public_key",
    "private_key",
  )

  r, err := bt.WebhookTesting().Request(
    braintree.SubscriptionChargedSuccessfullyWebhook,
    "123", // id
  )
  if err != nil {
    t.Fatal(err)
  }

  // You can now send the payload and signature to your webhook handler
  // and test your application's busines logic

  w := httptest.NewRecorder()
  router.ServeHTTP(w, r)

  // assertions
}

License

The MIT License (MIT)

Copyright (c) 2013 Lionel Barrow

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Contributors

Main metrics

Overview
Name With Ownerbraintree-go/braintree-go
Primary LanguageGo
Program languageGo (Language Count: 2)
Platform
License:
所有者活动
Created At2013-05-12 21:02:46
Pushed At2024-07-11 20:56:46
Last Commit At2019-03-11 06:44:47
Release Count19
Last Release Namev0.22.0 (Posted on 2018-11-20 05:35:16)
First Release Name0.5.0 (Posted on 2013-05-27 16:42:11)
用户参与
Stargazers Count246
Watchers Count15
Fork Count145
Commits Count555
Has Issues Enabled
Issues Count78
Issue Open Count9
Pull Requests Count186
Pull Requests Open Count9
Pull Requests Close Count48
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private