-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (94 loc) · 2.84 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
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
package main
import (
"embed"
"flag"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"net/url"
"os"
"github.com/unravelin/ravelin-3ds-demo/handler"
)
var (
//go:embed static templates
embeddedFS embed.FS
)
const (
defaultRavelinApiUrl = "https://pci.ravelin.com"
defaultMerchantUrl = "http://localhost:8085"
)
func main() {
var ravelinApiKey string
var ravelinApiUrl string
var merchantUrl string
flag.StringVar(&ravelinApiKey, "ravelin-api-key", ravelinApiKey, "Ravelin API Key - Can also be set as $RAVELIN_API_KEY")
flag.StringVar(&ravelinApiUrl, "ravelin-api-url", defaultRavelinApiUrl, "Ravelin API URL")
flag.StringVar(&merchantUrl, "merchant-url", defaultMerchantUrl, "Merchant URL - If url does not contain a port, server is run on $PORT")
flag.Parse()
if ravelinApiKey == "" {
ravelinApiKey = os.Getenv("RAVELIN_API_KEY")
if ravelinApiKey == "" {
panic("Ravelin API Key not set")
}
}
if ravelinApiUrl == "" {
panic("Ravelin API URL not set")
}
if merchantUrl == "" {
panic("Merchant URL not set")
}
mUrl, err := url.Parse(merchantUrl)
if err != nil {
panic("failed to parse Merchant URL")
}
h := handler.Handler{
RavelinApiUrl: ravelinApiUrl,
RavelinApiKey: ravelinApiKey,
MerchantUrl: merchantUrl,
ThreeDSTransactionStore: handler.NewThreeDSTransactionStore(),
}
h.MethodNotificationResponseTemplate, err = loadTemplate(embeddedFS, "templates/method-notification-response.html")
if err != nil {
panic(err)
}
h.ChallengeNotificationResponseTemplate, err = loadTemplate(embeddedFS, "templates/challenge-notification-response.html")
if err != nil {
panic(err)
}
staticFS, err := fs.Sub(embeddedFS, "static")
if err != nil {
panic(err)
}
frontEnd := http.FileServer(http.FS(staticFS))
mux := http.NewServeMux()
mux.Handle("/", frontEnd)
mux.HandleFunc(handler.CheckoutEndpoint, h.Checkout)
mux.HandleFunc(handler.AuthenticateEndpoint, h.Authenticate)
mux.HandleFunc(handler.MethodNotificationEndpoint, h.MethodNotification)
mux.HandleFunc(handler.ChallengeNotificationEndpoint, h.ChallengeNotification)
mux.HandleFunc(handler.TestCardsEndpoint, h.TestCards)
port := mUrl.Port()
if port == "" {
port = os.Getenv("PORT")
}
server := http.Server{
Handler: mux,
Addr: ":" + port,
}
log.Printf("Using Ravelin API URL %s", ravelinApiUrl)
log.Printf("Starting server on port %q using merchant URL %s", server.Addr, merchantUrl)
panic(server.ListenAndServe())
}
func loadTemplate(fs fs.ReadFileFS, filename string) (*template.Template, error) {
file, err := fs.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to load template from file %s : %v", filename, err)
}
t, err := template.New("template").Parse(string(file))
if err != nil {
return nil, fmt.Errorf("failed to load template from file %s : %v", filename, err)
}
return t, nil
}