~evanj/cms

ref: 991b370ac6b6208e5f2fd7dcada177d52be58d9f cms/internal/c/stripe/stripe.go -rw-r--r-- 1.0 KiB
991b370aEvan M Jones Feat(Big): Added a few things here... 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
	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)
		}
		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)
}