-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoice.go
97 lines (86 loc) · 2.52 KB
/
invoice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package sn
import (
"encoding/json"
"fmt"
"time"
)
type Invoice struct {
Id int `json:"id,string"`
Hash string `json:"hash"`
Hmac string `json:"hmac"`
Bolt11 string `json:"bolt11"`
SatsRequested int `json:"satsRequested"`
SatsReceived int `json:"satsReceived"`
Cancelled bool `json:"cancelled"`
ConfirmedAt time.Time `json:"createdAt"`
ExpiresAt time.Time `json:"expiresAt"`
Nostr map[string]interface{} `json:"nostr"`
IsHeld bool `json:"isHeld"`
Comment string `json:"comment"`
Lud18Data map[string]interface{} `json:"lud18Data"`
ConfirmedPreimage string `json:"confirmedPreimage"`
ActionState string `json:"actionState"`
ActionType string `json:"actionType"`
}
type PaymentMethod string
const (
PaymentMethodFeeCredits PaymentMethod = "FEE_CREDIT"
PaymentMethodOptimistic PaymentMethod = "OPTIMISTIC"
PaymentMethodPessimistic PaymentMethod = "PESSIMISTIC"
)
type CreateInvoiceArgs struct {
Amount int
ExpireSecs int
HodlInvoice bool
}
type CreateInvoiceResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
CreateInvoice Invoice `json:"createInvoice"`
} `json:"data"`
}
func (c *Client) CreateInvoice(args *CreateInvoiceArgs) (*Invoice, error) {
if args == nil {
args = &CreateInvoiceArgs{}
}
body := GqlBody{
// TODO: add createdAt
// when I wrote this code, createdAt returned null but is non-nullable
// so I had to remove it.
Query: `
mutation createInvoice($amount: Int!, $expireSecs: Int, $hodlInvoice: Boolean) {
createInvoice(amount: $amount, expireSecs: $expireSecs, hodlInvoice: $hodlInvoice) {
id
hash
hmac
bolt11
satsRequested
satsReceived
isHeld
comment
confirmedPreimage
expiresAt
confirmedAt
}
}`,
Variables: map[string]interface{}{
"amount": args.Amount,
},
}
resp, err := c.callApi(body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var respBody CreateInvoiceResponse
err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
err = fmt.Errorf("error decoding items: %w", err)
return nil, err
}
err = c.checkForErrors(respBody.Errors)
if err != nil {
return nil, err
}
return &respBody.Data.CreateInvoice, nil
}