-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (70 loc) · 1.67 KB
/
main.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
package main
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"github.com/frozzare/go-swish"
)
var client *swish.Client
func init() {
var err error
client, err = swish.NewClient(&swish.Options{
Env: "test",
Passphrase: "swish",
P12: "./certs/test.p12",
Root: "./certs/root.pem",
})
if err != nil {
log.Fatal(err)
}
}
func create(w http.ResponseWriter, r *http.Request) {
res, err := client.CreatePaymentRequest(context.Background(), &swish.PaymentRequest{
CallbackURL: "https://c06610e4.ngrok.io",
PayeePaymentReference: "0123456789",
PayeeAlias: "1231181189",
Amount: "100.00",
Currency: "SEK",
Message: "Kingston USB Flash Drive 8 GB",
})
if err != nil {
http.Error(w, err.Error(), 500)
return
}
writeJSON(w, res)
}
func status(w http.ResponseWriter, r *http.Request) {
res, err := client.PaymentRequest(context.Background(), r.URL.Query().Get("id"))
if err != nil {
http.Error(w, err.Error(), 500)
return
}
writeJSON(w, res)
}
func callback(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
log.Println(string(body))
io.WriteString(w, "ok")
}
func writeJSON(w http.ResponseWriter, data interface{}) {
j, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, string(j))
}
func main() {
http.HandleFunc("/create", create)
http.HandleFunc("/status", status)
http.HandleFunc("/callback", callback)
http.ListenAndServe(":5000", nil)
}