forked from chris-ramon/stripe-demo-golang
/
main.go
61 lines (46 loc) · 1.3 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
package main
import (
"fmt"
"github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/charge"
"github.com/stripe/stripe-go/currency"
"log"
"net/http"
)
const (
AmountToCharge uint64 = 100 // amount in cents
)
func rootHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
}
func createDebit(token string, amount uint64, description string) *stripe.Charge {
// get your secret test key from
// https://dashboard.stripe.com/account/apikeys and place it here
stripe.Key = "sk_test_goes_here"
params := &stripe.ChargeParams{
Amount: amount,
Currency: currency.USD,
Desc: "test charge",
}
// obtain token from stripe
params.SetSource(token)
ch, err := charge.New(params)
if err != nil {
log.Fatalf("error while trying to charge a cc", err)
}
log.Printf("debit created successfully %v\n", ch.ID)
return ch
}
func debitsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
createDebit(r.FormValue("stripeToken"), AmountToCharge, "testing charge description!")
fmt.Fprint(w, "successful payment.")
}
}
func main() {
http.HandleFunc("/", rootHandler)
http.HandleFunc("/debits", debitsHandler)
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.ListenAndServe(":8081", nil)
}