package kassa import ( "bytes" "encoding/base64" "encoding/json" "fmt" "github.com/google/uuid" "net/http" "relynolli-server/external/kassa/Measure" "relynolli-server/external/kassa/PaymentMode" "relynolli-server/external/kassa/PaymentSubject" "relynolli-server/external/kassa/TaxSystemCode" "relynolli-server/external/kassa/VatCodes" "sync" "time" ) var once sync.Once type KassaAmount struct { Value string `json:"value"` Currency string `json:"currency"` } type KassaReceiptItems struct { Description string `json:"description"` Amount KassaAmount `json:"amount"` VatCode VatCodes.VatCode `json:"vat_code"` Quantity string `json:"quantity"` Measure Measure.Measure `json:"measure"` PaymentSubject PaymentSubject.PaymentSubject `json:"payment_subject"` PaymentMode PaymentMode.PaymentMode `json:"payment_mode"` } type KassaCustomer struct { FullName string `json:"full_name"` Email string `json:"email"` Phone string `json:"phone"` } type KassaReceipt struct { Customer KassaCustomer `json:"customer"` Items []KassaReceiptItems `json:"items"` TaxSystemCode TaxSystemCode.TaxSystemCode `json:"tax_system_code"` } type KassaConfirmation struct { Type string `json:"type"` ReturnUrl string `json:"return_url"` } type KassaPaymentReq struct { Amount KassaAmount `json:"amount"` Description string `json:"description"` Receipt KassaReceipt `json:"receipt"` Confirmation KassaConfirmation `json:"confirmation"` Capture bool `json:"capture"` } type KassaResult struct { Id string `json:"id"` Status string `json:"status"` Amount KassaAmount `json:"amount"` Description string `json:"description"` Recipient struct { AccountId string `json:"account_id"` GatewayId string `json:"gateway_id"` } `json:"recipient"` CreatedAt time.Time `json:"created_at"` Confirmation struct { Type string `json:"type"` ConfirmationUrl string `json:"confirmation_url"` } `json:"confirmation"` Paid bool `json:"paid"` } func basicAuth(username, password string) string { auth := username + ":" + password return base64.StdEncoding.EncodeToString([]byte(auth)) } func CreatePayment(orderId int, sum float64, fullName string, email string, phone string, items []KassaReceiptItems) (map[string]interface{}, error) { req := KassaPaymentReq{ Amount: KassaAmount{Value: fmt.Sprintf("%f", sum), Currency: "RUB"}, Description: fmt.Sprintf("Заказ №%d", orderId), Confirmation: KassaConfirmation{ Type: "redirect", ReturnUrl: "https://relynolli.ru", }, Capture: true, Receipt: KassaReceipt{ Customer: KassaCustomer{ FullName: fullName, Email: email, Phone: phone, }, Items: items, TaxSystemCode: TaxSystemCode.GENERAL, }, } query, _ := json.Marshal(req) uid, err := uuid.NewUUID() client := http.Client{} request, _ := http.NewRequest(http.MethodPost, BASE_URL, bytes.NewBuffer(query)) request.Header.Set("Authorization", "Basic "+basicAuth(ACCOUNT_ID, PASSWORD)) request.Header.Set("Idempotence-Key", uid.String()) request.Header.Set("Content-Type", "application/json") response, err := client.Do(request) result := map[string]interface{}{} json.NewDecoder(response.Body).Decode(&result) if err != nil { return nil, err } return result, nil }