package api import ( "context" "encoding/json" "fmt" "io" "resty.dev/v3" ) func NewKasaInstance(token string) *Kasa { return &Kasa{ token: token, resty: resty.New(), } } type Kasa struct { token string resty *resty.Client } func createReceiptRow(name string, cnt int, price float64, comment string, disc float64, taxgrp string) ReceiptRow { return ReceiptRow{ Name: name, Cnt: cnt, Price: price, Disc: disc, Taxgrp: taxgrp, Comment: comment, } } func createReceiptPayCash(PayType int, sum float64, comment string) ReceiptPay { return ReceiptPay{ Type: PayType, Sum: sum, Comment: comment, } } func createReceiptPayCard(PayType int, sum float64, comment string, cardmask string, bankID string) ReceiptPay { return ReceiptPay{ Type: PayType, Sum: sum, Comment: comment, Paysys: "parking_pos", Cardmask: cardmask, BankID: bankID, } } func getReceiptSum(rows []ReceiptRow, taxgrp string) float64 { sum := 0.0 for _, row := range rows { sum += (row.Price - row.Disc) * float64(row.Cnt) } return sum } func createReceipt(PayType int, sum float64, comment string, cardmask string, bankID string, name string, cnt int, price float64, disc float64, taxgrp string) Receipt { rows := []ReceiptRow{createReceiptRow(name, cnt, price, comment, disc, taxgrp)} sum = getReceiptSum(rows, taxgrp) r := Receipt{ Sum: sum, Round: 0.00, Disc: 0, DiscType: 0, Rows: rows, Pays: []ReceiptPay{createReceiptPayCard(PayType, sum, comment, cardmask, bankID)}, } fmt.Println("Receipt sum:", r.Sum) return r } // "fiscal": { // "task": 1, // "cashier": "Парковка", // "receipt": { // "sum": 40.00, // "round": 0.00, // "comment_up": "Квитанція за паркування", // "comment_down": "Дякуємо за користування паркінгом!", // "disc": 0.00, // "disc_type": 0, // "rows": [ // { // "code": "PARK-3H", // "pop": "Оплата за послуги паркування", // "name": "Парковка, 3 години", // "cnt": 1, // "price": 40.00, // "disc": 0.00, // "taxgrp": "4", // "comment": "Тариф: 3 години" // } // ], // "pays": [ // { // "type": 0, // "sum": 40.00, // "change": 0.00, // "comment": "Оплата готівкою" // } // ] // } // } func createFiscal(source string, cashier string, PayType int, sum float64, comment string, cardmask string, bankID string, name string, cnt int, price float64, disc float64, taxgrp string) FiskalCheck { return FiskalCheck{ Source: source, Fiscal: Fiscal{ Task: 1, Cashier: cashier, Receipt: createReceipt(PayType, sum, comment, cardmask, bankID, name, cnt, price, disc, taxgrp), }, } } func (k *Kasa) NewSell(ctx context.Context, PayType int, sum float64, comment string, cardmask string, bankID string, name string, cnt int, price float64, disc float64, taxgrp string) (*KasaResponse, error) { fiscal := createFiscal("kasa", "test", PayType, sum, comment, cardmask, bankID, name, cnt, price, disc, taxgrp) // create a POST request to the kasa api https://kasa.vchasno.ua/api/v3/fiscal/execute with resty request, err := k.resty.R().SetBody(fiscal).SetHeader("Authorization", k.token).Post("https://kasa.vchasno.ua/api/v3/fiscal/execute") if err != nil { return nil, fmt.Errorf("failed to create a POST request to the kasa api: %w", err) } if request.IsError() { return nil, fmt.Errorf("failed to create a POST request to the kasa api: %v", request.Error()) } if request.StatusCode() != 200 { return nil, fmt.Errorf("failed to create a POST request to the kasa api: %v", request.Error()) } body, err := io.ReadAll(request.Body) if err != nil { return nil, fmt.Errorf("failed to read response body: %w", err) } fmt.Println(string(body)) var response KasaResponse if err := json.Unmarshal(body, &response); err != nil { return nil, fmt.Errorf("failed to unmarshal response: %w", err) } return &response, nil }