package ln import ( "bytes" "encoding/json" "fmt" "log" "net" "net/http" "net/url" "strconv" "time" "git.sp4ke.com/sp4ke/bit4sat/config" "github.com/gin-gonic/gin" ) var ( Client *http.Client ) func Info() (gin.H, error) { result := make(gin.H) resp, err := Client.Get(getUrl(InfoEndpoint)) if err != nil { return nil, err } jsonDec := json.NewDecoder(resp.Body) jsonDec.Decode(&result) return result, nil } // Shoudl be called in goroutine func PollPaidInvoice(in *Invoice, paidChan chan<- *Invoice) { reqParams := url.Values{} // Timeout in seconds reqParams.Set("timeout", strconv.Itoa(30)) reqURI := fmt.Sprintf("%s/%s/wait?%s", getUrl(PollInvoiceEndpoint), in.Id, reqParams.Encode()) log.Printf("polling to %s", reqURI) var err error resp := &http.Response{} for { //log.Println("new poll") resp, err = http.Get(reqURI) if err != nil { log.Printf("Error: ", err) } if resp.StatusCode == http.StatusPaymentRequired { log.Printf("invoice %s not yet paid", in.Id) continue } if resp.StatusCode == http.StatusGone { log.Printf("invoice expired ", in.Id) } if resp.StatusCode == http.StatusOK { break } //unknownData := gin.H{} //jsonDec := json.NewDecoder(resp.Body) //jsonDec.Decode(&data) log.Printf("InvoicePoll Error: unknown resopnse %s", resp.Status) } invoice := Invoice{} invoice.UploadId = in.UploadId jsonDec := json.NewDecoder(resp.Body) jsonDec.Decode(&invoice) log.Printf("Invoice paid %s", invoice) paidChan <- &invoice } func UploadInvoice(amount float32, curr Currency, uploadId string) (*Invoice, error) { webhookUrl := fmt.Sprintf("http://%s:%s/%s", config.ApiHost, config.ApiPort, config.ChargeCallbackEndpoint) reqData := gin.H{ "description": fmt.Sprintf("bit4sat upload: %s", uploadId), "webhook": webhookUrl, } // If Satoshi convert to millisat if curr == CurSat { reqData["msatoshi"] = amount * 1000 } else { reqData["currency"] = CurrencyString[curr] reqData["amount"] = amount } jsonEnc, err := json.Marshal(reqData) if err != nil { return nil, err } log.Printf("Asking for invoice with: %s", jsonEnc) resp, err := Client.Post(getUrl(InvoiceEndpoint), "application/json", bytes.NewReader(jsonEnc)) if err != nil { return nil, err } invoice := Invoice{} invoice.UploadId = uploadId jsonDec := json.NewDecoder(resp.Body) jsonDec.Decode(&invoice) return &invoice, nil } func getUrl(endpoint string) string { url, err := url.Parse(fmt.Sprintf("http://api-token:%s@%s/%s", config.LnChargeToken, config.LnChargeApi, endpoint)) if err != nil { log.Fatal(err) } return url.String() } func newClient() *http.Client { netTransport := &http.Transport{ Dial: (&net.Dialer{ Timeout: 5 * time.Second, }).Dial, TLSHandshakeTimeout: 5 * time.Second, } c := &http.Client{ Timeout: time.Second * 10, Transport: netTransport, } return c } func init() { Client = newClient() }