monobank

package module
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 25 Imported by: 0

README

go-monobank

Godoc Reference CI codecov

Monobank REST API client.

Features

  • Personal API(with Token authorization).
  • API for providers(corporate) with authorization.
  • Webhooks(including API for providers).
  • Jars(only in Personal API).

Installation

go get github.com/vtopc/go-monobank@latest

This will update yours go.mod file.

Usage examples

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 = "https://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"`
	SendID       string   `json:"sendId"`
	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"      // ФОП
	EAid     CardType = "eAid"     // єПідтримка
)

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 {
	ID         string   `json:"clientId"`
	Name       string   `json:"name"`
	WebHookURL string   `json:"webHookUrl"`
	Accounts   Accounts `json:"accounts"`
	Jars       Jars     `json:"jars"`
}

ClientInfo - client/user info Personal API - https://api.monobank.ua/docs/#/definitions/UserInfo Corporate API - https://api.monobank.ua/docs/corporate.html#/definitions/UserInfo

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

func (*CorpAuthMaker) NewPermissions added in v0.9.0

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

type CorpAuthMakerAPI added in v0.9.0

type CorpAuthMakerAPI interface {
	// New returns corp Authorizer for endpoints with Request ID.
	New(requestID string) Authorizer

	// NewPermissions returns corp Authorizer for Auth endpoint to get Request ID.
	// Omitting permissions means all permissions.
	NewPermissions(permissions ...string) Authorizer
}

type CorpSettings added in v0.21.0

type CorpSettings struct {
	Pubkey     string  `json:"pubkey"`
	Name       string  `json:"name"`
	Permission string  `json:"permission"`
	Webhook    *string `json:"webhook"`
}

type CorporateAPI added in v0.7.0

type CorporateAPI interface {
	CommonAPI

	// Settings returns information about company.
	// https://api.monobank.ua/docs/corporate.html#tag/Avtorizaciya-ta-nalashtuvannya-kompaniyi/paths/~1personal~1corp~1settings/get
	Settings(ctx context.Context) (*CorpSettings, error)

	// Auth initializes client access.
	// https://api.monobank.ua/docs/corporate.html#tag/Kliyentski-personalni-dani/paths/~1personal~1auth~1request/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#tag/Kliyentski-personalni-dani/paths/~1personal~1auth~1request/get
	CheckAuth(ctx context.Context, requestID string) error

	// ClientInfo
	// https://api.monobank.ua/docs/corporate.html#tag/Kliyentski-personalni-dani/paths/~1personal~1client-info/get
	ClientInfo(ctx context.Context, requestID string) (*ClientInfo, error)

	// Transactions - gets bank account statements(transactions)
	// https://api.monobank.ua/docs/corporate.html#tag/Kliyentski-personalni-dani/paths/~1personal~1statement~1{account}~1{from}~1{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) Settings added in v0.21.0

func (c CorporateClient) Settings(ctx context.Context) (*CorpSettings, error)

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 Jar added in v0.19.0

type Jar struct {
	ID           string `json:"id"`
	SendID       string `json:"sendId"`
	Title        string `json:"title"`
	Description  string `json:"description"`
	CurrencyCode int    `json:"currencyCode"`
	Balance      int64  `json:"balance"`
	Goal         int64  `json:"goal"`
}

type Jars added in v0.19.0

type Jars []Jar

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 in the account currency
	Amount int64 `json:"amount"`
	// OperationAmount in the transaction currency or amount after double conversion
	OperationAmount int64 `json:"operationAmount"`
	// ISO 4217 numeric code
	CurrencyCode   int    `json:"currencyCode"`
	CommissionRate int64  `json:"commissionRate"`
	CashbackAmount int64  `json:"cashbackAmount"`
	Balance        int64  `json:"balance"`
	Comment        string `json:"comment"`
	// For withdrawal only.
	ReceiptID string `json:"receiptId"`
	// For fop(ФОП) accounts only.
	InvoiceID string `json:"invoiceId"`
	// For fop(ФОП) accounts only.
	EDRPOU string `json:"counterEdrpou"`
	// For fop(ФОП) accounts only.
	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