monobank

package module
v0.14.0-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2021 License: MIT Imports: 25 Imported by: 0

README

go-monobank

Godoc Reference CI codecov

Monobank REST API client. Supports personal(with Token) and corporate authorizations.

Usage

NOTE: Do not forget to check errors.

Public client
package main

import (
    "context"
    "fmt"

    "github.com/vtopc/go-monobank"
)

func main() {
    // Create public client.
    client := monobank.NewClient(nil)

    response, _ := client.Currency(context.Background())
    fmt.Println(response)
}
Personal client
package main

import (
    "context"
    "fmt"
    "os"

    "github.com/vtopc/go-monobank"
)

func main() {
    token := os.Getenv("TOKEN")

    // Create authorized client.
    client := monobank.NewPersonalClient(nil).WithAuth(monobank.NewPersonalAuthorizer(token))

    response, _ := client.ClientInfo(context.Background())
    fmt.Println(response)
}
Corporate client
package main

import (
    "context"
    "fmt"

    "github.com/vtopc/go-monobank"
)

var secKey []byte // put here you private key
    
const webhook = "http://example.com/webhook"

func main() {
    // Create auth creator.
    authMaker, _ := monobank.NewCorpAuthMaker(secKey)

    // Create authorized client.
    client, _ := monobank.NewCorporateClient(nil, authMaker)

    // If the user is not authorized yet, do next:
    resp, _ := client.Auth(context.Background(), webhook, monobank.PermSt, monobank.PermPI)

    // Send `resp.AcceptURL` to the user and wait until it will authorize your client
    // in Monobank app on mobile, you will get GET request on `webhook` when it will be done.
    // See Documentation for details.
    // Store `resp.RequestID` somewhere.
    requestID := resp.RequestID

    // If user authorized already:
    response, _ := client.ClientInfo(context.Background(), requestID)
    fmt.Println(response)
}

Documentation

Similar projects

TODO

  • More unit tests

Documentation

Overview

Package monobank - is MonoBank API client https://api.monobank.ua/docs/

Index

Constants

View Source
const (
	// PermSt - statements(transactions) and client info of individual(фізичної особи).
	PermSt = "s"
	// PermPI - personal information(first and last names).
	PermPI = "p"
	// PermFOP - statements(transactions) and client info of private entrepreneur(ФОП).
	PermFOP = "f"
)

Permissions.

Variables

View Source
var (
	ErrDecodePrivateKey  = errors.New("failed to decode private key")
	ErrEncodePublicKey   = errors.New("failed to encode public key with sha1")
	ErrNoPrivateKey      = errors.New("failed to find private key block")
	ErrInvalidEC         = errors.New("invalid elliptic curve private key value")
	ErrInvalidPrivateKey = errors.New("invalid private key length")
)

Errors.

View Source
var ErrEmptyAuthMaker = errors.New("authMaker is nil")
View Source
var ErrEmptyRequest = errors.New("empty request")

Functions

This section is empty.

Types

type Account

type Account struct {
	AccountID    string   `json:"id"`
	Balance      int64    `json:"balance"`
	CreditLimit  int64    `json:"creditLimit"`
	CurrencyCode int      `json:"currencyCode"`
	CashbackType string   `json:"cashbackType"` // enum: None, UAH, Miles
	CardMasks    []string `json:"maskedPan"`    // card number masks
	Type         CardType `json:"type"`
	IBAN         string   `json:"iban"`
}

type Accounts

type Accounts []Account

type Authorizer

type Authorizer interface {
	// SetAuth modifies http.Request and sets authorization tokens
	SetAuth(*http.Request) error
}

type CardType added in v0.6.0

type CardType string
const (
	Black    CardType = "black"
	White    CardType = "white"
	Platinum CardType = "platinum"
	FOP      CardType = "fop"
)

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient added in v0.8.0

func NewClient(client *http.Client) Client

NewClient - returns public monobank Client

func (Client) Currency

func (c Client) Currency(ctx context.Context) (Currencies, error)

func (*Client) WithBaseURL

func (c *Client) WithBaseURL(uri string)

WithBaseURL updates baseURL

type ClientInfo

type ClientInfo struct {
	Name       string   `json:"name"`
	WebHookURL string   `json:"webHookUrl"`
	Accounts   Accounts `json:"accounts"`
}

ClientInfo - client/user info

type CommonAPI added in v0.9.0

type CommonAPI interface {
	PublicAPI

	// SetWebHook - sets webhook for statements
	SetWebHook(ctx context.Context, uri string) error
}

type CorpAuth added in v0.9.0

type CorpAuth struct {
	*CorpAuthMaker
	// contains filtered or unexported fields
}

func (CorpAuth) SetAuth added in v0.9.0

func (a CorpAuth) SetAuth(r *http.Request) error

type CorpAuthMaker added in v0.9.0

type CorpAuthMaker struct {
	KeyID string // X-Key-Id - ID key of the service
	// contains filtered or unexported fields
}

func NewCorpAuthMaker added in v0.9.0

func NewCorpAuthMaker(secKey []byte) (*CorpAuthMaker, error)

func (*CorpAuthMaker) New added in v0.9.0

func (c *CorpAuthMaker) New(requestID string) Authorizer

New returns corp Authorizer for endpoints with Request ID.

func (*CorpAuthMaker) NewPermissions added in v0.9.0

func (c *CorpAuthMaker) NewPermissions(permissions ...string) Authorizer

NewPermissions returns corp Authorizer for Auth endpoint to get Request ID

type CorpAuthMakerAPI added in v0.9.0

type CorpAuthMakerAPI interface {
	New(requestID string) Authorizer
	NewPermissions(permissions ...string) Authorizer
}

type CorporateAPI added in v0.7.0

type CorporateAPI interface {
	CommonAPI

	// Auth initializes access.
	// https://api.monobank.ua/docs/corporate.html#operation--personal-auth-request-post
	Auth(ctx context.Context, callbackURL string, permissions ...string) (*TokenRequest, error)

	// CheckAuth checks status of request for client's personal data.
	// https://api.monobank.ua/docs/corporate.html#operation--personal-auth-request-get
	CheckAuth(ctx context.Context, requestID string) error

	// ClientInfo - https://api.monobank.ua/docs/corporate.html#operation--personal-client-info-get
	ClientInfo(ctx context.Context, requestID string) (*ClientInfo, error)

	// Transactions - gets bank account statements(transactions)
	// https://api.monobank.ua/docs/corporate.html#operation--personal-statement--account---from---to--get
	Transactions(ctx context.Context, requestID, accountID string, from, to time.Time) (Transactions, error)
}

type CorporateClient added in v0.8.0

type CorporateClient struct {
	// contains filtered or unexported fields
}

func NewCorporateClient added in v0.8.0

func NewCorporateClient(client *http.Client, authMaker CorpAuthMakerAPI) (CorporateClient, error)

NewCorporateClient returns corporate client

func (CorporateClient) Auth added in v0.8.0

func (c CorporateClient) Auth(ctx context.Context, callbackURL string, permissions ...string) (*TokenRequest, error)

Auth initializes access.

func (CorporateClient) CheckAuth added in v0.8.0

func (c CorporateClient) CheckAuth(ctx context.Context, requestID string) error

func (CorporateClient) ClientInfo added in v0.8.0

func (c CorporateClient) ClientInfo(ctx context.Context, requestID string) (*ClientInfo, error)

func (CorporateClient) SetWebHook added in v0.8.0

func (c CorporateClient) SetWebHook(ctx context.Context, uri string) error

SetWebHook sets webhook for corporate API.

func (CorporateClient) Transactions added in v0.8.0

func (c CorporateClient) Transactions(ctx context.Context, requestID, accountID string, from, to time.Time) (Transactions, error)

type Currencies

type Currencies []Currency

type Currency

type Currency struct {
	CurrencyCodeA int           `json:"currencyCodeA"`
	CurrencyCodeB int           `json:"currencyCodeB"`
	Date          epoch.Seconds `json:"date"`
	RateSell      float64       `json:"rateSell"`
	RateBuy       float64       `json:"rateBuy"`
	RateCross     float64       `json:"rateCross"`
}

type PersAuth added in v0.9.0

type PersAuth struct {
	// contains filtered or unexported fields
}

func NewPersonalAuthorizer

func NewPersonalAuthorizer(token string) PersAuth

func (PersAuth) SetAuth added in v0.9.0

func (a PersAuth) SetAuth(req *http.Request) error

type PersonalAPI added in v0.2.0

type PersonalAPI interface {
	CommonAPI

	// ClientInfo - https://api.monobank.ua/docs/#/definitions/UserInfo
	ClientInfo(context.Context) (*ClientInfo, error)

	// Transactions - gets bank account statements
	// https://api.monobank.ua/docs/#/definitions/StatementItems
	Transactions(ctx context.Context, accountID string, from, to time.Time) (Transactions, error)
}

type PersonalClient added in v0.8.0

type PersonalClient struct {
	// contains filtered or unexported fields
}

func NewPersonalClient added in v0.8.0

func NewPersonalClient(client *http.Client) PersonalClient

func (PersonalClient) ClientInfo added in v0.8.0

func (c PersonalClient) ClientInfo(ctx context.Context) (*ClientInfo, error)

func (PersonalClient) SetWebHook added in v0.8.0

func (c PersonalClient) SetWebHook(ctx context.Context, uri string) error

func (PersonalClient) Transactions added in v0.8.0

func (c PersonalClient) Transactions(ctx context.Context, accountID string, from, to time.Time) (
	Transactions, error)

TODO: make `to` optional

func (PersonalClient) WithAuth added in v0.8.0

func (c PersonalClient) WithAuth(auth Authorizer) PersonalClient

WithAuth returns copy of PersonalClient with authorizer

type PublicAPI added in v0.2.0

type PublicAPI interface {
	// Currency https://api.monobank.ua/docs/#/definitions/CurrencyInfo
	Currency(context.Context) (Currencies, error)
}

type PublicAuthorizer

type PublicAuthorizer struct{}

func NewPublicAuthorizer

func NewPublicAuthorizer() PublicAuthorizer

func (PublicAuthorizer) SetAuth

func (a PublicAuthorizer) SetAuth(_ *http.Request) error

type TokenRequest added in v0.7.0

type TokenRequest struct {
	RequestID string `json:"tokenRequestId"` // Unique token request ID.
	AcceptURL string `json:"acceptUrl"`      // URL to redirect client or build QR on top of it.
}

type Transaction added in v0.2.0

type Transaction struct {
	ID              string        `json:"id"`
	Time            epoch.Seconds `json:"time"`
	Description     string        `json:"description"`
	MCC             int32         `json:"mcc"`
	OriginalMCC     int32         `json:"originalMcc"`
	Hold            bool          `json:"hold"`
	Amount          int64         `json:"amount"`
	OperationAmount int64         `json:"operationAmount"`
	CurrencyCode    int           `json:"currencyCode"`
	CommissionRate  int64         `json:"commissionRate"`
	CashbackAmount  int64         `json:"cashbackAmount"`
	Balance         int64         `json:"balance"`
	Comment         string        `json:"comment"`
	ReceiptID       string        `json:"receiptId"`
	EDRPOU          string        `json:"counterEdrpou"`
	IBAN            string        `json:"counterIban"`
}

Transaction - bank account statement

type Transactions added in v0.2.0

type Transactions []Transaction

Transactions - transactions

type WebHookData

type WebHookData struct {
	AccountID   string      `json:"account"`
	Transaction Transaction `json:"statementItem"`
}

type WebHookRequest

type WebHookRequest struct {
	WebHookURL string `json:"webHookUrl"`
}

type WebHookResponse

type WebHookResponse struct {
	Type string      `json:"type"` // "StatementItem"
	Data WebHookData `json:"data"`
}

Jump to

Keyboard shortcuts

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