Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d9bfa5ff7f | |||
|
|
b70e95ac5c | ||
| ac4f4670bb | |||
| 4a8b5cf0c5 | |||
| b33620f629 | |||
| 8006fd0935 | |||
| 4f9ca56157 | |||
| 328447f079 | |||
| 854187f98e |
@@ -3,15 +3,26 @@ package api
|
|||||||
import "resty.dev/v3"
|
import "resty.dev/v3"
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
token string
|
token string
|
||||||
resty *resty.Client
|
resty *resty.Client
|
||||||
apiBaseURL string
|
apiBaseURL string
|
||||||
|
fiscalEndpoint string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(token string) *Client {
|
func NewClient(token string) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
token: token,
|
token: token,
|
||||||
resty: resty.New(),
|
resty: resty.New(),
|
||||||
apiBaseURL: "https://kasa.vchasno.ua/api/v3",
|
apiBaseURL: "https://kasa.vchasno.ua/api/v3",
|
||||||
|
fiscalEndpoint: "/fiscal/execute",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDMClient(token string, dmURL string) *Client {
|
||||||
|
return &Client{
|
||||||
|
token: token,
|
||||||
|
resty: resty.New(),
|
||||||
|
apiBaseURL: dmURL,
|
||||||
|
fiscalEndpoint: "/dm/fiscal",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, response interface{}) error {
|
func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, response interface{}) error {
|
||||||
|
//execute request with json body in request
|
||||||
|
reqJson, err := json.Marshal(request)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := c.resty.R().
|
resp, err := c.resty.R().
|
||||||
SetContext(ctx).
|
SetContext(ctx).
|
||||||
SetHeader("Authorization", c.token).
|
SetHeader("Authorization", c.token).
|
||||||
SetBody(request).
|
SetBody(reqJson).
|
||||||
Post(c.apiBaseURL + "/fiscal/execute")
|
Post(c.apiBaseURL + c.fiscalEndpoint)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("request failed: %w", err)
|
return fmt.Errorf("request failed: %w", err)
|
||||||
@@ -71,15 +77,16 @@ func (c *Client) CloseShift(ctx context.Context, cashier string) (*ZReportRespon
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SellParams struct {
|
type SellParams struct {
|
||||||
Cashier string
|
Cashier string
|
||||||
Source string
|
Source string
|
||||||
Rows []ReceiptRow
|
Rows []ReceiptRow
|
||||||
Pays []ReceiptPay
|
Pays []ReceiptPay
|
||||||
Userinfo *Userinfo
|
Userinfo *Userinfo
|
||||||
|
CommentUP string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Sell(ctx context.Context, params SellParams) (*SellResponse, error) {
|
func (c *Client) Sell(ctx context.Context, params SellParams) (*SellResponse, error) {
|
||||||
receipt := NewReceipt(params.Rows, params.Pays)
|
receipt := NewReceipt(params.Rows, params.Pays, params.CommentUP)
|
||||||
|
|
||||||
request := FiscalRequest{
|
request := FiscalRequest{
|
||||||
Source: params.Source,
|
Source: params.Source,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func NewReceiptPayCash(sum float64, comment string) ReceiptPay {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReceiptPayCard(sum float64, cardmask, bankID, rrnCode, authCode string) ReceiptPay {
|
func NewReceiptPayCard(sum float64, cardmask, bankID, rrnCode, authCode, terminalID, bankName string) ReceiptPay {
|
||||||
return ReceiptPay{
|
return ReceiptPay{
|
||||||
Type: PayTypeCard,
|
Type: PayTypeCard,
|
||||||
Sum: sum,
|
Sum: sum,
|
||||||
@@ -26,6 +26,8 @@ func NewReceiptPayCard(sum float64, cardmask, bankID, rrnCode, authCode string)
|
|||||||
BankID: bankID,
|
BankID: bankID,
|
||||||
Rrn: rrnCode,
|
Rrn: rrnCode,
|
||||||
AuthCode: authCode,
|
AuthCode: authCode,
|
||||||
|
TermID: terminalID,
|
||||||
|
BankName: bankName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,13 +39,14 @@ func CalculateReceiptSum(rows []ReceiptRow) float64 {
|
|||||||
return sum
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReceipt(rows []ReceiptRow, pays []ReceiptPay) Receipt {
|
func NewReceipt(rows []ReceiptRow, pays []ReceiptPay, commentUp string) Receipt {
|
||||||
return Receipt{
|
return Receipt{
|
||||||
Sum: CalculateReceiptSum(rows),
|
Sum: CalculateReceiptSum(rows),
|
||||||
Round: 0.00,
|
Round: 0.00,
|
||||||
Disc: 0,
|
CommentUp: commentUp,
|
||||||
DiscType: 0,
|
Disc: 0,
|
||||||
Rows: rows,
|
DiscType: 0,
|
||||||
Pays: pays,
|
Rows: rows,
|
||||||
|
Pays: pays,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
type FiscalRequest struct {
|
type FiscalRequest struct {
|
||||||
Source string `json:"source"`
|
Source string `json:"source"`
|
||||||
|
Device string `json:"device,omitempty"`
|
||||||
Userinfo Userinfo `json:"userinfo,omitempty"`
|
Userinfo Userinfo `json:"userinfo,omitempty"`
|
||||||
Fiscal Fiscal `json:"fiscal"`
|
Fiscal Fiscal `json:"fiscal"`
|
||||||
}
|
}
|
||||||
|
|||||||
70
vchasno.go
70
vchasno.go
@@ -25,11 +25,13 @@ type Client struct {
|
|||||||
defaults *DefaultParams
|
defaults *DefaultParams
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// !! If you are using Device Manager, set the CustomURL to "http(s)://HOST/api/v3".
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Token string
|
Token string
|
||||||
Cashier string
|
Cashier string
|
||||||
Source string
|
Source string
|
||||||
Defaults *DefaultParams
|
Defaults *DefaultParams
|
||||||
|
CustomURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultParams struct {
|
type DefaultParams struct {
|
||||||
@@ -38,6 +40,7 @@ type DefaultParams struct {
|
|||||||
Taxgrp string
|
Taxgrp string
|
||||||
PayType int
|
PayType int
|
||||||
DefaultTimeout time.Duration
|
DefaultTimeout time.Duration
|
||||||
|
BankName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(config Config) *Client {
|
func NewClient(config Config) *Client {
|
||||||
@@ -64,8 +67,16 @@ func NewClient(config Config) *Client {
|
|||||||
defaults.DefaultTimeout = 30 * time.Second
|
defaults.DefaultTimeout = 30 * time.Second
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apicfg := &api.Client{}
|
||||||
|
|
||||||
|
if config.CustomURL == "" {
|
||||||
|
apicfg = api.NewClient(config.Token)
|
||||||
|
} else {
|
||||||
|
apicfg = api.NewDMClient(config.Token, config.CustomURL)
|
||||||
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
api: api.NewClient(config.Token),
|
api: apicfg,
|
||||||
cashier: config.Cashier,
|
cashier: config.Cashier,
|
||||||
source: config.Source,
|
source: config.Source,
|
||||||
defaults: defaults,
|
defaults: defaults,
|
||||||
@@ -98,13 +109,16 @@ type SellParams struct {
|
|||||||
PayType int
|
PayType int
|
||||||
CardParams *CardParams
|
CardParams *CardParams
|
||||||
Userinfo *api.Userinfo
|
Userinfo *api.Userinfo
|
||||||
|
CommentUP string
|
||||||
}
|
}
|
||||||
|
|
||||||
type CardParams struct {
|
type CardParams struct {
|
||||||
Cardmask string
|
Cardmask string
|
||||||
BankID string
|
BankID string
|
||||||
RrnCode string
|
RrnCode string
|
||||||
AuthCode string
|
AuthCode string
|
||||||
|
TerminalID string
|
||||||
|
BankName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) NewSellParams() *SellParamsBuilder {
|
func (c *Client) NewSellParams() *SellParamsBuilder {
|
||||||
@@ -155,6 +169,11 @@ func (b *SellParamsBuilder) Comment(comment string) *SellParamsBuilder {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *SellParamsBuilder) CommentUp(commentUp string) *SellParamsBuilder {
|
||||||
|
b.params.CommentUP = commentUp
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *SellParamsBuilder) PayCash() *SellParamsBuilder {
|
func (b *SellParamsBuilder) PayCash() *SellParamsBuilder {
|
||||||
b.params.PayType = PayTypeCash
|
b.params.PayType = PayTypeCash
|
||||||
b.params.CardParams = nil
|
b.params.CardParams = nil
|
||||||
@@ -230,17 +249,20 @@ func (c *Client) Sell(ctx context.Context, params SellParams) (*api.SellResponse
|
|||||||
params.CardParams.BankID,
|
params.CardParams.BankID,
|
||||||
params.CardParams.RrnCode,
|
params.CardParams.RrnCode,
|
||||||
params.CardParams.AuthCode,
|
params.CardParams.AuthCode,
|
||||||
|
params.CardParams.TerminalID,
|
||||||
|
params.CardParams.BankName,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
pay = api.NewReceiptPayCash(sum, params.Comment)
|
pay = api.NewReceiptPayCash(sum, params.Comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.api.Sell(ctx, api.SellParams{
|
return c.api.Sell(ctx, api.SellParams{
|
||||||
Cashier: c.cashier,
|
Cashier: c.cashier,
|
||||||
Source: c.source,
|
Source: c.source,
|
||||||
Rows: []api.ReceiptRow{row},
|
Rows: []api.ReceiptRow{row},
|
||||||
Pays: []api.ReceiptPay{pay},
|
Pays: []api.ReceiptPay{pay},
|
||||||
Userinfo: params.Userinfo,
|
Userinfo: params.Userinfo,
|
||||||
|
CommentUP: params.CommentUP,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,3 +284,23 @@ func (c *Client) QuickSellNamed(ctx context.Context, name string, price float64)
|
|||||||
Price: price,
|
Price: price,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) ZeroReceipt(ctx context.Context) (*api.SellResponse, error) {
|
||||||
|
return c.api.Sell(ctx, api.SellParams{
|
||||||
|
Cashier: c.cashier,
|
||||||
|
Source: c.source,
|
||||||
|
Rows: []api.ReceiptRow{},
|
||||||
|
Pays: []api.ReceiptPay{},
|
||||||
|
CommentUP: "Нульовий чек",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ZeroReceiptWithTimeout(timeout time.Duration) (*api.SellResponse, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
return c.ZeroReceipt(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ZeroReceiptDefault() (*api.SellResponse, error) {
|
||||||
|
return c.ZeroReceiptWithTimeout(c.defaults.DefaultTimeout)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user