p24

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

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

Go to latest
Published: Sep 21, 2022 License: MIT Imports: 16 Imported by: 2

README

p24 Build Status Go Report Card Coverage Status

This library provides privat24 marchant information api client.

Note: before using p24 you need to register merchant in privat24 system.

Install

go get -u github.com/dimboknv/p24

Usage

Also, you can visit p24-cli for an example with retryable, rate limited, logged client.

package main

import (
	"fmt"
	"log"
	"context"
	"net/http"
	"time"
	"github.com/dimboknv/p24"
)

func main() {
	client := p24.NewClient(p24.ClientOpts{
		HTTP: &http.Client{},
		Merchant: p24.Merchant{
			ID:   "merchant id",
			Pass: "merchant pass",
		},
	})

	// get merchant statements list for 2021-12-25 - 2021-12-27 date range
	// and "1234567891234567" card number
	ctx := context.Background()
	statements, err := client.GetStatements(ctx, p24.StatementsOpts{
		StartDate:  time.Date(2021, 12, 25, 0, 0, 0, 0, time.Local),
		EndDate:    time.Date(2021, 12, 27, 0, 0, 0, 0, time.Local),
		CardNumber: "1234567891234567",
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(statements)

	// get merchant card balance for "1234567891234567" card number
	cardBalance, err := client.GetCardBalance(ctx, p24.BalanceOpts{
		CardNumber: "1234567891234567",
		Country:    "UA",
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(cardBalance)
}

Documentation

Index

Constants

View Source
const (
	// DecimalPrecision of Amount
	DecimalPrecision int64 = 100
)

Variables

This section is empty.

Functions

func CheckCardNumber

func CheckCardNumber(card string) error

CheckCardNumber returns an error if card number is not valid

func NewKievLocation

func NewKievLocation() *time.Location

NewKievLocation returns time.Location of Europe/Kiev time zone

Types

type Amount

type Amount int64

Amount represents p24 amount. It is float value with accurate to two decimal places for example 123.45, -12, 12.02, 33.20 . All number after two decimal places will be skipped during parsing. "123.6789" parsed to 123.67

func (Amount) Float64

func (a Amount) Float64() float64

Float64 returns float64 representation of a. It returns 0 if a > math.MaxFloat64

func (Amount) MarshalText

func (a Amount) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for a

func (Amount) String

func (a Amount) String() string

String returns the decimal representation of a

func (*Amount) UnmarshalText

func (a *Amount) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for a. All number after two decimal places will be skipped during parsing. "123.6789" parsed to 123.67

type BalanceOpts

type BalanceOpts struct {
	CardNumber string
	Country    string
	CommonOpts
}

BalanceOpts is sets of options required for performs p24 card balance request

type Card

type Card struct {
	Account  string `xml:"account"`
	Number   string `xml:"card_number"`
	AccName  string `xml:"acc_name"`
	AccType  string `xml:"acc_type"`
	Currency string `xml:"currency"`
	Type     string `xml:"card_type"`
	MainCard string `xml:"main_card_number"`
	Status   string `xml:"card_stat"`
	Src      string `xml:"src"`
}

Card represents state of a p24 merchant card

type CardBalance

type CardBalance struct {
	Date       time.Time `xml:"-"`
	Dyn        string    `xml:"bal_dyn"`
	Card       Card      `xml:"card"`
	Available  Amount    `xml:"av_balance"`
	Balance    Amount    `xml:"balance"`
	FinLimit   Amount    `xml:"fin_limit"`
	TradeLimit Amount    `xml:"trade_limit"`
}

CardBalance is struct for mapping p24 card balance response. Represents balance of a p24 merchant card

func (CardBalance) MarshalXML

func (cb CardBalance) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements xml.Marshaler interface for cb

func (*CardBalance) UnmarshalXML

func (cb *CardBalance) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements xml.Unmarshaler interface for cb

type Client

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

Client performs p24 api calls with given Doer, Merchant, Logger. Implements p24 information API client. see: https://api.privatbank.ua/#p24/main

func NewClient

func NewClient(opts ClientOpts) *Client

NewClient returns Client instance with given opts

func (*Client) DoContext

func (c *Client) DoContext(ctx context.Context, url, method string, req Request, resp *Response) error

DoContext performs a p24 http api call with given url, method, request and unmarshal response body to resp if no errors occurred nolint:gocyclo // Is a complexity function

func (*Client) GetCardBalance

func (c *Client) GetCardBalance(ctx context.Context, opts BalanceOpts) (CardBalance, error)

GetCardBalance returns CardBalance for given opts. Performs p24 card balance api call. see: https://api.privatbank.ua/#p24/balance

func (*Client) GetStatements

func (c *Client) GetStatements(ctx context.Context, opts StatementsOpts) (Statements, error)

GetStatements returns Statements for given opts. Performs p24 orders api call. see: https://api.privatbank.ua/#p24/orders

type ClientOpts

type ClientOpts struct {
	HTTP     Doer
	Log      Logger
	Merchant Merchant
}

ClientOpts is a full set of all parameters to initialize Client

type CommonOpts

type CommonOpts struct {
	Oper string `xml:"oper"`
	Wait int    `xml:"wait"`
	Test int    `xml:"test"`
}

CommonOpts store common request options used across all p24 requests

func DefaultCommonOpts

func DefaultCommonOpts() CommonOpts

DefaultCommonOpts returns default CommonOpts

type DoFunc

type DoFunc func(req *http.Request) (*http.Response, error)

DoFunc type is an adapter to allow the use of ordinary functions as Doer

func (DoFunc) Do

func (do DoFunc) Do(req *http.Request) (*http.Response, error)

Do calls do(req)

type Doer

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
}

Doer defines minimal http client interface

type Error

type Error struct {
	Err         error
	URL, Method string
	Req, Resp   []byte
}

Error reports an error and the Method, URL, response/request body that caused it

func (*Error) Cause

func (e *Error) Cause() error

func (*Error) Error

func (e *Error) Error() (err string)

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Funds

type Funds struct {
	Currency string
	Amount   Amount
}

Funds represents p24 funds with special currency code and amount value. Funds string representation is "<amount> <currency>", <currency> can be empty string for example "23.12 UAH", "-12 USD", "0.0 "

func (Funds) MarshalText

func (f Funds) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for funds

func (Funds) String

func (f Funds) String() string

String returns string representation of funds

func (*Funds) UnmarshalText

func (f *Funds) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for funds

type LogFunc

type LogFunc func(format string, args ...interface{})

LogFunc type is an adapter to allow the use of ordinary functions as Logger

func (LogFunc) Logf

func (f LogFunc) Logf(format string, args ...interface{})

Logf calls funds(id)

type Logger

type Logger interface {
	Logf(format string, args ...interface{})
}

Logger defines minimal logger interface

type Merchant

type Merchant struct {
	ID   string
	Pass string
}

Merchant store p24 merchant id and password see: https://api.privatbank.ua/#p24/registration

func (Merchant) Sign

func (m Merchant) Sign(data []byte) MerchantSign

Sign returns MerchantSign of provided data

func (Merchant) VerifySign

func (m Merchant) VerifySign(data []byte, dataSign MerchantSign) error

VerifySign returns an error if dataSign not from merchant.Sign(data)

type MerchantSign

type MerchantSign struct {
	ID   string `xml:"id"`
	Sign string `xml:"signature"`
}

MerchantSign store p24 merchant id and signature. It required for all p24 responses/requests

type Request

type Request struct {
	XMLName      xml.Name     `xml:"request"`
	Version      string       `xml:"version,attr"`
	MerchantSign MerchantSign `xml:"merchant"`
	Data         RequestData  `xml:"data"`
}

Request is struct for mapping p24 api request

func NewRequest

func NewRequest(m Merchant, reqData RequestData) Request

NewRequest returns Request with MerchantSign of reqData

type RequestData

type RequestData struct {
	XMLName xml.Name `xml:"data"`
	Payment struct {
		ID   string `xml:"id,attr"`
		Prop []struct {
			Name  string `xml:"name,attr"`
			Value string `xml:"value,attr"`
		} `xml:"prop"`
	} `xml:"payment"`
	CommonOpts
}

RequestData is struct for mapping p24 request data

type Response

type Response struct {
	Data         ResponseData `xml:"data"`
	XMLName      xml.Name     `xml:"response"`
	MerchantSign MerchantSign `xml:"merchant"`
	Version      string       `xml:"version,attr"`
}

Response is struct for mapping p24 api response

type ResponseData

type ResponseData struct {
	Info    interface{} `xml:"info"`
	XMLName xml.Name    `xml:"data"`
	Oper    string      `xml:"oper"`
}

ResponseData is struct for mapping p24 response data

func (*ResponseData) UnmarshalXML

func (rd *ResponseData) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implement xml.Unmarshaler interface with decoding to 'Info interface{}' field. Info must be not zero value. nolint:gocyclo // UnmarshalXML is a complexity operation

type Statement

type Statement struct {
	Card        string    `xml:"card,attr"`
	Appcode     string    `xml:"appcode,attr"`
	Date        time.Time `xml:"-"`
	Terminal    string    `xml:"terminal,attr"`
	Description string    `xml:"description,attr"`
	Amount      Funds     `xml:"amount,attr"`
	CardAmount  Funds     `xml:"cardamount,attr"`
	Rest        Funds     `xml:"rest,attr"`
}

Statement represents a Statement of a p24 merchant

func (Statement) MarshalXML

func (s Statement) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements xml.Marshaler interface for s

func (*Statement) UnmarshalXML

func (s *Statement) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements xml.Unmarshaler interface for s

type Statements

type Statements struct {
	Status     string      `xml:"status,attr"`
	Statements []Statement `xml:"statement"`
	Credit     Amount      `xml:"credit,attr"`
	Debet      Amount      `xml:"debet,attr"`
}

Statements struct for mapping p24 get statements response. Represents statements list of a p24 merchant

type StatementsOpts

type StatementsOpts struct {
	StartDate  time.Time
	EndDate    time.Time
	CardNumber string
	CommonOpts
}

StatementsOpts is sets of options required for performs p24 statements request

func (StatementsOpts) Validate

func (r StatementsOpts) Validate() error

Validate r for current date range and card number. P24 statements api provide date range with max 90 days

Jump to

Keyboard shortcuts

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