-
Notifications
You must be signed in to change notification settings - Fork 1
/
btcpay_test.go
107 lines (88 loc) · 1.96 KB
/
btcpay_test.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
package btcpay
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"testing"
)
func TestInvoice(t *testing.T) {
store, err := Load("store.json")
if err != nil {
t.Fatal(err)
}
ir := &InvoiceRequest{
Amount: 1.23,
Currency: "EUR",
}
ir.OrderID = "Test"
created, err := store.CreateInvoice(ir)
if err != nil {
t.Fatal(err)
}
got, err := store.GetInvoice(created.ID)
if err != nil {
t.Fatal(err)
}
if got.Amount != 1.23 || got.Currency != "EUR" {
t.Fail()
}
// simulate a webhook
var body = []byte(fmt.Sprintf(`
{
"deliveryId": "delivery-abcdefghijkl",
"webhookId": "webhook-abcdefghijklmn",
"orignalDeliveryId": "delivery-abcdefghijkl",
"isRedelivery": false,
"type": "InvoiceCreated",
"timestamp": 1610000000,
"storeId": "%s",
"invoiceId": "%s",
"metadata": {
"orderId": "%s"
}
}`, store.ID, got.ID, got.InvoiceMetadata.OrderID))
var webhookRequest = &http.Request{
Body: io.NopCloser(bytes.NewReader(body)),
Header: make(http.Header),
}
var mac = hmac.New(sha256.New, []byte(store.WebhookSecret))
mac.Write(body)
webhookRequest.Header.Add("BTCPay-Sig", fmt.Sprintf("sha256=%s", hex.EncodeToString(mac.Sum(nil))))
event, err := store.ProcessWebhook(webhookRequest)
if err != nil {
t.Fatal(err)
}
if event.StoreID != store.ID || event.Type != EventInvoiceCreated || event.InvoiceID != got.ID {
t.Fail()
}
if event.InvoiceMetadata.OrderID != got.InvoiceMetadata.OrderID {
t.Fail()
}
}
func TestPaymentRequest(t *testing.T) {
store, err := Load("store.json")
if err != nil {
t.Fatal(err)
}
request := &PaymentRequestRequest{
Amount: 1.23,
Currency: "EUR",
Title: "Test payment request",
}
request.SetExpiryDays(7)
created, err := store.CreatePaymentRequest(request)
if err != nil {
t.Fatal(err)
}
got, err := store.GetPaymentRequest(created.ID)
if err != nil {
t.Fatal(err)
}
if got.Amount != 1.23 || got.Currency != "EUR" {
t.Fail()
}
}