Skip to content

Commit

Permalink
Merge pull request stakwork#2481 from stakwork/feat/refactor_generate…
Browse files Browse the repository at this point in the history
…_invoice

refactor: refactored GenerateInvoice to work for V2 Bot
  • Loading branch information
elraphty authored Jan 22, 2025
2 parents a2820e5 + 7f4d0e3 commit 2b90da8
Showing 1 changed file with 124 additions and 22 deletions.
146 changes: 124 additions & 22 deletions handlers/tribes.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,19 @@ func UpdateLeaderBoard(w http.ResponseWriter, r *http.Request) {
}

func GenerateInvoice(w http.ResponseWriter, r *http.Request) {
invoiceRes, invoiceErr := db.InvoiceResponse{}, db.InvoiceError{}

if config.IsV2Payment {
invoiceRes, invoiceErr = GenerateV2Invoice(w, r)
} else {
invoiceRes, invoiceErr = GenerateV1Invoice(w, r)
}

if invoiceErr.Error != "" {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(invoiceErr)
}

invoice := db.InvoiceRequest{}
body, err := io.ReadAll(r.Body)

Expand All @@ -486,11 +499,57 @@ func GenerateInvoice(w http.ResponseWriter, r *http.Request) {
pub_key := invoice.User_pubkey
owner_key := invoice.Owner_pubkey
date, _ := utils.ConvertStringToInt(invoice.Created)
memo := invoice.Memo
invoiceType := invoice.Type
routeHint := invoice.Route_hint
amount, _ := utils.ConvertStringToUint(invoice.Amount)

paymentRequest := invoiceRes.Response.Invoice
now := time.Now()

newInvoice := db.NewInvoiceList{
PaymentRequest: paymentRequest,
Type: db.InvoiceType(invoiceType),
OwnerPubkey: owner_key,
Created: &now,
Updated: &now,
Status: false,
}

newInvoiceData := db.UserInvoiceData{
PaymentRequest: paymentRequest,
Created: date,
Amount: amount,
UserPubkey: pub_key,
RouteHint: routeHint,
}

db.DB.ProcessAddInvoice(newInvoice, newInvoiceData)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(invoiceRes)
}

func GenerateV1Invoice(w http.ResponseWriter, r *http.Request) (db.InvoiceResponse, db.InvoiceError) {
invoice := db.InvoiceRequest{}
body, err := io.ReadAll(r.Body)

r.Body.Close()

if err != nil {
logger.Log.Error("%v", err)
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}

err = json.Unmarshal(body, &invoice)

if err != nil {
logger.Log.Error("%v", err)
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}

memo := invoice.Memo
amount, _ := utils.ConvertStringToUint(invoice.Amount)

url := fmt.Sprintf("%s/invoices", config.RelayUrl)

bodyData := fmt.Sprintf(`{"amount": %d, "memo": "%s"}`, amount, memo)
Expand All @@ -506,7 +565,7 @@ func GenerateInvoice(w http.ResponseWriter, r *http.Request) {

if err != nil {
log.Printf("Request Failed: %s", err)
return
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}

defer res.Body.Close()
Expand All @@ -515,7 +574,7 @@ func GenerateInvoice(w http.ResponseWriter, r *http.Request) {

if err != nil {
log.Printf("Reading body failed: %s", err)
return
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}

// Unmarshal result
Expand All @@ -525,33 +584,76 @@ func GenerateInvoice(w http.ResponseWriter, r *http.Request) {

if err != nil {
log.Printf("Unmarshal body failed: %s", err)
return
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}

paymentRequest := invoiceRes.Response.Invoice
now := time.Now()
return invoiceRes, db.InvoiceError{Success: true}
}

newInvoice := db.NewInvoiceList{
PaymentRequest: paymentRequest,
Type: db.InvoiceType(invoiceType),
OwnerPubkey: owner_key,
Created: &now,
Updated: &now,
Status: false,
func GenerateV2Invoice(w http.ResponseWriter, r *http.Request) (db.InvoiceResponse, db.InvoiceError) {
invoice := db.InvoiceRequest{}

var err error
body, err := io.ReadAll(r.Body)

r.Body.Close()

if err != nil {
logger.Log.Error("%v", err)
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}

newInvoiceData := db.UserInvoiceData{
PaymentRequest: paymentRequest,
Created: date,
Amount: amount,
UserPubkey: pub_key,
RouteHint: routeHint,
err = json.Unmarshal(body, &invoice)

if err != nil {
logger.Log.Error("%v", err)
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}

db.DB.ProcessAddInvoice(newInvoice, newInvoiceData)
url := fmt.Sprintf("%s/invoice", config.V2BotUrl)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(invoiceRes)
amount, _ := utils.ConvertStringToUint(invoice.Amount)

amountMsat := amount * 1000

bodyData := fmt.Sprintf(`{"amt_msat": %d}`, amountMsat)

jsonBody := []byte(bodyData)

client := &http.Client{}
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonBody))

req.Header.Set("x-admin-token", config.V2BotToken)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)

if err != nil {
log.Printf("Client Request Failed: %s", err)
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}

defer res.Body.Close()

body, err = io.ReadAll(res.Body)

if err != nil {
log.Printf("Reading body failed: %s", err)
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}

// Unmarshal result
v2InvoiceRes := db.V2CreateInvoiceResponse{}
err = json.Unmarshal(body, &v2InvoiceRes)

if err != nil {
log.Printf("Json Unmarshal failed: %s", err)
return db.InvoiceResponse{}, db.InvoiceError{Success: false, Error: err.Error()}
}
return db.InvoiceResponse{
Response: db.Invoice{
Invoice: v2InvoiceRes.Bolt11,
},
}, db.InvoiceError{Success: true}
}

func (th *tribeHandler) GenerateBudgetInvoice(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 2b90da8

Please sign in to comment.