diff --git a/example/simplepay/main b/example/simplepay/main index 585eb01..b84f720 100755 Binary files a/example/simplepay/main and b/example/simplepay/main differ diff --git a/example/simplepay/main.go b/example/simplepay/main.go index c36dc83..8e33ff3 100644 --- a/example/simplepay/main.go +++ b/example/simplepay/main.go @@ -12,6 +12,7 @@ import ( var midclient midtrans.Client var coreGateway midtrans.CoreGateway +var snapGateway midtrans.SnapGateway func main() { setupMidtrans() @@ -20,6 +21,20 @@ func main() { fmt.Println("Server started on port: ", *addr) http.Handle("/", &templateHandler{filename: "index.html"}) + http.Handle("/snap", &templateHandler{ + filename: "snap_index.html", + dataInitializer: func (t *templateHandler) { + snapResp, err := snapGateway.GetTokenQuick(generateOrderId(), 200000) + t.data = make(map[string]interface{}) + + if err != nil { + log.Fatal("Error generating snap token: ", err) + t.data["Token"] = "" + } else { + t.data["Token"] = snapResp.Token + } + }, + }) http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))) http.HandleFunc("/chargeDirect", chargeDirect) @@ -37,6 +52,10 @@ func setupMidtrans() { coreGateway = midtrans.CoreGateway{ Client: midclient, } + + snapGateway = midtrans.SnapGateway{ + Client: midclient, + } } func chargeDirect(w http.ResponseWriter, r *http.Request) { @@ -46,11 +65,15 @@ func chargeDirect(w http.ResponseWriter, r *http.Request) { TokenID: r.FormValue("card-token"), }, TransactionDetails: midtrans.TransactionDetails{ - OrderID: strconv.FormatInt(time.Now().UnixNano(), 10), + OrderID: generateOrderId(), GrossAmt: 200000, }, }) fmt.Println(chargeResp.ValMessages) fmt.Println(chargeResp.StatusMessage) +} + +func generateOrderId() string { + return strconv.FormatInt(time.Now().UnixNano(), 10) } \ No newline at end of file diff --git a/example/simplepay/template_handler.go b/example/simplepay/template_handler.go index fe3fbec..b864ede 100644 --- a/example/simplepay/template_handler.go +++ b/example/simplepay/template_handler.go @@ -11,6 +11,8 @@ type templateHandler struct { once sync.Once filename string templ *template.Template + data map[string]interface{} + dataInitializer func(t *templateHandler) } func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -18,9 +20,13 @@ func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename))) }) - data := map[string]interface{}{ - "Host": r.Host, + if t.dataInitializer != nil { + t.dataInitializer(t) + } else { + t.data = make(map[string]interface{}) } - t.templ.Execute(w, data) + t.data["Host"] = r.Host + + t.templ.Execute(w, t.data) } \ No newline at end of file diff --git a/example/simplepay/templates/index.html b/example/simplepay/templates/index.html index 3b4ca7f..f23582d 100644 --- a/example/simplepay/templates/index.html +++ b/example/simplepay/templates/index.html @@ -44,7 +44,7 @@
diff --git a/example/simplepay/templates/snap_index.html b/example/simplepay/templates/snap_index.html new file mode 100644 index 0000000..e0fb163 --- /dev/null +++ b/example/simplepay/templates/snap_index.html @@ -0,0 +1,55 @@ + + + +