Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 577e0d9

Browse files
committed
minor changes to prepare for the 60-day free trial of hosted version
1 parent 49872fc commit 577e0d9

File tree

5 files changed

+43
-50
lines changed

5 files changed

+43
-50
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ start: build
88
@./cmd/staticbackend
99

1010
deploy:
11-
CGO_ENABLED=0 go build
12-
scp staticbackend sb-poc:/home/dstpierre/sb
11+
@cd cmd && rm -rf staticbackend && CGO_ENABLED=0 go build -o staticbackend
12+
scp cmd/staticbackend sb-poc:/home/dstpierre/sb
13+
scp -qr ./templates/* sb-poc:/home/dstpierre/templates/
1314

1415
test:
1516
@JWT_SECRET=okdevmode go test --race --cover ./...

account.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
6161
}
6262

6363
stripeCustomerID, subID := "", ""
64+
active := true
65+
66+
if AppEnv == AppEnvProd && len(os.Getenv("STRIPE_KEY")) > 0 {
67+
active = false
6468

65-
if AppEnv == AppEnvProd {
6669
cusParams := &stripe.CustomerParams{
6770
Email: stripe.String(email),
6871
}
@@ -99,6 +102,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
99102
Email: email,
100103
StripeID: stripeCustomerID,
101104
SubscriptionID: subID,
105+
IsActive: active,
102106
Created: time.Now(),
103107
}
104108

@@ -127,6 +131,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
127131
ID: primitive.NewObjectID(),
128132
SBID: acctID,
129133
Name: dbName,
134+
IsActive: active,
130135
Whitelist: []string{"localhost"},
131136
}
132137

@@ -146,7 +151,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
146151
}
147152

148153
signUpURL := "no need to sign up in dev mode"
149-
if AppEnv == AppEnvProd {
154+
if AppEnv == AppEnvProd && len(os.Getenv("STRIPE_KEY")) > 0 {
150155
params := &stripe.BillingPortalSessionParams{
151156
Customer: stripe.String(stripeCustomerID),
152157
ReturnURL: stripe.String("https://staticbackend.com/stripe"),

internal/data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type BaseConfig struct {
1212
SBID primitive.ObjectID `bson:"accountId" json:"-"`
1313
Name string `bson:"name" json:"name"`
1414
Whitelist []string `bson:"whitelist" json:"whitelist"`
15-
Valid bool `bson:"valid" json:"valid"`
15+
IsActive bool `bson:"active" json:"-"`
1616
}
1717

1818
var (

middleware/withdb.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func WithDB(client *mongo.Client) Middleware {
5454
if err != nil {
5555
http.Error(w, err.Error(), http.StatusInternalServerError)
5656
return
57+
} else if !conf.IsActive {
58+
http.Error(w, "your account is not inactive. Please contact us [email protected]", http.StatusUnauthorized)
59+
return
5760
}
5861

5962
ctx = context.WithValue(ctx, ContextBase, conf)

stripe.go

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,55 @@ package staticbackend
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
6-
"log"
7+
"io"
78
"net/http"
9+
"os"
810
"staticbackend/internal"
911

12+
"github.com/stripe/stripe-go/v71"
1013
"go.mongodb.org/mongo-driver/bson"
1114
)
1215

1316
type stripeWebhook struct{}
1417

15-
type CustomerSourceCreated struct {
16-
Data struct {
17-
CustomerID string `json:"customer"`
18-
} `json:"data"`
19-
}
20-
2118
func (wh *stripeWebhook) process(w http.ResponseWriter, r *http.Request) {
22-
fmt.Println("inside stripe webhook")
23-
var evt map[string]interface{}
24-
if err := parseBody(r.Body, &evt); err != nil {
25-
log.Println(err)
26-
http.Error(w, err.Error(), http.StatusBadRequest)
27-
return
28-
}
29-
30-
typ, ok := evt["type"]
31-
if !ok {
32-
log.Println("no type specified")
33-
http.Error(w, "no type specified in the event data", http.StatusBadRequest)
19+
const MaxBodyBytes = int64(65536)
20+
r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes)
21+
payload, err := io.ReadAll(r.Body)
22+
if err != nil {
23+
fmt.Fprintf(os.Stderr, "Error reading request body: %v\n", err)
24+
w.WriteHeader(http.StatusServiceUnavailable)
3425
return
3526
}
3627

37-
fmt.Println("event type", typ)
28+
event := stripe.Event{}
3829

39-
var err error
40-
41-
switch typ {
42-
case "customer.source.created":
43-
err = wh.sourceCreated(evt["data"])
44-
}
45-
46-
if err != nil {
47-
log.Println(err)
48-
http.Error(w, err.Error(), http.StatusInternalServerError)
30+
if err := json.Unmarshal(payload, &event); err != nil {
31+
fmt.Fprintf(os.Stderr, "Failed to parse webhook body json: %v\n", err.Error())
32+
w.WriteHeader(http.StatusBadRequest)
4933
return
5034
}
5135

52-
respond(w, http.StatusOK, true)
53-
}
54-
55-
func (wh *stripeWebhook) sourceCreated(params interface{}) error {
56-
data, ok := params.(map[string]interface{})
57-
if !ok {
58-
return fmt.Errorf("unable to cast params: %v into a map[string]interface{}", params)
36+
// Unmarshal the event data into an appropriate struct depending on its Type
37+
switch event.Type {
38+
case "payment_method.attached":
39+
var paymentMethod stripe.PaymentMethod
40+
err := json.Unmarshal(event.Data.Raw, &paymentMethod)
41+
if err != nil {
42+
fmt.Fprintf(os.Stderr, "Error parsing webhook JSON: %v\n", err)
43+
w.WriteHeader(http.StatusBadRequest)
44+
return
45+
}
46+
wh.handlePaymentMethodAttached(paymentMethod)
5947
}
6048

61-
obj, ok := data["object"].(map[string]interface{})
62-
if !ok {
63-
return fmt.Errorf("unable to cast data[object] into a map[string]interface{}")
64-
}
49+
w.WriteHeader(http.StatusOK)
50+
}
6551

66-
stripeID, ok := obj["customer"].(string)
67-
if !ok {
68-
return fmt.Errorf("unable to convert %v to string", data["customer"])
69-
}
52+
func (wh *stripeWebhook) handlePaymentMethodAttached(pm stripe.PaymentMethod) error {
53+
stripeID := pm.Customer.ID
7054

7155
db := client.Database("sbsys")
7256
ctx := context.Background()

0 commit comments

Comments
 (0)