141 lines
4.1 KiB
Go
141 lines
4.1 KiB
Go
package kassa
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/google/uuid"
|
|
"net/http"
|
|
"os"
|
|
"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"
|
|
"time"
|
|
)
|
|
|
|
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) (*KassaResult, 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(os.Getenv("YOOKASSA_ACCOUNT_ID"),
|
|
os.Getenv("YOOKASSA_ACCOUNT_SECRET")))
|
|
request.Header.Set("Idempotence-Key", uid.String())
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
response, err := client.Do(request)
|
|
|
|
result := new(KassaResult)
|
|
json.NewDecoder(response.Body).Decode(&result)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
func CheckPayment(paymentId uuid.UUID) (*KassaResult, error) {
|
|
uid, err := uuid.NewUUID()
|
|
client := new(http.Client)
|
|
request, _ := http.NewRequest(http.MethodGet, BASE_URL+fmt.Sprintf("/%s", paymentId.String()), nil)
|
|
request.Header.Set("Authorization", "Basic "+basicAuth(os.Getenv("YOOKASSA_ACCOUNT_ID"),
|
|
os.Getenv("YOOKASSA_ACCOUNT_SECRET")))
|
|
request.Header.Set("Idempotence-Key", uid.String())
|
|
request.Header.Set("Content-Type", "application/json")
|
|
response, err := client.Do(request)
|
|
|
|
result := new(KassaResult)
|
|
json.NewDecoder(response.Body).Decode(&result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|