~evanj/cms

ref: eeb893a1458fb57bec63d7a97d3c7005c723715a cms/internal/c/stripe/stripe.go -rw-r--r-- 1.0 KiB
eeb893a1Evan M Jones Fix(HEAD~1): Minor bug fixes to previous commit. 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
47
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
	stripeWebhookSecret string
}

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, stripeWebhookSecret string) *StripeEndpoint {
	return &StripeEndpoint{c, l, db, striper, stripeWebhookSecret}
}

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)
}