Skip to content

Commit 2e2110e

Browse files
authored
Merge pull request #3 from pdnode-team/feat/create_migration_files
Feat/create migration files
2 parents f0258d2 + 63f92bd commit 2e2110e

File tree

6 files changed

+151
-936
lines changed

6 files changed

+151
-936
lines changed

cmd/web/main.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import (
1313
"github.com/pocketbase/pocketbase/core"
1414
"github.com/pocketbase/pocketbase/plugins/migratecmd"
1515
"github.com/stripe/stripe-go/v84"
16+
17+
_ "website-pb/migrations"
1618
)
1719

18-
// TODO: 创建订阅集合迁移文件
19-
// TODO: 创建用户集合迁移文件
20-
// TODO: 用.env初始化SMTP和设置
2120
// TODO: 发送各种邮件
2221

2322
const version string = "v1.0.0-alpha"
@@ -53,15 +52,6 @@ func main() {
5352

5453
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
5554

56-
settings := app.Settings()
57-
58-
config.InitRateLimitRule(settings)
59-
60-
err := app.Save(settings)
61-
if err != nil {
62-
return err
63-
}
64-
6555
se.Router.GET("/{path...}", apis.Static(os.DirFS("./web/build"), true))
6656

6757
// 调用订阅模块,把 app, se 和 cfg 传进去

internal/subscriptions/service.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package subscriptions
33
import (
44
"database/sql"
55
"errors"
6-
"fmt"
76
"time"
87
"website-pb/config"
98

@@ -60,15 +59,17 @@ func (s *SubscriptionService) CreateCheckoutSession(user *core.Record, plan stri
6059
return "", ErrPlanInvalid
6160
}
6261

63-
_, err := s.CheckValidSubscription(user.Original())
64-
65-
if !errors.Is(err, sql.ErrNoRows) {
62+
sub, err := s.CheckValidSubscription(user.Original())
6663

64+
// 情况 A: 找到了有效订阅 (err == nil)
65+
if err == nil && sub != nil {
6766
return "", ErrAlreadySubscribed
6867
}
6968

70-
if err != nil {
71-
s.app.Logger().Warn("Failed to create checkout session", err)
69+
// 情况 B: 发生了真正的数据库错误 (不是“没找到”)
70+
if err != nil && !errors.Is(err, sql.ErrNoRows) {
71+
// 结构化日志修复:使用 key-value
72+
s.app.Logger().Error("Failed to check subscription", "error", err.Error(), "userId", user.Id)
7273
return "", errors.New("check subscription failed")
7374
}
7475

@@ -77,11 +78,8 @@ func (s *SubscriptionService) CreateCheckoutSession(user *core.Record, plan stri
7778
CancelURL: stripe.String(frontendURL),
7879
Mode: stripe.String(string(stripe.CheckoutSessionModeSubscription)),
7980
ClientReferenceID: stripe.String(user.Id),
80-
SubscriptionData: &stripe.CheckoutSessionSubscriptionDataParams{
81-
Metadata: map[string]string{"plan": "pro"}, // 存到订阅对象里
82-
},
83-
Metadata: map[string]string{"plan": plan},
84-
LineItems: []*stripe.CheckoutSessionLineItemParams{{Price: stripe.String(priceID), Quantity: stripe.Int64(1)}},
81+
Metadata: map[string]string{"plan": plan},
82+
LineItems: []*stripe.CheckoutSessionLineItemParams{{Price: stripe.String(priceID), Quantity: stripe.Int64(1)}},
8583
}
8684

8785
// 关联已有 Stripe Customer ID
@@ -122,21 +120,16 @@ func (s *SubscriptionService) HandleInvoicePaid(inv stripe.Invoice) error {
122120

123121
record := core.NewRecord(collection)
124122

125-
fmt.Println(inv.Lines.Data[0].Pricing.PriceDetails.Price)
126-
127123
priceID := inv.Lines.Data[0].Pricing.PriceDetails.Price
128124

129125
priceIDMap := s.cfg.PriceToPlan[priceID]
130-
fmt.Println(priceID)
131126

132127
if priceIDMap == "" {
133128
s.app.Logger().Warn("No prices found for invoice ", inv)
134129
return errors.New("invalid price")
135130
}
136131

137132
expiresAt := time.Unix(inv.Lines.Data[0].Period.End, 0).UTC()
138-
fmt.Println("Expires at:", expiresAt)
139-
fmt.Println(inv.Lines.Data[0].Period.End)
140133

141134
record.Set("user_id", user.Id)
142135
record.Set("plan", priceIDMap)

0 commit comments

Comments
 (0)