coinbase

package module
v0.0.0-...-3c19e55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2021 License: GPL-3.0 Imports: 7 Imported by: 0

README

coinbase-commerce-go

Coinbase Commerce Golang

Table of contents

Documentation

For more details visit Coinbase API docs and Coinbase Commerce Go GoDoc

To start using library, you need to register on Commerce SignUp. And get your API_KEY from user settings.

Next create a APIClient object for interacting with the API:

import "github.com/KristenPire/coinbase-commerce-go"

client := coinbase.Client(API_KEY)

Client contains links to an every Golang Class representations of the API resources Checkout, Charge, Event

You can call Create, List, Get, Update, Delete methods from an API resource classes

client.Charge.Create
client.Checkout.List 
client.Event.Get
client.Checkout.Update
client.Checkout.Delete

as well as Save, Delete, Refresh methods from API resource class instances.

checkout, err := client.Checkout.Get(<id>)
checkout.Refresh()
checkout.Save()
checkout.Delete()

Each API method returns an API ressource instances (APICharge, APICheckout, APIEvent) representing the response from the API, all of the models are dumpable with JSON.
The response data is parsed into Golang objects, the appropriate APIObject subclasses will be used automatically.

Client support Common API Errors and Warnings handling. All errors occuring during interaction with the API will be return.

Error Status Code
APIError *
InvalidRequestError 400
ParamRequiredError 400
ValidationError 400
AuthenticationError 401
ResourceNotFoundError 404
RateLimitExceededError 429
InternalServerError 500
ServiceUnavailableError 503

Installation

Install with go get:

go get "github.com/KristenPire/coinbase-commerce-go"

Usage

import "github.com/KristenPire/coinbase-commerce-go"

client := coinbase.Client(API_KEY)

Checkouts

Checkouts API docs

Get
checkout, err := client.Checkout.Get(<checkout_id>)
Create
#by struct
checkout, err := client.Checkout.Create(coinbase.APICheckoutData{
    Name:"The Sovereign Individual",
    Description: "Mastering the Transition to the Information Age",
    Pricing_type: "fixed_price",
    Local_price: coinbase.Money{Amount : 100.00, Currency: "USD"},
    Requested_info: []string{"email", "name"},
   })

#or directly by json
checkout_info := `{
    "name": "The Sovereign Individual",
    "description": "Mastering the Transition to the Information Age",
    "pricing_type": "fixed_price",
    "local_price": {
        "amount": "100.00",
        "currency": "USD"
    },
    "requested_info": ["name", "email"]
}`
checkout, err := client.Checkout.Create(checkout_info)
Update
#by object method
checkout, err := client.Checkout.Get(<checkout_id>)
checkout.Data.Name := "new name"
checkout.Save()

#by API method and json
checkout_info := `{"name": "newName"}`

checkout, err := client.Checkout.Update('<checkout_id>', checkout_info)

#or by API method and object
checkout := coinbase.APICheckoutData{}
checkout.Name := "new name"

checkout, err := client.Checkout.Update('<checkout_id>', checkout)
Delete
#by object method
checkout , err := client.Checkout.Get(<checkout_id>)
checkout.Delete()

#by API method
client.Checkout.Delete('<checkout_id>')
List
checkouts, err := client.Checkout.List()
Iterations
checkouts, err := client.Checkout.List()
for err, checkout := range checkouts.Data{
    checkout.Delete()
}

Charges

Charges API docs

Retrieve
charge ,err := client.Charge.Get(<charge_id>)
Create
#by struct
charge, err := client.Charge.Create(coinbase.APIChargeData{
    Name:"The Sovereign Individual",
    Description: "Mastering the Transition to the Information Age",
    Pricing_type: "fixed_price",
    Local_price: coinbase.Money{Amount : 100.00, Currency: "USD"},
   })

#or directly by json
charge_info := `{
    "name": "The Sovereign Individual",
    "description": "Mastering the Transition to the Information Age",
    "pricing_type": "fixed_price",
    "local_price": {
        "amount": "100.00",
        "currency": "USD"
    }
}`
charge, err := client.Charge.Create(charge_info)
List
charges, err := client.Charge.List()
Iterations
charges, err := client.Charge.List()
for _, charge := range charges.Data{
    jsonStr, _ := json.Marshal(charge)
    fmt.Println(string(jsonStr))
}

Events

Events API Docs

Retrieve
event ,err := client.Event.Get(<event_id>)
List
events, err := client.Event.List()
Iterations
events, err := client.Event.List()
for _, event := range events.Data{
    jsonStr, _ := json.Marshal(event)
    fmt.Println(string(jsonStr))
}

Types

Checkout
APICheckoutData
type APICheckoutData struct {
	Id             string   `json:"id,omitempty"`
	Resource       string   `json:"ressource,omitempty"`
	Name           string   `json:"name,omitempty"`
	Description    string   `json:"description,omitempty"`
	Logo_url       string   `json:"logo_url,omitempty"`
	Requested_info []string `json:"requested_info,omitempty"`
	Pricing_type   string   `json:"pricing_type,omitempty"`
	Local_price    Money    `json:"local_price,omitempty"`
}
APICheckout
type APICheckout struct {
	father *ACheckout
	Data   APICheckoutData `json:"data,omitempty"`
	Errors []APIError      `json:"errors,omitempty"`
}
APICheckouts
type APICheckouts struct {
	Pagination APIPagination `json:"pagination,omitempty"`
	Checkouts  []APICheckout `json:"data,omitempty"`
	Errors     []APIError    `json:"errors,omitempty"`
}
Charge
APIChargeData
type APIChargeData struct {
	Id           string     `json:"id,omitempty"`
	Ressource     string     `json:"ressource,omitempty"`
	Code         string     `json:"code,omitempty"`
	Name         string     `json:"name,omitempty"`
	Description  string     `json:"description,omitempty"`
	Logo_url     string     `json:"logo_url,omitempty"`
	Hosted_url   string     `json:"Hosted_url,omitempty"`
	Created_at   *time.Time `json:"created_at,omitempty"`
	Updated_at   *time.Time `json:"updated_at,omitempty"`
	Confirmed_at *time.Time `json:"confirmed_at,omitempty"`
	Checkout     struct {
		Id string `json:"id,omitempty"`
	} `json:"checkout,omitempty"`
	Timeline []struct {
		Time    *time.Time `json:"id,omitempty"`
		Status  string     `json:"status,omitempty"`
		Context string     `json:"context,omitempty"`
	} `json:"timeline,omitempty"`
	Metadata     map[string]interface{} `json:"metadata,omitempty"`
	Pricing_type string                 `json:"pricing_type,omitempty"`
	Pricing      struct {
		Local       Money `json:"local,omitempty"`
		Bitcoin     Money `json:"bitcoin,omitempty"`
		Bitcoincash Money `json:"bitcoincash,omitempty"`
		Ethereum    Money `json:"ethereum,omitempty"`
		Litecoin    Money `json:"litecoin,omitempty"`
	} `json:"pricing,omitempty"`
	Payments []map[string]interface{} `json:"payements,omitempty"`
	Addresses struct {
		Bitcoin     string `json:"bitcoin,omitempty"`
		Bitcoincash string `json:"bitcoincash,omitempty"`
		Ethereum    string `json:"ethereum,omitempty"`
		Litecoin    string `json:"litecoin,omitempty"`
	} `json:"addresses,omitempty"`
	Local_price Money `json:"local_price,omitempty"`
}
APICharge
type APICharge struct {
	father *ACharge
	Data   APIChargeData `json:"data,omitempty"`
	Errors []APIError    `json:"errors,omitempty"`
}
APICharges
type APICharges struct {
	Pagination APIPagination `json:"pagination,omitempty"`
	Charges    []APICharge   `json:"data,omitempty"`
	Errors     []APIError    `json:"errors,omitempty"`
}
Event
APIEventData

type APIEventData struct {
	Id          string        `json:"id,omitempty"`
	Resource    string        `json:"ressource,omitempty"`
	Created_at  *time.Time    `json:"created_at,omitempty"`
	Api_version string        `json:"api_version,omitempty"`
	Data        APIChargeData `json:"data,omitempty"`
}
APIEvent
type APIEvent struct {
	father *AEvent
	Data   APIEventData `json:"data,omitempty"`
	Errors []APIError   `json:"errors,omitempty"`
}
APIEvents
type APIEvents struct {
	Pagination APIPagination `json:"pagination,omitempty"`
	Events     []APIEvent    `json:"data,omitempty"`
	Errors     []APIError    `json:"errors,omitempty"`
}
API
APIError
type APIError struct {
	Type    string `json:"type"`
	Message string `json:"message"`
}
APIPagination
type APIPagination struct {
	Order          string
	Starting_after string
	Ending_before  string
	Total          int
	Limit          int
	Previous_uri   string
	Next_uri       string
	Yielded        int
	Cursor_range   []string
}
Money
type Money struct {
	Amount   float64 `json:"amount,string,omitempty"`
	Currency string  `json:"currency,omitempty"`
}

Documentation

Index

Constants

View Source
const (
	// ENDPOINT defaults to https://api.commerce.coinbase.com
	// but can be overridden for test purposes
	ENDPOINT = "https://api.commerce.coinbase.com"
	// API_VERSION since version two you have to
	// specify a API version in your http request headers
	API_VERSION = "2018-03-22"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ACharge

type ACharge struct {
	Api *APIClient
}

ACharge is a class hosted inside the APIClient providing methods about charge

func (*ACharge) Create

func (a *ACharge) Create(data interface{}) (charge APICharge, err error)

Create a new charge and return his golang instance

func (*ACharge) Get

func (a *ACharge) Get(id string) (charge APICharge, err error)

Get provides the APICharge instance of the given charge id.

func (*ACharge) List

func (a *ACharge) List() (charges APICharges, err error)

List create a APICharges object with a list of APICharge instance.

type ACheckout

type ACheckout struct {
	Api *APIClient
}

func (*ACheckout) Create

func (a *ACheckout) Create(data interface{}) (checkout APICheckout, err error)

Create a new charge and return his golang instance

func (*ACheckout) Delete

func (a *ACheckout) Delete(id string) (err error)

Delete will erase the id given checkout

func (*ACheckout) Get

func (a *ACheckout) Get(id string) (checkout APICheckout, err error)

Get provides the APICharge instance of the given charge id.

func (*ACheckout) List

func (a *ACheckout) List() (checkouts APICheckouts, err error)

List create a APICharges object with a list of APICharge instance.

func (*ACheckout) Update

func (a *ACheckout) Update(id string, data interface{}) (checkout APICheckout, err error)

Update will changes the given field of the id given checkout

type AEvent

type AEvent struct {
	Api *APIClient
}

func (*AEvent) Get

func (a *AEvent) Get(id string) (event APIEvent, err error)

func (*AEvent) List

func (a *AEvent) List() (events APIEvents, err error)

type APICharge

type APICharge struct {
	Data   APIChargeData `json:"data,omitempty"`
	Errors []APIError    `json:"errors,omitempty"`
	// contains filtered or unexported fields
}

APICharge is the object API object returned by the api routes.

func (*APICharge) Refresh

func (a *APICharge) Refresh() (err error)

Refresh will update attributes and all nested data by making a fresh GET request to the relevant API endpoint.

type APIChargeData

type APIChargeData struct {
	Id           string     `json:"id,omitempty"`
	Ressource    string     `json:"ressource,omitempty"`
	Code         string     `json:"code,omitempty"`
	Name         string     `json:"name,omitempty"`
	Description  string     `json:"description,omitempty"`
	Logo_url     string     `json:"logo_url,omitempty"`
	Hosted_url   string     `json:"Hosted_url,omitempty"`
	Created_at   *time.Time `json:"created_at,omitempty"`
	Updated_at   *time.Time `json:"updated_at,omitempty"`
	Confirmed_at *time.Time `json:"confirmed_at,omitempty"`
	Checkout     struct {
		Id string `json:"id,omitempty"`
	} `json:"checkout,omitempty"`
	Timeline []struct {
		Time    *time.Time `json:"id,omitempty"`
		Status  string     `json:"status,omitempty"`
		Context string     `json:"context,omitempty"`
	} `json:"timeline,omitempty"`
	Metadata     map[string]interface{} `json:"metadata,omitempty"`
	Pricing_type string                 `json:"pricing_type,omitempty"`
	Pricing      struct {
		Local       Money `json:"local,omitempty"`
		Bitcoin     Money `json:"bitcoin,omitempty"`
		Bitcoincash Money `json:"bitcoincash,omitempty"`
		Ethereum    Money `json:"ethereum,omitempty"`
		Litecoin    Money `json:"litecoin,omitempty"`
	} `json:"pricing,omitempty"`
	Payments []struct {
		Network        string `json:"network,omitepty"`
		Transaction_id string `json:"transaction_id,omitepty"`
		Status         string `json:"status,omitepty"`
		Value          struct {
			Local struct {
				Amount   string `json:"amount,omitepty"`
				Currency string `json:"currency,omitepty"`
			}
			Crypto struct {
				Amount   string `json:"amount,omitepty"`
				Currency string `json:"currency,omitepty"`
			} `json:"crypto,omitepty"`
		} `json:"value,omitepty"`
		Block struct {
			Height                    int    `json:"height,omitepty"`
			Hash                      string `json:"hash,omitepty"`
			Confirmations_accumulated int    `json:"confirmations_accumulated ,omitepty"`
			Confirmations_required    int    `json:"confirmations_required,omitepty"`
		} `json:"block,omitepty"`
	} `json:"payments,omitempty"`
	Addresses struct {
		Bitcoin     string `json:"bitcoin,omitempty"`
		Bitcoincash string `json:"bitcoincash,omitempty"`
		Ethereum    string `json:"ethereum,omitempty"`
		Litecoin    string `json:"litecoin,omitempty"`
	} `json:"addresses,omitempty"`
	Local_price  Money  `json:"local_price,omitempty"`
	RedirectUrl  string `json:"redirect_url"`
	CancelUrl    string `json:"cancel_url"`
	SupportEmail string `json:"support_email"`
}

APIChargeData is the golang struct equivalent of the Charge resource. It's findable inside APICharge object

type APICharges

type APICharges struct {
	Pagination APIPagination `json:"pagination,omitempty"`
	Charges    []APICharge   `json:"data,omitempty"`
	Errors     []APIError    `json:"errors,omitempty"`
}

APICharges is the object API object filled with a list of APICharge object alongside of Pagination information and a list of Errors.

type APIChargesRequest

type APIChargesRequest struct {
	Pagination APIPagination   `json:"pagination,omitempty"`
	Data       []APIChargeData `json:"data,omitempty"`
	Errors     []APIError      `json:"errors,omitempty"`
}

APICharge is the golang struct equivalent of the List charges routes

type APICheckout

type APICheckout struct {
	Data   APICheckoutData `json:"data,omitempty"`
	Errors []APIError      `json:"errors,omitempty"`
	// contains filtered or unexported fields
}

func (*APICheckout) Delete

func (a *APICheckout) Delete() (err error)

Delete the checkout

func (*APICheckout) Refresh

func (a *APICheckout) Refresh() (err error)

Refresh will update attributes and all nested data by making a fresh GET request to the relevant API endpoint.

func (*APICheckout) Save

func (a *APICheckout) Save() (err error)

Save is the object method equivalent of update.

type APICheckoutData

type APICheckoutData struct {
	Id             string   `json:"id,omitempty"`
	Resource       string   `json:"ressource,omitempty"`
	Name           string   `json:"name,omitempty"`
	Description    string   `json:"description,omitempty"`
	Logo_url       string   `json:"logo_url,omitempty"`
	Requested_info []string `json:"requested_info"`
	Pricing_type   string   `json:"pricing_type,omitempty"`
	Local_price    Money    `json:"local_price,omitempty"`
}

type APICheckouts

type APICheckouts struct {
	Pagination APIPagination `json:"pagination,omitempty"`
	Checkouts  []APICheckout `json:"data,omitempty"`
	Errors     []APIError    `json:"errors,omitempty"`
}

type APICheckoutsRequest

type APICheckoutsRequest struct {
	Pagination APIPagination     `json:"pagination,omitempty"`
	Data       []APICheckoutData `json:"data,omitempty"`
	Errors     []APIError        `json:"errors,omitempty"`
}

type APIClient

type APIClient struct {
	Key        string
	Endpoint   string
	ApiVersion string
	Checkout   *ACheckout
	Charge     *ACharge
	Event      *AEvent
}

APIClient is the interface for most of the API calls If Endpoint or ApiVersion aren't defined the library will use the default https://api.commerce.coinbase.com

func Client

func Client(api_key string) (client APIClient)

func (*APIClient) Fetch

func (a *APIClient) Fetch(method, path string, body interface{}, result interface{}) error

Fetch works as a wrapper for all kind of http requests. It requires a http method and a relative path to the API endpoint. It will try to decode all results into a single interface type which you can provide.

type APIError

type APIError struct {
	Type    string `json:"type"`
	Message string `json:"message"`
	Code    int
}

func (*APIError) Error

func (e *APIError) Error() string

type APIEvent

type APIEvent struct {
	Data   APIEventData `json:"data,omitempty"`
	Errors []APIError   `json:"errors,omitempty"`
	// contains filtered or unexported fields
}

func (*APIEvent) Refresh

func (a *APIEvent) Refresh() (err error)

type APIEventData

type APIEventData struct {
	Id          string        `json:"id,omitempty"`
	Resource    string        `json:"ressource,omitempty"`
	Created_at  *time.Time    `json:"created_at,omitempty"`
	Api_version string        `json:"api_version,omitempty"`
	Data        APIChargeData `json:"data,omitempty"`
	Type        string        `json:"type"`
}

type APIEvents

type APIEvents struct {
	Pagination APIPagination `json:"pagination,omitempty"`
	Events     []APIEvent    `json:"data,omitempty"`
	Errors     []APIError    `json:"errors,omitempty"`
}

type APIEventsRequest

type APIEventsRequest struct {
	Pagination APIPagination  `json:"pagination,omitempty"`
	Data       []APIEventData `json:"data,omitempty"`
	Errors     []APIError     `json:"errors,omitempty"`
}

type APIPagination

type APIPagination struct {
	Order          string
	Starting_after string
	Ending_before  string
	Total          int
	Limit          int
	Previous_uri   string
	Next_uri       string
	Yielded        int
	Cursor_range   []string
}

type APIWebHook

type APIWebHook struct {
	Attempt_number int          `json:"attempt_number,omitempty"`
	Data           APIEventData `json:"event,omitempty"`
	Errors         []APIError   `json:"errors,omitempty"`
	// contains filtered or unexported fields
}

type Money

type Money struct {
	Amount   float64 `json:"amount,string,omitempty"`
	Currency string  `json:"currency,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL