revolut

package module
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2018 License: MIT Imports: 8 Imported by: 1

README

Revolut for Business (unofficial) Build Status GoDoc Go Report Card

Go SDK for accessing the Revolut for Business sandbox and production APIs.

What does it do?

This package gives you an SDK to access the Revolut for Business API, as described in their documentation. You can:

  • Retrieve a list of accounts or individual account information
  • Add, remove and view counterparties (businesses and individuals you send money to)
  • Initiate/schedule/cancel payments and check their status
  • Get transaction history
  • Transfer money between accounts
  • Add web-hooks and unmarshal their data

Requirements

Go 1.11 or newer is all you need to get started.

The required packages for the command line tool should install automatically when you go get this:

  • github.com/jessevdk/go-flags
  • github.com/Urethramancer/cross
  • github.com/Urethramancer/slog

Supported operating systems

Most or all systems supported by Go should be able to use this. Testing is mainly done on Unix-like systems, and the CLI tool does not have proper Windows support yet. It will run, but probably won't store its coniguration where you'd like it.

Installing it

go get github.com/Urethramancer/revolut

Using it

Retrieve a list of accounts

To retrieve a slice of accounts:

c := revolut.NewClient("prod_wibblewobblewomble") // Replace this string with your API key
list, _ := c.GetAccounts()

The NewClient() function will select the correct API to use based on the format of the key. Errors will be returned if the key looks wrong or when unable to connect.

Retrieve banking details for an account

Continuing from the code above, you could do this to get a slice of bank details for the first account:

details := c.GetAccountDetails(list[0].ID)
Transferring between own accounts

This transfers money within accounts on one API key:

// Custom request ID, source account, destination account, currency, optional message, amount
resp, err := c.Transfer("2cbd8cd60026c6b03f8c576c81f9a39dcf251e2b.", "374e6066-3830-4000-abbf-b2e240349000", "a4f9667b-4435-4dea-bad5-74cc4de9c2bf", "GBP, "I like GBP better", 200000)

The response is a TransferResponse, containing a new UUID for this request and its status. The reason field will contain an explanation if status is anything but "completed". Note that the currency must match the currency of the receiving account. You can't transfer GBP to an account set to USD.

Retrieve counterparties

Paying external recipients (counterparties) can be done like this:

resp, err := c.Pay(id, cmd.Args.Account, cmd.Args.Counterparty, cmd.RecAccount, cmd.Args.Currency, cmd.Reference, cmd.ScheduleTime, cmd.Args.Amount)

The response is a PaymentResponse, similar to transfers.

List transactions

All transfers and payments can be retrieved, optionally filtered by start and end dates, the receiving counterparty, a type of transaction and maximum number to show:

tr, err := c.GetTransactions("fee", "2018-11-20", "2018-12-02", "", 500) // Only fee transactions between two dates, any counterparty, max 500

This returns a slice of TransactionStatus structures, each containing a slice of Legs with information about the journey of the transaction.

Documentation

Overview

Package revolut provides an SDK to access the Revolut for Business API.

Index

Constants

View Source
const (

	// EventCreated is for TransactionCreated hooks.
	EventCreated = "TransactionCreated"
	// EventStateChange is for TransactionStateChanged hooks.
	EventStateChange = "TransactionStateChanged"
)
View Source
const (
	// ErrKeyFormat means the API key string is mangled.
	ErrKeyFormat = "API key has the wrong format - not starting with sand_ or prod_"
)

Internal errors

Variables

This section is empty.

Functions

func ValidKey

func ValidKey(s string) bool

ValidKey checks that the supplied string conforms roughly to a valid Revolut API key's format.

func ValidTransactionType added in v0.15.0

func ValidTransactionType(t string) bool

ValidTransactionType checks if a type filter is correct.

Types

type Account

type Account struct {
	// ID is the UUID, and is always available.
	ID string `json:"id"`
	// Name is the display name. Not used in counterparty responses.
	Name string `json:"name,omitempty"`
	// Balance is not used in counterparty responses.
	Balance float64 `json:"balance,omitempty"`
	// Currency is always available.
	Currency string `json:"currency"`
	// State is not used in counterparty responses.
	State string `json:"state,omitempty"`
	// Public is not used in counterparty responses.
	Public bool `json:"public,omitempty"`
	// Created is an ISO date/time. Not used in counterparty responses.
	Created string `json:"created_at,omitempty"`
	// Updated is an ISO date/time. Mot used in counterparty responses.
	Updated string `json:"updated_at,omitempty"`
	// Type is only used in counterparty responses.
	Type string `json:"type,omitempty"`
}

Account holds one business account, or the response from adding a counterparty.

type Address

type Address struct {
	// Street1 is line 1 of the address.
	Street1 string `json:"street_line1"`
	// Street2 is line 2 of the address.
	Street2 string `json:"street_line2"`
	// Region of the beneficiary.
	Region string `json:"region"`
	// City of the beneficiary.
	City string `json:"city"`
	// Country of the beneficiary. This is a two-letter ISO code.
	Country string `json:"country"`
	// Postcode of the beneficiary.
	Postcode string `json:"postcode"`
}

Address is an account-holder's address.

type BankDetails

type BankDetails struct {
	// IBAN if applicable.
	IBAN string `json:"iban,omitempty"`
	// BIC if applicable.
	BIC string `json:"bic,omitempty"`
	// AccountNo if applicable.
	AccountNo string `json:"account_no,omitempty"`
	// SortCode if applicable.
	SortCode string `json:"sort_code,omitempty"`
	// RoutingNo for the bank.
	RoutingNo string `json:"routing_number,omitempty"`
	// Beneficiary name.
	Beneficiary string `json:"beneficiary"`
	// BeneficiaryAddress is a sub-structure.
	Address Address `json:"beneficiary_address"`
	// Country of the bank. This is a two-letter ISO code.
	Country string `json:"bank_country"`
	// Pooled or unique status.
	Pooled bool `json:"pooled"`
	// UniqueReference of the pooled account.
	UniqueReference string `json:"unique_reference,omitempty"`
	//Schemes is one of: chaps, bacs, faster_payments, sepa, swift, ach
	Schemes []string `json:"schemes"`
	// EstimatedTime for transfers.
	EstimatedTime EstimatedTime `json:"estimated_time"`
}

BankDetails can be retrieved for an account ID.

type Card

type Card struct {
	// Number is the masked card number.
	Number string `json:"card_number"`
	// First name of the card holder.
	First string `json:"first_name"`
	// Last name of the card holder.
	Last string `json:"last_name"`
	// Phone number of the card holder.
	Phone string `json:"phone"`
}

Card is used for card payments.

type Client

type Client struct {
	http.Client

	//ErrorCode is the last HTTP error code.
	ErrorCode int
	// Agent should be customised per app.
	Agent string
	// contains filtered or unexported fields
}

Client is the core structure for Revolut API access.

func NewClient

func NewClient(key string) (*Client, error)

NewClient creates a new Revolut client with some reasonable HTTP request defaults.

func (*Client) AddExternalCounterparty

func (c *Client) AddExternalCounterparty(cp ExternalCounterparty) (*ExternalCounterpartyResponse, error)

AddExternalCounterparty adds a non-Revolut account as a counterparty.

func (*Client) AddRevolutCounterparty

func (c *Client) AddRevolutCounterparty(cp InternalCounterparty) (*CounterpartyResponse, error)

AddRevolutCounterparty adds a Revolut personal or business account as a counterparty.

func (*Client) CancelPayment added in v0.16.0

func (c *Client) CancelPayment(id string) error

CancelPayment if possible.

func (*Client) Delete

func (c *Client) Delete(path string) ([]byte, int, error)

Delete sends a delete command to an endpoint. The URL is the data and the HTTP response code is the only result.

func (*Client) DeleteCounterparty

func (c *Client) DeleteCounterparty(id string) error

DeleteCounterparty removes a counterparty by UUID.

func (*Client) GetAccount

func (c *Client) GetAccount(id string) (*Account, error)

GetAccount retrieves the basic information for a given account ID.

func (*Client) GetAccountDetails

func (c *Client) GetAccountDetails(id string) ([]BankDetails, error)

GetAccountDetails retrieves BankDetails for one specified account.

func (*Client) GetAccounts

func (c *Client) GetAccounts() ([]Account, error)

GetAccounts lists the accounts for a given API key.

func (*Client) GetCounterparties

func (c *Client) GetCounterparties() ([]Counterparty, error)

GetCounterparties returns a list of all counterparties for an API key.

func (*Client) GetCounterparty

func (c *Client) GetCounterparty(id string) (*Counterparty, error)

GetCounterparty gets a counterparty by ID.

func (*Client) GetJSON

func (c *Client) GetJSON(path string) ([]byte, int, error)

GetJSON builds the full endpoint path and gets the raw JSON.

func (*Client) GetTransactions

func (c *Client) GetTransactions(ttype, from, to, counterparty string, count int64) ([]TransactionStatus, error)

GetTransactions by optional filters. The from and to datescan be in the formats YYYY-MM-DD or RFC3339.

func (*Client) Pay added in v0.14.0

func (c *Client) Pay(id, account, cp, cpAccount, currency, reference, schedule string, amount float64) (*PaymentResponse, error)

Pay a Revolut account or external account.

func (*Client) PostJSON

func (c *Client) PostJSON(path string, data interface{}) ([]byte, int, error)

PostJSON builds the full endpoint path and posts the provided data, returning the JSON response.

func (*Client) SetAPI

func (c *Client) SetAPI(key string) error

SetAPI sets the API key and type to use (sandbox or production). Sandbox keys start with "sand_" and production keys start with "prod_".

func (*Client) TransactionStatus added in v0.15.0

func (c *Client) TransactionStatus(id string) (*TransactionStatus, error)

TransactionStatus of transfers or payments.

func (*Client) Transfer added in v0.13.0

func (c *Client) Transfer(id, sid, tid, currency, reference string, amount float64) (*TransferResponse, error)

Transfer money between own accounts.

type Counterparty

type Counterparty struct {
	// ID is a UUID.
	ID string `json:"id"`
	// Name of the counterparty.
	Name string `json:"name"`
	// Phone number.
	Phone string `json:"phone"`
	// Type is "personal" or "business".
	Type string `json:"profile_type"`
	// Country is a two-letter ISO code.
	Country string `json:"country"`
	// State of the counterparty is a status string.
	State string `json:"state"`
	// CreatedAt is a timestamp for when this was added.
	CreatedAt time.Time `json:"created_at"`
	// UpdatedAt is a timestamp for the last change to the counterparty,
	UpdatedAt time.Time `json:"updated_at"`
	// Accounts is a list of public accounts for this counterparty.
	Accounts []CounterpartyAccount `json:"accounts"`
}

Counterparty is returned from the /counterparty and /counterparties endpoints.

type CounterpartyAccount

type CounterpartyAccount struct {
	// ID is the UUID.
	ID string `json:"id"`
	// Currency is a three-letter shortname.
	Currency string `json:"currency"`
	// Type of account is either "revolut" or "external".
	Type string `json:"type"`
	// Account number.
	Account string `json:"account_no"`
	// SortCode if used.
	SortCode string `json:"sort_code"`
	// Email for the recipient.
	Email string `json:"email"`
	// Name of the business or person this account belongs to.
	Name string `json:"name"`
	// Country is a two-letter ISO code.
	Country string `json:"bank_country"`
	// Charges may be added.
	Charges string `json:"recipient_charges"`
}

CounterpartyAccount is embedded in Counterparty structures.

type CounterpartyResponse

type CounterpartyResponse struct {
	// ID is the UUID of the counterparty.
	ID string `json:"id"`
	// Name of the counterparty.
	Name string `json:"name"`
	// Phone number of a personal account.
	Phone string `json:"phone"`
	// ProfileType is "business" or "personal".
	ProfileType string `json:"profile_type"`
	// Country is a 2-letter code.
	Country string `json:"bank_country"`
	// State is either "created" or "deleted".
	State string `json:"state"`
	// CreatedAt is the ISO time when the counterparty was created.
	CreatedAt string `json:"created_at"`
	// UpdateAt is the ISO time when the counterparty was last updated.
	UpdatedAt string `json:"updated_at"`
	// Accounts is a list of all the counterparty's accounts.
	Accounts []Account `json:"accounts"`
}

CounterpartyResponse is returned after adding/removing a Revolut counterparty.

type ErrorResponse added in v0.13.0

type ErrorResponse struct {
	// Message is a clarification of what went wrong.
	Message string `json:"message"`
	// Code is an internal error code.
	Code int `json:"code"`
}

ErrorResponse from JSON endpoints.

type EstimatedTime

type EstimatedTime struct {
	// Unit is "days" or "hours".
	Unit string `json:"unit"`
	// Max days or hours.
	Max int `json:"max"`
	// Min days or hours.
	Min int `json:"min"`
}

EstimatedTime for transfers.

type ExternalAccount

type ExternalAccount struct {
	// ID of a counterparty's account.
	ID string `json:"id"`
	// Currency is a 3-letter ISO code.
	Currency string `json:"currency"`
	// Type is "revolut" or "external".
	Type string `json:"type"`
	// AccountNo for UK GBP, US USD and SWIFT accounts
	AccountNo string `json:"account_no"`
	// IBAN of a foreign account.
	IBAN string `json:"iban"`
	// SortCode of a UK GBP account.
	SortCode string `json:"sort_code"`
	// RoutingNo of a US USD account
	RoutingNo string `json:"routing_number"`
	// BIC of an IBAN/SWIFT account.
	BIC string `json:"bic"`
	// RecipientCharges is "no", "expected" or possible "free". TODO: Clarify with devs.
	RecipientCharges string `json:"recipient_charges"`
}

ExternalAccount for counterparties in other banks.

type ExternalCounterparty

type ExternalCounterparty struct {
	// Company must exist if Individual isn't present.
	Company string `json:"company_name,omitempty"`
	// Individual must exist if Company isn't present.
	Name *IndividualName `json:"individual_name,omitempty"`
	// BankCountry is a two-letter ISO code.
	BankCountry string `json:"bank_country"`
	// Currency is a 3-letter ISO code.
	Currency string `json:"currency"`
	// Email is convenient, but optional.
	Email string `json:"email,omitempty"`
	// Phone is convenient, but optional.
	Phone string `json:"phone,omitempty"`
	// Address is optional, although some bank systems require them to verify recipients.
	Address Address `json:"address,omitempty"`
	// AccountNo is required for UK GBP, US USD and SWIFT accounts.
	AccountNo string `json:"account_no"`
	// SortCode is required for UK GBP accounts.
	SortCode string `json:"sort_code,omitempty"`
	// RoutingNo is required for US USD accounts.
	RoutingNo string `json:"routing_number,omitempty"`
	// IBAN is required for IBAN countries.
	IBAN string `json:"iban,omitempty"`
	// BIC is required for IBAN/SWIFT accounts.
	BIC string `json:"bic,omitempty"`
}

ExternalCounterparty is used when adding a counterparty with a non-Revolut account.

type ExternalCounterpartyResponse

type ExternalCounterpartyResponse struct {
	// ID is the UUID of the counterparty.
	ID string `json:"id"`
	// Name
	Name string `json:"name"`
	// State is either "created" or "deleted".
	State string `json:"state"`
	// CreatedAt is the ISO time/date this counterparty was created.
	CreatedAt string `json:"created_at"`
	// UpdatedAt is the ISO time/date this counterparty was last modified.
	UpdatedAt string `json:"updated_at"`
	// Accounts known for this counterparty.
	Accounts []ExternalAccount `json:"accounts"`
}

ExternalCounterpartyResponse is returned after adding/removing an external counterparty.

type IndividualName

type IndividualName struct {
	// First name.
	First string `json:"first_name,omitempty"`
	// Last name.
	Last string `json:"last_name,omitempty"`
}

IndividualName of an account holder.

type InternalCounterparty

type InternalCounterparty struct {
	// ProfileType is "business" or "personal".
	ProfileType string `json:"profile_type"`
	// Name of the counterparty.
	Name string `json:"name,omitempty"`
	// Phone is used with personal accounts.
	Phone string `json:"phone,omitempty"`
	// Email is an optional field with the address of the admin for a business account.
	Email string `json:"email,omitempty"`
}

InternalCounterparty is used when adding an existing Revolut account as a counterparty (i.e. contact).

type Leg

type Leg struct {
	// ID of this leg.
	ID string `json:"leg_id"`
	// Amount of the transactiob.
	Amount float64 `json:"amount"`
	// Currency is the 3-letter ISO code for the transaction currency.
	Currency string `json:"currency"`
	// BillAmount is the amount for cross-currency transactions.
	BillAmount float64 `json:"bill_amount,omitempty"`
	// BillCurrency is the billing currency for cross-currency transactions.
	BillCurrency string `json:"bill_currency,omitempty"`
	// AccountID of the account this transaction is associated with.
	AccountID string `json:"account_id"`
	// Counterparty for this leg.
	Counterparty LegCounterparty `json:"counterparty"`
	// Description contains the leg purpose.
	Description string `json:"description"`
	// Card information only for card payments.
	Card Card `json:"card,omitempty"`
}

Leg of the transaction process.

type LegCounterparty

type LegCounterparty struct {
	ID string `json:"id"`
	// Type is "self", "revolut" or "external".
	Type      string `json:"type"`
	AccountID string `json:"account_id"`
}

LegCounterparty is a quick summary of the counterparty involved in this leg of the transaction.

type Merchant

type Merchant struct {
	// Name of the merchant.
	Name string `json:"name"`
	// City of the merchant.
	City string `json:"city"`
	// Category code for the transaction.
	Category string `json:"category_code"`
	// Country is the 3-letter ISO bank country code.
	Country string `json:"country"`
}

Merchant info.

type PaymentRequest

type PaymentRequest struct {
	// RequestID for the payment, provided by the client.
	RequestID string `json:"request_id"`
	// AccountID of the account to pay from.
	AccountID string `json:"account_id"`
	// Receiver of this payment.
	Receiver Receiver `json:"receiver"`
	// Amount to pay.
	Amount float64 `json:"amount"`
	// Currency for the transaction. 3-letter ISO code.
	Currency string `json:"currency"`
	// Reference is an optional text to show on the transaction. Highly recommended.
	Reference string `json:"reference,omitempty"`
	// ScheduleTime to initiate the payment. There's no guarantee this will be fulfilled right away if the current time is used.
	ScheduleTime string `json:"schedule_for,omitempty"`
}

PaymentRequest for payments to counterparties.

type PaymentResponse

type PaymentResponse struct {
	// ID of the created transaction.
	ID string `json:"id"`
	// State is one of "pending", "completed", "declined" or "failed".
	State string `json:"state"`
	// Reason is a code for the "declined" or "failed" states.
	Reason string `json:"reason_code"`
	// CreatedAt is the ISO time when the payment was requested.
	CreatedAt string `json:"created_at"`
	// CompletedAt is the ISO time when the payment finished. Not available for asynchronous or scheduled payments.
	CompletedAt string `json:"completed_at"`
}

PaymentResponse for a request.

type Receiver

type Receiver struct {
	// CounterpartyID for the receiving party.
	CounterpartyID string `json:"counterparty_id"`
	// AccountID is optional.
	AccountID string `json:"account_id,omitempty"`
}

Receiver of a payment.

type TransactionStatus

type TransactionStatus struct {
	// ID of the transaction.
	ID string `json:"id"`
	// Type of transaction.
	Type string `json:"type"`
	// RequestID provided by the client.
	RequestID string `json:"request_id"`
	// State is one of "pending", "completed", "declined" or "failed".
	State string `json:"state"`
	// Reason code for the "declined" and "failed" states.
	Reason string `json:"reason_code"`
	// CreatedAt is an ISO date/time.
	CreatedAt string `json:"created_at"`
	// UpdatedAt is an ISO date/time. Available when looking up transactions.
	UpdatedAt string `json:"updated_at,omitempty"`
	// CompletedAt is an ISO date/time.
	CompletedAt string `json:"completed_at,omitempty"`
	// Scheduled time is an ISO date/time the transaction was scheduled to run.
	ScheduledTime string `json:"scheduled_for"`
	// Merchant info.
	Merchant Merchant `json:"merchant"`
	// Reference for the payment provided by the user.
	Reference string `json:"reference"`
	// Legs of the transaction. There will be 2 legs between your Revolut accounts and 1 in other cases.
	Legs []Leg `json:"legs"`
}

TransactionStatus is returned by GetTransaction() and GetTransactions().

type TransferRequest

type TransferRequest struct {
	// Amount to transfer.
	Amount float64 `json:"amount"`
	// ID of the request, provided by the client.
	ID string `json:"request_id"`
	// SourceID of the account to transfer from.
	SourceID string `json:"source_account_id"`
	// TargetID of the account to transfer to.
	TargetID string `json:"target_account_id"`
	// Currency to use.
	Currency string `json:"currency"`
	// Reference is an optional text to show on the transaction. Highly recommended.
	Reference string `json:"reference"`
}

TransferRequest for money transfers within a business.

type TransferResponse

type TransferResponse struct {
	// ID of the created transaction.
	ID string `json:"id"`
	// State of the transaction. One of the following: "pending", "completed", "declined" or "failed".
	State string `json:"state"`
	// CreatedAT ISO date/time.
	CreatedAt string `json:"created_at"`
	// CompletedAt ISO date/time.
	CompletedAt string `json:"completed_at"`
}

TransferResponse to a transfer request.

Directories

Path Synopsis
cmd
revolut
Configuration editing from the command line.
Configuration editing from the command line.

Jump to

Keyboard shortcuts

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