/
routes.go
executable file
·41 lines (34 loc) · 909 Bytes
/
routes.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
package stripeexample
import (
"fmt"
"net/http"
"google.golang.org/appengine"
"html/template"
)
var tpls *template.Template
func init() {
tpls = template.Must(template.ParseGlob("templates/*.gohtml"))
http.HandleFunc("/", handleIndex)
http.HandleFunc("/payment", handlePayment)
}
func handlePayment(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.Error(res, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
ctx := appengine.NewContext(req)
stripeToken := req.FormValue("stripeToken")
err := chargeAccount(ctx, stripeToken)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
fmt.Fprintln(res, "Thank You for the Money Sucka")
}
func handleIndex(res http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/minions.jpg" {
http.ServeFile(res, req, "minions.jpg")
return
}
tpls.ExecuteTemplate(res, "index.gohtml", nil)
}