~evanj/cms

ref: 201c77bedc473fbe974340e7486d824352c76f8f cms/internal/c/stripe/stripe.go -rw-r--r-- 956 bytes
201c77beEvan M Jones Fix(save btn+stripe): Minor tweaks. 1 year, 2 months 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
44
45
46
package stripe

import (
	"log"
	"net/http"

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

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

type DBer interface {
	UserGetFromToken(token string) (user.User, error)
}

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.Error2(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)
}