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