~evanj/cms

ref: afc228cb59683180b3b2957aceb6bf1aa0bf0d0e cms/internal/c/stripe/stripe.go -rw-r--r-- 859 bytes
afc228cbEvan J Feat(db): Tweaking db.SetMaxOpenConns, db.SetMaxIdleConns, and 1 year, 14 days ago
                                                                                
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
package stripe

import (
	"log"
	"net/http"

	"git.sr.ht/~evanj/cms/internal/c"
)

type StripeEndpoint struct {
	*c.Controller
	log    *log.Logger
	db     DBer
	stripe Striper
}

type DBer interface{}

type Striper interface {
	CompleteCheckout(sessionID string) error
}

func New(c *c.Controller, l *log.Logger, db DBer, striper Striper) StripeEndpoint {
	return StripeEndpoint{c, l, db, striper}
}

func (s StripeEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	switch r.URL.Path {
	case "/success":
		err := s.stripe.CompleteCheckout(r.FormValue("session_id"))
		if err != nil {
			s.Error(w, r, http.StatusInternalServerError, err)
			return
		}
		s.Redirect(w, r, "/")
		return
	case "/error":
		s.ErrorString(w, r, http.StatusInternalServerError, "failed to process request: you have not been charged")
		return
	}

	http.NotFound(w, r)
}