2024-03-25 11:55:01 +02:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/antihax/optional"
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"main/firefly3"
|
|
|
|
|
"main/monobank/api/webhook/models"
|
2024-03-26 11:26:35 +02:00
|
|
|
|
"math"
|
2024-03-25 11:55:01 +02:00
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2024-03-26 12:39:04 +02:00
|
|
|
|
"slices"
|
2024-03-25 11:55:01 +02:00
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// https://api.monobank.ua/docs/index.html#tag/Kliyentski-personalni-dani/paths/~1personal~1statement~1{account}~1{from}~1{to}/get
|
|
|
|
|
// https://api-docs.firefly-iii.org/#/accounts/listAccount
|
|
|
|
|
|
2024-03-26 19:06:31 +02:00
|
|
|
|
// curl -X POST https://api.monobank.ua/personal/webhook -H 'Content-TransactionType: application/json' -H 'X-Token: ' -d '{"webHookUrl":"https://monobank-firefly3.stuzer.link/webhook"}'
|
2024-03-25 11:55:01 +02:00
|
|
|
|
|
2024-03-26 19:06:31 +02:00
|
|
|
|
// curl -X POST https://monobank-firefly3.stuzer.link/webhook -H 'Content-TransactionType: application/json' -d '{"test":123}'
|
2024-03-25 11:55:01 +02:00
|
|
|
|
|
2024-03-27 20:12:24 +02:00
|
|
|
|
// Configs
|
2024-03-26 19:06:31 +02:00
|
|
|
|
var ShopConfig []ShopConfigItem
|
2024-03-27 20:12:24 +02:00
|
|
|
|
var Firefy3AccountsConfig map[string]string
|
2024-03-26 11:26:35 +02:00
|
|
|
|
|
2024-03-25 11:55:01 +02:00
|
|
|
|
func handleWebhook(w http.ResponseWriter, r *http.Request) {
|
2024-03-26 19:06:31 +02:00
|
|
|
|
LogString("-----------------\nwebhook received!")
|
2024-03-25 11:55:01 +02:00
|
|
|
|
|
|
|
|
|
// read body bytes
|
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
|
if err != nil {
|
2024-03-26 19:06:31 +02:00
|
|
|
|
LogString(err.Error())
|
2024-03-25 11:55:01 +02:00
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//fmt.Println(string(body))
|
|
|
|
|
//w.WriteHeader(http.StatusOK)
|
|
|
|
|
//return
|
|
|
|
|
|
2024-03-27 19:06:44 +02:00
|
|
|
|
//body = []byte("{\"type\":\"StatementItem\",\"data\":{\"account\":\"4723djMLsLOCzhoeYjxqRw\",\"statementItem\":{\"id\":\"5_NQ0arGAmp2pyNzvA\",\"time\":1711544958,\"description\":\"Ілля Ш.\",\"mcc\":4829,\"originalMcc\":4829,\"amount\":-572000,\"operationAmount\":-572000,\"currencyCode\":980,\"commissionRate\":22000,\"cashbackAmount\":0,\"balance\":8101246,\"hold\":true,\"receiptId\":\"EMXC-P266-90PC-EB8C\"}}}")
|
2024-03-26 19:06:31 +02:00
|
|
|
|
LogString(string(body))
|
2024-03-25 11:55:01 +02:00
|
|
|
|
|
2024-03-27 20:12:24 +02:00
|
|
|
|
// check empty body
|
2024-03-26 12:39:04 +02:00
|
|
|
|
if len(string(body)) == 0 {
|
2024-03-26 19:06:31 +02:00
|
|
|
|
LogString("empty body")
|
2024-03-26 12:39:04 +02:00
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-25 11:55:01 +02:00
|
|
|
|
// parse body
|
|
|
|
|
var transaction models.Transaction
|
|
|
|
|
err = json.Unmarshal(body, &transaction)
|
|
|
|
|
if err != nil {
|
2024-03-26 19:06:31 +02:00
|
|
|
|
LogString(err.Error())
|
2024-03-25 11:55:01 +02:00
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-27 19:06:44 +02:00
|
|
|
|
//statement, err := requests.Statement(models2.StatementRequest{
|
|
|
|
|
// Account: transaction.Data.Account,
|
|
|
|
|
// From: transaction.Data.StatementItem.Time,
|
|
|
|
|
// To: transaction.Data.StatementItem.Time,
|
|
|
|
|
//})
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// fmt.Printf("%+v", err.Error())
|
|
|
|
|
// w.WriteHeader(http.StatusOK)
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//fmt.Printf("%+v", statement)
|
|
|
|
|
|
2024-03-25 11:55:01 +02:00
|
|
|
|
// init firefly3 client
|
|
|
|
|
clientConf := firefly3.NewConfiguration()
|
2024-03-26 11:26:35 +02:00
|
|
|
|
clientConf.BasePath = os.Getenv("FIREFLY3_API_URL")
|
2024-03-25 11:55:01 +02:00
|
|
|
|
clientConf.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("FIREFLY3_TOKEN"))
|
|
|
|
|
client := firefly3.NewAPIClient(clientConf)
|
|
|
|
|
|
2024-03-26 11:26:35 +02:00
|
|
|
|
// get firefly3 account
|
2024-03-25 11:55:01 +02:00
|
|
|
|
listOpts := firefly3.AccountsApiListAccountOpts{
|
|
|
|
|
Type_: optional.NewInterface("asset"),
|
|
|
|
|
}
|
2024-03-27 20:12:24 +02:00
|
|
|
|
accounts, _, err := client.AccountsApi.ListAccount(context.Background(), &listOpts)
|
2024-03-25 11:55:01 +02:00
|
|
|
|
if err != nil {
|
2024-03-26 19:06:31 +02:00
|
|
|
|
LogString(err.Error())
|
2024-03-26 11:26:35 +02:00
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return
|
2024-03-25 11:55:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var account firefly3.AccountRead
|
|
|
|
|
for _, row := range accounts.Data {
|
|
|
|
|
if row.Attributes.Notes == transaction.Data.Account {
|
|
|
|
|
account = row
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(account.Id) == 0 {
|
2024-03-26 19:06:31 +02:00
|
|
|
|
LogString("unable to find account " + transaction.Data.Account + " in firefly3")
|
2024-03-25 11:55:01 +02:00
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 12:39:04 +02:00
|
|
|
|
// create transaction
|
2024-03-26 19:36:29 +02:00
|
|
|
|
var firefly3Transactions []firefly3.TransactionSplitStore
|
|
|
|
|
|
2024-03-26 19:06:31 +02:00
|
|
|
|
transactionTypeWithdrawal := firefly3.WITHDRAWAL_TransactionTypeProperty
|
|
|
|
|
transactionTypeTransfer := firefly3.TRANSFER_TransactionTypeProperty
|
|
|
|
|
|
2024-03-26 12:40:23 +02:00
|
|
|
|
firefly3Transaction := firefly3.TransactionSplitStore{
|
2024-03-26 11:26:35 +02:00
|
|
|
|
Type_: &transactionTypeWithdrawal,
|
2024-03-26 12:39:04 +02:00
|
|
|
|
Date: time.Unix(int64(transaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
|
2024-03-26 11:26:35 +02:00
|
|
|
|
Notes: string(body),
|
2024-03-27 19:06:44 +02:00
|
|
|
|
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(transaction.Data.StatementItem.Amount/100)))) - int(math.Abs(math.Round(float64(transaction.Data.StatementItem.CommissionRate/100))))),
|
2024-03-26 11:26:35 +02:00
|
|
|
|
SourceId: account.Id,
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 12:39:04 +02:00
|
|
|
|
if slices.Contains([]string{"З чорної картки"}, transaction.Data.StatementItem.Description) {
|
2024-03-26 12:40:23 +02:00
|
|
|
|
firefly3Transaction.Type_ = &transactionTypeTransfer
|
|
|
|
|
firefly3Transaction.Description = "Transfer between accounts"
|
2024-03-27 20:12:24 +02:00
|
|
|
|
firefly3Transaction.DestinationId = Firefy3AccountsConfig["Mono White"]
|
2024-03-26 19:36:29 +02:00
|
|
|
|
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
|
2024-03-26 12:39:04 +02:00
|
|
|
|
} else if slices.Contains([]string{"З білої картки"}, transaction.Data.StatementItem.Description) {
|
2024-03-26 12:40:23 +02:00
|
|
|
|
firefly3Transaction.Type_ = &transactionTypeTransfer
|
|
|
|
|
firefly3Transaction.Description = "Transfer between accounts"
|
2024-03-27 20:12:24 +02:00
|
|
|
|
firefly3Transaction.DestinationId = Firefy3AccountsConfig["Mono Black"]
|
2024-03-26 19:36:29 +02:00
|
|
|
|
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
|
2024-03-27 19:48:29 +02:00
|
|
|
|
} else if slices.Contains([]string{"Термінал City24"}, transaction.Data.StatementItem.Description) {
|
|
|
|
|
firefly3Transaction.Type_ = &transactionTypeTransfer
|
|
|
|
|
firefly3Transaction.Description = "Transfer between accounts"
|
2024-03-27 20:12:24 +02:00
|
|
|
|
firefly3Transaction.SourceId = Firefy3AccountsConfig["Wallet cash (UAH)"]
|
2024-03-27 19:48:29 +02:00
|
|
|
|
firefly3Transaction.DestinationId = firefly3Transaction.SourceId
|
|
|
|
|
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
|
2024-03-27 20:14:22 +02:00
|
|
|
|
} else if slices.Contains([]string{"Термінал EasyPay", "City24", "Термінал City24"}, transaction.Data.StatementItem.Description) {
|
2024-03-27 19:48:29 +02:00
|
|
|
|
firefly3Transaction.Type_ = &transactionTypeTransfer
|
|
|
|
|
firefly3Transaction.Description = "Transfer between accounts"
|
2024-03-27 20:12:24 +02:00
|
|
|
|
firefly3Transaction.SourceId = Firefy3AccountsConfig["Wallet cash (UAH)"] // test
|
|
|
|
|
firefly3Transaction.DestinationId = account.Id // test
|
2024-03-27 19:48:29 +02:00
|
|
|
|
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
|
2024-03-27 20:14:22 +02:00
|
|
|
|
} else if slices.Contains([]string{"Банкомат DN00"}, transaction.Data.StatementItem.Description) {
|
|
|
|
|
firefly3Transaction.Type_ = &transactionTypeTransfer
|
|
|
|
|
firefly3Transaction.Description = "Transfer between accounts"
|
|
|
|
|
firefly3Transaction.DestinationId = Firefy3AccountsConfig["Wallet cash (UAH)"] // test
|
|
|
|
|
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
|
2024-03-26 19:06:31 +02:00
|
|
|
|
} else {
|
|
|
|
|
for _, row := range ShopConfig {
|
|
|
|
|
if slices.Contains(row.Names, transaction.Data.StatementItem.Description) || slices.Contains(row.MCCCodes, transaction.Data.StatementItem.Mcc) {
|
|
|
|
|
firefly3Transaction.Description = row.TransactionDescription
|
2024-03-27 19:06:44 +02:00
|
|
|
|
firefly3Transaction.DestinationName = row.TransactionDestination
|
|
|
|
|
firefly3Transaction.CategoryName = row.TransactionCategory
|
2024-03-26 19:36:29 +02:00
|
|
|
|
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
|
2024-03-26 19:06:31 +02:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-25 11:55:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 19:36:29 +02:00
|
|
|
|
// record transfer fee
|
|
|
|
|
if transaction.Data.StatementItem.CommissionRate > 0 {
|
|
|
|
|
firefly3Transactions = append(firefly3Transactions, firefly3.TransactionSplitStore{
|
|
|
|
|
Type_: &transactionTypeWithdrawal,
|
|
|
|
|
Date: time.Unix(int64(transaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
|
|
|
|
|
Notes: string(body),
|
|
|
|
|
Description: "Transfer fee",
|
|
|
|
|
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(transaction.Data.StatementItem.CommissionRate / 100))))),
|
|
|
|
|
SourceId: account.Id,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-27 20:12:24 +02:00
|
|
|
|
// log firefly3 transactions
|
2024-03-26 19:36:29 +02:00
|
|
|
|
if len(firefly3Transactions) > 0 {
|
2024-03-25 11:55:01 +02:00
|
|
|
|
transactionOpts := firefly3.TransactionsApiStoreTransactionOpts{}
|
2024-03-27 19:06:44 +02:00
|
|
|
|
|
|
|
|
|
for _, transaction := range firefly3Transactions {
|
2024-03-27 20:12:24 +02:00
|
|
|
|
_, _, err = client.TransactionsApi.StoreTransaction(context.Background(), firefly3.TransactionStore{
|
2024-03-27 19:06:44 +02:00
|
|
|
|
ApplyRules: true,
|
|
|
|
|
Transactions: []firefly3.TransactionSplitStore{transaction},
|
|
|
|
|
}, &transactionOpts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
LogString(err.Error())
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-25 11:55:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-03-27 20:12:24 +02:00
|
|
|
|
// load .env
|
2024-03-25 11:55:01 +02:00
|
|
|
|
err := godotenv.Load(".env")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Error loading .env file")
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-27 20:12:24 +02:00
|
|
|
|
// Configure
|
|
|
|
|
ShopConfig, Firefy3AccountsConfig = Configure()
|
2024-03-26 19:06:31 +02:00
|
|
|
|
|
2024-03-27 20:12:24 +02:00
|
|
|
|
// set webhook
|
2024-03-25 11:55:01 +02:00
|
|
|
|
http.HandleFunc("/webhook", handleWebhook)
|
2024-03-26 19:06:31 +02:00
|
|
|
|
|
2024-03-27 20:12:24 +02:00
|
|
|
|
// listen server
|
2024-03-25 11:55:01 +02:00
|
|
|
|
fmt.Println("Webhook server listening on :3021")
|
2024-03-26 19:06:31 +02:00
|
|
|
|
err = http.ListenAndServe(":3021", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
}
|
2024-03-25 11:55:01 +02:00
|
|
|
|
}
|