-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
server.go
107 lines (90 loc) · 3.88 KB
/
server.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 main
import (
"net/http"
"github.com/gorilla/mux"
"github.com/rs/cors"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"github.com/rs/zerolog/log"
"github.com/imrenagi/go-payment/datastore/inmemory"
dssql "github.com/imrenagi/go-payment/datastore/sql"
"github.com/imrenagi/go-payment/gateway/midtrans"
"github.com/imrenagi/go-payment/invoice"
"github.com/imrenagi/go-payment/manage"
"github.com/imrenagi/go-payment/server"
"github.com/imrenagi/go-payment/subscription"
"github.com/imrenagi/go-payment/util/localconfig"
)
func main() {
config, err := localconfig.LoadConfig("example/server/config.yaml")
if err != nil {
panic(err)
}
secret, err := localconfig.LoadSecret("example/server/secret.yaml")
if err != nil {
panic(err)
}
db, err := gorm.Open(sqlite.Open("example/server/gorm.db"), &gorm.Config{})
if err != nil {
log.Fatal().Msg(err.Error())
}
db.AutoMigrate(
&midtrans.TransactionStatus{},
&invoice.Invoice{},
&invoice.Payment{},
&invoice.CreditCardDetail{},
&invoice.LineItem{},
&invoice.BillingAddress{},
&subscription.Subscription{},
&subscription.Schedule{},
)
m := manage.NewManager(*config, secret.Payment)
m.MustMidtransTransactionStatusRepository(dssql.NewMidtransTransactionRepository(db))
m.MustInvoiceRepository(dssql.NewInvoiceRepository(db))
m.MustSubscriptionRepository(dssql.NewSubscriptionRepository(db))
m.MustPaymentConfigReader(inmemory.NewPaymentConfigRepository("example/server/payment-methods.yaml"))
srv := srv{
Router: mux.NewRouter(),
paymentSrv: server.NewServer(m),
}
srv.routes()
if err := http.ListenAndServe(":8080", srv.GetHandler()); err != nil {
log.Fatal().Msgf("Server can't run. Got: `%v`", err)
}
}
type srv struct {
Router *mux.Router
paymentSrv *server.Server
}
// GetHandler returns http.Handler which intercepted by the cors checker.
func (s *srv) GetHandler() http.Handler {
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000", "https://localhost:3000"},
AllowedMethods: []string{"POST", "GET", "PUT", "DELETE", "HEAD", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization", "Mode"},
MaxAge: 60, // 1 minutes
AllowCredentials: true,
OptionsPassthrough: false,
Debug: false,
})
return c.Handler(s.Router)
}
func (s *srv) Healthcheck() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}
}
func (s srv) routes() {
s.Router.HandleFunc("/payment/methods", s.paymentSrv.GetPaymentMethodsHandler()).Methods("GET")
s.Router.HandleFunc("/payment/invoices", s.paymentSrv.CreateInvoiceHandler()).Methods("POST")
s.Router.HandleFunc("/payment/midtrans/callback", s.paymentSrv.MidtransTransactionCallbackHandler()).Methods("POST")
s.Router.HandleFunc("/payment/xendit/invoice/callback", s.paymentSrv.XenditInvoiceCallbackHandler()).Methods("POST")
s.Router.HandleFunc("/payment/xendit/ovo/callback", s.paymentSrv.XenditOVOCallbackHandler()).Methods("POST")
s.Router.HandleFunc("/payment/xendit/dana/callback", s.paymentSrv.XenditDanaCallbackHandler()).Methods("POST")
s.Router.HandleFunc("/payment/xendit/linkaja/callback", s.paymentSrv.XenditLinkAjaCallbackHandler()).Methods("POST")
s.Router.HandleFunc("/payment/xendit/ewallet/callback", s.paymentSrv.XenditEWalletCallbackHandler()).Methods("POST")
s.Router.HandleFunc("/payment/subscriptions", s.paymentSrv.CreateSubscriptionHandler()).Methods("POST")
s.Router.HandleFunc("/payment/subscriptions/{subscription_number}/pause", s.paymentSrv.PauseSubscriptionHandler()).Methods("POST", "PUT")
s.Router.HandleFunc("/payment/subscriptions/{subscription_number}/stop", s.paymentSrv.StopSubscriptionHandler()).Methods("POST", "PUT")
s.Router.HandleFunc("/payment/subscriptions/{subscription_number}/resume", s.paymentSrv.ResumeSubscriptionHandler()).Methods("POST", "PUT")
}