-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoiceClient.go
169 lines (149 loc) · 4.5 KB
/
invoiceClient.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ptcpayclient
//***********************************************
//* Copyright (c) 2021 Ulbora Labs LLC
//* Copyright (c) 2021 Ken Williamson
//***********************************************
import (
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
)
//CreateInvoice CreateInvoice
func (a *BTCPayClient) CreateInvoice(inv *InvoiceReq) *InvoiceResponse {
var rtn InvoiceResponse
var url = a.host + "/invoices"
a.log.Debug("url: ", url)
aJSON, err := json.Marshal(inv)
bodyStr := string(aJSON)
a.log.Debug("bodyStr: ", bodyStr)
if err == nil {
var headers Headers
urlb := []byte(url + bodyStr)
signVal, _ := a.crypto.Sign(urlb, a.kp)
headers.Set("x-identity", a.crypto.GetPublicKey(a.kp))
headers.Set("x-signature", hex.EncodeToString(signVal))
a.log.Debug("headers: ", headers)
req := a.buildRequest(http.MethodPost, url, headers, aJSON)
suc, stat := a.proxy.Do(req, &rtn) //--------------------
// //test------------------------
// client := &http.Client{}
// resp, err := client.Do(req)
// fmt.Println("client err: ", err)
// defer resp.Body.Close()
// stat := resp.StatusCode
// decoder := json.NewDecoder(resp.Body)
// bodyBytes, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// log.Fatal(err)
// }
// bodyString := string(bodyBytes)
// fmt.Println("body: ", bodyString)
// error := decoder.Decode(&rtn)
// var suc = true
// fmt.Println("error: ", error)
// //-------------------
a.log.Debug("suc: ", suc)
a.log.Debug("stat: ", stat)
//rtn.Code = int64(stat)
if !suc || stat != http.StatusOK {
a.log.Debug("proxy call failed to : ", url)
}
}
a.log.Debug("rtn: ", rtn)
return &rtn
}
//GetInvoice GetInvoice
func (a *BTCPayClient) GetInvoice(invoiceID string) *InvoiceResponse {
var rtn InvoiceResponse
var url = a.host + "/invoices/" + invoiceID + "?token=" + a.tokens.Token
a.log.Debug("url: ", url)
var headers Headers
urlbi := []byte(url)
signVal, _ := a.crypto.Sign(urlbi, a.kp)
headers.Set("x-identity", a.crypto.GetPublicKey(a.kp))
headers.Set("x-signature", hex.EncodeToString(signVal))
fmt.Println("headers: ", headers)
reqi := a.buildRequest(http.MethodGet, url, headers, nil)
suc, stat := a.proxy.Do(reqi, &rtn) //--------------------
// //test------------------------
// client := &http.Client{}
// resp, err := client.Do(req)
// fmt.Println("client err: ", err)
// defer resp.Body.Close()
// stat := resp.StatusCode
// decoder := json.NewDecoder(resp.Body)
// bodyBytes, erri := ioutil.ReadAll(resp.Body)
// if erri != nil {
// log.Fatal(err)
// }
// bodyString := string(bodyBytes)
// fmt.Println("body: ", bodyString)
// error := decoder.Decode(&rtn)
// var suc = true
// fmt.Println("error: ", error)
// //-------------------
a.log.Debug("suc: ", suc)
a.log.Debug("stat: ", stat)
if !suc || stat != http.StatusOK {
a.log.Debug("proxy call failed to : ", url)
}
a.log.Debug("rtn: ", rtn)
return &rtn
}
//GetInvoices GetInvoices
func (a *BTCPayClient) GetInvoices(args *InvoiceArgs) *InvoiceListResponse {
var rtn InvoiceListResponse
var url = a.host + "/invoices?token=" + a.tokens.Token
if args.Status != "" {
url += "&status=" + args.Status
}
if args.DateStart != "" {
url += "&dateStart=" + args.DateStart
}
if args.DateEnd != "" {
url += "&dateEnd=" + args.DateEnd
}
if args.OrderID != "" {
url += "&orderId=" + args.OrderID
}
if args.Limit != "" {
url += "&limit=" + args.Limit
}
if args.Offset != "" {
url += "&offset=" + args.Offset
}
a.log.Debug("url: ", url)
var headers Headers
urlbis := []byte(url)
signVal, _ := a.crypto.Sign(urlbis, a.kp)
headers.Set("x-identity", a.crypto.GetPublicKey(a.kp))
headers.Set("x-signature", hex.EncodeToString(signVal))
fmt.Println("headers: ", headers)
reqis := a.buildRequest(http.MethodGet, url, headers, nil)
suc, stat := a.proxy.Do(reqis, &rtn) //--------------------
// //test------------------------
// client := &http.Client{}
// resp, err := client.Do(req)
// fmt.Println("client err: ", err)
// defer resp.Body.Close()
// stat := resp.StatusCode
// decoder := json.NewDecoder(resp.Body)
// bodyBytes, erris := ioutil.ReadAll(resp.Body)
// if erris != nil {
// log.Fatal(err)
// }
// bodyString := string(bodyBytes)
// fmt.Println("body: ", bodyString)
// error := decoder.Decode(&rtn)
// var suc = true
// fmt.Println("error: ", error)
// //-------------------
a.log.Debug("suc: ", suc)
a.log.Debug("stat: ", stat)
if !suc || stat != http.StatusOK {
a.log.Debug("proxy call failed to : ", url)
}
a.log.Debug("rtn: ", rtn)
return &rtn
}