initial commit
This commit is contained in:
150
api/kasa.go
Normal file
150
api/kasa.go
Normal file
@@ -0,0 +1,150 @@
|
||||
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
|
||||
}
|
||||
93
api/structs.go
Normal file
93
api/structs.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package api
|
||||
|
||||
type FiskalCheck struct {
|
||||
Source string `json:"source"`
|
||||
Userinfo Userinfo `json:"userinfo"`
|
||||
Fiscal Fiscal `json:"fiscal"`
|
||||
}
|
||||
|
||||
type Userinfo struct {
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
}
|
||||
|
||||
type Fiscal struct {
|
||||
Task int `json:"task"`
|
||||
Cashier string `json:"cashier"`
|
||||
Receipt Receipt `json:"receipt"`
|
||||
}
|
||||
|
||||
type Receipt struct {
|
||||
Sum float64 `json:"sum"`
|
||||
Round float64 `json:"round"`
|
||||
CommentUp string `json:"comment_up"`
|
||||
CommentDown string `json:"comment_down"`
|
||||
Disc float64 `json:"disc"`
|
||||
DiscType int `json:"disc_type"`
|
||||
Rows []ReceiptRow `json:"rows"`
|
||||
Pays []ReceiptPay `json:"pays"`
|
||||
}
|
||||
|
||||
type ReceiptRow struct {
|
||||
Code string `json:"code"`
|
||||
Pop string `json:"pop,omitempty"`
|
||||
Code1 string `json:"code1"`
|
||||
Code2 string `json:"code2"`
|
||||
CodeAa []string `json:"code_aa,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Cnt int `json:"cnt"`
|
||||
Price float64 `json:"price"`
|
||||
Disc float64 `json:"disc"`
|
||||
Taxgrp string `json:"taxgrp"`
|
||||
Comment string `json:"comment"`
|
||||
CodeA string `json:"code_a,omitempty"`
|
||||
}
|
||||
|
||||
type ReceiptPay struct {
|
||||
Type int `json:"type"`
|
||||
Sum float64 `json:"sum"`
|
||||
Change float64 `json:"change,omitempty"`
|
||||
Comment string `json:"comment"`
|
||||
Commission float64 `json:"commission,omitempty"`
|
||||
Paysys string `json:"paysys,omitempty"`
|
||||
Rrn string `json:"rrn,omitempty"`
|
||||
OperType string `json:"oper_type,omitempty"`
|
||||
Cardmask string `json:"cardmask,omitempty"`
|
||||
TermID string `json:"term_id,omitempty"`
|
||||
BankName string `json:"bank_name,omitempty"`
|
||||
BankID string `json:"bank_id,omitempty"`
|
||||
AuthCode string `json:"auth_code,omitempty"`
|
||||
ShowAdditionalInfo bool `json:"show_additional_info,omitempty"`
|
||||
}
|
||||
|
||||
type KasaResponse struct {
|
||||
Task int `json:"task"`
|
||||
Type int `json:"type"`
|
||||
Ver int `json:"ver"`
|
||||
Source string `json:"source"`
|
||||
Device string `json:"device"`
|
||||
Tag string `json:"tag"`
|
||||
Dt string `json:"dt"`
|
||||
Res int `json:"res"`
|
||||
ResAction int `json:"res_action"`
|
||||
Errortxt string `json:"errortxt"`
|
||||
Warnings []string `json:"warnings"`
|
||||
Info Info `json:"info"`
|
||||
ErrorExtra interface{} `json:"error_extra"`
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
Task int `json:"task"`
|
||||
Fisid string `json:"fisid"`
|
||||
Dataid int `json:"dataid"`
|
||||
Doccode string `json:"doccode"`
|
||||
Dt string `json:"dt"`
|
||||
Cashier string `json:"cashier"`
|
||||
Dtype int `json:"dtype"`
|
||||
Isprint int `json:"isprint"`
|
||||
Isoffline bool `json:"isoffline"`
|
||||
Safe float64 `json:"safe"`
|
||||
ShiftLink int `json:"shift_link"`
|
||||
Docno int `json:"docno"`
|
||||
Cancelid string `json:"cancelid"`
|
||||
}
|
||||
Reference in New Issue
Block a user