-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
170 lines (146 loc) · 4.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
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
170
package main
import (
"context"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"time"
"database/sql"
mb "github.com/JeremyJalpha/MenuBotLib"
wa "github.com/febriliankr/whatsapp-cloud-api"
"github.com/go-chi/chi/v5"
_ "github.com/lib/pq"
)
// Example app.env file:
// DATABASE_URL=postgresql://postgres:************@localhost:5432/WhatsAppBot6?sslmode=disable
// PFHOST=https://sandbox.payfast.co.za/eng/process
// HOST_NUMBER=27000000000
// HOMEBASEURL=https://yourhomedomain.ngrok-free.app/
// MERCHANTID=XXXXXXXX
// MERCHANTKEY=*************
// PASSPHRASE=*************
// PHONE_ID=XXXXXXXXXXXXXXX
// VERIFY_TOKEN=XXXXXX
// WHATSAPP_TOKEN=XXXXXX
const (
catalogueID string = "Pig"
prclstPreamble = "All fertilizer quoted per gram."
isTest = false
webhookURL = "/webhook"
staleMsgTimeOut int = 10
pymntRtrnBase = "payment_return"
pymntCnclBase = "payment_canceled"
returnBaseURL = "/" + pymntRtrnBase
cancelBaseURL = "/" + pymntCnclBase
notifyBaseURL = "/payment_notify"
ItemNamePrefix = "Order"
isAutoInc = false
)
type EnvVars struct {
Pwd string
Port string
VerifyToken string
WhatsAppToken string
DBConn string
HostNumber string
PhoneID string
HomebaseURL string
MerchantId string
MerchantKey string
Passphrase string
PfHost string
}
func getEnvVar(name string) string {
value := os.Getenv(name)
if value == "" {
log.Fatalf("%s environment variable does not exist", name)
}
return value
}
// TODO: if WhatsApp token is stale app just exits silently without error or warning - please fix.
func main() {
log.Println("Loading Env Vars...")
envVars := EnvVars{
Pwd: getEnvVar("PWD"),
Port: getEnvVar("PORT"),
VerifyToken: getEnvVar("VERIFY_TOKEN"),
WhatsAppToken: getEnvVar("WHATSAPP_TOKEN"),
DBConn: getEnvVar("DATABASE_URL"),
HostNumber: getEnvVar("HOST_NUMBER"),
PhoneID: getEnvVar("PHONE_ID"),
HomebaseURL: getEnvVar("HOMEBASEURL"),
MerchantId: getEnvVar("MERCHANTID"),
MerchantKey: getEnvVar("MERCHANTKEY"),
Passphrase: getEnvVar("PASSPHRASE"),
PfHost: getEnvVar("PFHOST"),
}
bgCtx := context.Background()
_, cancel := context.WithTimeout(bgCtx, 10*time.Second)
defer cancel()
//Initialize a new WhatsApp instance
wa := wa.NewWhatsapp(envVars.WhatsAppToken, envVars.PhoneID)
// Open the database connection
db, err := sql.Open("postgres", envVars.DBConn)
if err != nil {
log.Fatal("Error opening database: ", err)
}
defer func() {
if err := db.Close(); err != nil {
log.Fatal("Error closing database: ", err)
}
}()
log.Println("Loading HTML templates...")
// Construct the path to the template file
pymntRtrnTplPath := filepath.Join(envVars.Pwd, "templates", pymntRtrnBase+".html")
pymntCnclTplPath := filepath.Join(envVars.Pwd, "templates", pymntCnclBase+".html")
pymntRtrnTpl := template.Must(template.ParseFiles(pymntRtrnTplPath))
pymntCnclTpl := template.Must(template.ParseFiles(pymntCnclTplPath))
r := chi.NewRouter()
log.Println("Chi router started...")
checkoutInfo := mb.CheckoutInfo{
ReturnURL: envVars.HomebaseURL + returnBaseURL,
CancelURL: envVars.HomebaseURL + cancelBaseURL,
NotifyURL: envVars.HomebaseURL + notifyBaseURL,
MerchantId: envVars.MerchantId,
MerchantKey: envVars.MerchantKey,
Passphrase: envVars.Passphrase,
HostURL: envVars.PfHost,
ItemNamePrefix: ItemNamePrefix,
}
log.Println("Loading pricelist from DB...")
ctlgItms, err := mb.GetCatalogueItemsFromDB(db, catalogueID)
if err != nil {
log.Fatal("Error reading pricelist from database: ", err)
}
ctlgSelections := mb.CmpsCtlgSlctnsFromCtlgItms(ctlgItms)
prclist := mb.Pricelist{
PrlstPreamble: prclstPreamble,
Catalogue: ctlgSelections,
}
// Define routes
r.Post(webhookURL, WebhookHandler(envVars.VerifyToken, envVars.HostNumber, staleMsgTimeOut, wa, db, prclist, checkoutInfo))
r.Get(webhookURL, VerificationHandler(envVars.VerifyToken))
// Define other routes
r.Get(returnBaseURL, PaymentReturnHandler(pymntRtrnTpl))
r.Get(notifyBaseURL, PaymentNotifyHandler(envVars.Passphrase, envVars.PfHost))
r.Get(cancelBaseURL, PaymentCancelHandler(pymntCnclTpl))
if envVars.Port == "" {
log.Fatalf("Fatal Error Port not set")
}
err = http.ListenAndServe(":"+envVars.Port, r)
if err != nil {
log.Fatal("Fatal Error serving app: ", err)
}
//server := &http.Server{
// Addr: ":" + port,
// Handler: r,
//}
//var serverErr error
//serverErr = server.ListenAndServeTLS("path/to/cert.pem", "path/to/key.pem")
//if err != nil {
// log.Fatal(serverErr)
//}
log.Println("Server is running on port ", envVars.Port)
}