2019-03-26 21:01:54 +00:00
|
|
|
package ln
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2019-03-29 18:50:11 +00:00
|
|
|
"strconv"
|
2019-03-26 21:01:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2019-03-29 18:50:11 +00:00
|
|
|
type Currency int
|
|
|
|
|
2019-03-26 21:01:54 +00:00
|
|
|
const (
|
2019-03-29 18:50:11 +00:00
|
|
|
CurMSat Currency = iota
|
2019-03-26 21:01:54 +00:00
|
|
|
CurSat
|
2019-03-29 18:50:11 +00:00
|
|
|
CurBTC
|
2019-03-26 21:01:54 +00:00
|
|
|
CurUSD
|
2019-03-29 18:50:11 +00:00
|
|
|
CurEur
|
2019-03-26 21:01:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-03-29 18:50:11 +00:00
|
|
|
LNChargeAPIEnv = "LN_CHARGE_API"
|
|
|
|
LNChargeTokenEnv = "LN_CHARGE_TOKEN"
|
|
|
|
InfoEndpoint = "info"
|
|
|
|
InvoiceEndpoint = "invoice"
|
|
|
|
PollInvoiceEndpoint = "invoice"
|
2019-03-26 21:01:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
LNCEndpoint string
|
|
|
|
LNChargeToken string
|
|
|
|
)
|
|
|
|
|
2019-03-29 18:50:11 +00:00
|
|
|
var (
|
|
|
|
CurrencyString = map[Currency]string{
|
|
|
|
CurUSD: "USD",
|
|
|
|
CurSat: "SAT",
|
|
|
|
CurBTC: "BTC",
|
|
|
|
CurMSat: "MSAT",
|
|
|
|
CurEur: "EUR",
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrencyID = map[string]Currency{
|
|
|
|
"USD": CurUSD,
|
|
|
|
"SAT": CurSat,
|
|
|
|
"MSAT": CurMSat,
|
|
|
|
"BTC": CurBTC,
|
|
|
|
"EUR": CurEur,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-03-26 21:01:54 +00:00
|
|
|
type Charge struct {
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCharge() *Charge {
|
|
|
|
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 &Charge{
|
|
|
|
client: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Charge) Info() (gin.H, error) {
|
|
|
|
|
|
|
|
result := make(gin.H)
|
|
|
|
resp, err := c.client.Get(getUrl(InfoEndpoint))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonDec := json.NewDecoder(resp.Body)
|
|
|
|
jsonDec.Decode(&result)
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:50:11 +00:00
|
|
|
func (c *Charge) PollInvoice(id string) (*Invoice, error) {
|
|
|
|
|
|
|
|
reqParams := url.Values{}
|
|
|
|
// Timeout in seconds
|
|
|
|
reqParams.Set("timeout", strconv.Itoa(30))
|
|
|
|
reqURI := fmt.Sprintf("%s/%s/wait?%s",
|
|
|
|
getUrl(PollInvoiceEndpoint),
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusPaymentRequired {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusGone {
|
|
|
|
log.Printf("invoice expired")
|
|
|
|
return nil, fmt.Errorf("invoice expired")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
//unknownData := gin.H{}
|
|
|
|
//jsonDec := json.NewDecoder(resp.Body)
|
|
|
|
//jsonDec.Decode(&data)
|
|
|
|
log.Printf("unknown resopnse %s", resp.Status)
|
|
|
|
return nil, fmt.Errorf("unknown response %s", resp.Status)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice := Invoice{}
|
|
|
|
jsonDec := json.NewDecoder(resp.Body)
|
|
|
|
jsonDec.Decode(&invoice)
|
|
|
|
log.Printf("Invoice paid %s", invoice)
|
|
|
|
|
|
|
|
return &invoice, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Charge) Invoice(amount float32, curr Currency, uploadId string) (*Invoice, error) {
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.client.Post(getUrl(InvoiceEndpoint),
|
|
|
|
"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",
|
|
|
|
LNChargeToken,
|
|
|
|
LNCEndpoint,
|
|
|
|
endpoint))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.String()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var set bool
|
|
|
|
LNCEndpoint, set = os.LookupEnv(LNChargeAPIEnv)
|
|
|
|
if !set {
|
|
|
|
log.Fatalf("%s not set", LNChargeAPIEnv)
|
|
|
|
}
|
|
|
|
|
|
|
|
LNChargeToken, set = os.LookupEnv(LNChargeTokenEnv)
|
|
|
|
if !set {
|
|
|
|
log.Fatalf("%s not set", LNChargeAPIEnv)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|