14 Commits

6 changed files with 141 additions and 38 deletions

View File

@@ -1,17 +1,48 @@
package api
import "resty.dev/v3"
import (
"crypto/tls"
"resty.dev/v3"
)
type Client struct {
token string
resty *resty.Client
apiBaseURL string
token string
device string
isDM bool
resty *resty.Client
apiBaseURL string
fiscalEndpoint string
}
func NewClient(token string) *Client {
return &Client{
token: token,
resty: resty.New(),
apiBaseURL: "https://kasa.vchasno.ua/api/v3",
token: token,
isDM: false,
resty: resty.New(),
apiBaseURL: "https://kasa.vchasno.ua/api/v3",
fiscalEndpoint: "/fiscal/execute",
}
}
func NewDMClient(token, dmURL, device string) *Client {
restyClient := resty.New()
restyClient.SetTLSClientConfig(&tls.Config{
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
},
})
return &Client{
token: token,
device: device,
isDM: true,
resty: restyClient,
apiBaseURL: dmURL,
fiscalEndpoint: "/dm/execute",
}
}

View File

@@ -5,34 +5,69 @@ import (
"encoding/json"
"fmt"
"io"
"log"
)
type APIError struct {
Code int
Message string
}
func (e *APIError) Error() string {
return fmt.Sprintf("vchasno api error %d: %s", e.Code, e.Message)
}
func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, response interface{}) error {
request.Device = c.device
if c.isDM {
request.Ver = 6
request.Type = 1
}
reqJson, err := json.Marshal(request)
if err != nil {
return fmt.Errorf("failed to marshal request: %w", err)
}
url := c.apiBaseURL + c.fiscalEndpoint
log.Printf("[VCHASNO] POST %s", url)
log.Printf("[VCHASNO] Request: %s", string(reqJson))
resp, err := c.resty.R().
SetContext(ctx).
SetHeader("Authorization", c.token).
SetBody(request).
Post(c.apiBaseURL + "/fiscal/execute")
SetHeader("Content-Type", "application/json").
SetBody(reqJson).
Post(url)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
if resp.IsError() {
return fmt.Errorf("api error: %v", resp.Error())
}
if resp.StatusCode() != 200 {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode())
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response: %w", err)
}
log.Printf("[VCHASNO] Response (status %d): %s", resp.StatusCode(), string(body))
if resp.StatusCode() != 200 {
return fmt.Errorf("api error (status %d): %s", resp.StatusCode(), string(body))
}
if err := json.Unmarshal(body, response); err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
return fmt.Errorf("failed to unmarshal response: %w, body: %s", err, string(body))
}
var baseResp BaseResponse
if err := json.Unmarshal(body, &baseResp); err == nil {
if baseResp.HasError() {
return &APIError{
Code: baseResp.Res,
Message: baseResp.Errortxt,
}
}
}
return nil
@@ -41,8 +76,7 @@ func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, resp
func (c *Client) OpenShift(ctx context.Context, cashier string) (*SellResponse, error) {
request := FiscalRequest{
Fiscal: Fiscal{
Task: TaskOpenShift,
Cashier: cashier,
Task: TaskOpenShift,
},
}
@@ -57,8 +91,7 @@ func (c *Client) OpenShift(ctx context.Context, cashier string) (*SellResponse,
func (c *Client) CloseShift(ctx context.Context, cashier string) (*ZReportResponse, error) {
request := FiscalRequest{
Fiscal: Fiscal{
Task: TaskZReport,
Cashier: cashier,
Task: TaskZReport,
},
}
@@ -95,12 +128,6 @@ func (c *Client) Sell(ctx context.Context, params SellParams) (*SellResponse, er
request.Userinfo = *params.Userinfo
}
reqJson, err := json.Marshal(request)
if err != nil {
return nil, err
}
fmt.Println(string(reqJson))
var response SellResponse
if err := c.executeRequest(ctx, request, &response); err != nil {
return nil, err

View File

@@ -1,7 +1,11 @@
package api
type FiscalRequest struct {
Source string `json:"source"`
Ver int `json:"ver,omitempty"`
Source string `json:"source,omitempty"`
Device string `json:"device,omitempty"`
Tag string `json:"tag,omitempty"`
Type int `json:"type,omitempty"`
Userinfo Userinfo `json:"userinfo,omitempty"`
Fiscal Fiscal `json:"fiscal"`
}
@@ -13,7 +17,7 @@ type Userinfo struct {
type Fiscal struct {
Task int `json:"task"`
Cashier string `json:"cashier"`
Cashier string `json:"cashier,omitempty"`
Receipt *Receipt `json:"receipt,omitempty"`
}

View File

@@ -15,6 +15,17 @@ type BaseResponse struct {
ErrorExtra interface{} `json:"error_extra"`
}
func (r *BaseResponse) HasError() bool {
return r.Res != 0
}
func (r *BaseResponse) Error() string {
if r.Errortxt != "" {
return r.Errortxt
}
return ""
}
type SellResponse struct {
BaseResponse
Info SellInfo `json:"info"`

View File

@@ -7,9 +7,10 @@ import (
func ExampleBasicUsage() {
client := NewClient(Config{
Token: "your-token",
Cashier: "Иванов",
Source: "parking",
Token: "your-token",
Cashier: "Иванов",
Source: "parking",
CustomURL: "DM URL/",
})
ctx := context.Background()

View File

@@ -26,10 +26,12 @@ type Client struct {
}
type Config struct {
Token string
Cashier string
Source string
Defaults *DefaultParams
Token string
Cashier string
Source string
Defaults *DefaultParams
CustomURL string
Device string
}
type DefaultParams struct {
@@ -65,8 +67,15 @@ func NewClient(config Config) *Client {
defaults.DefaultTimeout = 30 * time.Second
}
var apicfg *api.Client
if config.CustomURL == "" {
apicfg = api.NewClient(config.Token)
} else {
apicfg = api.NewDMClient(config.Token, config.CustomURL, config.Device)
}
return &Client{
api: api.NewClient(config.Token),
api: apicfg,
cashier: config.Cashier,
source: config.Source,
defaults: defaults,
@@ -274,3 +283,23 @@ func (c *Client) QuickSellNamed(ctx context.Context, name string, price float64)
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)
}